Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
96
plugins/inputs/fibaro/hc2/parser.go
Normal file
96
plugins/inputs/fibaro/hc2/parser.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package hc2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
)
|
||||
|
||||
// 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 == "true" ||
|
||||
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 {
|
||||
if fValue, err := strconv.ParseFloat(*device.Properties.BatteryLevel, 64); err == nil {
|
||||
fields["batteryLevel"] = fValue
|
||||
}
|
||||
}
|
||||
|
||||
if device.Properties.Energy != nil {
|
||||
if fValue, err := strconv.ParseFloat(*device.Properties.Energy, 64); err == nil {
|
||||
fields["energy"] = fValue
|
||||
}
|
||||
}
|
||||
|
||||
if device.Properties.Power != nil {
|
||||
if fValue, err := strconv.ParseFloat(*device.Properties.Power, 64); err == nil {
|
||||
fields["power"] = fValue
|
||||
}
|
||||
}
|
||||
|
||||
if device.Properties.Value != nil {
|
||||
value := device.Properties.Value
|
||||
switch value {
|
||||
case "true":
|
||||
value = "1"
|
||||
case "false":
|
||||
value = "0"
|
||||
}
|
||||
|
||||
if fValue, err := strconv.ParseFloat(value.(string), 64); err == nil {
|
||||
fields["value"] = fValue
|
||||
}
|
||||
}
|
||||
|
||||
if device.Properties.Value2 != nil {
|
||||
if fValue, err := strconv.ParseFloat(*device.Properties.Value2, 64); err == nil {
|
||||
fields["value2"] = fValue
|
||||
}
|
||||
}
|
||||
|
||||
acc.AddFields("fibaro", fields, tags)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
37
plugins/inputs/fibaro/hc2/types.go
Normal file
37
plugins/inputs/fibaro/hc2/types.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package hc2
|
||||
|
||||
// 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 *string `json:"batteryLevel"`
|
||||
Dead string `json:"dead"`
|
||||
Energy *string `json:"energy"`
|
||||
Power *string `json:"power"`
|
||||
Value interface{} `json:"value"`
|
||||
Value2 *string `json:"value2"`
|
||||
} `json:"properties"`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue