diff options
Diffstat (limited to 'www')
| -rw-r--r-- | www/js/EventServer.js | 373 | ||||
| -rw-r--r-- | www/js/EventServerSettingsCtrl.js | 10 |
2 files changed, 211 insertions, 172 deletions
diff --git a/www/js/EventServer.js b/www/js/EventServer.js index e142dc8d..f252cca4 100644 --- a/www/js/EventServer.js +++ b/www/js/EventServer.js @@ -16,65 +16,174 @@ angular.module('zmApp.controllers') var localNotificationId = 0; var pushInited = false; var isTimerOn = false; + //-------------------------------------------------------------------------- // called when the websocket is opened //-------------------------------------------------------------------------- - function openHandshake() { - NVRDataModel.log("Inside openHandshake"); - var loginData = NVRDataModel.getLogin(); - if (loginData.isUseEventServer == false || loginData.eventServer == "") { - NVRDataModel.log("openHandShake: no event server"); - return; - } - - if (typeof ws == 'undefined') { - NVRDataModel.debug("websocket is undefined, need to create ws before I can sent auth"); - ws = $websocket(loginData.eventServer, { - reconnectIfNotNormalClose: false + function handleOpen(event) { + + NVRDataModel.debug ("WebSocket open called with:"+JSON.stringify(event.data)); + var loginData = NVRDataModel.getLogin(); + NVRDataModel.log("openHandshake: Websocket open, sending Auth"); + sendMessage("auth", + { + user: loginData.username, + password: loginData.password }); - // ws.onOpen(openHandshake); - // return; - } - NVRDataModel.log("openHandshake: Websocket open, sending Auth"); - ws.send({ - event: 'auth', - data: { - user: loginData.username, - password: loginData.password + + if ($rootScope.apnsToken != '') { + var plat = $ionicPlatform.is('ios') ? 'ios' : 'android'; + var ld = NVRDataModel.getLogin(); + var pushstate = "enabled"; + if (ld.disablePush == true) + pushstate = "disabled"; + + NVRDataModel.debug("openHandShake: state of push is " + pushstate); + // let's do this only if disabled. If enabled, I suppose registration + // will be called? + //if (ld.disablePush) + //console.log ("HANDSHAKE MESSAGE WITH "+$rootScope.monstring); + + sendMessage ("push", { + type: 'token', + platform: plat, + token: $rootScope.apnsToken, + monlist: $rootScope.monstring, + intlist: $rootScope.intstring, + state: pushstate + + }); } + + } - }); + function handleClose(event) { - if ($rootScope.apnsToken != '') { - var plat = $ionicPlatform.is('ios') ? 'ios' : 'android'; - var ld = NVRDataModel.getLogin(); - var pushstate = "enabled"; - if (ld.disablePush == true) - pushstate = "disabled"; + + console.log ("*********** WEBSOCKET CLOSE CALLED WITH "+JSON.stringify(event.data)); - NVRDataModel.debug("openHandShake: state of push is " + pushstate); - // let's do this only if disabled. If enabled, I suppose registration - // will be called? - //if (ld.disablePush) - if (1) { - //console.log ("HANDSHAKE MESSAGE WITH "+$rootScope.monstring); - ws.send({ - event: 'push', - data: { - type: 'token', - platform: plat, - token: $rootScope.apnsToken, - monlist: $rootScope.monstring, - intlist: $rootScope.intstring, - state: pushstate - } - }); - } + + if (!isTimerOn) { + NVRDataModel.log("Will try to reconnect in 10 sec.."); + $timeout(init, 10000); + isTimerOn = true; } + } + + function handleError(event) { + + console.log ("*********** WEBSOCKET CLOSE CALLED WITH "+JSON.stringify(event.data)); + //return; + if (!isTimerOn) { + NVRDataModel.log("Will try to reconnect in 10 sec.."); + $timeout(init, 10000); + isTimerOn = true; + } + } + + function handleMessage(smsg) { + //NVRDataModel.debug ("Websocket received message:"+smsg); + str = JSON.parse(smsg); + NVRDataModel.debug("Real-time event: " + JSON.stringify(str)); + + + + // Error messages + if (str.status != 'Success') { + NVRDataModel.log("Event Error: " + JSON.stringify(str)); + + if (str.reason == 'APNSDISABLED') { + console.log ("FORCE CLOSING"); + ws.close(); + NVRDataModel.displayBanner('error', ['Event Server: APNS disabled'], 2000, 6000); + $rootScope.apnsToken = ""; + } + + } + + if (str.status == 'Success' && (str.event == 'auth')) { + if (str.version == undefined) + str.version = "0.1"; + if (NVRDataModel.versionCompare(str.version, zm.minEventServerVersion) == -1) { + $rootScope.zmPopup = $ionicPopup.alert({ + title: $translate.instant('kEventServerVersionTitle'), + template: $translate.instant('kEventServerVersionBody1') + " " + str.version + ". " + $translate.instant('kEventServerVersionBody2') + " " + + zm.minEventServerVersion, + okText: $translate.instant('kButtonOk'), + cancelText: $translate.instant('kButtonCancel'), + }); + } + + } + + if (str.status == 'Success' && str.event == 'alarm') // new events + { + + var localNotText; + // ZMN specific hack for Event Server + if (str.supplementary != 'true') { + new Audio('sounds/blop.mp3').play(); + localNotText = ""; + $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(); + } + + } else { + NVRDataModel.debug("received supplementary event information over websockets"); + } + var eventsToDisplay = []; + var listOfMonitors = []; + for (var iter = 0; iter < str.events.length; iter++) { + // lets stack the display so they don't overwrite + //eventsToDisplay.push(str.events[iter].Name + ": latest new alarm (" + str.events[iter].EventId + ")"); + var txt = str.events[iter].EventId; + if (str.events[iter].Cause) { + txt = str.events[iter].Cause; + } + eventsToDisplay.push(str.events[iter].Name + ": " + txt); + localNotText = localNotText + str.events[iter].Name + ": " + txt + ","; + listOfMonitors.push(str.events[iter].MonitorId); + + } + localNotText = localNotText.substring(0, localNotText.length - 1); + + // if we are in background, do a local notification, else do an in app display + if (!NVRDataModel.isBackground()) { + + //emit alarm details - this is when received over websockets + $rootScope.$broadcast('alarm', { + message: listOfMonitors + }); + + if (str.supplementary != 'true') { + + NVRDataModel.debug("App is in foreground, displaying banner"); + if (eventsToDisplay.length > 0) { + + if (eventsToDisplay.length == 1) { + //console.log("Single Display: " + eventsToDisplay[0]); + NVRDataModel.displayBanner('alarm', [eventsToDisplay[0]], 5000, 5000); + } else { + NVRDataModel.displayBanner('alarm', eventsToDisplay, + 5000, 5000 * eventsToDisplay.length); + } + + } + } + } + } + } @@ -83,130 +192,69 @@ angular.module('zmApp.controllers') //-------------------------------------------------------------------------- function init() { - - $rootScope.isAlarm = 0; $rootScope.alarmCount = "0"; - isTimerOn = false; - var d = $q.defer(); - var loginData = NVRDataModel.getLogin(); - //console.log ("INIT GOT " + JSON.stringify(loginData)); - if (loginData.isUseEventServer == false || !loginData.eventServer) { NVRDataModel.log("No Event Server present. Not initializing"); d.reject("false"); return d.promise; } - //if (!$rootScope.apnsToken) - if (!pushInited) pushInit(); - - // console.log ("WS TYPEOF="+ typeof ws); - // console.log ("WS="+JSON.stringify(ws)); - /*if (typeof ws !== 'undefined') - { - NVRDataModel.debug("websocket already initialized --Forcing close"); - ws.close(true); - ws=undefined; - - }*/ - NVRDataModel.log("Initializing Websocket with URL " + loginData.eventServer); - /* ws = $websocket.$new( - { - url: loginData.eventServer, - reconnect: true, - reconnectInterval: 60000, - lazy: true - });*/ - - ws = $websocket(loginData.eventServer, { - reconnectIfNotNormalClose: false - }); - ws.onOpen(openHandshake); + - initCalled = true; - // Transmit auth information to server - // ws.$on('$open', openHandshake); + + ws = new WebSocket(loginData.eventServer); - if (ws) { - ws.onErrorCallbacks = []; - NVRDataModel.debug("Removing error handlers for websocket"); - } - NVRDataModel.debug("Setting up websocket error handler"); - //ws.$on('$error', function(e) - ws.onError(function (e) { + ws.onopen = function (event) { + handleOpen(event); + if (!pushInited) { NVRDataModel.debug ("Initializing FCM push"); pushInit();} + d.resolve("true"); + return; + }; - NVRDataModel.debug("Websocket Errorhandler called"); + ws.onclose = function (event) { + handleClose(event); + d.reject ("error"); + return; + + }; - var timeElapsedSinceResume = NVRDataModel.getTimeSinceResumed(); + ws.onerror = function (event) { + handleError(event); + d.reject ("error"); + return; + - if (timeElapsedSinceResume == -1) { - // so we display error - timeElapsedSinceResume = moment().subtract('1', 'hour'); - } - - var duration = moment.duration(moment().diff(timeElapsedSinceResume)).asSeconds().toFixed(1); - - NVRDataModel.debug(">> time since resumed is " + duration + " seconds"); - if (duration > zm.waitTimeTillResume) { + + }; - $timeout(function () { - var eserr = $translate.instant('kEventServerConnErr'); - NVRDataModel.displayBanner('error', [eserr]); - }, 1000); // leave time for transitions - - } else { - NVRDataModel.debug("ES error happened " + timeElapsedSinceResume + " secs after resume, maybe fake, lets wait..."); - } + ws.onmessage = function (event) { + + var smsg = event.data; + handleMessage (smsg); - /*if (typeof ws !== 'undefined'){ - NVRDataModel.debug ("-->Forcing socket close"); - ws.close(true); + + }; - }*/ - ws = undefined; - NVRDataModel.log("Will try to reconnect in 10 sec.."); - if (!isTimerOn) { - $timeout(init, 10000); - isTimerOn = true; - } - - - //console.log ("VALUE TIME " + lastEventServerCheck); - //console.log ("NOW TIME " + Date.now()); - }); - - ws.onClose(function () - // ws.$on('$close', function() - { - NVRDataModel.log("Websocket closed"); - ws = undefined; - - var ld = NVRDataModel.getLogin(); + - /* if (ld.isUseEventServer && !isTimerOn) { - // this means remote error, because zmN still - // wants it on - $timeout ( init, 10000 ); - isTimerOn = true; - }*/ - }); + - // Handles responses back from ZM ES + /* ws.onMessage(function (str) // ws.$on('$message', function(str) @@ -309,7 +357,11 @@ angular.module('zmApp.controllers') } //end of success handler }); - d.resolve("true"); + + + */ + + return (d.promise); } @@ -323,9 +375,11 @@ angular.module('zmApp.controllers') NVRDataModel.log("Clearing error/close cbk, disconnecting and deleting Event Server socket..."); // ws.$close(); - ws.onErrorCallbacks = []; - ws.onCloseCallbacks = []; - ws.close(true); // force close + + + ws.onmessage = null; + + ws.close(); // ws.$un('open'); // ws.$un('close'); // ws.$un('message'); @@ -354,38 +408,17 @@ angular.module('zmApp.controllers') } // console.log (">>>>>>>>>>>>>>>>>EVENT SERVER SENDING: type="+type+" DATA="+JSON.stringify(obj)); - ws.send({ + + var msg = { 'event': type, 'data': obj - }); + }; - /*if (ws.$status() == ws.$CLOSED) - { - NVRDataModel.log("Websocket was closed, trying to re-open"); - ws.$un('$open'); - //ws.$on ('$open', openHandshake); - ws.$open(); - - ws.$on('$open', openHandshake, function() - { - - //console.log(" sending " + type + " " + - // JSON.stringify(obj)); - //console.log("sending " + type + " " + JSON.stringify(obj)); - ws.$emit(type, obj); - - ws.$un('$open'); - ws.$on('$open', openHandshake); - - }); - - } - else* - { - ws.send(type, obj); - // console.log("sending " + type + " " + JSON.stringify(obj)); - }*/ + var jmsg = JSON.stringify(msg); + NVRDataModel.debug ("~~~~ sendMessage: Sending->"+jmsg); + ws.send(jmsg); + } //-------------------------------------------------------------------------- diff --git a/www/js/EventServerSettingsCtrl.js b/www/js/EventServerSettingsCtrl.js index 24928007..a4ce03b0 100644 --- a/www/js/EventServerSettingsCtrl.js +++ b/www/js/EventServerSettingsCtrl.js @@ -212,7 +212,8 @@ EventServer.sendMessage("control", { type: 'filter', monlist: monstring, - intlist: intervalstring + intlist: intervalstring, + token: $rootScope.apnsToken, }, 1); if ($rootScope.apnsToken != "") @@ -231,7 +232,12 @@ } - }); + }, + function (err) { + NVRDataModel.debug("Event Server init failed"); + } + + ); } // no event server configured/enabled else { |
