Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A lightweight fix for webofthings/webofthings.js#3. #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# IDE
.project

# Logs
logs
*.log
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -62,8 +61,6 @@
"ws": "^1.0.1"
},
"optionalDependencies": {
"onoff": "^1.0.4",
"node-dht-sensor": "^0.0.8"
},
"devDependencies": {
"assert": "^1.3.0",
Expand Down
12 changes: 11 additions & 1 deletion plugins/corePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
56 changes: 49 additions & 7 deletions servers/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand All @@ -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
Expand Down