Compare commits
4 Commits
7e61941789
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 960e2dc180 | |||
| 6ae4854180 | |||
| a8460bbfd7 | |||
| ac21d0dc46 |
+4
-1
@@ -1,3 +1,6 @@
|
||||
config.json
|
||||
devices.json
|
||||
node_modules/
|
||||
node_modules/
|
||||
run.sh
|
||||
watch.sh
|
||||
dec0ntamination.service
|
||||
|
||||
@@ -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,
|
||||
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
npm install
|
||||
|
||||
cat <<EOF> run.sh
|
||||
#!/bin/bash
|
||||
cd `pwd`
|
||||
`which node` run.js
|
||||
EOF
|
||||
|
||||
cat <<EOF> watch.sh
|
||||
#!/bin/bash
|
||||
cd `pwd`
|
||||
`which node` watcher.js
|
||||
EOF
|
||||
|
||||
chmod +x run.sh watch.sh
|
||||
|
||||
cat <<EOF> dec0ntamination.service
|
||||
[Unit]
|
||||
Description=Deconz Logger to Influx and Door Chime
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=`pwd`/watch.sh
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
User=1000
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
sudo cp dec0ntamination.service /etc/systemd/system/dec0ntamination.service
|
||||
|
||||
sudo systemctl daemon-reload
|
||||
|
||||
sudo systemctl enable --now dec0ntamination.service
|
||||
+31
-6
@@ -3,17 +3,31 @@ 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}`,
|
||||
axios.post(`${config.servers.influx.url}/api/v2/write?orgID=${config.servers.influx.orgId}&bucket=${config.servers.influx.bucket}`,
|
||||
influxPayload,
|
||||
{
|
||||
headers: {
|
||||
@@ -25,18 +39,29 @@ ws.on('message', msg => {
|
||||
console.log(response.data)
|
||||
}).catch(error => {
|
||||
console.error(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 {
|
||||
console.log(exec.execSync(device.openScript).toString())
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.log("ERROR")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user