Browse Source

commands get/add/edit/del

develop
Robin Thoni 8 years ago
parent
commit
5128c64199

+ 6
- 0
admin/app.js View File

@@ -46,6 +46,12 @@ camotion.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$loca
46 46
             controller:'SensorsController'
47 47
         });
48 48
 
49
+        $stateProvider.state('commands',{
50
+            url:'/commands',
51
+            templateUrl:'views/commands.html',
52
+            controller:'CommandsController'
53
+        });
54
+
49 55
         $urlRouterProvider.otherwise('/');
50 56
 
51 57
         $httpProvider.interceptors.push(['luticateAuthCache', '$injector', '$q',

+ 76
- 0
admin/controllers/commands.controller.js View File

@@ -0,0 +1,76 @@
1
+angular.module('camotionAdmin')
2
+    .controller('CommandsController', ['$scope', 'CommandsService', 'HelperService', '$q', 'luticateAuthCache',
3
+        function($scope, CommandsService, HelperService, $q, luticateAuthCache) {
4
+
5
+            $scope.foreighLoaded = false;
6
+            $scope.hosts = {};
7
+            $scope.commandTypes = {};
8
+
9
+            $scope.luTable = {
10
+                columns: [
11
+                    {
12
+                        name: "Name",
13
+                        width: 3,
14
+                        getValue: function (item) {
15
+                            return item.Name;
16
+                        }
17
+                    },
18
+                    {
19
+                        name: "Type",
20
+                        width: 3,
21
+                        getValue: function (item) {
22
+                            return $scope.commandTypes[item.CommandTypeId].Name;
23
+                        }
24
+                    }, {
25
+                        name: "Description",
26
+                        width: 6,
27
+                        getValue: function (item) {
28
+                            return item.Description;
29
+                        }
30
+                    }
31
+                ],
32
+
33
+                canAdd: function()
34
+                {
35
+                    return luticateAuthCache.hasAllEffectivePermissions([
36
+                        'CAMOTION_COMMAND_ADD',
37
+                        'CAMOTION_HOST_GET'
38
+                    ])
39
+                },
40
+
41
+                canDel: 'CAMOTION_COMMAND_DEL',
42
+
43
+                canEdit: function()
44
+                {
45
+                    return true;
46
+                },
47
+
48
+                getLoadPagePromise: function (page, perPage, query, promise) {
49
+                    var defer = $q.defer();
50
+                    CommandsService.getAll({page: page, perPage: perPage, query: query}, promise)
51
+                        .then(function(commands)
52
+                        {
53
+                            if (!$scope.foreighLoaded) {
54
+                                HelperService.getForeignEntities(CommandsService, promise).then(function (data) {
55
+                                    $scope.hosts = data.hosts;
56
+                                    $scope.commandTypes = data.types;
57
+                                    $scope.foreighLoaded = true;
58
+                                    defer.resolve(commands);
59
+                                }, defer.reject);
60
+                            }
61
+                            else {
62
+                                defer.resolve(commands);
63
+                            }
64
+                        }, defer.reject);
65
+                    return defer.promise;
66
+                },
67
+
68
+                getDelPromise: function (id, promise) {
69
+                    return CommandsService.del({command_id: id}, promise);
70
+                },
71
+
72
+                getEditController: function () {
73
+                    return "CommandEdit";
74
+                }
75
+            };
76
+    }]);

+ 82
- 0
admin/controllers/modals/commandedit.controller.js View File

@@ -0,0 +1,82 @@
1
+/**
2
+ * Created by robin on 11/4/15.
3
+ */
4
+
5
+angular.module('camotionAdmin')
6
+    .controller('CommandEditController', ['$scope', 'CommandsService', 'data', 'dialogs', '$q', 'luticateAuthCache', 'HelperService',
7
+        function($scope, CommandsService, data, dialogs, $q, luticateAuthCache, HelperService) {
8
+            if (data != null) {
9
+                $scope.command = data;
10
+                $scope.command.Data = JSON.parse($scope.command.Data);
11
+                $scope.command.command_id = $scope.command.Id;
12
+                $scope.permission = "CAMOTION_COMMAND_EDIT";
13
+            }
14
+            else {
15
+                $scope.command = {
16
+                    Name: "",
17
+                    Description: "",
18
+                    Data: null,
19
+                    HostId: null,
20
+                    CommandTypeId: null
21
+                };
22
+                $scope.permission = "CAMOTION_COMMAND_ADD";
23
+            }
24
+
25
+            $scope.hosts = {};
26
+            $scope.commandTypes = {};
27
+            var promiseLoadForeign = {
28
+                id: "promiseLoadForeign",
29
+                groups: ["modal"]
30
+            };
31
+
32
+            HelperService.getForeignEntities(CommandsService, promiseLoadForeign).then(function(data)
33
+            {
34
+                $scope.hosts = data.hosts;
35
+                $scope.commandTypes = data.types;
36
+                if ($scope.command.Id == null) {
37
+                    var keys = Object.keys($scope.hosts);
38
+                    if (keys.length != 0) {
39
+                        $scope.command.HostId = $scope.hosts[keys[0]].Id;
40
+                    }keys = Object.keys($scope.commandTypes);
41
+                    if (keys.length != 0) {
42
+                        $scope.command.CommandTypeId = $scope.commandTypes[keys[0]].Id;
43
+                        $scope.setCommandType($scope.commandTypes[keys[0]]);
44
+                    }
45
+                }
46
+                else if ($scope.command.Data == null) {
47
+                    $scope.setCommandType($scope.commandTypes[$scope.command.CommandTypeId]);
48
+                }
49
+            });
50
+
51
+            $scope.submitForm = function()
52
+            {
53
+                if (luticateAuthCache.hasEffectivePermission($scope.permission)) {
54
+                    $scope.command.Data = JSON.stringify($scope.command.Data);
55
+                    if ($scope.command.Id != null) {
56
+                        var promiseEditCommand = {
57
+                            id: "promiseEditCommand",
58
+                            loaderGroups: ["modal"]
59
+                        };
60
+                        return CommandsService.edit($scope.command, promiseEditCommand);
61
+                    }
62
+                    else {
63
+                        var promiseAddCommand = {
64
+                            id: "promiseAddCommand",
65
+                            loaderGroups: ["modal"]
66
+                        };
67
+                        return CommandsService.add($scope.command, promiseAddCommand);
68
+                    }
69
+                }
70
+                else {
71
+                    var defer = $q.defer();
72
+                    defer.resolve();
73
+                    return defer.promise;
74
+                }
75
+            };
76
+
77
+            $scope.setCommandType = function(commandType)
78
+            {
79
+                $scope.command.CommandTypeId = commandType.Id;
80
+                $scope.command.Data = JSON.parse($scope.commandTypes[$scope.command.CommandTypeId].DefaultData);
81
+            };
82
+        }]);

+ 3
- 0
admin/index.html View File

@@ -42,17 +42,20 @@
42 42
     <script src="controllers/hosts.controller.js"></script>
43 43
     <script src="controllers/cameras.controller.js"></script>
44 44
     <script src="controllers/sensors.controller.js"></script>
45
+    <script src="controllers/commands.controller.js"></script>
45 46
 
46 47
     <!-- Modal Controller -->
47 48
     <script src="controllers/modals/hostedit.controller.js"></script>
48 49
     <script src="controllers/modals/cameraedit.controller.js"></script>
49 50
     <script src="controllers/modals/sensoredit.controller.js"></script>
51
+    <script src="controllers/modals/commandedit.controller.js"></script>
50 52
 
51 53
     <!-- SDK -->
52 54
     <script src="../sdk/helper.js"></script>
53 55
     <script src="../sdk/hosts.js"></script>
54 56
     <script src="../sdk/cameras.js"></script>
55 57
     <script src="../sdk/sensors.js"></script>
58
+    <script src="../sdk/commands.js"></script>
56 59
 
57 60
     <!-- Directives -->
58 61
 

+ 4
- 0
admin/views/commands.html View File

@@ -0,0 +1,4 @@
1
+<!-- Page Content -->
2
+<div class="container">
3
+    <lu-edit-table options="luTable" class="row col-sm-8 col-sm-offset-2"></lu-edit-table>
4
+</div>

+ 44
- 0
admin/views/modals/commandedit.html View File

@@ -0,0 +1,44 @@
1
+<dialog-ok-cancel title="{{ command.Id == null ? 'Add' : 'Edit'}} command {{ host.Name }}">
2
+    <div class="form-group">
3
+        <label for="name" class="col-sm-2 control-label">Name</label>
4
+        <div class="col-sm-9">
5
+            <input id="name" class="form-control" ng-model="command.Name" required lu-enable-permission="{{ permission }}"/>
6
+        </div>
7
+    </div>
8
+    <div class="form-group">
9
+        <label for="url" class="col-sm-2 control-label">Description</label>
10
+        <div class="col-sm-9">
11
+            <input id="url" class="form-control" ng-model="command.Description" lu-enable-permission="{{ permission }}"/>
12
+        </div>
13
+    </div>
14
+    <div class="form-group">
15
+        <label for="dropdownHost" class="col-sm-2 control-label">Host</label>
16
+        <div class="btn-group">
17
+                <button class="btn btn-default dropdown-toggle" type="button" id="dropdownHost" data-toggle="dropdown">
18
+                    {{ hosts[command.HostId].Name }}
19
+                    <span class="caret"></span>
20
+                </button>
21
+                <ul class="dropdown-menu">
22
+                    <li ng-repeat="host in hosts"><a ng-click="command.HostId = host.Id" href="">{{ host.Name }}</a></li>
23
+                </ul>
24
+            </div>
25
+    </div>
26
+    <div class="form-group">
27
+        <label for="dropdownType" class="col-sm-2 control-label">Type</label>
28
+        <div class="btn-group">
29
+            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownType" data-toggle="dropdown">
30
+                {{ commandTypes[command.CommandTypeId].Name }}
31
+                <span class="caret"></span>
32
+            </button>
33
+            <ul class="dropdown-menu">
34
+                <li ng-repeat="type in commandTypes"><a ng-click="setCommandType(type)" href="">{{ type.Name }}</a></li>
35
+            </ul>
36
+        </div>
37
+    </div>
38
+    <div class="form-group">
39
+        <label for="data" class="col-sm-2 control-label">Data</label>
40
+        <div class="col-sm-9 jsonView">
41
+            <json id="data" child="command.Data" default-collapsed="false" type="object"></json>
42
+        </div>
43
+    </div>
44
+</dialog-ok-cancel>

+ 1
- 0
admin/views/navbar.html View File

@@ -19,6 +19,7 @@
19 19
                 <li><a ui-sref="hosts" lu-show-permission="CAMOTION_HOST_GET">Hosts</a></li>
20 20
                 <li><a ui-sref="cameras" lu-show-permission="CAMOTION_CAMERA_GET,CAMOTION_HOST_GET">Cameras</a></li>
21 21
                 <li><a ui-sref="sensors" lu-show-permission="CAMOTION_SENSOR_GET,CAMOTION_HOST_GET">Sensors</a></li>
22
+                <li><a ui-sref="commands" lu-show-permission="CAMOTION_COMMAND_GET,CAMOTION_HOST_GET">Commands</a></li>
22 23
             </ul>
23 24
             <ul class="nav navbar-nav navbar-right">
24 25
                 <ul class="nav navbar-nav navbar-right">

+ 44
- 0
sdk/commands.js View File

@@ -0,0 +1,44 @@
1
+/**
2
+ * Created by robin on 11/21/15.
3
+ */
4
+(function () {
5
+    'use strict';
6
+
7
+    angular.module('camotionAdmin')
8
+        .factory('CommandsService', ['luticateRequest', function (luticateRequest) {
9
+
10
+            var CommandsService = {};
11
+
12
+            CommandsService.add = function(data, promise)
13
+            {
14
+                return luticateRequest.post("/api/commands/add", {command: JSON.stringify(data)}, null, promise);
15
+            };
16
+
17
+            CommandsService.getAll = function(data, promise)
18
+            {
19
+                return luticateRequest.get("/api/commands", data, promise);
20
+            };
21
+
22
+            CommandsService.getAllTypes = function(promise)
23
+            {
24
+                return luticateRequest.get("/api/commands/types", null, promise);
25
+            };
26
+
27
+            CommandsService.get = function(data, promise)
28
+            {
29
+                return luticateRequest.get("/api/commands/" + data.command_id , null, promise);
30
+            };
31
+
32
+            CommandsService.edit = function(data, promise)
33
+            {
34
+                return luticateRequest.post("/api/commands/" + data.command_id + "/edit", {command: JSON.stringify(data)}, null, promise);
35
+            };
36
+
37
+            CommandsService.del = function(data, promise)
38
+            {
39
+                return luticateRequest.post("/api/commands/" + data.command_id + "/del", null, null, promise);
40
+            };
41
+
42
+            return CommandsService;
43
+        }]);
44
+})();

Loading…
Cancel
Save