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
|
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */
// Websockets
angular.module('zmApp.controllers')
.factory('EventServer',
[ 'ZMDataModel', '$rootScope','$websocket', '$ionicPopup', '$cordovaLocalNotification', '$cordovaBadge', function
( ZMDataModel, $rootScope, $websocket, $ionicPopup,$cordovaLocalNotification, $cordovaBadge) {
var ws;
var localNotificationId=5;
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
});
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");
});
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++)
{
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);
// lets stack the display so they don't overwrite
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 slow last 5
{
localNotificationId = 5;
}
if ($cordovaLocalNotification.isPresent(localNotificationId))
{
$cordovaLocalNotification.clear(localNotificationId);
}
$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.
}, function(err) {
// You do not have permission.
});
// You have permission
}, function(no) {
ZMDataModel.zmDebug("zmNinja does not have badge permissions. Please check your phone notification settings");
});
$rootScope.isAlarm = 1;
if ($rootScope.alarmCount == "99")
{
$rootScope.alarmCount="99+";
}
if ($rootScope.alarmCount != "99+")
{
$rootScope.alarmCount = (parseInt($rootScope.alarmCount)+1).toString();
}
}
});
}
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
};
}]);
|