Allow passing event value into openScript
This commit is contained in:
22
watcher.js
22
watcher.js
@@ -24,10 +24,10 @@ ws.on('message', msg => {
|
||||
if (data.state) {
|
||||
let device = config.wsDevices.find(device => device.uniqueId === data.uniqueid)
|
||||
if (device) {
|
||||
let payloadValue = (device.valueKey=='open')?(data.state['open']?0:1):data.state[device.valueKey]
|
||||
let payloadValue = (device.valueKey == 'open') ? (data.state['open'] ? 0 : 1) : data.state[device.valueKey]
|
||||
let influxPayload = `${device.name} ${device.influxKey}=${payloadValue}`
|
||||
console.log(`${new Date().toString()} 👉🏻 Sending ${influxPayload}`)
|
||||
axios.post(`${config.servers.influx.url}/api/v2/write?orgID=${config.servers.influx.orgId}&bucket=${config.servers.influx.bucket}`,
|
||||
axios.post(`${config.servers.influx.url}/api/v2/write?orgID=${config.servers.influx.orgId}&bucket=${config.servers.influx.bucket}`,
|
||||
influxPayload,
|
||||
{
|
||||
headers: {
|
||||
@@ -39,21 +39,29 @@ ws.on('message', msg => {
|
||||
console.log(response.data)
|
||||
}).catch(error => {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
console.log("ERROR")
|
||||
})
|
||||
if (device.openScript && data.state[device.valueKey]){
|
||||
if (device.openScript && data.state[device.valueKey]) {
|
||||
console.log("Running open script")
|
||||
let scriptToRun = device.openScript.replace("%VALUE%", payloadValue)
|
||||
try {
|
||||
exec.exec(device.openScript)
|
||||
|
||||
exec.exec(scriptToRun, (error, stdout, stderr) => {
|
||||
if (error || stderr) {
|
||||
console.error(`Error executing script: ${error ? error.message : stderr}`);
|
||||
return;
|
||||
}
|
||||
console.log(`Script output: ${stdout}`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
console.log("ERROR")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
console.log("ERROR")
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user