dec0ntamination/watcher.js

42 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-12-18 04:24:19 +00:00
import axios from 'axios'
import WebSocket from 'ws'
import fs from 'fs'
2024-12-18 04:36:02 +00:00
import exec from 'child_process'
2024-12-18 04:24:19 +00:00
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
let ws = new WebSocket(`ws://${config.servers.deconz.url}:${config.servers.deconz.websocketPort}`)
ws.on('message', msg => {
let data = JSON.parse(msg)
try {
if (data.state) {
let device = config.wsDevices.find(device => device.uniqueId === data.uniqueid)
if (device) {
let influxPayload = `${device.name} ${device.influxKey}=${data.state[device.valueKey]?0:1}`
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}`,
influxPayload,
{
headers: {
Authorization: `Token ${config.servers.influx.apiKey}`,
'Content-Type': 'text/plain'
}
}
).then(response => {
console.log(response.data)
}).catch(error => {
console.error(error)
})
2024-12-18 04:41:16 +00:00
if (device.openScript && data.state[device.valueKey]){
2024-12-18 04:36:02 +00:00
console.log("Running open script")
try {
console.log(exec.execSync(device.openScript).toString())
} catch (error) {
console.error(error)
}
}
2024-12-18 04:24:19 +00:00
}
}
} catch (error) {
console.error(error)
}
})