Procházet zdrojové kódy

sensors get values

develop
Robin Thoni před 9 roky
rodič
revize
c72a917b57

+ 2
- 1
admin/app.js Zobrazit soubor

@@ -10,7 +10,8 @@ var camotion = angular.module('camotionAdmin', [
10 10
     'LocalStorageModule',
11 11
     'dialogs.main',
12 12
     'ngSanitize',
13
-    'JSONedit'
13
+    'JSONedit',
14
+    'camotionSdk'
14 15
 ]);
15 16
 
16 17
 camotion.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider',

+ 1
- 0
admin/index.html Zobrazit soubor

@@ -51,6 +51,7 @@
51 51
     <script src="controllers/modals/commandedit.controller.js"></script>
52 52
 
53 53
     <!-- SDK -->
54
+    <script src="../sdk/sdk.js"></script>
54 55
     <script src="../sdk/helper.js"></script>
55 56
     <script src="../sdk/hosts.js"></script>
56 57
     <script src="../sdk/cameras.js"></script>

+ 1
- 0
admin/views/navbar.html Zobrazit soubor

@@ -25,6 +25,7 @@
25 25
                 <ul class="nav navbar-nav navbar-right">
26 26
                     <li ng-show="isLogged()"><a ui-sref="home">{{ getUsername() }}</a></li>
27 27
                     <li ng-show="isLogged()"><a href="" ng-click="logout()">Logout</a></li>
28
+                    <li ng-show="!isLogged()"><a href="" ng-click="logout()">Login</a></li>
28 29
                 </ul>
29 30
             </ul>
30 31
         </div>

+ 44
- 13
camotion/app.js Zobrazit soubor

@@ -9,34 +9,65 @@ var camotion = angular.module('camotion', [
9 9
     'luticateAuth',
10 10
     'LocalStorageModule',
11 11
     'dialogs.main',
12
-    'ngSanitize'
12
+    'ngSanitize',
13
+    'camotionSdk'
13 14
 ]);
14 15
 
15 16
 camotion.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider',
16 17
     function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) {
17 18
 
18
-
19
-
20
-        $locationProvider.html5Mode(true);
19
+        $stateProvider.state('login',{
20
+            url:'/',
21
+            templateUrl:'views/login.html',
22
+            controller:'LoginController'
23
+        });
21 24
 
22 25
         $stateProvider.state('home',{
23
-            url:'/',
26
+            url:'/home',
24 27
             templateUrl:'views/home.html',
25
-            controller:'HomeController',
26
-            revolve:{}
28
+            controller:'HomeController'
29
+        });
30
+
31
+        $stateProvider.state('cameras',{
32
+            url:'/cameras',
33
+            templateUrl:'views/cameras.html',
34
+            controller:'CamerasController'
35
+        });
36
+
37
+        $stateProvider.state('sensors',{
38
+            url:'/sensors',
39
+            templateUrl:'views/sensors.html',
40
+            controller:'SensorsController'
27 41
         });
28 42
 
29
-        $stateProvider.state('qcm',{
30
-            url:'/qcm/{id:int}',
31
-            templateUrl:'views/qcm.html',
32
-            controller:'QcmController',
33
-            resolve:{}
43
+        $stateProvider.state('commands',{
44
+            url:'/commands',
45
+            templateUrl:'views/commands.html',
46
+            controller:'CommandsController'
34 47
         });
35 48
 
36 49
         $urlRouterProvider.otherwise('/');
37 50
 
51
+        $httpProvider.interceptors.push(['luticateAuthCache', '$injector', '$q',
52
+            function (luticateAuthCache, $injector, $q) {
53
+                return {
54
+                    'request': function (config) {
55
+                        var token = luticateAuthCache.getToken();
56
+
57
+                        if (token != null)
58
+                            config.headers['X-Authorization'] = token;
59
+
60
+                        return config;
61
+                    },
62
+                    'responseError': function(rejection) {
63
+                        return $q.reject(rejection);
64
+                    }
65
+                };
66
+            }]);
67
+
38 68
     }])
39
-    .run(['$rootScope', '$state',function ($rootScope, $state) {
69
+    .run(['$rootScope', '$state', 'luticateAuthUsers', function ($rootScope, $state, luticateAuthUsers) {
70
+        luticateAuthUsers.loadPermissions(null);
40 71
         $rootScope.$on('$stateChangeError', function (e, curr, prev) {
41 72
             $state.go('login');
42 73
         });

+ 76
- 0
camotion/controllers/commands.controller.js Zobrazit soubor

@@ -0,0 +1,76 @@
1
+angular.module('camotion')
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
+    }]);

+ 32
- 0
camotion/controllers/login.controller.js Zobrazit soubor

@@ -0,0 +1,32 @@
1
+angular.module('camotion')
2
+    .controller('LoginController', ['$scope', '$state', 'luticateAuthUsers', 'luticateAuthCache',
3
+        function($scope, $state, luticateAuthUsers, luticateAuthCache) {
4
+            $scope.username = "";
5
+            $scope.password = "";
6
+            $scope.errorString = null;
7
+
8
+            var promiseLogin = {
9
+                id: "promiseLogin",
10
+                loaderGroups: ["loginForm"]
11
+            };
12
+
13
+            var user = luticateAuthCache.getUser();
14
+            if (user != null) {
15
+                $state.go('home');
16
+                return;
17
+            }
18
+
19
+            $scope.login = function()
20
+            {
21
+                $scope.errorString = null;
22
+                luticateAuthUsers.login({username: $scope.username, password: $scope.password}, promiseLogin)
23
+                    .then(function(user)
24
+                    {
25
+                        $state.go('home');
26
+                    }, function(error)
27
+                    {
28
+                        $scope.errorString = error.Data;
29
+                    });
30
+            };
31
+        }
32
+    ]);

+ 46
- 0
camotion/controllers/navbar.controller.js Zobrazit soubor

@@ -0,0 +1,46 @@
1
+/**
2
+ * Created by robin on 11/1/15.
3
+ */
4
+
5
+angular.module('camotion')
6
+    .controller('NavBarController', ['$scope', 'luticateAuthCache', 'luticateAuthUsers', '$state',
7
+        function($scope, luticateAuthCache, luticateAuthUsers, $state) {
8
+
9
+
10
+            $scope.logout = function()
11
+            {
12
+                var promiseLogout = {
13
+                    id: "promiseLogout",
14
+                    loaderGroups: ["body"]
15
+                };
16
+                luticateAuthUsers.logout(promiseLogout).finally(function()
17
+                {
18
+                    $state.go('login');
19
+                });
20
+            };
21
+
22
+            $scope.isLogged = function()
23
+            {
24
+                var user = luticateAuthCache.getUser();
25
+                return user != null && user.Id != 0;
26
+            };
27
+
28
+            $scope.getUsername = function()
29
+            {
30
+                var user = luticateAuthCache.getUser();
31
+                if (user != null) {
32
+                    return user.Username;
33
+                }
34
+                return "";
35
+            };
36
+
37
+            $scope.canSeeAdmin = function()
38
+            {
39
+                return luticateAuthCache.hasOneEffectivePermission([
40
+                    "LU_USER_GET",
41
+                    "LU_GROUP_GET",
42
+                    "LU_PERM_GET",
43
+                    "CAMOTION_HOST_GET"
44
+                ]);
45
+            };
46
+        }]);

+ 71
- 0
camotion/controllers/sensors.controller.js Zobrazit soubor

@@ -0,0 +1,71 @@
1
+angular.module('camotion')
2
+    .controller('SensorsController', ['$scope', 'SensorsService', '$timeout',
3
+        function($scope, SensorsService, $timeout) {
4
+
5
+            $scope.values = {};
6
+            $scope.interval = 1000;
7
+
8
+            $scope.luTable = {
9
+                columns: [
10
+                    {
11
+                        name: "Name",
12
+                        width: 3,
13
+                        getValue: function (item) {
14
+                            return item.Name;
15
+                        }
16
+                    },{
17
+                        name: "Description",
18
+                        width: 7,
19
+                        getValue: function (item) {
20
+                            return item.Description;
21
+                        }
22
+                    },{
23
+                        name: "Value",
24
+                        width: 2,
25
+                        getValue: function (item) {
26
+                            return $scope.values[item.Id].value;
27
+                        }
28
+                    }
29
+                ],
30
+
31
+                getLoadPagePromise: function (page, perPage, query, promise) {
32
+                    return SensorsService.getAll({page: page, perPage: perPage, query: query}, promise);
33
+                },
34
+
35
+                onPageChanged: function()
36
+                {
37
+                    for (var i in $scope.values) {
38
+                        $timeout.cancel($scope.values[i].timerId);
39
+                    }
40
+                    $scope.values = {};
41
+                    var items = $scope.luTable.luBasicTableScope.items.Data;
42
+                    for (i = 0; i < items.length; ++i) {
43
+                        var item = angular.copy(items[i]);
44
+                        $scope.values[item.Id] = {
45
+                            value: 0,
46
+                            timerId: 0
47
+                        };
48
+                        $scope.updateSensor(item);
49
+                    }
50
+                }
51
+            };
52
+
53
+            $scope.updateSensor = function(sensor)
54
+            {
55
+                SensorsService.getValue({sensor_id: sensor.Id})
56
+                    .then(function(value) {
57
+                        if (typeof $scope.values[sensor.Id] != 'undefined') {
58
+                            $scope.values[sensor.Id] = {
59
+                                value: value,
60
+                                timerId: $timeout(function () {
61
+                                    $scope.updateSensor(sensor);
62
+                                }, $scope.interval)
63
+                            }
64
+                        }
65
+                    });
66
+            };
67
+
68
+            $scope.$on('$destroy', function(){
69
+                $scope.values = {};
70
+            });
71
+    }]);

+ 9
- 1
camotion/index.html Zobrazit soubor

@@ -33,11 +33,19 @@
33 33
     <script src="app.js"></script>
34 34
 
35 35
     <!-- Controller -->
36
+    <script src="controllers/login.controller.js"></script>
36 37
     <script src="controllers/home.controller.js"></script>
38
+    <script src="controllers/navbar.controller.js"></script>
39
+    <script src="controllers/sensors.controller.js"></script>
40
+    <script src="controllers/commands.controller.js"></script>
37 41
 
38 42
     <!-- Modal Controller -->
39 43
 
40 44
     <!-- SDK -->
45
+    <script src="../sdk/sdk.js"></script>
46
+    <script src="../sdk/cameras.js"></script>
47
+    <script src="../sdk/sensors.js"></script>
48
+    <script src="../sdk/commands.js"></script>
41 49
 
42 50
     <!-- Directives -->
43 51
 
@@ -45,7 +53,7 @@
45 53
 
46 54
 </head>
47 55
 <body>
48
-<ng-include src="'views/header.html'"></ng-include>
56
+<ng-include src="'views/navbar.html'"></ng-include>
49 57
 <div ui-view></div>
50 58
 <ng-include src="'views/footer.html'"></ng-include>
51 59
 

+ 4
- 0
camotion/views/commands.html Zobrazit soubor

@@ -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>

+ 26
- 0
camotion/views/login.html Zobrazit soubor

@@ -0,0 +1,26 @@
1
+<!-- Page Content -->
2
+<div class="container">
3
+    <div class="row">
4
+        <div class="col-sm-offset-4 col-sm-4">
5
+            <h1 class="text-center">Login</h1>
6
+
7
+            <form ng-submit="login()" lu-busy="loginForm">
8
+                <label for="login">Username</label><br />
9
+                <input id="login" ng-model="username" class="form-control" required/>
10
+
11
+                <label for="password">Password</label><br />
12
+                <input id="password" ng-model="password" type="password" class="form-control" required/>
13
+
14
+                <br />
15
+                <div class="col-sm-12 center-block">
16
+                    <button type="submit" class="btn btn-default center-block">Login</button>
17
+                </div>
18
+
19
+                <br />
20
+                <div>
21
+                    <span class="error" ng-show="errorString != null">{{ errorString }}</span>
22
+                </div>
23
+            </form>
24
+        </div>
25
+    </div>
26
+</div>

camotion/views/header.html → camotion/views/navbar.html Zobrazit soubor

@@ -1,6 +1,6 @@
1 1
 <body>
2 2
 <!-- Navigation -->
3
-<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
3
+<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" ng-controller="NavBarController">
4 4
     <div class="container">
5 5
         <!-- Brand and toggle get grouped for better mobile display -->
6 6
         <div class="navbar-header">
@@ -14,13 +14,18 @@
14 14
         </div>
15 15
         <!-- Collect the nav links, forms, and other content for toggling -->
16 16
         <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
17
+            <ul class="nav navbar-nav navbar-left">
18
+                <li><a ui-sref="cameras" lu-show-permission="CAMOTION_CAMERA_GET">Cameras</a></li>
19
+                <li><a ui-sref="sensors" lu-show-permission="CAMOTION_SENSOR_GET">Sensors</a></li>
20
+                <li><a ui-sref="commands" lu-show-permission="CAMOTION_COMMAND_GET">Commands</a></li>
21
+                <li><a href="/admin" ng-show="canSeeAdmin()">Administration</a></li>
22
+            </ul>
17 23
             <ul class="nav navbar-nav navbar-right">
18
-                <li>
19
-                    <a href="#">A propos</a>
20
-                </li>
21
-                <li>
22
-                    <a href="#">Contact</a>
23
-                </li>
24
+                <ul class="nav navbar-nav navbar-right">
25
+                    <li ng-show="isLogged()"><a ui-sref="cameras">{{ getUsername() }}</a></li>
26
+                    <li ng-show="isLogged()"><a href="" ng-click="logout()">Logout</a></li>
27
+                    <li ng-show="!isLogged()"><a href="" ng-click="logout()">Login</a></li>
28
+                </ul>
24 29
             </ul>
25 30
         </div>
26 31
         <!-- /.navbar-collapse -->

+ 4
- 0
camotion/views/sensors.html Zobrazit soubor

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

+ 1
- 1
sdk/cameras.js Zobrazit soubor

@@ -4,7 +4,7 @@
4 4
 (function () {
5 5
     'use strict';
6 6
 
7
-    angular.module('camotionAdmin')
7
+    angular.module('camotionSdk')
8 8
         .factory('CamerasService', ['luticateRequest', function (luticateRequest) {
9 9
 
10 10
             var CamerasService = {};

+ 1
- 1
sdk/commands.js Zobrazit soubor

@@ -4,7 +4,7 @@
4 4
 (function () {
5 5
     'use strict';
6 6
 
7
-    angular.module('camotionAdmin')
7
+    angular.module('camotionSdk')
8 8
         .factory('CommandsService', ['luticateRequest', function (luticateRequest) {
9 9
 
10 10
             var CommandsService = {};

+ 1
- 1
sdk/helper.js Zobrazit soubor

@@ -5,7 +5,7 @@
5 5
 (function () {
6 6
     'use strict';
7 7
 
8
-    angular.module('camotionAdmin')
8
+    angular.module('camotionSdk')
9 9
         .factory('HelperService', ['HostsService', '$q', function (HostsService, $q) {
10 10
 
11 11
             var HelperService = {};

+ 1
- 1
sdk/hosts.js Zobrazit soubor

@@ -4,7 +4,7 @@
4 4
 (function () {
5 5
     'use strict';
6 6
 
7
-    angular.module('camotionAdmin')
7
+    angular.module('camotionSdk')
8 8
         .factory('HostsService', ['luticateRequest', function (luticateRequest) {
9 9
 
10 10
             var HostsService = {};

+ 9
- 0
sdk/sdk.js Zobrazit soubor

@@ -0,0 +1,9 @@
1
+/**
2
+ * Created by robin on 11/22/15.
3
+ */
4
+
5
+(function () {
6
+    'use strict';
7
+
8
+    angular.module('camotionSdk', []);
9
+})();

+ 6
- 1
sdk/sensors.js Zobrazit soubor

@@ -4,7 +4,7 @@
4 4
 (function () {
5 5
     'use strict';
6 6
 
7
-    angular.module('camotionAdmin')
7
+    angular.module('camotionSdk')
8 8
         .factory('SensorsService', ['luticateRequest', function (luticateRequest) {
9 9
 
10 10
             var SensorsService = {};
@@ -39,6 +39,11 @@
39 39
                 return luticateRequest.post("/api/sensors/" + data.sensor_id + "/del", null, null, promise);
40 40
             };
41 41
 
42
+            SensorsService.getValue = function(data, promise)
43
+            {
44
+                return luticateRequest.post("/api/sensors/" + data.sensor_id + "/value", null, null, promise);
45
+            };
46
+
42 47
             return SensorsService;
43 48
         }]);
44 49
 })();

Načítá se…
Zrušit
Uložit