diff --git a/README.md b/README.md index 0bad5d6..6d76e70 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # `Dec0ntamination` +Uses Node 20 + +Takes sensors from deconz and uploads the data to Influx as defined in config.json \ No newline at end of file diff --git a/example.config.json b/example.config.json index 2ac5916..8769d2d 100644 --- a/example.config.json +++ b/example.config.json @@ -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" + } ] } diff --git a/getDevices.js b/getDevices.js index e38c0e1..95d31d4 100644 --- a/getDevices.js +++ b/getDevices.js @@ -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)) diff --git a/getKey.js b/getKey.js index 65e4eb5..1408357 100644 --- a/getKey.js +++ b/getKey.js @@ -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 => { diff --git a/index.js b/index.js index 0bebcf8..e6f75f4 100644 --- a/index.js +++ b/index.js @@ -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 = [] diff --git a/package-lock.json b/package-lock.json index eeb0c9a..f24636d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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 + } + } } } } diff --git a/package.json b/package.json index 1697722..5eaa0f8 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "type": "module", "dependencies": { "axios": "^1.7.9", - "meow": "^13.2.0" + "meow": "^13.2.0", + "ws": "^8.18.0" } } diff --git a/watcher.js b/watcher.js new file mode 100644 index 0000000..a9bfb5b --- /dev/null +++ b/watcher.js @@ -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) + } +}) \ No newline at end of file