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
|
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */
//--------------------------------------------------------------------------
// This factory interacts with the ZM Event Server
// over websockets and is responsible for rendering real time notifications
//--------------------------------------------------------------------------
angular.module('zmApp.controllers')
.factory('EventServer',
[ 'ZMDataModel', '$rootScope','$websocket', '$ionicPopup', '$cordovaLocalNotification', '$cordovaBadge', function
( ZMDataModel, $rootScope, $websocket, $ionicPopup,$cordovaLocalNotification, $cordovaBadge) {
var ws;
// Display a max of 5 local notifications
var localNotificationId=5;
//--------------------------------------------------------------------------
// Called once at app start. Does a lazy definition of websockets open
//--------------------------------------------------------------------------
function init()
{
$rootScope.isAlarm = 0;
$rootScope.alarmCount="0";
var loginData = ZMDataModel.getLogin();
if (loginData.isUseEventServer =='0' || !loginData.eventServer)
{
ZMDataModel.zmLog("No Event Server present. Not initializing");
return;
}
ZMDataModel.zmLog("Initializing Websocket with URL " +
loginData.eventServer+" , will connect later...");
ws = $websocket.$new ({
url:loginData.eventServer,
reconnect:true,
reconnectInterval:5000,
lazy:true
});
// Transmit auth information to server
ws.$on ('$open', function() {
ZMDataModel.zmLog("Websocket open");
ws.$emit('auth',
{user:loginData.username,
password:loginData.password});
});
ws.$on ('$close', function() {
ZMDataModel.zmLog ("Websocket closed");
});
// Handles responses back from ZM ES
ws.$on ('$message', function(str) {
ZMDataModel.zmLog("Real-time event: " + JSON.stringify(str));
if (str.status != 'Success')
{
ZMDataModel.zmLog ("Event Error: " + JSON.stringify(str));
ws.$close();
ZMDataModel.displayBanner('error',['Event server rejected credentials', 'Please re-check credentials'],2000,6000);
}
var localNotText = "New Alarms: ";
if (str.status == 'Success' && str.events) // new events
{
var eventsToDisplay=[];
for (var iter=0; iter<str.events.length; iter++)
{
// lets stack the display so they don't overwrite
eventsToDisplay.push(str.events[iter].Name+": new event ("+str.events[iter].EventId+")");
localNotText = localNotText + str.events[iter].Name+",";
}
localNotText = localNotText.substring(0, localNotText.length - 1);
// if we are in background, do a local notification, else do an in app display
if (!ZMDataModel.isBackground())
{
ZMDataModel.zmDebug("App is in foreground, displaying banner");
if (eventsToDisplay.length > 0)
{
ZMDataModel.displayBanner('alarm', eventsToDisplay, 5000, 5000*eventsToDisplay.length);
}
}
else
{
ZMDataModel.zmDebug("App is in background, displaying localNotification");
localNotificationId--;
if ( localNotificationId == 0) // only show last 5
{
localNotificationId = 5;
}
// This is how I am reusing local notifications
// I really don't want to stack local notififcations beyond 5
if ($cordovaLocalNotification.isPresent(localNotificationId))
{
ZMDataModel.zmDebug("Cancelling notification ID " + localNotificationId);
$cordovaLocalNotification.cancel(localNotificationId);
}
ZMDataModel.zmDebug("Creating notification ID " + localNotificationId + " with " +localNotText);
$cordovaLocalNotification.schedule({
id: localNotificationId,
title: 'ZoneMinder Alarms',
text: localNotText,
sound:"file://sounds/blop.mp3"
}).then(function (result) {
// do nothing for now
});
}
// lets set badge of app irrespective of background or foreground
$cordovaBadge.hasPermission().then(function(yes) {
$cordovaBadge.set($rootScope.alarmCount).then(function() {
// You have permission, badge set.
ZMDataModel.zmDebug("Setting badge to " + $rootScope.alarmCount);
}, function(err) {
// You do not have permission.
ZMDataModel.zmDebug("Error Setting badge to " + $rootScope.alarmCount);
});
// You have permission
}, function(no) {
ZMDataModel.zmDebug("zmNinja does not have badge permissions. Please check your phone notification settings");
});
$rootScope.isAlarm = 1;
// Show upto a max of 99 when it comes to display
// so aesthetics are maintained
if ($rootScope.alarmCount == "99")
{
$rootScope.alarmCount="99+";
}
if ($rootScope.alarmCount != "99+")
{
$rootScope.alarmCount = (parseInt($rootScope.alarmCount)+1).toString();
}
}
});
}
//--------------------------------------------------------------------------
// Called each time we resume
//--------------------------------------------------------------------------
function refresh()
{
var loginData = ZMDataModel.getLogin();
if ((!loginData.eventServer) || (loginData.isUseEventServer=="0"))
{
ZMDataModel.zmLog("No Event Server configured, skipping refresh");
// Let's also make sure that if the socket was open
// we close it - this may happen if you disable it after using it
if (typeof ws !== 'undefined')
{
if (ws.$status() != ws.$CLOSED)
{
ZMDataModel.zmDebug("Closing open websocket as event server was disabled");
ws.$close();
}
}
return;
}
if (typeof ws === 'undefined')
{
ZMDataModel.zmDebug ("Calling websocket init");
init();
}
// refresh is called when
// The following situations will close the socket
// a) In iOS the client went to background -- we should reconnect
// b) The Event Server died
// c) The network died
// Seems to me in all cases we should give re-open a shot
if (ws.$status() == ws.$CLOSED)
{
ZMDataModel.zmLog("Websocket was closed, trying to re-open");
ws.$open();
}
}
return {
refresh:refresh,
init:init
};
}]);
|