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
|
// This is my central data respository and common functions
// that many other controllers use
// It's grown over time. I guess I may have to split this into multiple services in the future
angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ionicLoading', function ($http, $q, $ionicLoading) {
// var deferred='';
var monitorsLoaded = 0;
var simSize = 30; // how many monitors to simulate
var eventSimSize = 40; // events to simulare per monitor
var montageSize = 3;
var monitors = [];
var oldevents = [];
var loginData = {
'username': '',
'password': '',
'url': '', // This is ZM portal API (Don't add /zm)
'apiurl': '', // This is the API path
'simulationMode': false // if true, data will be simulated
};
// This is really a test mode. This is how I am validating
// how my app behaves if you have many monitors. If you set simulationMode to true
// then this is the function that getMonitors and getEvents calls
var simulation = {
fillSimulatedMonitors: function (cnt) {
var simmonitors = [];
console.log("*** SIM MONITORS: Returning " + cnt + " simulated monitors");
for (var i = 0; i < cnt; i++) {
simmonitors.push({
"Monitor": {
// Obviously this is dummy data
"Id": i.toString(),
"Name": "Sim-Monitor" + i.toString(),
"Type": "Remote",
"Function": "Modect",
"Enabled": "1",
"Width": "1280",
"Height": "960",
"Colours": "4",
"MaxFPS": "10.00",
"AlarmMaxFPS": "10.00",
"AlarmFrameCount": "10.00"
}
});
}
// console.log ("Simulated: "+JSON.stringify(simmonitors));
return simmonitors;
},
fillSimulatedEvents: function (cnt) {
var simevents = [];
if (monitors.length == 0) // we have arrived at events before simulating monitors
{ // not sure if it will ever happen as I have a route resolve
console.log("Monitors have not been simulated yet. Filling them in");
monitors = simulation.fillSimulatedMonitors(simSize);
}
console.log("*** Returning " + cnt + " simulated events Per " + monitors.length + "Monitors");
var causes = ["Motion", "Signal", "Something else"];
for (var mon = 0; mon < monitors.length; mon++) {
console.log("Simulating " + cnt + "events for Monitor " + mon);
for (var i = 0; i < cnt; i++) {
simevents.push({
"Event": {
// Obviously this is dummy data
"Id":Math.floor(Math.random() * (5000 - 100 + 1)) + 1000,
"MonitorId": mon.toString(),
"Cause": causes[Math.floor(Math.random() * (2 - 0 + 1)) + 0],
"Length": Math.floor(Math.random() * (700 - 20 + 1)) + 20,
"Name": "Event Simulation " + i.toString(),
"Frames": Math.floor(Math.random() * (700 - 20 + 1)) + 20,
"AlarmFrames": Math.floor(Math.random() * (700 - 20 + 1)) + 20,
"TotScore": Math.floor(Math.random() * (100 - 2 + 1)) + 2,
"StartTime": "2015-04-24 09:00:00",
"Notes": "This is simulated",
}
});
} // for i
} // for mon
// console.log ("Simulated: "+JSON.stringify(simmonitors));
return simevents;
},
};
return {
// This function is called when the app is ready to run
// sets up various variables
// including persistent login data for the ZM apis and portal
// The reason I need both is because as of today, there is no way
// to access images using the API and they are authenticated via
// the ZM portal authentication, which is pretty messy. But unless
// the ZM authors fix this and streamline the access of images
// from APIs, I don't have an option
init: function () {
console.log("****** DATAMODEL INIT SERVICE CALLED ********");
var montageSize = 2;
if (window.localStorage.getItem("username") != undefined) {
loginData.username =
window.localStorage.getItem("username");
}
if (window.localStorage.getItem("password") != undefined) {
loginData.password =
window.localStorage.getItem("password");
}
if (window.localStorage.getItem("url") != undefined) {
loginData.url =
window.localStorage.getItem("url");
}
if (window.localStorage.getItem("apiurl") != undefined) {
loginData.apiurl =
window.localStorage.getItem("apiurl");
}
if (window.localStorage.getItem("simulationMode") != undefined) {
// Remember to convert back to Boolean!
var tvar = window.localStorage.getItem("simulationMode");
loginData.simulationMode = (tvar === "true");
console.log("***** STORED SIMULATION IS " + tvar);
console.log("******* BOOLEAN VALUE IS " + loginData.simulationMode);
}
monitorsLoaded = 0;
console.log("Getting out of ZMDataModel init");
},
isLoggedIn: function () {
if (loginData.username != "" && loginData.password != "" && loginData.url != "" && loginData.apiurl != "") {
return 1;
} else
return 0; {}
},
isSimulated: function () {
return loginData.simulationMode;
},
setSimulated: function (mode) {
loginData.simulationMode = mode;
},
getLogin: function () {
return loginData;
},
setLogin: function (newLogin) {
loginData = newLogin;
window.localStorage.setItem("username", loginData.username);
window.localStorage.setItem("password", loginData.password);
window.localStorage.setItem("url", loginData.url);
window.localStorage.setItem("apiurl", loginData.apiurl);
window.localStorage.setItem("simulationMode", loginData.simulationMode);
console.log("********** SIMULATION IS " + loginData.simulationMode);
},
// This function returns a list of monitors
// if forceReload == 1 then it will force an HTTP API request to get a list of monitors
// if 0. then it will return back the previously loaded monitor list if one exists, else
// will issue a new HTTP API to get it
// I've wrapped this function in my own promise even though http returns a promise. This is because
// depending on forceReload and simulation mode, http may or may not be called. So I'm promisifying
// the non http stuff too to keep it consistent to the calling function.
getMonitors: function (forceReload) {
console.log("** Inside ZMData getMonitors with forceReload=" + forceReload);
$ionicLoading.show({
template: 'Loading Monitors...',
animation: 'fade-in',
showBackdrop: true,
duration:10000,
maxWidth: 200,
showDelay: 0
});
var d = $q.defer();
if (((monitorsLoaded == 0) || (forceReload == 1)) && (loginData.simulationMode != true)) // monitors are empty or force reload
{
console.log("ZMDataModel: Invoking HTTP get to load monitors");
var apiurl = loginData.apiurl;
var myurl = apiurl + "/monitors.json";
$http.get(myurl, {timeout:10000})
.success(function (data) {
//console.log("HTTP success got " + JSON.stringify(data.monitors));
monitors = data.monitors;
console.log("promise resolved inside HTTP success");
monitorsLoaded = 1;
d.resolve(monitors);
$ionicLoading.hide();
})
.error(function (err) {
console.log("HTTP Error " + err);
// To keep it simple for now, I'm translating an error
// to imply no monitors could be loaded. FIXME: conver to proper error
monitors = [];
console.log("promise resolved inside HTTP fail");
d.resolve(monitors);
$ionicLoading.hide();
});
return d.promise;
} else // monitors are loaded
{
if (loginData.simulationMode == true) {
monitors = simulation.fillSimulatedMonitors(simSize);
//fillSimulatedMonitors
}
console.log("Returning pre-loaded list of " + monitors.length + " monitors");
d.resolve(monitors);
$ionicLoading.hide();
return d.promise;
}
},
setMonitors: function (mon) {
console.log("ZMData setMonitors called with " + mon.length + " monitors");
monitors = mon;
},
// Not sure if I am using this anymore. I was using this for graphs, but I
// don't think I am now
getAllPreexistingEvents: function () {
console.log("returning " + oldevents.length + " preexisting events");
return oldevents;
},
// This function returns events for specific monitor or all monitors
// You get here by tapping on events in the monitor screen or from
// the menu events option
// monitorId == 0 means all monitors (ZM starts from 1)
getEvents: function (monitorId) {
console.log("ZMData getEvents called with ID=" + monitorId);
$ionicLoading.show({
template: 'Loading Events...',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0,
duration:10000, //specifically for Android - http seems to get stuck at times
});
var d = $q.defer();
var myevents = [];
var apiurl = loginData.apiurl;
// FIXME: THIS IS JUST FETCHING PAGE 1 !!
// looks like I have more work to do here
var myurl = (monitorId == 0) ? apiurl + "/events.json" : apiurl + "/events/index/MonitorId:" + monitorId + ".json";
console.log("Constructed URL is " + myurl);
// FIXME: When retrieving lots of events, I really need to do pagination here - more complex
// as I have to do that in list scrolling too. For now, I hope your events don't kill the phone
// memory
if (loginData.simulationMode == true) {
console.log("Events will be simulated");
myevents = simulation.fillSimulatedEvents(eventSimSize);
d.resolve(myevents);
$ionicLoading.hide();
return d.promise;
} else { // not simulated
$http.get(myurl, {timeout:10000})
.success(function (data) {
$ionicLoading.hide();
myevents = data.events.reverse();
if (monitorId == 0) {
oldevents = myevents;
}
//console.log (JSON.stringify(data));
console.log("Returning " + myevents.length + "events");
d.resolve(myevents);
return d.promise;
})
.error(function (err) {
$ionicLoading.hide();
console.log("HTTP Events error " + err);
d.resolve(myevents);
if (monitorId == 0) {
// FIXME: make this into a proper error
oldevents = [];
}
return d.promise;
})
return d.promise;
} // not simulated
},
getMontageSize: function () {
return montageSize;
},
setMontageSize: function (montage) {
montageSize = montage;
},
getMonitorsLoaded: function () {
console.log("**** Inside promise function ");
var deferred = $q.defer();
if (monitorsLoaded != 0) {
deferred.resolve(monitorsLoaded);
}
return deferred.promise;
},
setMonitorsLoaded: function (loaded) {
console.log("ZMData.setMonitorsLoaded=" + loaded);
monitorsLoaded = loaded;
},
// Given a monitor Id it returns the monitor name
// FIXME: Can I do a better job with associative arrays?
getMonitorName: function (id) {
var idnum = parseInt(id);
for (var i = 0; i < monitors.length; i++) {
if (parseInt(monitors[i].Monitor.Id) == idnum) {
// console.log ("Matched, exiting getMonitorname");
return monitors[i].Monitor.Name;
}
}
return "(Unknown)";
},
getMontageImagePath: function () {
var path = "{{LoginData.url}}/cgi-bin/nph-zms?mode=jpeg&monitor={{monitor.Monitor.Id}}&scale=100&maxfps=3&buffer=1000&user={{LoginData.username}}&pass={{LoginData.password}}&rand={{rand}}";
return (path);
}
};
}]);
|