Skip to content

Commit

Permalink
Always pass hashes with this to resolve param. Fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
nitriques committed Oct 30, 2014
1 parent b73511b commit 7b21917
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
13 changes: 7 additions & 6 deletions lib/tosr0x.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ T.prototype.send = function (cmd, callback) {
s.drain(function (err) {
if (resolved(err, rej, callback)) {
call(callback, bytesWritten);
res(bytesWritten);
res({bytesWritten:bytesWritten, ctl: t});
}
});
}
Expand All @@ -292,7 +292,7 @@ T.prototype.switch = function (relay, on, callback) {
}
var cmd = !!on ? T.commands.on : T.commands.off;
return this.send(String.fromCharCode(cmd + relay), function (err, ret) {
callback(err, ret);
call(callback, err, ret);
if (!!err) {
return;
}
Expand Down Expand Up @@ -329,7 +329,7 @@ T.prototype.states = T.prototype.refreshStates = function (callback) {
t.states[relayCount - x] = (binStates & mask) === mask;
}
call(callback, t.states);
res(t.states);
res({states: t.states, ctl: t});
});
}, this._options.timeout, function () { t.closeImmediate(); });
this.send(T.commands.states);
Expand All @@ -346,8 +346,8 @@ T.prototype.version = function (callback) {
if (data[0] == T.device.moduleId) {
v = data[1];
}
call(callback, v);
res(v);
call(callback, v, t);
res({version: v, ctl: t});
});
}, this._options.timeout, function () { t.closeImmediate(); });
this.send(T.commands.version);
Expand All @@ -362,7 +362,7 @@ T.prototype.voltage = function (callback) {
// is a fixed floating point with precision of 1
var v = data[0] * 0.1;
call(callback, v);
res(v);
res({voltage: v, ctl: t});
});
}, this._options.timeout, function () { t.closeImmediate(); });
this.send(T.commands.voltage);
Expand All @@ -374,6 +374,7 @@ T.prototype.temperature = function (callback) {
var promise = ptime(function temperature(res, rej) {
t.receive(function (data) {
call(callback, data);
res({temperature: data, ctl: t});
}, true);
}, this._options.timeout, function () { t.closeImmediate(); });
this.send(T.commands.temperature);
Expand Down
60 changes: 39 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,72 @@
'use strict';
var Tosr0x = require('./lib/tosr0x').Tosr0x;
var relay = 1;
var ctl;

//Tosr0x.fromPortScan(null, function (err, ctl) {
Tosr0x.fromPortScan().then(function (c) {
ctl = c;
console.time('');
Tosr0x.create().then(function (ctl) {
console.log('Controller found!');
console.timeEnd('');
console.time('');
//ctl.open(function (err, ctl) {
return ctl.open();
})
.then(function (ctl) {
console.log('Connected!!!');
console.timeEnd('');
console.time('');
return ctl.version();
})
.then(function (version) {
console.log('Version is ' + version);
return ctl.voltage();
.then(function (ret) {
console.log('Version is ' + ret.version);
console.timeEnd('');
console.time('');
return ret.ctl.voltage();
})
.then(function (voltage) {
console.log('Voltage is ' + voltage);
return ctl.refreshStates();
.then(function (ret) {
console.log('Voltage is ' + ret.voltage);
console.timeEnd('');
console.time('');
return ret.ctl.refreshStates();
})
.then(function (states) {
console.log('State', states);
.then(function (ret) {
console.log('State', ret.states);
console.timeEnd('');
console.time('');
return ret.ctl;
})
.then(function () {
.then(function (ctl) {
console.log('Turning realy ' + relay + ' on');
console.timeEnd('');
console.time('');
//ctl.on(1, function () {
return ctl.on(relay);
})
.then(function () {
.then(function (ret) {
console.log('Relay ' + relay + ' is on!!!');
console.timeEnd('');
return new (require('rsvp').Promise)(function (res, rej) {
setTimeout(function () {
res();
console.time('');
res(ret.ctl);
}, 2000);
});
})
.then(function () {
.then(function (ctl) {
console.log('Turning relay ' + relay + ' off');
console.timeEnd('');
console.time('');
return ctl.off(relay);
})
.then(function () {
return ctl.close();
.then(function (ret) {
console.log('Closing...');
console.timeEnd('');
console.time('');
return ret.ctl.close();
})
.catch(function (err) {
console.error('ERROR ' + err);
})
.finally(function () {
if (!!ctl) {
ctl.closeImmediate();
}
console.timeEnd('');
console.log('Exit.');
});

0 comments on commit 7b21917

Please sign in to comment.