diff --git a/.gitignore b/.gitignore index c1d347b..bec289b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# IDE +.project + # Logs logs *.log diff --git a/package.json b/package.json index 19df7a8..7eb7b40 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "dependencies": { "bl": "^1.0.0", "body-parser": "^1.13.1", - "coap": "^0.13.1", "consolidate": "^0.13.1", "cors": "^2.7.1", "crypto": "0.0.3", @@ -62,8 +61,6 @@ "ws": "^1.0.1" }, "optionalDependencies": { - "onoff": "^1.0.4", - "node-dht-sensor": "^0.0.8" }, "devDependencies": { "assert": "^1.3.0", diff --git a/plugins/corePlugin.js b/plugins/corePlugin.js index 3e644b7..a4ae317 100644 --- a/plugins/corePlugin.js +++ b/plugins/corePlugin.js @@ -62,10 +62,20 @@ CorePlugin.prototype.showValue = function () { console.info('Current value for %s is %s', this.model.name, util.inspect(this.model.data[this.model.data.length-1])); }; +/** + * Fake ObjectObserve if not available + */ +var objectObserver = Object.observe || function (resource, callback, extra) { + resource.push = function (data) { + Array.prototype.push.call(resource, data); + callback([{object: resource}]); + }; +}; + CorePlugin.prototype.observeActions = function () { var self = this; _.forEach(self.actions, function (actionId) { //#F - Object.observe(resources.links.actions.resources[actionId].data, function (changes) { + objectObserver(resources.links.actions.resources[actionId].data, function (changes) { var action = changes[0].object[changes[0].object.length -1]; console.info('[plugin action detected] %s', actionId); if (self.doAction) self.doAction(action); diff --git a/servers/websockets.js b/servers/websockets.js index 9c8e9f7..bb543ee 100755 --- a/servers/websockets.js +++ b/servers/websockets.js @@ -3,6 +3,32 @@ var WebSocketServer = require('ws').Server, resources = require('./../resources/model'), utils = require('./../utils/utils'); +/** + * Fake Array.Observe if not available + */ +if (Array.observe) { + var ArrayObserver = function (res, cb, methods) { + Array.observe(res.res.data,cb, methods); + }; +} else { + var callbacklist={'properties': {}, 'actions' : {} }; + var ArrayObserver= function (res, cb, methods) { + if (! callbacklist[res.type][res.resname]) { + callbacklist[res.type][res.resname]=[]; + } + callbacklist[res.type][res.resname].push(cb); + res.res.data.push = function (data) { + Array.prototype.push.call(res.res.data, data); + if (callbacklist[res.type][res.resname]) { + callbacklist[res.type][res.resname].forEach(function(cb) { + cb([{object: res.res.data}]); + }); + } + + }; + }; +}; + exports.listen = function (server) { var wss = new WebSocketServer({server: server}); //#A console.info('WebSocket server started...'); @@ -11,28 +37,44 @@ exports.listen = function (server) { if (!utils.isTokenValid(reqUrl.query.token)) { ws.send(JSON.stringify({'error': 'Invalid access token.'})); } else { - try { - Array.observe(selectResouce(reqUrl.pathname), function (changes) { //#C + var observedResource = selectResource(reqUrl.pathname); + var resObjserver = function (changes) { //#C ws.send(JSON.stringify(changes[0].object[changes[0].object.length - 1]), function () { }); - }, ['add']) + }; + try { + ArrayObserver(observedResource, resObjserver, ['add']); } catch (e) { //#D console.log('Unable to observe %s resource!', url); } + ws.on('close', function (code, message) { + if (! Array.observe) { + var array = callbacklist[observedResource.type][observedResource.resname] + var index = array.indexOf(resObjserver); + if (index > -1) { + array.splice(index, 1); + } + } + console.log("ws connection is closed"); + }); + } }); }; -function selectResouce(url) { //#E +function selectResource(url) { //#E var parts = url.split('/'); parts.shift(); + var result; + //console.log(" parts ==> ", parts) if (parts[0] === 'actions') { - result = resources.links.actions.resources[parts[1]].data; + result = resources.links.actions.resources[parts[1]]; } else { - result = resources.links.properties.resources[parts[1]].data; + result = resources.links.properties.resources[parts[1]]; } - return result; + // console.log("===> ", result); + return { res: result, type: parts[0], resname: parts[1]}; } //#A Create a WebSocket server by passing it the Express server