Websocket Monitor for door sensors

This commit is contained in:
Elizabeth Cray 2024-12-17 23:24:19 -05:00
parent a5725edb36
commit 7a71b0c216
8 changed files with 75 additions and 7 deletions

View File

@ -1,2 +1,5 @@
# `Dec0ntamination`
Uses Node 20
Takes sensors from deconz and uploads the data to Influx as defined in config.json

View File

@ -1,9 +1,10 @@
{
"servers": {
"deconz": {
"url": "http://zigbee.hacdcserver.org",
"url": "zigbee.hacdcserver.org",
"apiKey": "",
"clientName": "dec0ntaminator"
"clientName": "dec0ntaminator",
"websocketPort": 8082
},
"influx": {
"url": "http://influx.hacdc.org",
@ -20,5 +21,13 @@
"valueModifier": "1",
"influxKey": ""
}
],
"wsDevices": [
{
"name": "",
"uniqueId": "",
"valueKey": "open",
"influxKey": "state"
}
]
}

View File

@ -2,7 +2,7 @@ const axios = require('axios')
const fs = require('fs')
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
axios.get(config.servers.deconz.url + '/api/' + config.servers.deconz.apiKey + '/sensors')
axios.get(`http://${config.servers.deconz.url}:${config.servers.deconz.apiPort}/api/${config.servers.deconz.apiKey}/sensors`)
.then(response => {
// console.log(response.data)
fs.writeFileSync('devices.json', JSON.stringify(response.data, null, 2))

View File

@ -24,7 +24,7 @@ if (config.servers.deconz.apiKey.length > 0 && !cli.flags.force) {
process.exit(1)
}
axios.post(config.servers.deconz.url + '/api', {
axios.post(`http://${config.servers.deconz.url}:${config.servers.deconz.apiPort}/api`, {
devicetype: config.servers.deconz.clientName +'l',
})
.then(response => {

View File

@ -4,7 +4,7 @@ let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
// name influxKey=data[valueKey],influxKey=data[valueKey],...
axios.get(config.servers.deconz.url + '/api/' + config.servers.deconz.apiKey + '/sensors')
axios.get(`http://${config.servers.deconz.url}:${config.servers.deconz.apiPort}/api/${config.servers.deconz.apiKey}/sensors`)
.then(response => {
let sensors = response.data
let data = []

24
package-lock.json generated
View File

@ -10,7 +10,8 @@
"license": "MIT",
"dependencies": {
"axios": "^1.7.9",
"meow": "^13.2.0"
"meow": "^13.2.0",
"ws": "^8.18.0"
}
},
"node_modules/asynckit": {
@ -123,6 +124,27 @@
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
"license": "MIT"
},
"node_modules/ws": {
"version": "8.18.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
"integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}

View File

@ -15,6 +15,7 @@
"type": "module",
"dependencies": {
"axios": "^1.7.9",
"meow": "^13.2.0"
"meow": "^13.2.0",
"ws": "^8.18.0"
}
}

33
watcher.js Normal file
View File

@ -0,0 +1,33 @@
import axios from 'axios'
import WebSocket from 'ws'
import fs from 'fs'
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)
})
}
}
} catch (error) {
console.error(error)
}
})