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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
|
// Controller for the montage view
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console,ionic,Masonry,moment */
// FIXME: This is a copy of montageCtrl - needs a lot of code cleanup
angular.module('zmApp.controllers').controller('zmApp.MontageHistoryCtrl', ['$scope', '$rootScope', 'ZMDataModel', 'message', '$ionicSideMenuDelegate', '$timeout', '$interval', '$ionicModal', '$ionicLoading', '$http', '$state', '$ionicPopup', '$stateParams', '$ionicHistory', '$ionicScrollDelegate', '$ionicPlatform', 'zm', '$ionicPopover', '$controller', 'imageLoadingDataShare', '$window', function ($scope, $rootScope, ZMDataModel, message, $ionicSideMenuDelegate, $timeout, $interval, $ionicModal, $ionicLoading, $http, $state, $ionicPopup, $stateParams, $ionicHistory, $ionicScrollDelegate, $ionicPlatform, zm, $ionicPopover, $controller, imageLoadingDataShare, $window) {
$controller('zmApp.BaseController', {
$scope: $scope
});
//--------------------------------------
// formats events dates in a nice way
//---------------------------------------
$scope.prettifyDate = function (str) {
return moment(str).format('MMM Do, YYYY '+ZMDataModel.getTimeFormat());
};
function prettifyDate(str) {
return moment(str).format('MMM Do');
}
$scope.prettifyTime = function (str) {
return moment(str).format('h:mm a');
};
$scope.prettify = function (str) {
return moment(str).format(ZMDataModel.getTimeFormat()+' on MMMM Do YYYY');
};
//--------------------------------------
// pause/unpause nph-zms
//---------------------------------------
$scope.togglePause = function (mid)
{
//console.log ("TOGGLE PAUSE " + mid);
var m = -1;
for (var i=0; i < $scope.MontageMonitors.length; i++)
{
if ($scope.MontageMonitors[i].Monitor.Id == mid)
{
m = i;
break;
}
}
if (m != -1)
{
$scope.MontageMonitors[m].Monitor.isPaused = !$scope.MontageMonitors[m].Monitor.isPaused;
var cmd = $scope.MontageMonitors[m].Monitor.isPaused? 1:2;
ZMDataModel.zmDebug ("Sending CMD:"+cmd+" for monitor "+$scope.MontageMonitors[m].Monitor.Name);
controlEventStream(cmd,"",$scope.MontageMonitors[m].Monitor.connKey,-1);
}
};
//--------------------------------------
// Called when ion-footer collapses
// note that on init it is also called
//---------------------------------------
$scope.footerCollapse = function()
{
footerCollapse();
};
/* Note this is also called when the view is first loaded */
function footerCollapse()
{
if (readyToRun == false)
{
ZMDataModel.zmDebug ("fake call to footerCollapse - ignoring");
return;
}
ZMDataModel.stopNetwork();
var ld = ZMDataModel.getLogin();
$scope.sliderVal.realRate = $scope.sliderVal.rate *100;
//ZMDataModel.zmDebug ("Playback rate is:" + $scope.sliderVal.realRate);
var TimeObjectFrom = moment($scope.datetimeValueFrom.value).format("YYYY-MM-DD HH:mm");
var TimeObjectTo = moment($scope.datetimeValueTo.value).format('YYYY-MM-DD HH:mm');
// console.log ("TIME START: " + TimeObjectFrom + " " + TimeObjectTo);
//console.log ("TIME START: " + TimeObjectFrom + " " + TimeObjectTo);
var apiurl;
// release all active streams
for (var i=0; i< $scope.MontageMonitors.length; i++)
{
// generate new connKeys if timeline changes
if ($scope.MontageMonitors[i].Monitor.eventUrl !='img/noevent.png')
{
ZMDataModel.zmLog ("footerCollapse: Calling kill with " + $scope.MontageMonitors[i].Monitor.connKey + " because url is " +$scope.MontageMonitors[i].Monitor.eventUrl ) ;
var tmpCK = angular.copy($scope.MontageMonitors[i].Monitor.connKey);
timedControlEventStream(2500,17,"",tmpCK,-1);
$scope.MontageMonitors[i].Monitor.eventUrl = "img/noevent.png";
$scope.MontageMonitors[i].Monitor.connKey = (Math.floor((Math.random() * 999999) + 1)).toString();
//console.log ("Generating connkey: " +$scope.MontageMonitors[i].Monitor.connKey);
}
else
{
//console.log ("footerCollapse: Skipped kill: connkey:"+$scope.MontageMonitors[i].Monitor.connKey + " function " + $scope.MontageMonitors[i].Monitor.Function + " listDisplay " + $scope.MontageMonitors[i].Monitor.lisDisplay + " enabled " + $scope.MontageMonitors[i].Monitor.Enabled + " eventURL " + $scope.MontageMonitors[i].Monitor.eventUrl);
}
}
// grab events that start on or before the time and end on or after the time
// this should only bring up events that span that time
apiurl= ld.apiurl + "/events/index/StartTime >=:"+TimeObjectFrom+"/EndTime <=:"+TimeObjectTo+".json";
ZMDataModel.zmLog ("Event timeline API is " + apiurl);
$http.get(apiurl)
.success( function(data) {
var ld = ZMDataModel.getLogin();
ZMDataModel.zmDebug ("Got "+data.events.length+"new history events...");
var eid, mid, stime;
for (i=0; i<data.events.length; i++)
{
mid = data.events[i].Event.MonitorId;
eid = data.events[i].Event.Id;
stime = data.events[i].Event.StartTime;
// only take the first one for each monitor
for (var j=0; j < $scope.MontageMonitors.length; j++)
{
// that's the earliest match and play gapless from there
if ($scope.MontageMonitors[j].Monitor.Id == mid)
{
if ($scope.MontageMonitors[j].Monitor.eventUrl == 'img/noevent.png')
{
// console.log ("Old value of event url " + $scope.MontageMonitors[j].eventUrl);
//console.log ("ldurl is " + ld.streamingurl);
$scope.MontageMonitors[j].Monitor.eventUrl=ld.streamingurl+"/nph-zms?source=event&mode=jpeg&event="+eid+"&frame=1&replay=gapless&rate="+$scope.sliderVal.realRate+"&connkey="+$scope.MontageMonitors[j].Monitor.connKey+"&scale="+ld.montageHistoryQuality+"&rand="+$rootScope.rand;
//console.log ("Setting event URL to " +$scope.MontageMonitors[j].Monitor.eventUrl);
// console.log ("SWITCHING TO " + $scope.MontageMonitors[j].eventUrl);
$scope.MontageMonitors[j].Monitor.eventUrlTime = stime;
}
}
}
}
// make sure we do our best to get that duration for all monitors
// in the above call, is possible some did not make the cut in the first page
ZMDataModel.zmLog ("Making sure all monitors have a fair chance...");
for (i=0; i<$scope.MontageMonitors.length; i++)
{
if ($scope.MontageMonitors[i].Monitor.eventUrl=='img/noevent.png')
{
var indivGrab = ld.apiurl + "/events/index/MonitorId:"+$scope.MontageMonitors[i].Monitor.Id+"/StartTime >=:"+TimeObjectFrom+"/EndTime <=:"+TimeObjectTo+".json";
ZMDataModel.zmDebug("Monitor " + $scope.MontageMonitors[i].Monitor.Id+":"+$scope.MontageMonitors[i].Monitor.Name + " does not have events, trying "+indivGrab);
getExpandedEvents(i,indivGrab);
}
}
})
.error (function (data) {
ZMDataModel.zmDebug ("history ERROR:"+ JSON.stringify(data));
});
function getExpandedEvents(i,indivGrab)
{
var ld = ZMDataModel.getLogin();
// console.log ("EXPANDED EVENT " + i + " " + indivGrab);
$http.get(indivGrab)
.success(function(data)
{
// console.log ("EXPANDED DATA FOR MONITOR " + i + JSON.stringify(data));
if (data.events.length > 0 )
{
if (!ZMDataModel.isBackground())
{
$scope.MontageMonitors[i].Monitor.eventUrl=ld.streamingurl+"/nph-zms?source=event&mode=jpeg&event="+data.events[0].Event.Id+"&frame=1&replay=gapless&rate="+$scope.sliderVal.realRate+"&connkey="+$scope.MontageMonitors[i].Monitor.connKey+"&scale="+ld.montageHistoryQuality+"&rand="+$rootScope.rand;
//console.log ("SWITCHING TO " + $scope.MontageMonitors[i].eventUrl);
$scope.MontageMonitors[i].Monitor.eventUrlTime = data.events[0].Event.StartTime;
ZMDataModel.zmLog ("Found expanded event "+data.events[0].Event.Id+" for monitor " + $scope.MontageMonitors[i].Monitor.Id);
}
else
{
// $scope.MontageMonitors[i].eventUrl="img/noevent.png";
// $scope.MontageMonitors[i].eventUrlTime = "";
// ZMDataModel.zmLog ("Setting img src to null as data received in background");
}
}
})
.error (function(data)
{
});
}
}
//---------------------------------------------------------
// This is periodically called to get the current playing
// event by zms. I use this to display a timestamp
// Its a 2 step process - get event Id then go a Event
// API call to get time stamp. Sucks
//---------------------------------------------------------
function checkAllEvents()
{
//console.log ("Events are checked....");
for (var i=0; i<$scope.MontageMonitors.length; i++)
{
// don't check for monitors that are not shown
// because nph connkey won't exist and the response
// will fail
if ($scope.MontageMonitors[i].Monitor.eventUrl !="" && $scope.MontageMonitors[i].Monitor.eventUrl !='img/noevent.png' && $scope.MontageMonitors[i].Monitor.connKey !='' &&
$scope.MontageMonitors[i].Monitor.Function !='None' &&
$scope.MontageMonitors[i].Monitor.listDisplay!='noshow' &&
$scope.MontageMonitors[i].Monitor.Enabled !='0')
{
ZMDataModel.zmDebug ("Checking event status for " + $scope.MontageMonitors[i].Monitor.Name + ":"+$scope.MontageMonitors[i].Monitor.eventUrl+":"+$scope.MontageMonitors[i].Monitor.Function+":"+$scope.MontageMonitors[i].Monitor.listDisplay);
controlEventStream('99','',$scope.MontageMonitors[i].Monitor.connKey, i);
}
}
}
$scope.dateChanged = function()
{
// window.stop();
// console.log (">>>>>>>>>>>>>>>>>>>>>>>>>>>>> BAD BAD");
footerCollapse();
};
//--------------------------------------------------------------
// Used to control zms for a connkey. If ndx is not -1,
// then it also calls an event API for the returned eid
// and stores its time in the montage monitors array
//--------------------------------------------------------------
$scope.controlEventStream = function (cmd,disp,connkey,ndx)
{
controlEventStream(cmd,disp,connkey,ndx);
};
function timedControlEventStream (mTime, cmd, disp, connkey, ndx)
{
var mMtime = mTime || 2000;
ZMDataModel.zmDebug ("Deferring control " + cmd + " by " + mMtime);
$timeout ( function()
{
subControlStream(cmd,connkey);
},mMtime);
}
function subControlStream(cmd,connkey)
{
var loginData = ZMDataModel.getLogin();
var myauthtoken = $rootScope.authSession.replace("&auth=","");
//&auth=
var req = $http({
method: 'POST',
/*timeout: 15000,*/
url: loginData.url + '/index.php',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//'Accept': '*/*',
},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var foo = str.join("&");
//console.log("****SUB RETURNING " + foo);
return foo;
},
data: {
view: "request",
request: "stream",
connkey: connkey,
command: cmd,
auth: myauthtoken,
// user: loginData.username,
// pass: loginData.password
}
});
req.success (function (resp) {
ZMDataModel.zmDebug ("subControl success:"+JSON.stringify(resp));
});
req.error (function (resp) {
ZMDataModel.zmDebug ("subControl error:"+JSON.stringify(resp));
});
}
function controlEventStream(cmd, disp, connkey, ndx) {
// console.log("Command value " + cmd);
if (disp) {
$ionicLoading.hide();
$ionicLoading.show({
template: "please wait...",
noBackdrop: true,
duration: zm.loadingTimeout,
});
}
var loginData = ZMDataModel.getLogin();
/*
var CMD_NONE = 0;
var CMD_PAUSE = 1;
var CMD_PLAY = 2;
var CMD_STOP = 3;
var CMD_FASTFWD = 4;
var CMD_SLOWFWD = 5;
var CMD_SLOWREV = 6;
var CMD_FASTREV = 7;
var CMD_ZOOMIN = 8;
var CMD_ZOOMOUT = 9;
var CMD_PAN = 10;
var CMD_SCALE = 11;
var CMD_PREV = 12;
var CMD_NEXT = 13;
var CMD_SEEK = 14;
var CMD_QUERY = 99;
*/
// You need to POST commands to control zms
// Note that I am url encoding the parameters into the URL
// If I leave it as JSON, it gets converted to OPTONS due
// to CORS behaviour and ZM/Apache don't seem to handle it
//console.log("POST: " + loginData.url + '/index.php');
//console.log ("AUTH IS " + $rootScope.authSession);
var myauthtoken = $rootScope.authSession.replace("&auth=","");
//&auth=
var req = $http({
method: 'POST',
/*timeout: 15000,*/
url: loginData.url + '/index.php',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//'Accept': '*/*',
},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var foo = str.join("&");
// console.log("****RETURNING " + foo);
return foo;
},
data: {
view: "request",
request: "stream",
connkey: connkey,
command: cmd,
auth: myauthtoken,
// user: loginData.username,
// pass: loginData.password
}
});
req.success(function (resp) {
// console.log("SUCCESS FOR: " + JSON.stringify(resp));
if (resp.result=="Ok" && ndx != -1)
{
var ld = ZMDataModel.getLogin();
var apiurl= ld.apiurl + "/events/"+resp.status.event+".json";
//console.log ("API " + apiurl);
$http.get (apiurl)
.success (function (data)
{
var currentEventTime = moment(data.event.Event.StartTime);
var maxTime = moment($scope.datetimeValueTo.value);
//ZMDataModel.zmDebug ("Monitor: " + $scope.MontageMonitors[ndx].Monitor.Id + " max time="+maxTime + "("+$scope.datetimeValueTo.value+")"+ " current="+currentEventTime + "("+data.event.Event.StartTime+")");
if ($scope.MontageMonitors[ndx].Monitor.eventUrlTime!=data.event.Event.StartTime && currentEventTime.diff(maxTime) <= 0 )
{
var ld = ZMDataModel.getLogin();
var element = angular.element(document.getElementById($scope.MontageMonitors[ndx].Monitor.Id+"-timeline"));
element.removeClass ('animated flipInX');
element.addClass('animated flipOutX');
$timeout (function() {
element.removeClass ('animated flipOutX');
element.addClass('animated flipInX');
$scope.MontageMonitors[ndx].Monitor.eventUrlTime=data.event.Event.StartTime;
$scope.MontageMonitors[ndx].Monitor.eventUrl=ld.streamingurl+"/nph-zms?source=event&mode=jpeg&event="+data.event.Event.Id+"&frame=1&replay=gapless&rate="+$scope.sliderVal.realRate+"&connkey="+$scope.MontageMonitors[ndx].Monitor.connKey+"&scale="+ld.montageHistoryQuality+"&rand="+$rootScope.rand;
},700);
}
else if ( currentEventTime.diff(maxTime)>0)
{
ZMDataModel.zmDebug (">>>>>>>>Monitor " + $scope.MontageMonitors[ndx].Monitor.Id + " event time of " + data.event.Event.StartTime + " exceeds " + $scope.datetimeValueTo.value +" stopping...");
subControlStream(17,connkey);
}
})
.error (function (data)
{
$scope.MontageMonitors[ndx].Monitor.eventUrlTime="-";
});
}
});
req.error(function (resp) {
//console.log("ERROR: " + JSON.stringify(resp));
ZMDataModel.zmLog("Error sending event command " + JSON.stringify(resp), "error");
});
}
//---------------------------------------------------------------------
// Controller main
//---------------------------------------------------------------------
document.addEventListener("pause", onPause, false);
document.addEventListener("resume", onResume, false);
$scope.timeFormat = "yyyy-MM-dd "+ZMDataModel.getTimeFormat();
$scope.displayDateTimeSliders = true;
$scope.showtimers = true;
$scope.loginData = ZMDataModel.getLogin();
var curYear = new Date().getFullYear();
var readyToRun = false;
var i;
$scope.sliderVal = {
rate:1,
realRate:100,
hideNoEvents:false,
enableGapless:true,
exactMatch:false,
showTimeline:true
};
// default = start of day
var timeto = moment();
var timefrom = moment().startOf('day');
$scope.sliderVal.rate = 1;
$scope.sliderVal.realRate = $scope.sliderVal.rate *100;
//var tdatetimeValueFrom = new Date();
//tdatetimeValueFrom.setDate(tdatetimeValueFrom.getDate()-1);
$scope.datetimeValueFrom = {value:""};
$scope.datetimeValueTo = {value:""};
$scope.datetimeValueFrom.value = timefrom.toDate();
$scope.datetimeValueTo.value = timeto.toDate();
$rootScope.eventQueryInterval="";
var commonCss =
{
background: {
"background-color": "silver"
},
before: {
"background-color": "purple"
},
default: {
"background-color": "white"
}, // default value: 1px
after: {
"background-color": "green"
}, // zone after default value
pointer: {
"background-color": "red"
}, // circle pointer
range: {
"background-color": "red"
} // use it if double value
};
$scope.slider_modal_options_rate = {
from: 1,
to: 10,
realtime: true,
step: 1,
className: "mySliderClass",
//modelLabels:function(val) {return "";},
smooth: false,
css: commonCss,
dimension:'X'
};
var isLongPressActive = false;
$scope.isReorder = false;
var intervalHandleMontage; // will hold image resize timer on long press
var montageIndex = 0; // will hold monitor ID to scale in timer
var gridcontainer;
$scope.monitorSize = []; // array with montage sizes per monitor
$scope.scaleDirection = []; // 1 = increase -1 = decrease
$scope.slider = {};
//console.log ("************ HISTORY " + ZMDataModel.getMontageSize());
$scope.slider.monsize = ZMDataModel.getMontageSize();
$scope.revMonSize = 11 - parseInt($scope.slider.monsize);
// The difference between old and original is this:
// old will have a copy of the last re-arranged monitor list
// while original will have a copy of the order returned by ZM
var oldMonitors = []; // To keep old order if user cancels after sort;
// Montage display order may be different so don't
// mangle monitors as it will affect other screens
// in Montage screen we will work with this local copy
//$scope.MontageMonitors = angular.copy ($scope.monitors);
var montageOrder = []; // This array will keep the ordering in montage view
var hiddenOrder = []; // 1 = hide, 0 = don't hide
var tempMonitors = message;
if (tempMonitors.length == 0)
{
$rootScope.zmPopup= $ionicPopup.alert({
title: "No Monitors found",
template: "Please check your credentials"
});
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go("login");
return;
}
// console.log ("TEMP MONITORS IS " + JSON.stringify(tempMonitors));
var tempResponse = ZMDataModel.applyMontageMonitorPrefs(message, 0);
$scope.monitors = tempResponse[0];
montageOrder = tempResponse[1];
hiddenOrder = tempResponse[2];
ZMDataModel.zmLog("Inside MontageHistoryCtrl:We found " + $scope.monitors.length + " monitors");
$scope.MontageMonitors = ZMDataModel.applyMontageMonitorPrefs (message, 1)[0];
var loginData = ZMDataModel.getLogin();
$scope.packMontage = loginData.packMontage;
// init monitors
ZMDataModel.zmDebug(">>Initializing connkeys and images...");
for ( i=0; i< $scope.MontageMonitors.length; i++)
{
//$scope.MontageMonitors[i].Monitor.connKey='';
$scope.MontageMonitors[i].Monitor.connKey = (Math.floor((Math.random() * 999999) + 1)).toString();
$scope.MontageMonitors[i].Monitor.eventUrl ='img/noevent.png';
$scope.MontageMonitors[i].Monitor.eventUrlTime="";
$scope.MontageMonitors[i].Monitor.isPaused=false;
}
readyToRun = true;
// --------------------------------------------------------
// Handling of back button in case modal is open should
// close the modal
// --------------------------------------------------------
$ionicPlatform.registerBackButtonAction(function (e) {
e.preventDefault();
if ($scope.modal && $scope.modal.isShown())
{
// switch off awake, as liveview is finished
ZMDataModel.zmDebug("Modal is open, closing it");
ZMDataModel.setAwake(false);
$scope.modal.remove();
$scope.isModalActive = false;
}
else
{
ZMDataModel.zmDebug("Modal is closed, so toggling or exiting");
if (!$ionicSideMenuDelegate.isOpenLeft())
{
$ionicSideMenuDelegate.toggleLeft();
}
else
{
navigator.app.exitApp();
}
}
}, zm.eventHistoryTimer);
$scope.showSizeButtons = false;
$ionicPopover.fromTemplateUrl('templates/help/montage-help.html', {
scope: $scope,
}).then(function (popover) {
$scope.popover = popover;
});
var timestamp = new Date().getUTCMilliseconds();
$scope.minimal = $stateParams.minimal;
$scope.zmMarginTop = $scope.minimal ? 0:15;
//console.log ("********* MARGIN IS " + $scope.zmMarginTop);
$scope.isRefresh = $stateParams.isRefresh;
var sizeInProgress = false;
$scope.imageStyle = true;
$ionicSideMenuDelegate.canDragContent(false);
// Do we have a saved montage array size? No?
// if (window.localStorage.getItem("montageArraySize") == undefined) {
if (loginData.montageArraySize == '0') {
for ( i = 0; i < $scope.monitors.length; i++) {
$scope.monitorSize.push(ZMDataModel.getMontageSize());
$scope.scaleDirection.push(1);
}
} else // recover previous settings
{
var msize = loginData.montageArraySize;
//console.log("MontageArrayString is=>" + msize);
$scope.monitorSize = msize.split(":");
var j;
for (j = 0; j < $scope.monitorSize.length; j++) {
// convert to number other wise adding to it concatenates :-)
$scope.monitorSize[j] = parseInt($scope.monitorSize[j]);
$scope.scaleDirection.push(1);
//console.log("Montage size for monitor " + j + " is " + $scope.monitorSize[j]);
}
}
// $scope.monitorSize = monitorSize;
// $scope.scaleDirection = scaleDirection;
$scope.LoginData = ZMDataModel.getLogin();
$scope.monLimit = $scope.LoginData.maxMontage;
if ($rootScope.platformOS!='ios')
{
ZMDataModel.zmLog ("Limiting montage to 5, thanks to Chrome's stupid connection limit");
$scope.monLimit = 5;
}
//console.log("********* Inside MontageHistoryCtrl, MAX LIMIT=" + $scope.monLimit);
$rootScope.authSession = "undefined";
$ionicLoading.show({
template: 'negotiating stream authentication...',
animation: 'fade-in',
showBackdrop: true,
duration: zm.loadingTimeout,
maxWidth: 300,
showDelay: 0
});
var ld = ZMDataModel.getLogin();
//console.log ("MONITORS " + JSON.stringify($scope.monitors));
$rootScope.validMonitorId = $scope.monitors[0].Monitor.Id;
ZMDataModel.getAuthKey($rootScope.validMonitorId)
.then(function (success) {
$ionicLoading.hide();
//console.log(success);
$rootScope.authSession = success;
ZMDataModel.zmLog("Stream authentication construction: " +
$rootScope.authSession);
},
function (error) {
$ionicLoading.hide();
ZMDataModel.zmDebug("MontageHistoryCtrl: Error in authkey retrieval " + error);
//$rootScope.authSession="";
ZMDataModel.zmLog("MontageHistoryCtrl: Error returned Stream authentication construction. Retaining old value of: " + $rootScope.authSession);
});
// I was facing a lot of problems with Chrome/crosswalk getting stuck with
// pending HTTP requests after a while. There is a problem with chrome handling
// multiple streams of always open HTTP get's (image streaming). This problem
// does not arise when the image is streamed for a single monitor - just multiple
// To work around this I am taking a single snapshot of ZMS and have implemented a timer
// to reload the snapshot every 1 second. Seems to work reliably even thought its a higer
// load. Will it bonk with many monitors? Who knows. I have tried with 5 and 1280x960@32bpp
function loadNotifications() {
//$rootScope.rand = Math.floor((Math.random() * 100000) + 1);
//console.log ("Inside Montage timer...");
}
var intervalHandle;
$scope.isModalActive = false;
var modalIntervalHandle;
$scope.closeReorderModal = function () {
//console.log("Close & Destroy Monitor Modal");
// switch off awake, as liveview is finished
//ZMDataModel.setAwake(false);
$scope.modal.remove();
};
$scope.isBackground = function()
{
return ZMDataModel.isBackground();
};
//----------------------------------------------------------------
// Alarm notification handling
//----------------------------------------------------------------
$scope.handleAlarms = function()
{
$rootScope.isAlarm=!$rootScope.isAlarm;
if (!$rootScope.isAlarm)
{
$rootScope.alarmCount="0";
$ionicHistory.nextViewOptions({disableBack: true});
$state.go("events", {"id": 0}, { reload: true });
}
};
$scope.handleAlarmsWhileMinimized = function()
{
$rootScope.isAlarm=!$rootScope.isAlarm;
$scope.minimal = !$scope.minimal;
ZMDataModel.zmDebug("MontageHistoryCtrl: switch minimal is " + $scope.minimal);
ionic.Platform.fullScreen($scope.minimal, !$scope.minimal);
$interval.cancel(intervalHandle);
$interval.cancel($rootScope.eventQueryInterval);
if (!$rootScope.isAlarm)
{
$rootScope.alarmCount="0";
$ionicHistory.nextViewOptions({disableBack: true});
$state.go("events", {"id": 0}, { reload: true });
}
};
//-------------------------------------------------------------
// this is checked to make sure we are not pulling images
// when app is in background. This is a problem with Android,
// for example
//-------------------------------------------------------------
$scope.isBackground = function()
{
//console.log ("Is background called from Montage and returned " +
//ZMDataModel.isBackground());
return ZMDataModel.isBackground();
};
//-------------------------------------------------------------
// Called when user taps on the reorder button
//-------------------------------------------------------------
$scope.reorderList = function () {
//console.log("REORDER");
$scope.data.showDelete = false;
$scope.data.showReorder = !$scope.data.showReorder;
};
$scope.deleteList = function () {
//console.log("DELETE");
$scope.data.showDelete = !$scope.data.showDelete;
$scope.data.showReorder = false;
};
$scope.reloadReorder = function () {
var refresh = ZMDataModel.getMonitors(1);
refresh.then(function (data) {
$scope.monitors = data;
$scope.MontageMonitors = data;
oldMonitors = angular.copy($scope.monitors);
var i;
montageOrder = [];
for (i = 0; i < $scope.monitors.length; i++) {
montageOrder[i] = i;
hiddenOrder[i] = 0;
}
loginData.montageOrder = montageOrder.toString();
loginData.montageHiddenOrder = hiddenOrder.toString();
ZMDataModel.setLogin(loginData);
//window.localStorage.setItem("montageOrder", montageOrder.toString());
//window.localStorage.setItem("montageHiddenOrder", hiddenOrder.toString());
ZMDataModel.zmLog("Montage order saved on refresh: " + montageOrder.toString() + " and hidden order: " + hiddenOrder.toString());
});
};
$scope.saveReorder = function () {
loginData.montageOrder = montageOrder.toString();
loginData.montageHiddenOrder = hiddenOrder.toString();
ZMDataModel.setLogin(loginData);
//window.localStorage.setItem("montageOrder", montageOrder.toString());
// window.localStorage.setItem("montageHiddenOrder",
// hiddenOrder.toString());
//console.log("Saved " + montageOrder.toString());
ZMDataModel.zmLog("User press OK. Saved Monitor Order as: " +
montageOrder.toString() +
" and hidden order as " + hiddenOrder.toString());
$scope.modal.remove();
};
$scope.cancelReorder = function () {
// user tapped cancel
var i, myhiddenorder;
if (loginData.montageOrder == '') {
//if (window.localStorage.getItem("montageOrder") == undefined) {
for (i = 0; i < $scope.MontageMonitors.length; i++) {
montageOrder[i] = i;
hiddenOrder[i] = 0;
}
//console.log("Order string is " + montageOrder.toString());
ZMDataModel.zmLog("User press Cancel. Reset Monitor Order to: " + montageOrder.toString());
} else // montageOrder exists
{
var myorder = loginData.montageOrder;
if (loginData.montageHiddenOrder=='') {
for (i = 0; i < $scope.MontageMonitors.length; i++) {
hiddenOrder[i] = 0;
}
} else {
myhiddenorder = loginData.montageHiddenOrder;
hiddenOrder = myhiddenorder.split(",");
}
//console.log("Montage order is " + myorder + " and hidden order is " + myhiddenorder);
montageOrder = myorder.split(",");
for (i = 0; i < montageOrder.length; i++) {
montageOrder[i] = parseInt(montageOrder[i]);
hiddenOrder[i] = parseInt(hiddenOrder[i]);
}
$scope.MontageMonitors = oldMonitors;
ZMDataModel.zmLog("User press Cancel. Restored Monitor Order as: " + montageOrder.toString() + " and hidden order as: " + hiddenOrder.toString());
}
$scope.modal.remove();
};
$scope.toggleReorder = function () {
$scope.isReorder = !$scope.isReorder;
$scope.data = {};
$scope.data.showDelete = false;
$scope.data.showReorder = false;
var i;
oldMonitors = angular.copy($scope.monitors);
/*for (i=0; i<$scope.monitors.length; i++)
{
$scope.monitors[i].Monitor.listDisplay="show";
}*/
ld = ZMDataModel.getLogin();
if (ld.enableDebug) {
// Lets show the re-order list
for (i = 0; i < $scope.MontageMonitors.length; i++) {
ZMDataModel.zmDebug("Montage reorder list: " + $scope.MontageMonitors[i].Monitor.Name +
":listdisplay->" + $scope.MontageMonitors[i].Monitor.listDisplay);
}
}
$ionicModal.fromTemplateUrl('templates/reorder-modal.html', {
scope: $scope,
animation: 'slide-in-up'
})
.then(function (modal) {
$scope.modal = modal;
$scope.modal.show();
});
};
/*
$scope.onSwipeLeft = function ($index) {
$scope.showSizeButtons = true;
};
$scope.onSwipeRight = function ($index) {
$timeout(function () {
$scope.showSizeButtons = false;
}, 1000);
};*/
//---------------------------------------------------------------------
// This marks a monitor as hidden in montage view
//---------------------------------------------------------------------
$scope.deleteItem = function (index) {
var findindex = montageOrder.indexOf(index);
// $scope.monitors[index].Monitor.Function = 'None';
if ($scope.MontageMonitors[index].Monitor.listDisplay == 'show') {
$scope.MontageMonitors[index].Monitor.listDisplay = 'noshow';
hiddenOrder[findindex] = 1;
} else {
$scope.MontageMonitors[index].Monitor.listDisplay = 'show';
// we need to find the index of Montage Order that contains index
// because remember, hiddenOrder does not change its orders as monitors
// move
hiddenOrder[findindex] = 0;
}
//window.localStorage.setItem("montageOrder", montageOrder.toString());
//console.log("DELETE: Order Array now is " + montageOrder.toString());
//console.log("DELETE: Hidden Array now is " + hiddenOrder.toString());
ZMDataModel.zmLog("Marked monitor " + findindex + " as " + $scope.MontageMonitors[index].Monitor.listDisplay + " in montage");
};
//---------------------------------------------------------------------
// When we re-arrange the montage, all the ordering index moves
// horrible horrible code
//---------------------------------------------------------------------
function reorderItem(item, from, to, reorderHidden) {
ZMDataModel.zmDebug("MontageHistoryCtrl: Reorder from " + from + " to " + to);
$scope.MontageMonitors.splice(from, 1);
$scope.MontageMonitors.splice(to, 0, item);
// Now we need to re-arrange the montageOrder
// hiddenOrder remains the same
var i, j;
for (i = 0; i < $scope.monitors.length; i++) {
for (j = 0; j < $scope.MontageMonitors.length; j++) {
if ($scope.monitors[i].Monitor.Id == $scope.MontageMonitors[j].Monitor.Id) {
montageOrder[i] = j;
break;
}
}
}
ZMDataModel.zmLog("New Montage Order is: " + montageOrder.toString());
}
$scope.reorderItem = function (item, from, to) {
reorderItem(item, from, to, true);
};
//---------------------------------------------------------------------
// Triggered when you enter/exit full screen
//---------------------------------------------------------------------
$scope.switchMinimal = function () {
$scope.minimal = !$scope.minimal;
ZMDataModel.zmDebug("MontageHistoryCtrl: switch minimal is " + $scope.minimal);
//console.log("Hide Statusbar");
ionic.Platform.fullScreen($scope.minimal, !$scope.minimal);
$interval.cancel(intervalHandle); //we will renew on reload
// We are reloading this view, so we don't want entry animations
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
$state.go("montage", {
minimal: $scope.minimal,
isRefresh: true
});
};
//---------------------------------------------------------------------
// Show/Hide PTZ control in monitor view
//---------------------------------------------------------------------
$scope.togglePTZ = function () {
$scope.showPTZ = !$scope.showPTZ;
};
$scope.callback = function () {
// console.log("dragging");
};
$scope.onDropComplete = function (index, obj, event) {
//console.log("dragged");
var otherObj = $scope.monitors[index];
var otherIndex = $scope.monitors.indexOf(obj);
$scope.monitors[index] = obj;
$scope.monitors[otherIndex] = otherObj;
};
//---------------------------------------------------------------------
// changes order of montage display
//---------------------------------------------------------------------
$scope.toggleMontageDisplayOrder = function()
{
$scope.packMontage = !$scope.packMontage;
loginData.packMontage = $scope.packMontage;
ZMDataModel.setLogin(loginData);
//console.log ("Switching orientation");
};
//---------------------------------------------------------------------
// In Android, the app runs full steam while in background mode
// while in iOS it gets suspended unless you ask for specific resources
// So while this view, we DON'T want Android to keep sending 1 second
// refreshes to the server for images we are not seeing
//---------------------------------------------------------------------
function onPause() {
ZMDataModel.zmDebug("MontageHistoryCtrl: onpause called");
$interval.cancel($rootScope.eventQueryInterval);
$interval.cancel(intervalHandle);
// $interval.cancel(modalIntervalHandle);
// FIXME: Do I need to setAwake(false) here?
}
function onResume() {
// FIXME: Do we need to resume timers? when you resume, you go to portal and then here
/*
if (!$scope.isModalActive) {
var ld = ZMDataModel.getLogin();
ZMDataModel.zmDebug("MontageHistoryCtrl: onresume called");
ZMDataModel.zmLog("Restarting eventQuery timer on resume");
console.log ("************** TIMER STARTED INSIDE RESUME ***************");
//$rootScope.rand = Math.floor((Math.random() * 100000) + 1);
$interval.cancel($rootScope.eventQueryInterval);
$rootScope.eventQueryInterval = $interval(function () {
checkAllEvents();
// console.log ("Refreshing Image...");
}.bind(this),zm.eventHistoryTimer);
} else // modal is active
{
// $rootScope.modalRand = Math.floor((Math.random() * 100000) + 1);
}*/
}
$scope.openMenu = function () {
$timeout(function () {
$rootScope.stateofSlide = $ionicSideMenuDelegate.isOpen();
}, 500);
$ionicSideMenuDelegate.toggleLeft();
};
$scope.$on('$destroy', function () {
ZMDataModel.zmDebug("Cancelling eventQueryInterval");
$interval.cancel($rootScope.eventQueryInterval);
});
$scope.$on('$ionicView.loaded', function () {
//console.log("**VIEW ** MontageHistoryCtrl Loaded");
});
$scope.$on('$ionicView.enter', function () {
ZMDataModel.zmDebug("**VIEW ** MontageHistory Ctrl Entered, Starting loadNotifications");
var ld = ZMDataModel.getLogin();
//console.log("Setting Awake to " + ZMDataModel.getKeepAwake());
ZMDataModel.setAwake(ZMDataModel.getKeepAwake());
$interval.cancel($rootScope.eventQueryInterval);
//console.log ("****************** TIMER STARTED INSIDE ENTER");
$rootScope.eventQueryInterval = $interval(function () {
checkAllEvents();
// console.log ("Refreshing Image...");
}.bind(this), zm.eventHistoryTimer);
});
/*$scope.$on ('$ionicView.unloaded', function() {
console.log ("******** HISTORY UNLOADED KILLING WINDOW ************");
window.stop();
});*/
$scope.$on('$ionicView.beforeEnter', function () {
// ZMDataModel.zmLog ("Before Enter History: initing connkeys");
});
$scope.$on('$ionicView.beforeLeave', function () {
//console.log("**VIEW ** Event History Ctrl Left, force removing modal");
if ($scope.modal) $scope.modal.remove();
ZMDataModel.zmLog ("BeforeLeave: Nullifying the streams...");
for (i=0; i< $scope.MontageMonitors.length; i++)
{
var element = document.getElementById("img-"+i);
/*if (element)
{
ZMDataModel.zmDebug("BeforeLeave: Nullifying " + element.src);
element.src="";
//element.removeAttribute('src');
//$scope.$apply(nullify(element));
//element.src="";
}*/
}
ZMDataModel.zmLog("Cancelling event query timer");
$interval.cancel($rootScope.eventQueryInterval);
ZMDataModel.zmLog ("MontageHistory:Stopping network pull...");
// make sure this is applied in scope digest to stop network pull
// thats why we are doing it beforeLeave
for ( i=0; i<$scope.MontageMonitors.length; i++)
{
if ($scope.MontageMonitors[i].Monitor.connKey !='' &&
$scope.MontageMonitors[i].Monitor.eventUrl !='img/noevent.png' &&
$scope.MontageMonitors[i].Monitor.Function !='None' &&
$scope.MontageMonitors[i].Monitor.lisDisplay!='noshow' &&
$scope.MontageMonitors[i].Monitor.Enabled !='0')
{
ZMDataModel.zmLog ("Before leave: Calling kill with " + $scope.MontageMonitors[i].Monitor.connKey);
var tmpCK = angular.copy($scope.MontageMonitors[i].Monitor.connKey);
timedControlEventStream(2500,17,"",tmpCK,-1);
}
}
ZMDataModel.zmLog ("Forcing a window.stop() here");
ZMDataModel.stopNetwork();
});
$scope.$on('$ionicView.unloaded', function () {
});
//---------------------------------------------------------
// This function readjusts montage size
// and stores current size to persistent memory
//---------------------------------------------------------
function processSliderChanged(val) {
if (sizeInProgress) return;
sizeInProgress = true;
//console.log('Size has changed');
ZMDataModel.setMontageSize(val);
//console.log("ZMData Montage is " + ZMDataModel.getMontageSize() +
// " and slider montage is " + $scope.slider.monsize);
// Now go ahead and reset sizes of entire monitor array
var monsizestring = "";
var i;
for (i = 0; i < $scope.monitors.length; i++) {
$scope.monitorSize[i] = parseInt(ZMDataModel.getMontageSize());
//console.log("Resetting Monitor " + i + " size to " + $scope.monitorSize[i]);
$scope.scaleDirection[i] = 1;
monsizestring = monsizestring + $scope.monitorSize[i] + ':';
}
monsizestring = monsizestring.slice(0, -1); // kill last :
//console.log("Setting monsize string:" + monsizestring);
loginData.montageArraySize = monsizestring;
ZMDataModel.setLogin(loginData);
//window.localStorage.setItem("montageArraySize", monsizestring);
sizeInProgress = false;
}
$scope.$on('$ionicView.afterEnter', function () {
// This rand is really used to reload the monitor image in img-src so it is not cached
// I am making sure the image in montage view is always fresh
// I don't think I am using this anymore FIXME: check and delete if needed
// $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
});
$scope.reloadView = function () {
$rootScope.rand = Math.floor((Math.random() * 100000) + 1);
ZMDataModel.zmLog("User action: image reload " + $rootScope.rand);
};
$scope.doRefresh = function () {
//console.log("***Pull to Refresh, recomputing Rand");
ZMDataModel.zmLog("Reloading view for montage view, recomputing rand");
$rootScope.rand = Math.floor((Math.random() * 100000) + 1);
$scope.monitors = [];
imageLoadingDataShare.set(0);
var refresh = ZMDataModel.getMonitors(1);
refresh.then(function (data) {
$scope.monitors = data;
$scope.$broadcast('scroll.refreshComplete');
});
};
}]);
|