Compare commits

..

4 Commits

Author SHA1 Message Date
Elizabeth Cray 960e2dc180 Add SystemD Setup tool 2026-03-11 18:43:49 -04:00
Elizabeth Cray 6ae4854180 Allow passing event value into openScript 2026-03-11 18:18:08 -04:00
Liz a8460bbfd7 Some config usage adjustments 2026-03-09 15:40:01 -04:00
Liz ac21d0dc46 Fix shell running 2025-10-25 10:50:37 -04:00
4 changed files with 73 additions and 8 deletions
+4 -1
View File
@@ -1,3 +1,6 @@
config.json
devices.json
node_modules/
node_modules/
run.sh
watch.sh
dec0ntamination.service
-1
View File
@@ -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,
+38
View File
@@ -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
View File
@@ -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")
}
})
})