Compare commits
2 Commits
7e61941789
...
a8460bbfd7
| Author | SHA1 | Date | |
|---|---|---|---|
| a8460bbfd7 | |||
| ac21d0dc46 |
@@ -46,7 +46,6 @@ axios.get(`http://${config.servers.deconz.url}:${config.servers.deconz.apiPort}/
|
||||
influxPayload += `${device} ${devices[device].join(",")}\n`
|
||||
}
|
||||
influxPayload = influxPayload.slice(0, -1)
|
||||
|
||||
// Send data to influx
|
||||
axios.post(`${config.servers.influx.url}/api/v2/write?orgID=${config.servers.influx.orgId}&bucket=${config.servers.influx.bucket}`,
|
||||
influxPayload,
|
||||
|
||||
+21
-4
@@ -3,15 +3,29 @@ import WebSocket from 'ws'
|
||||
import fs from 'fs'
|
||||
import exec from 'child_process'
|
||||
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
|
||||
let ws = new WebSocket(`ws://${config.servers.deconz.url}:${config.servers.deconz.websocketPort}`)
|
||||
let SOCKET_URL = `ws://${config.servers.deconz.url}:${config.servers.deconz.websocketPort}`
|
||||
console.log(SOCKET_URL)
|
||||
let ws = new WebSocket(SOCKET_URL)
|
||||
|
||||
ws.on('error', (error) => {
|
||||
console.error('WebSocket error:', error)
|
||||
console.log("ERROR")
|
||||
})
|
||||
ws.on('close', (code, reason) => {
|
||||
console.log(`Connection closed with code ${code} and reason: ${reason}`);
|
||||
// Clean up any resources here
|
||||
})
|
||||
|
||||
|
||||
ws.on('message', msg => {
|
||||
let data = JSON.parse(msg)
|
||||
//fs.appendFileSync('socket.log', msg+'\n')
|
||||
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}`
|
||||
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}`,
|
||||
influxPayload,
|
||||
@@ -25,18 +39,21 @@ ws.on('message', msg => {
|
||||
console.log(response.data)
|
||||
}).catch(error => {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
})
|
||||
if (device.openScript && data.state[device.valueKey]){
|
||||
console.log("Running open script")
|
||||
try {
|
||||
console.log(exec.execSync(device.openScript).toString())
|
||||
exec.exec(device.openScript)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user