Allow passing event value into openScript
This commit is contained in:
+14
-6
@@ -24,7 +24,7 @@ ws.on('message', msg => {
|
|||||||
if (data.state) {
|
if (data.state) {
|
||||||
let device = config.wsDevices.find(device => device.uniqueId === data.uniqueid)
|
let device = config.wsDevices.find(device => device.uniqueId === data.uniqueid)
|
||||||
if (device) {
|
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}`
|
let influxPayload = `${device.name} ${device.influxKey}=${payloadValue}`
|
||||||
console.log(`${new Date().toString()} 👉🏻 Sending ${influxPayload}`)
|
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}`,
|
||||||
@@ -39,21 +39,29 @@ ws.on('message', msg => {
|
|||||||
console.log(response.data)
|
console.log(response.data)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error(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")
|
console.log("Running open script")
|
||||||
|
let scriptToRun = device.openScript.replace("%VALUE%", payloadValue)
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
console.log("ERROR")
|
console.log("ERROR")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
console.log("ERROR")
|
console.log("ERROR")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user