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

Add methods to retrieve uptime and reboot bulb #63

Open
wants to merge 2 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
34 changes: 34 additions & 0 deletions lib/lifx/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,40 @@ Light.prototype.getHardwareVersion = function(callback) {
}, sqnNumber);
};

/**
* Requests uptime from the light
* @param {Function} callback a function to accept the data with error and
* message as parameters
*/
Light.prototype.getUptime = function(callback) {
if (typeof callback !== 'function') {
throw new TypeError('LIFX light getUptime method expects callback to be a function');
}
var packetObj = packet.create('getInfo', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateInfo', function(err, msg) {
if (err) {
return callback(err, null);
}
callback(null, msg.uptime);
}, sqnNumber);
};

/**
* Reboots the light
* @param {Function} callback called when light did receive message
*/
Light.prototype.reboot = function(callback) {
if (typeof callback !== 'function') {
throw new TypeError('LIFX light reboot method expects callback to be a function');
}
var packetObj = packet.create('rebootRequest', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('rebootResponse', callback, sqnNumber);
};

/**
* Requests used version from the microcontroller unit of the light
* @param {Function} callback a function to accept the data
Expand Down
4 changes: 4 additions & 0 deletions lib/lifx/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Packet.typeList = [
{id: 25, name: 'stateLabel'},
{id: 32, name: 'getVersion'},
{id: 33, name: 'stateVersion'},
{id: 34, name: 'getInfo'},
{id: 35, name: 'stateInfo'},
{id: 38, name: 'rebootRequest'},
{id: 43, name: 'rebootResponse'},
{id: 45, name: 'acknowledgement'},
{id: 48, name: 'getLocation'},
{id: 50, name: 'stateLocation'},
Expand Down
7 changes: 7 additions & 0 deletions lib/lifx/packets/getInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var Packet = {
size: 0
};

module.exports = Packet;
6 changes: 6 additions & 0 deletions lib/lifx/packets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ packets.statePower = require('./statePower');
packets.getVersion = require('./getVersion');
packets.stateVersion = require('./stateVersion');

packets.getInfo = require('./getInfo');
packets.stateInfo = require('./stateInfo');

packets.rebootRequest = require('./rebootRequest');
packets.rebootResponse = require('./rebootResponse');

packets.acknowledgement = require('./acknowledgement');

packets.echoRequest = require('./echoRequest');
Expand Down
7 changes: 7 additions & 0 deletions lib/lifx/packets/rebootRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var Packet = {
size: 0
};

module.exports = Packet;
7 changes: 7 additions & 0 deletions lib/lifx/packets/rebootResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var Packet = {
size: 0
};

module.exports = Packet;
46 changes: 46 additions & 0 deletions lib/lifx/packets/stateInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

var utils = require('../../lifx').utils;

var Packet = {
size: 24
};

Packet.parseNanoseconds = function(buf) {
if (buf.length !== 8) {
throw new Error('Invalid length given for nanoseconds field');
}

var low = buf.readUInt32LE(0);
var high = buf.readUInt32LE(4);

return (high * 2**32 + low) / 1.0E9;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double ** here is giving issues on the tests. Please also make this line a separate function to help understand what happens here.

};

/**
* Converts packet specific data from a buffer to an object
* @param {Buffer} buf Buffer containing only packet specific data no header
* @return {Object} Information contained in packet
*/
Packet.toObject = function(buf) {
var obj = {};
var offset = 0;

// Check length
if (buf.length !== this.size) {
throw new Error('Invalid length given for stateInfo LIFX packet');
}

obj.time = this.parseNanoseconds(utils.readUInt64LE(buf, offset));
offset += 8;

obj.uptime = this.parseNanoseconds(utils.readUInt64LE(buf, offset));
offset += 8;

obj.downtime = this.parseNanoseconds(utils.readUInt64LE(buf, offset));
offset += 8;

return obj;
};

module.exports = Packet;