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

More tests for various things #5

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Node bindings for telldus-core
telldus-core-js===============================Node bindings for telldus-core# Basic UsageMake sure telldusd is running on the same machine.```javascriptvar tellduscore = require('telldus-core-js');var devices = tellduscore.getDevices();```API===getDevices----------Returns an array of device dictionary objects.Only configured devices are returned.Signature:```javascriptvar devices = tellduscore.getDevices();``````javascript[ { id: 1, name: 'name from telldus.conf', methods: [ 'TURNON', 'TURNOFF' ], model: 'codeswitch', type: 'DEVICE', status: {status: 'OFF'} }, ...]```turnOn------Turns a configured device ON.Signature:```javascripttellduscore.turnOn(deviceId);```Similar to the command```bashtdtool --on deviceId```turnOff-------Turns a configured device OFF.Signature:```javascripttellduscore.turnOff(deviceId);```Similar to the command```bashtdtool --off deviceId```dim---Dims a configured device to a certain level.Signature:```javascripttellduscore.dim(deviceId, level);```addRawDeviceEventListener-------------------------Add a listener for raw device events.This is usefull for scanning for devices not yet configuredSignature:```javascriptvar listener = tellduscore.addRawDeviceEventListener(function(arg1, values, arg3, arg4, arg5) {});```* `arg1`, `arg3`, `arg4`, `arg5`: TODO* `values`: A semicolon separated string with colon separated key / value pairs.```javascript'class:command;protocol:arctech;model:selflearning;house:5804222;unit:2;group:0;method:turnon;'```addDeviceEventListener----------------------Add a listener for device eventsSignature:```javascriptvar listener = tellduscore.addDeviceEventListener(function(deviceId, status) {});```* `status`: is an object of the form:``` {"status": "the status"}```removeEventListener-------------------Remove a previously added listener.Signature:```javascripttellduscore.removeEventListener(listener);```
Expand Down
117 changes: 97 additions & 20 deletions test/telldus.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@

/*global describe, it, before */
/*jshint laxcomma:true, node:true */
"use strict";
var assert = require('assert')
, should = require('should');
, should = require('should')
, util = require('util');


var telldus = require('../telldus');
var tellduscore = require('..');

describe("telldus wrapper", function () {
/*
* Used to decode the values from rawDeviceEventListener
*/
function decode(data) {
var result={};
var pairs = data.split(';');
//all pairs end with ; including last
for(var i = 0; i < pairs.length-1; i++) {
var keyval = pairs[i].split(':');
result[keyval[0]] = keyval[1];
}
return result;
}

/* for this test to work there must be a running
* telldusd and the first device must be a switch with
* TURNON,TURNOFF
*/
/* for this test to work there must be a running
* tellduscored and the first device must be a switch with
* TURNON,TURNOFF
*/
describe("tellduscore library should", function () {
before(function () {
this.devices = tellduscore.getDevices();
});
it("list devices", function () {
var devices = telldus.getDevices();
var devices = this.devices;
var some_really_big_number = 100;
//if devices is zero length then
//telldusd is probably not running
devices.length.should.be.within(1, 50);
//tellduscored is probably not running
devices.length.should.be.within(1, some_really_big_number);
var dev1 = devices[0];
dev1.should.have.property('name');
dev1.should.have.property('methods');
dev1.should.have.property('model');
dev1.should.have.property('type');
dev1.should.have.property('status');
//test status
dev1.status.should.have.property('name');
dev1.status.should.have.property('status');

//test methods
var methods = dev1.methods;
Expand All @@ -33,17 +53,74 @@ describe("telldus wrapper", function () {
});

it('turnOff', function() {
var devices = telldus.getDevices();
telldus.turnOff(devices[0].id);
devices = telldus.getDevices();
devices[0].status.should.have.property('name', 'OFF');
var device = this.devices[0];
tellduscore.turnOff(device.id);
//refresh
device = tellduscore.getDevices()[0];
device.status.should.have.property('status', 'OFF');
});

it('turnOn', function() {
var devices = telldus.getDevices();
telldus.turnOn(devices[0].id);
devices = telldus.getDevices();
devices[0].status.should.have.property('name', 'ON');
var device = this.devices[0];
tellduscore.turnOn(device.id);
//refresh
device = tellduscore.getDevices()[0];
device.status.should.have.property('status', 'ON');
});

describe('support events', function () {
/*
* For getting these tests to work something will have
* to generate at least 1 event during each test.
* That could be you pushing a remote.
*/
it("using rawDeviceEventListener", function (done) {
var seconds = 5; //for how many seconds should we wait for an event
console.log("waiting", seconds, "seconds for some raw events");
var count = 0; //counter for the events
this.timeout(seconds*1000+1000); //increase the test timeout
//set up a log file
var fs = require('fs');
var stream = fs.createWriteStream("eventdump.log");
//when the file stream is open , go
stream.once('open', function() {
var listener = tellduscore.addRawDeviceEventListener(function (arg1, values, arg3, arg4, arg5) {
//TODO:what is arg1 + arg3 - arg5
var data = decode(values);
//log the event
stream.write(util.format((new Date()), arguments) + '\n');
data.should.have.property('class');
data.should.have.property('protocol');
count++;
});

//after the defined time, remove listener and close stream
setTimeout(function () {
tellduscore.removeEventListener(listener); //remove the listener
stream.end(); //close the file stream
count.should.be.within(1,100); //we should have gotten at least 1 event.
done(); //consider the test done
},seconds*1000);
});//stream open
});//it should listen

it("deviceEventListener", function (done) {
var seconds = 5; // for how many seconds should we wait for an aevent
this.timeout(seconds * 1000 + 1000);
console.log("waiting", seconds, "seconds for some Device Events");
var count = 0;
var listener = tellduscore.addDeviceEventListener( function (device, status) {
device.should.be.within(1,100);
status.should.have.property('status');
count++;
});
setTimeout(function () {
tellduscore.removeEventListener(listener);
//we should have at least 1 event
count.should.be.within(1, 100);
done(); //consider the test done
},seconds * 1000);
});
});//describe events
});