Skip to content

Commit

Permalink
npm init
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterEB committed Feb 26, 2016
1 parent 98538af commit 499470f
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 179 deletions.
32 changes: 26 additions & 6 deletions clientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ var coapNode = new CoapNode('nodeTest');

coapNode.on('ready', function (msg) {
console.log('ready');

setInterval(function () {
coapNode._dumpObj(3303, 0, function (err, data) {
console.log(data);
});
}, 30000);
});

coapNode.on('close', function (msg) {
Expand All @@ -17,11 +23,24 @@ coapNode.on('update', function (msg) {
coapNode.initResrc(3303, 0, {
5700: 21,
5701: 'C',
5702: {'a': 1, 'b': 2},
5703: { exec: function (val1, val2, cb) {
console.log(val1 + val2);
cb(null, 'good');
}}
5702: {
read: function (cb) {
var time = new Date();
cb(null, time.toString());
}
},
5703: {
write: function (val, cb) {
console.log('write ' + val);
cb(null, val);
}
},
5704: {
exec: function (val1, val2, cb) {
console.log(val1 + val2);
cb(null, 'good');
}
}
});

coapNode.initResrc(3303, 1, {
Expand All @@ -30,5 +49,6 @@ coapNode.initResrc(3303, 1, {
});

coapNode.register('127.0.0.1', 5683, function (err, msg) {
console.log(msg);

});

3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./lib/coap-node.js');
102 changes: 74 additions & 28 deletions lib/coap-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ CoapNode.prototype.readResrc = function (oid, iid, rid, callback) {
cb(err, data);
});

// if (!_.isNil(data)) _checkAndReportResrc(self, oid, iid, rid, data);
if (!_.isNil(data)) _checkAndReportResrc(self, oid, iid, rid, data);
}

if (!target.exist) {
Expand All @@ -148,12 +148,13 @@ CoapNode.prototype.readResrc = function (oid, iid, rid, callback) {
rsc.read(function (err, val) {
invokeCb(null, val, callback);
});
} else if (_.isFunction(rsc.exec)) {
invokeCb(null, '_exec', callback);
} else {
invokeCb(new Error('unreadable.', null, callback));
invokeCb(null, '_unreadable', callback);
}
} else if (_.isObject(rsc)) {
rsc = _.omit(rsc, ['_isCb']);
invokeCb(null, rsc, callback);
invokeCb(null, _.omit(rsc, ['_isCb']), callback);
} else {
invokeCb(null, rsc, callback);
}
Expand All @@ -163,7 +164,8 @@ CoapNode.prototype._dumpObj = function (oid, iid, callback) {
var self = this,
target,
dump = {},
chkErr;
chkErr,
count = 0;

if (_.isFunction(iid)) {
callback = iid;
Expand All @@ -173,35 +175,52 @@ CoapNode.prototype._dumpObj = function (oid, iid, callback) {
target = this._target(oid, iid);

if (target.exist && target.type === 'object') {

_.forEach(target.value, function (iObj, ii) {
dump[ii] = {};
_.forEach(iObj, function (rsc, rid) {
count += 1;
});
});

_.forEach(target.value, function (iObj, ii) {
dump[ii] = {};
_.forEach(iObj, function (rsc, rid) {
self.readResrc(oid, ii, rid, function (err, data) {
if (err)
chkErr = err;
count -= 1;

if (err)
chkErr = chkErr || err;
else
dump[ii][cutils.ridNumber(oid, rid)] = data;

if (count === 0 && _.isFunction(callback))
callback(null, dump);
});
});
});
} else if (target.exist && target.type === 'instance') {

_.forEach(target.value, function (rsc, rid) {
count += 1;
});

_.forEach(target.value, function (rsc, rid) {
self.readResrc(oid, iid, rid, function (err, data) {
count -= 1;

if (err)
chkErr = err;
chkErr = chkErr || err;
else
dump[cutils.ridNumber(oid, rid)] = data;

if (count === 0 && _.isFunction(callback))
callback(null, dump);
});
});
} else {
dump = null;
}

if (chkErr)
callback(chkErr);
else
callback(null, dump);
}
};

CoapNode.prototype.writeResrc = function (oid, iid, rid, value, callback) {
Expand All @@ -217,7 +236,7 @@ CoapNode.prototype.writeResrc = function (oid, iid, rid, value, callback) {
cb(err, data);
});

// if (!_.isNil(data)) _checkAndReportResrc(self, oid, iid, rid, data);
if (!_.isNil(data)) _checkAndReportResrc(self, oid, iid, rid, data);
}

if (!target.exist){
Expand All @@ -227,8 +246,10 @@ CoapNode.prototype.writeResrc = function (oid, iid, rid, value, callback) {
rsc.write(value, function (err, val) {
invokeCb(null, val, callback);
});
} else if (_.isFunction(rsc.exec)) {
invokeCb(null, '_exec', callback);
} else {
invokeCb(new Error('unreadable.', null, callback));
invokeCb(null, '_unwriteable', callback);
}
} else {
if (typeof rsc !== typeof value) {
Expand Down Expand Up @@ -268,7 +289,7 @@ CoapNode.prototype.execResrc = function (oid, iid, rid, argus, callback) {
});
rsc.exec.apply(this, argus);
} else {
invokeCb(new Error('unreadable.', null, callback));
invokeCb(null, '_unexecable', callback);
}
}
};
Expand Down Expand Up @@ -632,18 +653,16 @@ CoapNode.prototype._enableReport = function (oid, iid, rid, rsp, callback) {

pmin = rAttrs.pmin * 1000;
pmax = rAttrs.pmax * 1000;
rAttrs.cancel = false;
this._reporters[key] = { min: null, max: null, write: null };
rpt = this._reporters[key];

rpt.min = setTimeout(function () {
rAttrs.mute = false;
}, pmin);

function reporterMax () {
rAttrs.mute = true;

function reporterMax () {
dumper(function (err, val) {
rAttrs.mute = true;
rpt.write(val);
});

Expand All @@ -667,11 +686,15 @@ CoapNode.prototype._enableReport = function (oid, iid, rid, rsp, callback) {

if (!_.isNil(rpt.max))
clearInterval(rpt.max);

rpt.max = setInterval(reporterMax, pmax);
};

dumper(callback);
dumper(function (err, data) {
rAttrs.cancel = false;
rAttrs.lastRpVal = data;
callback(err, data);
});
};

CoapNode.prototype._disableReport = function (oid, iid, rid) {
Expand Down Expand Up @@ -758,7 +781,7 @@ function _serverReadHandler (node, req, rsp) {
} else {
rsp.code = '2.05';
rsp.setOption('Content-Format', 'application/json');
rsp.end(JSON.stringify(target.value));
rsp.end(JSON.stringify(dump));
}
});
} else if (target.type === 'resource') {
Expand Down Expand Up @@ -842,7 +865,7 @@ function _serverWriteHandler (node, req, rsp) {
function _serverWriteAttrHandler (node, req, rsp) {
var pathObj = cutils.getSoKeyObj(req.url),
target = node._target(pathObj.oid, pathObj.iid, pathObj.rid),
attrs = cutils.buildAttr(req);
attrs = cutils.buildRptAttr(req);

if (!target.exist) {
rsp.code = '4.04';
Expand Down Expand Up @@ -994,7 +1017,7 @@ function _checkAndReportResrc(cn, oid, iid, rid, val) {
rkey,
key,
rpt,
chkRpVal;
chkRp = false;

if (rAttrs.cancel)
return false;
Expand All @@ -1013,12 +1036,35 @@ function _checkAndReportResrc(cn, oid, iid, rid, val) {
if (rAttrs.mute) {
setTimeout(function () {
_checkAndReportResrc(cn, oid, iid, rid, val);
}, rAttrs.pmin);
}, rAttrs.pmin * 1000);
} else {
if (_.isObject(val)) {
if (_.isObject(lastRpVal)) {
_.forEach(lastRpVal, function (v, k) {
chkRp = chkRp || (v !== lastRpVal[k]);
});
} else {
chkRp = true;
}
} else if (!_.isNumber(val)) {
chkRp = (lastRpVal !== val);
} else {
if (_.isNumber(gt) && _.isNumber(lt) && lt > gt) {
chkRp = (lastRpVal !== val) && (val > gt) && (val < lt);
} else if (_.isNumber(gt) && _.isNumber(lt)) {
chkRp = _.isNumber(gt) && (lastRpVal !== val) && (val > gt);
chkRp = chkRp || (_.isNumber(lt) && (lastRpVal !== val) && (val < lt));
} else {
chkRp = (lastRpVal !== val);
}

if (_.isNumber(step)) {
chkRp = chkRp || (Math.abs(val - lastRpVal) > step);
}
}
}

if (chkRpVal) {
if (chkRp && _.isFunction(rpt.write)) {
rpt.write(val);
rAttrs.lastRpVal = val;
}
Expand Down
Loading

0 comments on commit 499470f

Please sign in to comment.