homebridge-syntex-magichome
Advanced tools
Comparing version 1.0.0-b25 to 1.0.0-b26
{ | ||
"name": "homebridge-syntex-magichome", | ||
"version": "1.0.0-b25", | ||
"version": "1.0.0-b26", | ||
"description": "Homebridge Plugin for MagicHome LED Strips with preset scenes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,192 +0,220 @@ | ||
// | ||
// clearDirSwitch.js | ||
// Sahil Chaddha | ||
// | ||
// Created by Sahil Chaddha on 26/08/2018. | ||
// Copyright © 2018 sahilchaddha.com. All rights reserved. | ||
// | ||
const convert = require('color-convert'); | ||
const Accessory = require('./base'); | ||
const convert = require('color-convert') | ||
const Accessory = require('./base') | ||
const LightBulb = class extends Accessory | ||
{ | ||
constructor(config, log, homebridge) | ||
{ | ||
super(config, log, homebridge); | ||
const LightBulb = class extends Accessory { | ||
constructor(config, log, homebridge) { | ||
super(config, log, homebridge) | ||
this.name = config.name || 'LED Controller' | ||
this.ip = config.ip | ||
this.setup = config.setup || 'RGBW' | ||
this.color = { H: 0, S: 0, L: 100 } | ||
this.purewhite = config.purewhite || false | ||
this.timeout = config.timeout != null ? config.timeout : 60000 | ||
setTimeout(() => { | ||
this.updateState() | ||
}, 3000) | ||
} | ||
this.name = config.name || 'LED Controller'; | ||
this.ip = config.ip; | ||
this.setup = config.setup || 'RGBW'; | ||
this.color = { H: 0, S: 0, L: 100 }; | ||
this.purewhite = config.purewhite || false; | ||
this.timeout = config.timeout != null ? config.timeout : 60000; | ||
setTimeout(() => { | ||
getAccessoryServices() { | ||
var lightbulbService = new this.homebridge.Service.Lightbulb(this.name) | ||
this.updateState(); | ||
lightbulbService | ||
.getCharacteristic(this.homebridge.Characteristic.On) | ||
.on('get', this.getPowerState.bind(this)) | ||
.on('set', this.setPowerState.bind(this)) | ||
}, 3000) | ||
} | ||
lightbulbService | ||
.addCharacteristic(new this.homebridge.Characteristic.Hue()) | ||
.on('get', this.getHue.bind(this)) | ||
.on('set', this.setHue.bind(this)) | ||
getAccessoryServices() | ||
{ | ||
var lightbulbService = new this.homebridge.Service.Lightbulb(this.name); | ||
lightbulbService | ||
.addCharacteristic(new this.homebridge.Characteristic.Saturation()) | ||
.on('get', this.getSaturation.bind(this)) | ||
.on('set', this.setSaturation.bind(this)) | ||
lightbulbService.getCharacteristic(this.homebridge.Characteristic.On) | ||
.on('get', this.getPowerState.bind(this)) | ||
.on('set', this.setPowerState.bind(this)); | ||
lightbulbService | ||
.addCharacteristic(new this.homebridge.Characteristic.Brightness()) | ||
.on('get', this.getBrightness.bind(this)) | ||
.on('set', this.setBrightness.bind(this)) | ||
lightbulbService.addCharacteristic(new this.homebridge.Characteristic.Hue()) | ||
.on('get', this.getHue.bind(this)) | ||
.on('set', this.setHue.bind(this)); | ||
return [lightbulbService] | ||
} | ||
lightbulbService.addCharacteristic(new this.homebridge.Characteristic.Saturation()) | ||
.on('get', this.getSaturation.bind(this)) | ||
.on('set', this.setSaturation.bind(this)); | ||
sendCommand(command, callback) { | ||
this.executeCommand(this.ip, command, callback) | ||
} | ||
lightbulbService.addCharacteristic(new this.homebridge.Characteristic.Brightness()) | ||
.on('get', this.getBrightness.bind(this)) | ||
.on('set', this.setBrightness.bind(this)); | ||
getModelName() { | ||
return 'Light Bulb' | ||
} | ||
return [lightbulbService]; | ||
} | ||
getSerialNumber() { | ||
return '00-001-LightBulb' | ||
} | ||
sendCommand(command, callback) | ||
{ | ||
this.executeCommand(this.ip, command, callback); | ||
} | ||
logMessage(...args) { | ||
if (this.config.debug) { | ||
this.log(args) | ||
} | ||
} | ||
getModelName() | ||
{ | ||
return 'Light Bulb'; | ||
} | ||
startTimer() { | ||
if (this.timeout === 0) return | ||
setTimeout(() => { | ||
this.updateState() | ||
}, this.timeout) | ||
} | ||
getSerialNumber() | ||
{ | ||
return '00-001-LightBulb'; | ||
} | ||
updateState() { | ||
const self = this | ||
this.logMessage('Polling Light', this.ip) | ||
self.getState((settings) => { | ||
self.isOn = settings.on | ||
self.color = settings.color | ||
self.logMessage('Updating Device', self.ip, self.color, self.isOn) | ||
self.services[0] | ||
.getCharacteristic(this.homebridge.Characteristic.On) | ||
.updateValue(self.isOn) | ||
self.services[0] | ||
.getCharacteristic(this.homebridge.Characteristic.Hue) | ||
.updateValue(self.color.H) | ||
self.services[0] | ||
.getCharacteristic(this.homebridge.Characteristic.Saturation) | ||
.updateValue(self.color.S) | ||
self.services[0] | ||
.getCharacteristic(this.homebridge.Characteristic.Brightness) | ||
.updateValue(self.color.L) | ||
this.startTimer() | ||
}) | ||
} | ||
logMessage(...args) | ||
{ | ||
if(this.config.debug) | ||
{ | ||
this.log(args); | ||
} | ||
} | ||
getState(callback) { | ||
this.sendCommand('-i', (error, stdout) => { | ||
var settings = { | ||
on: false, | ||
color: { H: 255, S: 100, L: 50 }, | ||
} | ||
startTimer() | ||
{ | ||
if (this.timeout === 0) return; | ||
var colors = stdout.match(/\(.*,.*,.*\)/g) | ||
var isOn = stdout.match(/\] ON /g) | ||
if (isOn && isOn.length > 0) { | ||
settings.on = true | ||
} | ||
if (colors && colors.length > 0) { | ||
// Remove last char ) | ||
var str = colors.toString().substring(0, colors.toString().length - 1) | ||
// Remove First Char ( | ||
str = str.substring(1, str.length) | ||
const rgbColors = str.split(',').map((item) => { | ||
return item.trim() | ||
}) | ||
var converted = convert.rgb.hsv(rgbColors) | ||
settings.color = { | ||
H: converted[0], | ||
S: converted[1], | ||
L: converted[2], | ||
} | ||
} | ||
setTimeout(() => { | ||
callback(settings) | ||
}) | ||
} | ||
this.updateState(); | ||
getPowerState(callback) { | ||
callback(null, this.isOn) | ||
} | ||
}, this.timeout); | ||
} | ||
setPowerState(value, callback) { | ||
const self = this | ||
this.sendCommand(value ? '--on' : '--off', () => { | ||
self.isOn = value | ||
callback() | ||
}) | ||
} | ||
updateState() | ||
{ | ||
const self = this; | ||
getHue(callback) { | ||
callback(null, this.color.H) | ||
} | ||
this.logMessage('Polling Light', this.ip); | ||
setHue(value, callback) { | ||
this.color.H = value | ||
this.setToCurrentColor() | ||
callback() | ||
} | ||
self.getState((settings) => { | ||
getBrightness(callback) { | ||
callback(null, this.color.L) | ||
} | ||
self.isOn = settings.on; | ||
self.color = settings.color; | ||
setBrightness(value, callback) { | ||
this.color.L = value | ||
this.setToCurrentColor() | ||
callback() | ||
} | ||
self.logMessage('Updating Device', self.ip, self.color, self.isOn); | ||
getSaturation(callback) { | ||
callback(null, this.color.S) | ||
} | ||
self.services[0].getCharacteristic(this.homebridge.Characteristic.On).updateValue(self.isOn); | ||
self.services[0].getCharacteristic(this.homebridge.Characteristic.Hue).updateValue(self.color.H); | ||
self.services[0].getCharacteristic(this.homebridge.Characteristic.Saturation).updateValue(self.color.S); | ||
self.services[0].getCharacteristic(this.homebridge.Characteristic.Brightness).updateValue(self.color.L); | ||
setSaturation(value, callback) { | ||
this.color.S = value | ||
this.setToCurrentColor() | ||
callback() | ||
} | ||
this.startTimer(); | ||
}); | ||
} | ||
setToWarmWhite() { | ||
this.sendCommand('-w ' + this.color.L) | ||
} | ||
getState(callback) | ||
{ | ||
this.sendCommand('-i', (error, stdout) => { | ||
setToCurrentColor() { | ||
var color = this.color | ||
if (color.S === 0 && color.H === 0 && this.purewhite) { | ||
this.setToWarmWhite() | ||
return | ||
} | ||
var settings = { | ||
on: false, | ||
color: { H: 255, S: 100, L: 50 } | ||
}; | ||
var converted = convert.hsv.rgb([color.H, color.S, color.L]) | ||
this.logMessage('Setting New Color From ', this.ip, color, converted) | ||
var base = '-x ' + this.setup + ' -c ' | ||
this.sendCommand(base + converted[0] + ',' + converted[1] + ',' + converted[2]) | ||
} | ||
var colors = stdout.match(/\(.*,.*,.*\)/g); | ||
var isOn = stdout.match(/\] ON /g); | ||
if(isOn && isOn.length > 0) | ||
{ | ||
settings.on = true; | ||
} | ||
if(colors && colors.length > 0) | ||
{ | ||
// Remove last char ) | ||
var str = colors.toString().substring(0, colors.toString().length - 1); | ||
// Remove First Char ( | ||
str = str.substring(1, str.length); | ||
const rgbColors = str.split(',').map((item) => { | ||
return item.trim() | ||
}); | ||
var converted = convert.rgb.hsv(rgbColors); | ||
settings.color = { | ||
H: converted[0], | ||
S: converted[1], | ||
L: converted[2] | ||
}; | ||
} | ||
callback(settings); | ||
}) | ||
} | ||
getPowerState(callback) | ||
{ | ||
callback(null, this.isOn); | ||
} | ||
setPowerState(value, callback) | ||
{ | ||
const self = this; | ||
this.sendCommand(value ? '--on' : '--off', () => { | ||
self.isOn = value; | ||
callback(); | ||
}); | ||
} | ||
getHue(callback) | ||
{ | ||
callback(null, this.color.H); | ||
} | ||
setHue(value, callback) | ||
{ | ||
this.color.H = value; | ||
this.setToCurrentColor(); | ||
callback(); | ||
} | ||
getBrightness(callback) | ||
{ | ||
callback(null, this.color.L); | ||
} | ||
setBrightness(value, callback) | ||
{ | ||
this.color.L = value; | ||
this.setToCurrentColor(); | ||
callback(); | ||
} | ||
getSaturation(callback) | ||
{ | ||
callback(null, this.color.S); | ||
} | ||
setSaturation(value, callback) | ||
{ | ||
this.color.S = value; | ||
this.setToCurrentColor(); | ||
callback(); | ||
} | ||
setToWarmWhite() | ||
{ | ||
this.sendCommand('-w ' + this.color.L); | ||
} | ||
setToCurrentColor() | ||
{ | ||
var color = this.color; | ||
if(color.S === 0 && color.H === 0 && this.purewhite) | ||
{ | ||
this.setToWarmWhite(); | ||
return; | ||
} | ||
var converted = convert.hsv.rgb([color.H, color.S, color.L]); | ||
this.logMessage('Setting New Color From ', this.ip, color, converted); | ||
var base = '-x ' + this.setup + ' -c '; | ||
this.sendCommand(base + converted[0] + ',' + converted[1] + ',' + converted[2]); | ||
} | ||
} | ||
module.exports = LightBulb | ||
module.exports = LightBulb; |
@@ -1,113 +0,140 @@ | ||
// | ||
// presetSwitch.js | ||
// Sahil Chaddha | ||
// | ||
// Created by Sahil Chaddha on 13/10/2018. | ||
// Copyright © 2018 sahilchaddha.com. All rights reserved. | ||
// | ||
const Accessory = require('./base'); | ||
const preset = require('../presets'); | ||
const emitter = require('../lib/emitter'); | ||
const Accessory = require('./base') | ||
const preset = require('../presets') | ||
const emitter = require('../lib/emitter') | ||
const PresetSwitch = class extends Accessory | ||
{ | ||
constructor(config, log, homebridge) | ||
{ | ||
super(config, log, homebridge); | ||
const PresetSwitch = class extends Accessory { | ||
constructor(config, log, homebridge) { | ||
super(config, log, homebridge) | ||
this.isOn = false | ||
this.name = config.name || 'LED Controller Presets' | ||
this.ips = Object.keys(config.ips) | ||
this.preset = config.preset || 'seven_color_cross_fade' | ||
this.sceneValue = preset[this.preset] | ||
if (this.sceneValue == null) { | ||
log('Present Not Found... Try Different Preset') | ||
this.sceneValue = 37 | ||
} | ||
this.speed = config.speed || 40 | ||
// Should Turn Off Light When Turn Off Preset | ||
this.shouldTurnOff = config.shouldTurnOff || true | ||
this.bindEmitter() | ||
} | ||
this.isOn = false; | ||
this.name = config.name || 'LED Controller Presets'; | ||
this.ips = Object.keys(config.ips); | ||
this.preset = config.preset || 'seven_color_cross_fade'; | ||
this.sceneValue = preset[this.preset]; | ||
bindEmitter() { | ||
const self = this | ||
emitter.on('MagicHomeSynTexPresetTurnedOn', (presetName) => { | ||
if (presetName !== self.name) { | ||
self.updateState(false) | ||
} | ||
}) | ||
} | ||
if(this.sceneValue == null) | ||
{ | ||
log('Present Not Found... Try Different Preset'); | ||
this.sceneValue = 37; | ||
} | ||
getAccessoryServices() { | ||
const switchService = new this.homebridge.Service.Switch(this.name) | ||
switchService | ||
.getCharacteristic(this.homebridge.Characteristic.On) | ||
.on('get', this.getState.bind(this)) | ||
.on('set', this.switchStateChanged.bind(this)) | ||
return [switchService] | ||
} | ||
this.speed = config.speed || 40; | ||
// Should Turn Off Light When Turn Off Preset | ||
this.shouldTurnOff = config.shouldTurnOff || true; | ||
this.bindEmitter(); | ||
} | ||
sendCommand(command, callback) { | ||
this.executeCommand(this.ips, command, callback) | ||
} | ||
bindEmitter() | ||
{ | ||
const self = this; | ||
switchStateChanged(newState, callback) { | ||
this.isOn = newState | ||
const self = this | ||
if (newState === true) { | ||
// Turn Off Other Running Scenes | ||
emitter.emit('MagicHomeSynTexPresetTurnedOn', self.name) | ||
self.sendCommand('--on', () => { | ||
setTimeout(() => { | ||
self.sendCommand('-p ' + self.sceneValue + ' ' + self.speed, () => { | ||
callback() | ||
}) | ||
}, 3000) | ||
}) | ||
} else { | ||
// Turning OFF | ||
var promiseArray = [] | ||
Object.keys(self.config.ips).forEach((ip) => { | ||
const newPromise = new Promise((resolve) => { | ||
self.executeCommand(ip, ' -c ' + self.config.ips[ip], () => { | ||
resolve() | ||
}) | ||
}) | ||
promiseArray.push(newPromise) | ||
}) | ||
emitter.on('MagicHomeSynTexPresetTurnedOn', (presetName) => { | ||
Promise.all(promiseArray) | ||
.then(() => { | ||
if (self.shouldTurnOff) { | ||
setTimeout(() => { | ||
self.sendCommand('--off', () => { | ||
callback() | ||
}) | ||
}, 3000) | ||
} else { | ||
callback() | ||
} | ||
}) | ||
} | ||
} | ||
if(presetName !== self.name) | ||
{ | ||
self.updateState(false); | ||
} | ||
}) | ||
} | ||
updateState(newValue) { | ||
this.isOn = newValue | ||
this.services[0] | ||
.getCharacteristic(this.homebridge.Characteristic.On) | ||
.updateValue(this.isOn) | ||
} | ||
getAccessoryServices() | ||
{ | ||
const switchService = new this.homebridge.Service.Switch(this.name); | ||
getState(callback) { | ||
callback(null, this.isOn) | ||
} | ||
switchService.getCharacteristic(this.homebridge.Characteristic.On) | ||
.on('get', this.getState.bind(this)) | ||
.on('set', this.switchStateChanged.bind(this)); | ||
getModelName() { | ||
return 'Preset Switch' | ||
} | ||
return [switchService]; | ||
} | ||
getSerialNumber() { | ||
return '00-001-PresetSwitch' | ||
} | ||
sendCommand(command, callback) | ||
{ | ||
this.executeCommand(this.ips, command, callback); | ||
} | ||
switchStateChanged(newState, callback) | ||
{ | ||
this.isOn = newState; | ||
const self = this; | ||
if(newState === true) | ||
{ | ||
// Turn Off Other Running Scenes | ||
emitter.emit('MagicHomeSynTexPresetTurnedOn', self.name); | ||
self.sendCommand('--on', () => { | ||
setTimeout(() => { | ||
self.sendCommand('-p ' + self.sceneValue + ' ' + self.speed, () => { | ||
callback(); | ||
}); | ||
}, 3000); | ||
}); | ||
} | ||
else | ||
{ | ||
// Turning OFF | ||
var promiseArray = []; | ||
Object.keys(self.config.ips).forEach((ip) => { | ||
const newPromise = new Promise((resolve) => { | ||
self.executeCommand(ip, ' -c ' + self.config.ips[ip], () => { | ||
resolve(); | ||
}); | ||
}); | ||
promiseArray.push(newPromise); | ||
}); | ||
Promise.all(promiseArray).then(() => { | ||
if(self.shouldTurnOff) | ||
{ | ||
setTimeout(() => { | ||
self.sendCommand('--off', () => { | ||
callback(); | ||
}); | ||
}, 3000); | ||
} | ||
else | ||
{ | ||
callback(); | ||
} | ||
}); | ||
} | ||
} | ||
updateState(newValue) | ||
{ | ||
this.isOn = newValue; | ||
this.services[0].getCharacteristic(this.homebridge.Characteristic.On).updateValue(this.isOn); | ||
} | ||
getState(callback) | ||
{ | ||
callback(null, this.isOn); | ||
} | ||
getModelName() | ||
{ | ||
return 'Preset Switch'; | ||
} | ||
getSerialNumber() | ||
{ | ||
return '00-001-PresetSwitch'; | ||
} | ||
} | ||
module.exports = PresetSwitch | ||
module.exports = PresetSwitch; |
@@ -1,79 +0,90 @@ | ||
// | ||
// clearDirSwitch.js | ||
// Sahil Chaddha | ||
// | ||
// Created by Sahil Chaddha on 13/10/2018. | ||
// Copyright © 2018 sahilchaddha.com. All rights reserved. | ||
// | ||
const Accessory = require('./base'); | ||
const emitter = require('../lib/emitter'); | ||
const Accessory = require('./base') | ||
const emitter = require('../lib/emitter') | ||
const ResetSwitch = class extends Accessory | ||
{ | ||
constructor(config, log, homebridge) | ||
{ | ||
super(config, log, homebridge); | ||
const ResetSwitch = class extends Accessory { | ||
constructor(config, log, homebridge) { | ||
super(config, log, homebridge) | ||
this.name = config.name || 'Reset LED Controller Presets' | ||
this.ips = Object.keys(config.ips) | ||
} | ||
this.name = config.name || 'Reset LED Controller Presets'; | ||
this.ips = Object.keys(config.ips); | ||
} | ||
getAccessoryServices() { | ||
const switchService = new this.homebridge.Service.Switch(this.name) | ||
switchService | ||
.getCharacteristic(this.homebridge.Characteristic.On) | ||
.on('get', this.getState.bind(this)) | ||
.on('set', this.switchStateChanged.bind(this)) | ||
return [switchService] | ||
} | ||
getAccessoryServices() | ||
{ | ||
const switchService = new this.homebridge.Service.Switch(this.name); | ||
sendCommand(command, callback) { | ||
this.executeCommand(this.ips, command, callback) | ||
} | ||
switchService.getCharacteristic(this.homebridge.Characteristic.On) | ||
.on('get', this.getState.bind(this)) | ||
.on('set', this.switchStateChanged.bind(this)); | ||
switchStateChanged(newState, callback) { | ||
const self = this | ||
emitter.emit('MagicHomeSynTexPresetTurnedOn', self.name) | ||
var promiseArray = [] | ||
Object.keys(self.config.ips).forEach((ip) => { | ||
const newPromise = new Promise((resolve) => { | ||
self.executeCommand(ip, ' -c ' + self.config.ips[ip], () => { | ||
resolve() | ||
}) | ||
}) | ||
promiseArray.push(newPromise) | ||
}) | ||
return [switchService]; | ||
} | ||
Promise.all(promiseArray) | ||
.then(() => { | ||
setTimeout(() => { | ||
self.sendCommand('--off', () => { | ||
callback() | ||
}) | ||
}, 3000) | ||
}) | ||
.then(() => { | ||
setTimeout(() => { | ||
self.updateState() | ||
}, 2000) | ||
}) | ||
} | ||
sendCommand(command, callback) | ||
{ | ||
this.executeCommand(this.ips, command, callback); | ||
} | ||
updateState() { | ||
this.services[0] | ||
.getCharacteristic(this.homebridge.Characteristic.On) | ||
.updateValue(false) | ||
} | ||
switchStateChanged(newState, callback) | ||
{ | ||
const self = this; | ||
getState(callback) { | ||
callback(null, false) | ||
} | ||
emitter.emit('MagicHomeSynTexPresetTurnedOn', self.name); | ||
getModelName() { | ||
return 'Reset Switch' | ||
} | ||
var promiseArray = []; | ||
getSerialNumber() { | ||
return '00-001-ResetSwitch' | ||
} | ||
Object.keys(self.config.ips).forEach((ip) => { | ||
const newPromise = new Promise((resolve) => { | ||
self.executeCommand(ip, ' -c ' + self.config.ips[ip], () => { | ||
resolve(); | ||
}); | ||
}); | ||
promiseArray.push(newPromise); | ||
}); | ||
Promise.all(promiseArray).then(() => { | ||
setTimeout(() => { | ||
self.sendCommand('--off', () => { | ||
callback(); | ||
}); | ||
}, 3000); | ||
}).then(() => { | ||
setTimeout(() => { | ||
self.updateState(); | ||
}, 2000); | ||
}); | ||
} | ||
updateState() | ||
{ | ||
this.services[0].getCharacteristic(this.homebridge.Characteristic.On).updateValue(false); | ||
} | ||
getState(callback) | ||
{ | ||
callback(null, false); | ||
} | ||
getModelName() | ||
{ | ||
return 'Reset Switch'; | ||
} | ||
getSerialNumber() | ||
{ | ||
return '00-001-ResetSwitch'; | ||
} | ||
} | ||
module.exports = ResetSwitch | ||
module.exports = ResetSwitch; |
@@ -1,9 +0,1 @@ | ||
// | ||
// lightAgent.js | ||
// Sahil Chaddha | ||
// | ||
// Created by Sahil Chaddha on 22/10/2018. | ||
// Copyright © 2018 sahilchaddha.com. All rights reserved. | ||
// | ||
const cp = require('child_process') | ||
@@ -17,166 +9,231 @@ const path = require('path') | ||
constructor() { | ||
this.cachedAddress = {} | ||
this.pollingInterval = 300 * 1000 // 5 Minutes | ||
this.logger = null | ||
this.storage = null | ||
this.hasDiscoveryStarted = false | ||
this.isVerbose = false | ||
this.shouldDiscover = true | ||
} | ||
constructor() | ||
{ | ||
this.cachedAddress = {}; | ||
this.pollingInterval = 300 * 1000; | ||
this.logger = null; | ||
this.storage = null; | ||
this.hasDiscoveryStarted = false; | ||
this.isVerbose = false; | ||
this.shouldDiscover = true; | ||
} | ||
getCachedAddress() { | ||
if (!this.storage) { | ||
return {} | ||
} | ||
this.log('Getting Bulbs from Cache') | ||
return this.storage.getItem(cacheKey) | ||
.then((data) => { | ||
var devices = {} | ||
if (data) { | ||
try { | ||
devices = JSON.parse(data) | ||
} catch (error) { | ||
devices = {} | ||
} | ||
} | ||
this.log(' ** Fetched Lights from Cache **') | ||
this.log(devices) | ||
return devices | ||
}) | ||
} | ||
getCachedAddress() | ||
{ | ||
if(!this.storage) | ||
{ | ||
return {}; | ||
} | ||
saveAddress(res) { | ||
if (this.storage) { | ||
const data = JSON.stringify(res) | ||
this.log('Saving Lights') | ||
this.log(data) | ||
this.log('Getting Bulbs from Cache'); | ||
this.storage.setItem(cacheKey, data) | ||
.then(() => { | ||
this.log('Lights Saved.') | ||
}) | ||
} | ||
} | ||
return this.storage.getItem(cacheKey).then((data) => { | ||
disableDiscovery() { | ||
this.shouldDiscover = false | ||
} | ||
var devices = {}; | ||
startDiscovery() { | ||
if (!this.hasDiscoveryStarted && this.shouldDiscover) { | ||
this.hasDiscoveryStarted = true | ||
this.getDevices() | ||
} | ||
} | ||
if(data) | ||
{ | ||
try | ||
{ | ||
devices = JSON.parse(data); | ||
} | ||
catch(error) | ||
{ | ||
devices = {}; | ||
} | ||
} | ||
setLogger(logger) { | ||
this.logger = logger | ||
} | ||
this.log(' ** Fetched Lights from Cache **'); | ||
this.log(devices); | ||
return devices; | ||
}); | ||
} | ||
setVerbose() { | ||
this.isVerbose = true | ||
} | ||
saveAddress(res) | ||
{ | ||
if(this.storage) | ||
{ | ||
const data = JSON.stringify(res); | ||
setPersistPath(persistPath) { | ||
if (!this.storage) { | ||
this.storage = require('node-persist') | ||
const self = this | ||
this.storage.init({ dir: path.join(persistPath, 'syntex-magichome'), forgiveParseErrors: true, ttl: false, logging: false }) | ||
.then(() => { | ||
return self.getCachedAddress() | ||
}) | ||
.then((devices) => { | ||
self.cachedAddress = devices | ||
}) | ||
} | ||
} | ||
this.log('Saving Lights'); | ||
this.log(data); | ||
log(message) { | ||
if (this.logger && this.isVerbose) { | ||
this.logger(message) | ||
} | ||
} | ||
this.storage.setItem(cacheKey, data).then(() => { | ||
parseDevices(res) { | ||
if (!res) { | ||
return this.cachedAddress | ||
} | ||
if (res.length > 0) { | ||
const lines = res.split('\n') | ||
if (lines.length < 3) { | ||
return this.cachedAddress | ||
} | ||
// Format Response | ||
var devices = {} | ||
lines.splice(0, 1) | ||
lines.splice(-1, 1) | ||
lines.forEach((element) => { | ||
const mappedAddr = element.split('=') | ||
devices[mappedAddr[0]] = mappedAddr[1] | ||
devices[mappedAddr[1]] = mappedAddr[1] | ||
}) | ||
var newDevices = this.cachedAddress | ||
Object.keys(devices).forEach((element) => { | ||
newDevices[element] = devices[element] | ||
}) | ||
// Cache IPS | ||
this.saveAddress(newDevices) | ||
return newDevices | ||
} | ||
return this.cachedAddress | ||
} | ||
this.log('Lights Saved.'); | ||
}); | ||
} | ||
} | ||
getCachedDevice(addr) { | ||
var address = '' | ||
if (this.cachedAddress[addr] && this.shouldDiscover) { | ||
address = this.cachedAddress[addr] | ||
} else { | ||
address = addr | ||
} | ||
return address + ' ' | ||
} | ||
disableDiscovery() | ||
{ | ||
this.shouldDiscover = false; | ||
} | ||
getDevices() { | ||
const self = this | ||
const cmd = path.join(__dirname, '../flux_led.py') | ||
self.log('Discovering Devices') | ||
this.proc = spawn(cmd, ['-s']) | ||
this.proc.stdout.on('data', (data) => { | ||
const newData = '' + data | ||
self.log(newData) | ||
self.cachedAddress = self.parseDevices(newData) | ||
}) | ||
startDiscovery() | ||
{ | ||
if(!this.hasDiscoveryStarted && this.shouldDiscover) | ||
{ | ||
this.hasDiscoveryStarted = true; | ||
this.getDevices(); | ||
} | ||
} | ||
this.proc.stderr.on('data', (data) => { | ||
self.log('Error : ' + data) | ||
}) | ||
setLogger(logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
this.proc.on('close', () => { | ||
self.log('Discovery Finished'); | ||
self.rediscoverLights() | ||
}) | ||
} | ||
setVerbose() | ||
{ | ||
this.isVerbose = true; | ||
} | ||
rediscoverLights() { | ||
this.proc = null | ||
this.log(this.cachedAddress) | ||
setTimeout(this.getDevices.bind(this), this.pollingInterval) | ||
} | ||
setPersistPath(persistPath) | ||
{ | ||
if(!this.storage) | ||
{ | ||
this.storage = require('node-persist'); | ||
getAddress(address) { | ||
var ips = '' | ||
if (typeof address === 'string') { | ||
ips = this.getCachedDevice(address) | ||
} else if (address.length > 0) { | ||
address.forEach((addr) => { | ||
ips += this.getCachedDevice(addr) | ||
}) | ||
} | ||
return ips | ||
} | ||
const self = this; | ||
this.storage.init({ dir: path.join(persistPath, 'syntex-magichome'), forgiveParseErrors: true, ttl: false, logging: false }).then(() => { | ||
return self.getCachedAddress(); | ||
}).then((devices) => { | ||
self.cachedAddress = devices; | ||
}); | ||
} | ||
} | ||
log(message) | ||
{ | ||
if(this.logger && this.isVerbose) | ||
{ | ||
this.logger(message); | ||
} | ||
} | ||
parseDevices(res) | ||
{ | ||
if(!res) | ||
{ | ||
return this.cachedAddress; | ||
} | ||
if(res.length > 0) | ||
{ | ||
const lines = res.split('\n'); | ||
if(lines.length < 3) | ||
{ | ||
return this.cachedAddress; | ||
} | ||
// Format Response | ||
var devices = {}; | ||
lines.splice(0, 1); | ||
lines.splice(-1, 1); | ||
lines.forEach((element) => { | ||
const mappedAddr = element.split('='); | ||
devices[mappedAddr[0]] = mappedAddr[1]; | ||
devices[mappedAddr[1]] = mappedAddr[1]; | ||
}); | ||
var newDevices = this.cachedAddress; | ||
Object.keys(devices).forEach((element) => { | ||
newDevices[element] = devices[element]; | ||
}); | ||
// Cache IPS | ||
this.saveAddress(newDevices); | ||
return newDevices; | ||
} | ||
return this.cachedAddress; | ||
} | ||
getCachedDevice(addr) | ||
{ | ||
var address = ''; | ||
if(this.cachedAddress[addr] && this.shouldDiscover) | ||
{ | ||
address = this.cachedAddress[addr]; | ||
} | ||
else | ||
{ | ||
address = addr; | ||
} | ||
return address + ' '; | ||
} | ||
getDevices() | ||
{ | ||
const self = this; | ||
const cmd = path.join(__dirname, '../flux_led.py'); | ||
self.log('Discovering Devices'); | ||
this.proc = spawn(cmd, ['-s']); | ||
this.proc.stdout.on('data', (data) => { | ||
const newData = '' + data; | ||
self.log(newData); | ||
self.cachedAddress = self.parseDevices(newData); | ||
}); | ||
this.proc.stderr.on('data', (data) => { | ||
self.log('Error : ' + data) | ||
}); | ||
this.proc.on('close', () => { | ||
self.log('Discovery Finished'); | ||
self.rediscoverLights(); | ||
}); | ||
} | ||
rediscoverLights() | ||
{ | ||
this.proc = null; | ||
this.log(this.cachedAddress); | ||
setTimeout(this.getDevices.bind(this), this.pollingInterval); | ||
} | ||
getAddress(address) | ||
{ | ||
var ips = ''; | ||
if(typeof address === 'string') | ||
{ | ||
ips = this.getCachedDevice(address); | ||
} | ||
else if (address.length > 0) | ||
{ | ||
address.forEach((addr) => { | ||
ips += this.getCachedDevice(addr); | ||
}); | ||
} | ||
return ips; | ||
} | ||
} | ||
const agent = new LightAgent() | ||
const agent = new LightAgent(); | ||
module.exports = agent | ||
module.exports = agent; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
738
97251
3