69 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import axios from 'axios'
 | 
						|
import fs from 'fs'
 | 
						|
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
 | 
						|
 | 
						|
// name influxKey=data[valueKey],influxKey=data[valueKey],...
 | 
						|
 | 
						|
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 = []
 | 
						|
    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)
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // 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)
 | 
						|
})
 |