homebridge-plugin-command-dev
Advanced tools
Comparing version 1.0.22 to 1.0.23
@@ -33,2 +33,7 @@ { | ||
}, | ||
"poll_check": { | ||
"title": "(Optional) Interval to check status for changes automatically", | ||
"type": "string", | ||
"description": "(Optional) Interval in seconds or 5d2h3m10s format to check for status and update. Make sure that the command completes within this time, else it will not be considered. Needs to have the status command set." | ||
}, | ||
"invert_status": { | ||
@@ -35,0 +40,0 @@ "title": "Invert check status exit code", |
56
index.js
@@ -7,2 +7,18 @@ const { execSync } = require('child_process'); | ||
function durationSeconds(timeExpr) { | ||
if (!isNaN(timeExpr)) { | ||
return parseInt(timeExpr, 10); | ||
} | ||
var units = { 'd': 86400, 'h': 3600, 'm': 60, 's': 1 }; | ||
var regex = /(\d+)([dhms])/g; | ||
let seconds = 0; | ||
var match; | ||
while ((match = regex.exec(timeExpr))) { | ||
seconds += parseInt(match[1], 10) * units[match[2]]; | ||
} | ||
return seconds; | ||
} | ||
class CommandAccessoryPlugin { | ||
@@ -15,4 +31,2 @@ constructor(log, config, api) { | ||
this.log.debug(`Command Accessory Plugin: ${this.config.name} Loaded`); | ||
// your accessory must have an AccessoryInformation service | ||
@@ -31,2 +45,23 @@ this.informationService = new this.api.hap.Service.AccessoryInformation() | ||
.onSet(this.setState.bind(this)); // bind to setStateHandler method below | ||
if (this.config.check_status && this.config.poll_check) { | ||
let secs = durationSeconds(this.config.poll_check); | ||
if (isNaN(secs) || secs < 1) { | ||
this.log.error("Too frequent or incorrect poll check time, polling disabled."); | ||
return; | ||
} | ||
this.log.info(`Setting poll interval to ${secs}s`); | ||
this.interval = setInterval(async () => { | ||
this.log.debug("Polling status"); | ||
let oldState = this.currentState; | ||
if (await this.getState(secs * 1000) != oldState) { | ||
this.log.debug("Updating state") | ||
this.switchService.getCharacteristic(this.api.hap.Characteristic.On) | ||
.updateValue(this.currentState); | ||
} | ||
this.log.debug(`Polling done`); | ||
}, secs * 1000); | ||
} | ||
this.log.info(`Command Accessory Plugin Loaded`); | ||
} | ||
@@ -41,4 +76,4 @@ | ||
async getState() { | ||
this.log.info(`Getting ${this.config.name} switch state`); | ||
async getState(timeout = undefined) { | ||
this.log.debug(`Getting switch state`); | ||
@@ -52,16 +87,15 @@ if (!this.config.check_status) { | ||
let state = null; | ||
try { | ||
execSync(this.config.check_status); | ||
state = !this.config.invert_status; | ||
execSync(this.config.check_status, { timeout: timeout }); | ||
this.currentState = !this.config.invert_status; | ||
} catch (error) { | ||
state = false; | ||
this.currentState = false; | ||
} | ||
this.log.debug(`Returning: ${state}`); | ||
return state; | ||
this.log.debug(`Returning: ${this.currentState}`); | ||
return this.currentState; | ||
} | ||
async setState(value) { | ||
this.log.info(`Setting ${this.config.name} switch state to: `, value); | ||
this.log.debug(`Setting switch state to: `, value); | ||
@@ -68,0 +102,0 @@ let cmd = value ? this.config.turn_on : this.config.turn_off; |
{ | ||
"name": "homebridge-plugin-command-dev", | ||
"version": "1.0.22", | ||
"version": "1.0.23", | ||
"description": "This Homebridge plugin enables you to create virtual switches that can run any custom command when it is turned on or off.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,3 +10,3 @@ # homebridge-plugin-command | ||
By default, `check_status` assumes turned on if the exit code of the command is 0. Set `invert_status` to true to flip it. | ||
`check_status` command is optional, and if not provided, it just alternates as you turn it on and off. | ||
`check_status` command is optional, and if not provided, it just alternates as you turn it on and off. You can enable polling the check status and automatically pushing the change by setting the `poll_check` to number of seconds or in this format `1d2h3m4s`. | ||
@@ -22,4 +22,5 @@ If you like a JSON config, see the sample below. | ||
"check_status": "ls /tmp/testFile", | ||
"poll_check": "5", | ||
"invert_status": false | ||
} | ||
``` |
AI-detected potential malware
Supply chain riskAI has identified this package as malware. This is a strong signal that the package may be malicious.
Found 1 instance in 1 package
19017
150
25
1