Selaa lähdekoodia

sensors get/add/edit/del

develop
Robin Thoni 9 vuotta sitten
vanhempi
commit
f2c673efa5

+ 6
- 0
admin/app.js Näytä tiedosto

@@ -40,6 +40,12 @@ camotion.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$loca
40 40
             controller:'CamerasController'
41 41
         });
42 42
 
43
+        $stateProvider.state('sensors',{
44
+            url:'/sensors',
45
+            templateUrl:'views/sensors.html',
46
+            controller:'SensorsController'
47
+        });
48
+
43 49
         $urlRouterProvider.otherwise('/');
44 50
 
45 51
         $httpProvider.interceptors.push(['luticateAuthCache', '$injector', '$q',

+ 82
- 0
admin/controllers/modals/sensoredit.controller.js Näytä tiedosto

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

+ 76
- 0
admin/controllers/sensors.controller.js Näytä tiedosto

@@ -0,0 +1,76 @@
1
+angular.module('camotionAdmin')
2
+    .controller('SensorsController', ['$scope', 'SensorsService', 'HelperService', '$q', 'luticateAuthCache',
3
+        function($scope, SensorsService, HelperService, $q, luticateAuthCache) {
4
+
5
+            $scope.foreighLoaded = false;
6
+            $scope.hosts = {};
7
+            $scope.sensorTypes = {};
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.sensorTypes[item.SensorTypeId].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_SENSOR_ADD',
37
+                        'CAMOTION_HOST_GET'
38
+                    ])
39
+                },
40
+
41
+                canDel: 'CAMOTION_SENSOR_DEL',
42
+
43
+                canEdit: function()
44
+                {
45
+                    return true;
46
+                },
47
+
48
+                getLoadPagePromise: function (page, perPage, query, promise) {
49
+                    var defer = $q.defer();
50
+                    SensorsService.getAll({page: page, perPage: perPage, query: query}, promise)
51
+                        .then(function(sensors)
52
+                        {
53
+                            if (!$scope.foreighLoaded) {
54
+                                HelperService.getForeignEntities(SensorsService, promise).then(function (data) {
55
+                                    $scope.hosts = data.hosts;
56
+                                    $scope.sensorTypes = data.types;
57
+                                    $scope.foreighLoaded = true;
58
+                                    defer.resolve(sensors);
59
+                                }, defer.reject);
60
+                            }
61
+                            else {
62
+                                defer.resolve(sensors);
63
+                            }
64
+                        }, defer.reject);
65
+                    return defer.promise;
66
+                },
67
+
68
+                getDelPromise: function (id, promise) {
69
+                    return SensorsService.del({sensor_id: id}, promise);
70
+                },
71
+
72
+                getEditController: function () {
73
+                    return "SensorEdit";
74
+                }
75
+            };
76
+    }]);

+ 3
- 0
admin/index.html Näytä tiedosto

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

+ 44
- 0
admin/views/modals/sensoredit.html Näytä tiedosto

@@ -0,0 +1,44 @@
1
+<dialog-ok-cancel title="{{ sensor.Id == null ? 'Add' : 'Edit'}} sensor {{ 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="sensor.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="sensor.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[sensor.HostId].Name }}
19
+                    <span class="caret"></span>
20
+                </button>
21
+                <ul class="dropdown-menu">
22
+                    <li ng-repeat="host in hosts"><a ng-click="sensor.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
+                {{ sensorTypes[sensor.SensorTypeId].Name }}
31
+                <span class="caret"></span>
32
+            </button>
33
+            <ul class="dropdown-menu">
34
+                <li ng-repeat="type in sensorTypes"><a ng-click="setSensorType(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="sensor.Data" default-collapsed="false" type="object"></json>
42
+        </div>
43
+    </div>
44
+</dialog-ok-cancel>

+ 2
- 1
admin/views/navbar.html Näytä tiedosto

@@ -17,7 +17,8 @@
17 17
             <ul class="nav navbar-nav navbar-left">
18 18
                 <li><a href="/luticate" ng-show="canSeeLuticate()">Luticate</a></li>
19 19
                 <li><a ui-sref="hosts" lu-show-permission="CAMOTION_HOST_GET">Hosts</a></li>
20
-                <li><a ui-sref="cameras" lu-show-permission="CAMOTION_CAMERA_GET">Cameras</a></li>
20
+                <li><a ui-sref="cameras" lu-show-permission="CAMOTION_CAMERA_GET,CAMOTION_HOST_GET">Cameras</a></li>
21
+                <li><a ui-sref="sensors" lu-show-permission="CAMOTION_SENSOR_GET,CAMOTION_HOST_GET">Sensors</a></li>
21 22
             </ul>
22 23
             <ul class="nav navbar-nav navbar-right">
23 24
                 <ul class="nav navbar-nav navbar-right">

+ 4
- 0
admin/views/sensors.html Näytä tiedosto

@@ -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
sdk/sensors.js Näytä tiedosto

@@ -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('SensorsService', ['luticateRequest', function (luticateRequest) {
9
+
10
+            var SensorsService = {};
11
+
12
+            SensorsService.add = function(data, promise)
13
+            {
14
+                return luticateRequest.post("/api/sensors/add", {sensor: JSON.stringify(data)}, null, promise);
15
+            };
16
+
17
+            SensorsService.getAll = function(data, promise)
18
+            {
19
+                return luticateRequest.get("/api/sensors", data, promise);
20
+            };
21
+
22
+            SensorsService.getAllTypes = function(promise)
23
+            {
24
+                return luticateRequest.get("/api/sensors/types", null, promise);
25
+            };
26
+
27
+            SensorsService.get = function(data, promise)
28
+            {
29
+                return luticateRequest.get("/api/sensors/" + data.sensor_id , null, promise);
30
+            };
31
+
32
+            SensorsService.edit = function(data, promise)
33
+            {
34
+                return luticateRequest.post("/api/sensors/" + data.sensor_id + "/edit", {sensor: JSON.stringify(data)}, null, promise);
35
+            };
36
+
37
+            SensorsService.del = function(data, promise)
38
+            {
39
+                return luticateRequest.post("/api/sensors/" + data.sensor_id + "/del", null, null, promise);
40
+            };
41
+
42
+            return SensorsService;
43
+        }]);
44
+})();

Loading…
Peruuta
Tallenna