1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */
// controller for State View
angular.module('zmApp.controllers').controller('zmApp.StateCtrl', ['$ionicPopup', '$scope', 'zm', 'NVRDataModel', '$ionicSideMenuDelegate', '$ionicLoading', '$ionicModal', '$state', '$http', '$rootScope', '$timeout', '$ionicHistory', '$translate', '$stateParams', function (
$ionicPopup, $scope, zm, NVRDataModel, $ionicSideMenuDelegate, $ionicLoading, $ionicModal, $state, $http, $rootScope, $timeout, $ionicHistory, $translate, $stateParams) {
//----------------------------------------------------------------------
// Controller main
//----------------------------------------------------------------------
$scope.zmRun = "...";
$scope.zmLoad = "...";
$scope.zmDisk = "...";
$scope.color = "";
$scope.showDanger = false;
$scope.dangerText = [$translate.instant('kStateShowControls'), $translate.instant('kStateHideControls')];
$scope.dangerButtonColor = ["button-positive", "button-assertive"];
$scope.customState = "";
$scope.allStateNames = [];
$rootScope.zmPopup = "";
var loginData = NVRDataModel.getLogin();
var apiRun = loginData.apiurl + "/host/daemonCheck.json";
var apiLoad = loginData.apiurl + "/host/getLoad.json";
var apiStorage = loginData.apiurl + "/storage.json";
var apiServer = loginData.apiurl + "/servers.json";
var apiCurrentState = loginData.apiurl + "/States.json";
var apiExec = loginData.apiurl + "/states/change/";
var inProgress = 0; // prevents user from another op if one is in progress
getRunStatus();
getLoadStatus();
getCurrentState();
getStorageStatus();
getServerStatus();
// credit https://stackoverflow.com/a/14919494/1361529
$scope.humanFileSize = function(bytes, si) {
var thresh = si ? 1000 : 1024;
bytes = parseFloat(bytes);
if (isNaN(bytes)) bytes=0;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = si? ['kB','MB','GB','TB','PB','EB','ZB','YB']:['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
};
$scope.matchServer = function (id) {
var str = id;
var name = "";
for (var i=0; i< $scope.servers.length; i++) {
if ($scope.servers[i].Server.Id == id) {
name = $scope.servers[i].Server.Name;
break;
}
}
if (name) {
str = name + " ("+id+")";
}
return str;
};
$scope.toggleStorage = function() {
$scope.showStorage = !$scope.showStorage;
};
$scope.toggleServer = function() {
$scope.showServer = !$scope.showServer;
};
/*
$timeout(function () {
NVRDataModel.debug("invoking DiskStatus...");
getDiskStatus();
}, 6000);
*/
//-------------------------------------------------------------------------
// Lets make sure we set screen dim properly as we enter
// The problem is we enter other states before we leave previous states
// from a callback perspective in ionic, so we really can't predictably
// reset power state on exit as if it is called after we enter another
// state, that effectively overwrites current view power management needs
//------------------------------------------------------------------------
$scope.$on ('$ionicView.beforeEnter', function () {
$scope.showStorage = true;
$scope.showServer = true;
$scope.$on ( "process-push", function () {
NVRDataModel.debug (">> StateCtrl: push handler");
var s = NVRDataModel.evaluateTappedNotification();
NVRDataModel.debug("tapped Notification evaluation:"+ JSON.stringify(s));
$ionicHistory.nextViewOptions({
disableAnimate:true,
disableBack: true
});
$state.go(s[0],s[1],s[2]);
});
});
$scope.$on('$ionicView.enter', function () {
// console.log("**VIEW ** Montage Ctrl Entered");
NVRDataModel.setAwake(false);
});
$scope.$on('$ionicView.afterEnter', function () {
// console.log("**VIEW ** Montage Ctrl Entered");
console.log("STATE SHORTCUT: " + JSON.stringify($stateParams));
$stateParams.shortcut && $stateParams.shortcut.fn &&
$scope[$stateParams.shortcut.fn]($stateParams.shortcut.fnargs); // jshint ignore:line
});
//---------------------------------------------------------
// This gets the current run state custom name
// if applicable
//---------------------------------------------------------
function getCurrentState() {
NVRDataModel.debug("StateCtrl: getting state using " + apiCurrentState);
$http.get(apiCurrentState)
.then(
function (success) {
NVRDataModel.debug("State results: " + JSON.stringify(success));
var customStateArray = success.data.states;
var i = 0;
var found = false;
$scope.allStateNames = [];
for (i = 0; i < customStateArray.length; i++) {
$scope.allStateNames.push(customStateArray[i].State.Name);
if (customStateArray[i].State.IsActive == '1') {
$scope.customState = customStateArray[i].State.Name;
found = true;
}
}
if (!found) $scope.customState = "";
},
function (error) {
NVRDataModel.debug("StateCtrl: Error retrieving state list " + JSON.stringify(error));
$scope.customState = "";
}
);
}
//----------------------------------------------------------------
// Alarm notification handling
//----------------------------------------------------------------
$scope.handleAlarms = function () {
$rootScope.isAlarm = !$rootScope.isAlarm;
if (!$rootScope.isAlarm) {
$rootScope.alarmCount = "0";
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go("app.events", {
"id": 0,
"playEvent": false
}, {
reload: true
});
}
};
//---------------------------------------------------------
// Allows the user to select a custom run state
//---------------------------------------------------------
$scope.selectCustomState = function () {
selectCustomState();
};
function selectCustomState() {
$scope.myopt = {
selectedState: ""
};
//console.log(JSON.stringify($scope.allStateNames));
NVRDataModel.log("List of custom states: " + JSON.stringify($scope.allStateNames));
$rootScope.zmPopup = $ionicPopup.show({
scope: $scope,
template: '<ion-radio-fix ng-repeat="item in allStateNames" ng-value="item" ng-model="myopt.selectedState"> {{item}} </ion-radio-fix>',
title: $translate.instant('kSelectRunState'),
subTitle: $translate.instant('kCurrentState') + $scope.customState ? ($translate.instant('kCurrentState') + ": " + $scope.customState) : "",
buttons: [{
text: $translate.instant('kButtonCancel'),
onTap: function (e) {
return "CANCEL";
}
},
{
text: $translate.instant('kButtonOk'),
onTap: function (e) {
return "OK";
}
}
]
});
// It seems invoking a popup within a popup handler
// causes issues. Doing this outside due to that reason
$rootScope.zmPopup.then(function (res) {
// console.log("GOT : " + JSON.stringify(res));
if (res == "OK") {
if ($scope.myopt.selectedState != "")
controlZM($scope.myopt.selectedState);
}
});
}
//----------------------------------------------------------------------
// returns Storage data
//----------------------------------------------------------------------
function getStorageStatus() {
$scope.storage = [];
NVRDataModel.debug("StorageStatus: " + apiStorage);
$http.get(apiStorage)
.then(
function (success) {
$scope.storage = success.data.storage;
//console.log (JSON.stringify($scope.storage));
},
function (error) {
$scope.zmDisk = "unknown";
// console.log("ERROR:" + JSON.stringify(error));
NVRDataModel.log("Error retrieving DiskStatus: " + JSON.stringify(error), "error");
}
);
}
//----------------------------------------------------------------------
// returns Storage data
//----------------------------------------------------------------------
function getServerStatus() {
$scope.servers = [];
NVRDataModel.debug("ServerStatus: " + apiStorage);
$http.get(apiServer)
.then(
function (success) {
$scope.servers = success.data.servers;
// console.log (JSON.stringify($scope.storage));
},
function (error) {
$scope.zmDisk = "unknown";
// console.log("ERROR:" + JSON.stringify(error));
NVRDataModel.log("Error retrieving DiskStatus: " + JSON.stringify(error), "error");
}
);
}
//----------------------------------------------------------------------
// returns ZM running status
//----------------------------------------------------------------------
function getRunStatus() {
NVRDataModel.debug("StateCtrl/getRunStatus: " + apiRun);
$http.get(apiRun)
.then(
function (success) {
NVRDataModel.debug("StateCtrl/getRunStatus: success");
NVRDataModel.debug("Run results: " + JSON.stringify(success));
switch (success.data.result) {
case 1:
$scope.zmRun = $translate.instant('kZMRunning');
$scope.color = 'green';
break;
case 0:
$scope.zmRun = $translate.instant('kZMStopped');
$scope.color = 'red';
break;
default:
$scope.zmRun = $translate.instant('kZMUndetermined');
$scope.color = 'orange';
break;
}
// console.log("X"+success.data.result+"X");
},
function (error) {
//console.log("ERROR in getRun: " + JSON.stringify(error));
NVRDataModel.log("Error getting RunStatus " + JSON.stringify(error), "error");
$scope.color = 'red';
$scope.zmRun = $translate.instant('kZMUndetermined');
}
);
}
//----------------------------------------------------------------------
// gets ZM load - max[0], avg[1], min[2]
//----------------------------------------------------------------------
function getLoadStatus() {
NVRDataModel.debug("StateCtrl/getLoadStatus: " + apiLoad);
$http.get(apiLoad)
.then(
function (success) {
NVRDataModel.debug("Load results: " + JSON.stringify(success));
//console.log(JSON.stringify(success));
// load returns 3 params - one in the middle is avg.
NVRDataModel.debug("StateCtrl/getLoadStatus: success");
$scope.zmLoad = success.data.load[1];
// console.log("X"+success.data.result+"X");
},
function (error) {
//console.log("ERROR in getLoad: " + JSON.stringify(error));
NVRDataModel.log("Error retrieving loadStatus " + JSON.stringify(error), "error");
$scope.zmLoad = 'undetermined';
}
);
}
//----------------------------------------------------------------------
// start/stop/restart ZM
//----------------------------------------------------------------------
function performZMoperation(str) {
NVRDataModel.debug("inside performZMoperation with " + str);
$scope.zmRun = "...";
$scope.color = 'orange';
$scope.customState = "";
NVRDataModel.debug("StateCtrl/controlZM: POST Control command is " + apiExec + str + ".json");
inProgress = 1;
$http.post(apiExec + str + ".json")
.then(
function (success) {
NVRDataModel.debug("StateCtrl/controlZM: returned success");
inProgress = 0;
switch (str) {
case "stop":
$scope.zmRun = $translate.instant('kZMStopped');
$scope.color = 'red';
break;
default:
$scope.zmRun = $translate.instant('kZMRunning');
$scope.color = 'green';
getCurrentState();
break;
}
},
function (error) {
//if (error.status) // it seems to return error with status 0 if ok
// {
//console.log("ERROR in Change State:" + JSON.stringify(error));
NVRDataModel.debug("StateCtrl/controlZM: returned error");
NVRDataModel.log("Error in change run state:" + JSON.stringify(error), "error");
$scope.zmRun = $translate.instant('kZMUndetermined');
$scope.color = 'orange';
inProgress = 0;
});
}
function controlZM(str) {
if (inProgress) {
NVRDataModel.debug("StateCtrl/controlZM: operation in progress");
$ionicPopup.alert({
title: $translate.instant('kOperationInProgressTitle'),
template: $translate.instant('kOperationInProgressBody') + '...',
okText: $translate.instant('kButtonOk'),
cancelText: $translate.instant('kButtonCancel'),
});
return;
}
var statesearch = "startstoprestart";
var promptstring = $translate.instant('kStateAreYouSure') + str + ' Zoneminder?';
if (statesearch.indexOf(str) == -1) {
promptstring = "Are you sure you want to change state to " + str;
}
$rootScope.zmPopup = $ionicPopup.show({
title: $translate.instant('kPleaseConfirm'),
template: promptstring,
buttons: [{
text: $translate.instant('kButtonCancel'),
type: 'button-positive'
},
{
text: $translate.instant('kButtonOk'),
type: 'button-assertive',
onTap: function (e) {
performZMoperation(str);
}
}
]
});
}
// Binder so template can call controlZM
$scope.controlZM = function (str) {
controlZM(str);
};
$scope.openMenu = function () {
$ionicSideMenuDelegate.toggleLeft();
};
$scope.$on('$ionicView.leave', function () {
// console.log("**VIEW ** State Ctrl Left");
// FIXME not the best way...
// If the user exits a view before its complete,
// make sure he can come back in and redo
inProgress = 0;
});
$scope.doRefresh = function () {
//console.log("***Pull to Refresh");
NVRDataModel.debug("StateCtrl/refresh: calling getRun/Load/Disk/CurrentState");
getRunStatus();
getLoadStatus();
getCurrentState();
getStorageStatus();
getServerStatus();
$scope.$broadcast('scroll.refreshComplete');
};
}]);
|