dec0ntamination/index.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-12-18 02:15:50 +00:00
import axios from 'axios'
import fs from 'fs'
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
2024-12-17 02:22:13 +00:00
2024-12-18 02:15:50 +00:00
// name influxKey=data[valueKey],influxKey=data[valueKey],...
2024-12-17 02:22:13 +00:00
2024-12-18 04:24:19 +00:00
axios.get(`http://${config.servers.deconz.url}:${config.servers.deconz.apiPort}/api/${config.servers.deconz.apiKey}/sensors`)
2024-12-18 02:15:50 +00:00
.then(response => {
let sensors = response.data
let data = []
let influxPayload = ""
let devices = {}
// Filter only the data we find relevant
for (let sensor in sensors) { // sensor: "10"
try {
if (sensors[sensor].config.reachable) {
data.push({
uniqueId: sensors[sensor].uniqueid,
data: sensors[sensor].state
})
}
} catch (error) {
console.error(error)
}
}
2024-12-17 02:22:13 +00:00
2024-12-18 02:15:50 +00:00
// Assemble data from specified sensors
[...config.devices].forEach(device => {
try{
if (!devices[device.name]) {
devices[device.name] = []
}
let relevantData = data.filter(sensor => sensor.uniqueId.includes(device.uniqueId))
let value = relevantData[0].data[device.valueKey]/device.valueModifier
if (device.valueModifier != 1){
value = Math.round(relevantData[0].data[device.valueKey]/device.valueModifier).toFixed(2)
}
devices[device.name].push(`${device.influxKey}=${value}`)
} catch (error) {
console.error(error)
}
})
// Assemble influx payload
for (let device in devices) {
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,
{
headers: {
Authorization: `Token ${config.servers.influx.apiKey}`,
'Content-Type': 'text/plain'
}
}
).then(response => {
console.log(response.data)
})
.catch(error => {
console.log(error.response.data)
})
})
.catch(error => {
console.error(error)
})