UNPKG

irrelon-reactor-device

Version:

A module for easily creating reactor core devices.

171 lines (153 loc) 6.4 kB
"use strict"; var async = require('async'); var Device = require('../').default; var tb = require('testbear'); tb.config.noCatch = true; tb.test('Device', 'Test instantiate and usage', function (finishTest) { var rd1 = new Device({ "_id": "test-device-1", "accountId": "rob@irrelon.com", "type": "irrelon-reactor-alarm", "name": "Main Alarm", "location": { "title": "Property 1" } }); var rd2 = new Device({ "_id": "test-device-2", "accountId": "rob@irrelon.com", "type": "irrelon-alarm-controller", "name": "Lobby Alarm Controller", "location": { "title": "Property 1" } }); rd1.on('stateUpdateRequest', function (stateId, oldVal, newVal, callback) { // Indicate that we are happy to allow the update to proceed // and ensure we pass the final value we want to set callback(false, newVal); console.log('Remote updated state', stateId, 'from', oldVal, 'to', newVal); }); rd1.discover({"type": "irrelon-alarm-controller"}); rd2.discover({"type": "irrelon-reactor-alarm"}); rd1.on('clientAdded', function (client) { tb.strictEqual(client._id, 'test-device-2', 'RD1 found correct client: ' + client.name); }); rd2.on('clientAdded', function (client) { tb.strictEqual(client._id, 'test-device-1', 'RD2 found correct client: ' + client.name); }); rd1.startDevice('0.0.0.0', 0, function (err1, serverAddress1) { rd2.startDevice('0.0.0.0', 0, function (err2, serverAddress2) { rd1.advertise(); rd2.advertise(); async.series([function (finish) { // Delete any existing state - not part of the test tb.ok(true, '1: Deleting rd1 state'); rd1.delete('rd1State', function (err, data) { finish(); }); }, function (finish) { // Delete any existing state - not part of the test tb.ok(true, '2: Deleting rd2 state'); rd2.delete('rd2State', function (err, data) { finish(); }); }, function (finish) { tb.ok(true, '3: Setting rd2 clientStateUpdate listener'); rd2.once('clientStateUpdate', function (client, stateId, oldVal, newVal) { //console.log('3: rd2\'s client "' + client._id + '" state "' + stateId + '" updated from "' + oldVal + '" to new value "' + newVal + '"'); tb.strictEqual(client._id, 'test-device-1', 'State update received from correct client: ' + client.name); finish(false); }); tb.ok(true, '4: Updating rd1 state'); rd1.update('rd1State', 'on', function (err, data) { tb.ok(!err, '4: State updated with no error'); tb.strictEqual(data._id, "rd1State", "4: State id was correct"); tb.strictEqual(data.val, "on", "4: State data was correct"); }); }, function (finish) { tb.ok(true, '5: Setting rd1 clientStateUpdate listener'); rd1.once('clientStateUpdate', function (client, stateId, oldVal, newVal) { //console.log('5: rd1\'s client "' + client._id + '" state "' + stateId + '" updated from "' + oldVal + '" to new value "' + newVal + '"'); tb.strictEqual(client._id, 'test-device-2', '5: State update received from correct client: ' + client.name); finish(false); }); tb.ok(true, '6: Updating rd2 state'); rd2.update('rd2State', 'on', function (err, data) { tb.ok(!err, '6: State updated with no error'); tb.strictEqual(data._id, "rd2State", "6: State id was correct"); tb.strictEqual(data.val, "on", "6: State data was correct"); }); }, function (finish) { tb.ok(true, '7: Setting rd1 clientStateUpdate listener'); rd1.once('clientStateUpdate', function (client, stateId, oldVal, newVal) { //console.log('7: rd1\'s client "' + client._id + '" state "' + stateId + '" updated from "' + oldVal + '" to new value "' + newVal + '"'); tb.strictEqual(client._id, 'test-device-2', '7: State update received from correct client: ' + client.name); tb.strictEqual(stateId, 'rd2State', '7: State update has correct stateId: ' + stateId); tb.strictEqual(newVal, 'off', '7: State update has correct new value: ' + newVal); finish(false); }); tb.ok(true, '8: Updating rd2 state'); rd2.update('rd2State', 'off', function (err, data) { tb.ok(!err, '8: State updated with no error'); tb.strictEqual(data._id, "rd2State", "8: State id was correct"); tb.strictEqual(data.val, "off", "8: State data was correct"); }); }, function (finish) { tb.ok(true, '9: Checking rd1 state'); rd1.state('rd1State', function (err, data) { tb.ok(!err, '9: State find with no error'); tb.strictEqual(data._id, "rd1State", "9: State id was correct"); tb.strictEqual(data.val, "on", "9: State data was correct"); finish(err); }); }, function (finish) { tb.ok(true, '10: Checking rd2 state'); rd2.state('rd2State', function (err, data) { //console.log(data); tb.ok(!err, '10: State find with no error'); tb.strictEqual(data._id, "rd2State", "10: State id was correct"); tb.strictEqual(data.val, "off", "10: State data was correct"); finish(err); }); }, function (finish) { tb.ok(true, '11: Deleting rd1 state'); rd1.delete('rd1State', function (err, data) { tb.ok(!err, '11: State deleted with no error'); tb.strictEqual(data._id, "rd1State", "11: State id was correct"); tb.strictEqual(data.val, "on", "11: State data was correct"); finish(); }); }, function (finish) { tb.ok(true, '12: Deleting rd2 state'); rd2.delete('rd2State', function (err, data) { tb.ok(!err, '12: State deleted with no error'); tb.strictEqual(data._id, "rd2State", "12: State id was correct"); tb.strictEqual(data.val, "off", "12: State data was correct"); finish(); }); }, function (finish) { tb.ok(true, '13: Checking rd1 state'); rd1.state('rd1State', function (err, data) { tb.ok(!err, '13: State find with no error'); tb.strictEqual(data, undefined, "13: State data was correct"); finish(err); }); }, function (finish) { tb.ok(true, '14: Checking rd2 state'); rd2.state('rd2State', function (err, data) { tb.ok(!err, '14: State find with no error'); tb.strictEqual(data, undefined, "14: State data was correct"); finish(err); }); }], function (err) { if (err) { console.log('Error: ' + err); } tb.expect(46, 'Correct number'); finishTest(); }); }); }); }); tb.start();