From 0abb8311531453a959040964c268fb5067aecc55 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Sat, 25 Nov 2017 23:43:52 +1000 Subject: [PATCH 1/2] Add method to retrieve light uptime --- lib/lifx/light.js | 20 +++++++++++++++ lib/lifx/packet.js | 2 ++ lib/lifx/packets/getInfo.js | 7 ++++++ lib/lifx/packets/index.js | 3 +++ lib/lifx/packets/stateInfo.js | 46 +++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 lib/lifx/packets/getInfo.js create mode 100644 lib/lifx/packets/stateInfo.js diff --git a/lib/lifx/light.js b/lib/lifx/light.js index 429aa6d..e2b92c5 100644 --- a/lib/lifx/light.js +++ b/lib/lifx/light.js @@ -285,6 +285,26 @@ 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); +}; + /** * Requests used version from the microcontroller unit of the light * @param {Function} callback a function to accept the data diff --git a/lib/lifx/packet.js b/lib/lifx/packet.js index 070d405..34880f8 100644 --- a/lib/lifx/packet.js +++ b/lib/lifx/packet.js @@ -46,6 +46,8 @@ Packet.typeList = [ {id: 25, name: 'stateLabel'}, {id: 32, name: 'getVersion'}, {id: 33, name: 'stateVersion'}, + {id: 34, name: 'getInfo'}, + {id: 35, name: 'stateInfo'}, {id: 45, name: 'acknowledgement'}, {id: 48, name: 'getLocation'}, {id: 50, name: 'stateLocation'}, diff --git a/lib/lifx/packets/getInfo.js b/lib/lifx/packets/getInfo.js new file mode 100644 index 0000000..574e163 --- /dev/null +++ b/lib/lifx/packets/getInfo.js @@ -0,0 +1,7 @@ +'use strict'; + +var Packet = { + size: 0 +}; + +module.exports = Packet; diff --git a/lib/lifx/packets/index.js b/lib/lifx/packets/index.js index eb48f42..6d2119d 100644 --- a/lib/lifx/packets/index.js +++ b/lib/lifx/packets/index.js @@ -29,6 +29,9 @@ packets.statePower = require('./statePower'); packets.getVersion = require('./getVersion'); packets.stateVersion = require('./stateVersion'); +packets.getInfo = require('./getInfo'); +packets.stateInfo = require('./stateInfo'); + packets.acknowledgement = require('./acknowledgement'); packets.echoRequest = require('./echoRequest'); diff --git a/lib/lifx/packets/stateInfo.js b/lib/lifx/packets/stateInfo.js new file mode 100644 index 0000000..dbbb227 --- /dev/null +++ b/lib/lifx/packets/stateInfo.js @@ -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; +}; + +/** + * 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; From 57d194de0a9db2500a341239c43803b984901092 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Sat, 25 Nov 2017 23:44:38 +1000 Subject: [PATCH 2/2] Add method to reboot a light --- lib/lifx/light.js | 14 ++++++++++++++ lib/lifx/packet.js | 2 ++ lib/lifx/packets/index.js | 3 +++ lib/lifx/packets/rebootRequest.js | 7 +++++++ lib/lifx/packets/rebootResponse.js | 7 +++++++ 5 files changed, 33 insertions(+) create mode 100644 lib/lifx/packets/rebootRequest.js create mode 100644 lib/lifx/packets/rebootResponse.js diff --git a/lib/lifx/light.js b/lib/lifx/light.js index e2b92c5..8e02ddb 100644 --- a/lib/lifx/light.js +++ b/lib/lifx/light.js @@ -305,6 +305,20 @@ Light.prototype.getUptime = function(callback) { }, 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 diff --git a/lib/lifx/packet.js b/lib/lifx/packet.js index 34880f8..c0a932b 100644 --- a/lib/lifx/packet.js +++ b/lib/lifx/packet.js @@ -48,6 +48,8 @@ Packet.typeList = [ {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'}, diff --git a/lib/lifx/packets/index.js b/lib/lifx/packets/index.js index 6d2119d..bc73632 100644 --- a/lib/lifx/packets/index.js +++ b/lib/lifx/packets/index.js @@ -32,6 +32,9 @@ 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'); diff --git a/lib/lifx/packets/rebootRequest.js b/lib/lifx/packets/rebootRequest.js new file mode 100644 index 0000000..574e163 --- /dev/null +++ b/lib/lifx/packets/rebootRequest.js @@ -0,0 +1,7 @@ +'use strict'; + +var Packet = { + size: 0 +}; + +module.exports = Packet; diff --git a/lib/lifx/packets/rebootResponse.js b/lib/lifx/packets/rebootResponse.js new file mode 100644 index 0000000..574e163 --- /dev/null +++ b/lib/lifx/packets/rebootResponse.js @@ -0,0 +1,7 @@ +'use strict'; + +var Packet = { + size: 0 +}; + +module.exports = Packet;