1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,81 @@
package hc3
import (
"encoding/json"
"fmt"
"strconv"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
)
// Parse parses data from sections, rooms and devices, and adds measurements containing parsed data.
func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, devicesBytes []byte) error {
var tmpSections []Sections
if err := json.Unmarshal(sectionBytes, &tmpSections); err != nil {
return err
}
sections := make(map[uint16]string, len(tmpSections))
for _, v := range tmpSections {
sections[v.ID] = v.Name
}
var tmpRooms []Rooms
if err := json.Unmarshal(roomBytes, &tmpRooms); err != nil {
return err
}
rooms := make(map[uint16]linkRoomsSections, len(tmpRooms))
for _, v := range tmpRooms {
rooms[v.ID] = linkRoomsSections{Name: v.Name, SectionID: v.SectionID}
}
var devices []Devices
if err := json.Unmarshal(devicesBytes, &devices); err != nil {
return err
}
for _, device := range devices {
// skip device in some cases
if device.RoomID == 0 ||
!device.Enabled ||
device.Properties.Dead ||
device.Type == "com.fibaro.zwaveDevice" {
continue
}
tags := map[string]string{
"deviceId": strconv.FormatUint(uint64(device.ID), 10),
"section": sections[rooms[device.RoomID].SectionID],
"room": rooms[device.RoomID].Name,
"name": device.Name,
"type": device.Type,
}
fields := make(map[string]interface{})
if device.Properties.BatteryLevel != nil {
fields["batteryLevel"] = *device.Properties.BatteryLevel
}
if device.Properties.Energy != nil {
fields["energy"] = *device.Properties.Energy
}
if device.Properties.Power != nil {
fields["power"] = *device.Properties.Power
}
// Value can be a JSON bool, string, or numeric value
if device.Properties.Value != nil {
v, err := internal.ToFloat64(device.Properties.Value)
if err != nil {
acc.AddError(fmt.Errorf("unable to convert value: %w", err))
} else {
fields["value"] = v
}
}
acc.AddFields("fibaro", fields, tags)
}
return nil
}

View file

@ -0,0 +1,37 @@
package hc3
// LinkRoomsSections links rooms to sections
type linkRoomsSections struct {
Name string
SectionID uint16
}
// Sections contains sections information
type Sections struct {
ID uint16 `json:"id"`
Name string `json:"name"`
}
// Rooms contains rooms information
type Rooms struct {
ID uint16 `json:"id"`
Name string `json:"name"`
SectionID uint16 `json:"sectionID"`
}
// Devices contains devices information
type Devices struct {
ID uint16 `json:"id"`
Name string `json:"name"`
RoomID uint16 `json:"roomID"`
Type string `json:"type"`
Enabled bool `json:"enabled"`
Properties struct {
BatteryLevel *float64 `json:"batteryLevel"`
Dead bool `json:"dead"`
Energy *float64 `json:"energy"`
Power *float64 `json:"power"`
Value interface{} `json:"value"`
Value2 *string `json:"value2"`
} `json:"properties"`
}