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
|
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */
// Websockets
angular.module('zmApp.controllers')
.factory('EventServer',
[ 'ZMDataModel', '$rootScope','$websocket', '$ionicPopup', function
( ZMDataModel, $rootScope, $websocket, $ionicPopup) {
var ws;
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);
}
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+")");
}
// lets stack the display so they don't overwrite
if (eventsToDisplay.length > 0)
{
ZMDataModel.displayBanner('alarm', eventsToDisplay, 5000, 5000*eventsToDisplay.length);
}
$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
};
}]);
|