hue_exporter/hue/hue.go

177 lines
5.4 KiB
Go
Raw Normal View History

2020-12-22 18:24:23 +01:00
package hue
import (
"fmt"
"log"
"github.com/aexel90/hue_exporter/metric"
2020-12-28 08:49:18 +01:00
hue "github.com/shamx9ir/gohue" // github.com/collinux/gohue
2020-12-22 18:24:23 +01:00
)
// Exporter data
type Exporter struct {
BaseURL string
Username string
}
const (
2020-12-27 14:58:21 +01:00
TypeLight = "light"
2020-12-28 08:49:18 +01:00
TypeSensor = "sensor"
2020-12-27 14:58:21 +01:00
LabelName = "Name"
LabelType = "Type"
LabelModelID = "Model_ID"
LabelManufacturerName = "Manufacturer_Name"
LabelSWVersion = "SW_Version"
LabelUniqueID = "Unique_ID"
LabelStateOn = "State_On"
LabelStateAlert = "State_Alert"
LabelStateBri = "State_Bri"
LabelStateCT = "State_CT"
LabelStateReachable = "State_Reachable"
LabelStateSaturation = "State_Saturation"
LabelStateButtonEvent = "State_Button_Event"
LabelStateDaylight = "State_Daylight"
LabelStateLastUpdated = "State_Last_Updated"
LabelStateLastUpdatedTime = "State_Last_Updated_Time"
2020-12-28 08:49:18 +01:00
LabelStateTemperature = "State_Temperature"
LabelConfigBattery = "Config_Battery"
2020-12-27 14:58:21 +01:00
LabelConfigOn = "Config_On"
LabelConfigReachable = "Config_Reachable"
2020-12-22 18:24:23 +01:00
)
// InitMetrics func
func (exporter *Exporter) InitMetrics() (metrics []*metric.Metric) {
metrics = append(metrics, &metric.Metric{
2020-12-28 08:49:18 +01:00
HueType: TypeLight,
FqName: "hue_light_info",
Help: "Non-numeric data, value is always 1",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateOn, LabelStateAlert, LabelStateBri, LabelStateCT, LabelStateReachable, LabelStateSaturation},
})
2020-12-27 14:58:21 +01:00
metrics = append(metrics, &metric.Metric{
2020-12-28 08:49:18 +01:00
HueType: TypeSensor,
FqName: "hue_sensor_info",
Help: "Non-numeric data, value is always 1",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID, LabelStateButtonEvent, LabelStateDaylight, LabelStateLastUpdated, LabelStateLastUpdatedTime, LabelStateTemperature, LabelConfigBattery, LabelConfigOn, LabelConfigReachable},
})
metrics = append(metrics, &metric.Metric{
HueType: TypeSensor,
FqName: "hue_sensor_battery",
Help: "battery level percentage",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID},
ResultKey: LabelConfigBattery,
})
metrics = append(metrics, &metric.Metric{
HueType: TypeSensor,
FqName: "hue_sensor_temperature",
Help: "temperature level celsius degree",
Labels: []string{LabelName, LabelType, LabelModelID, LabelManufacturerName, LabelSWVersion, LabelUniqueID},
ResultKey: LabelStateTemperature,
})
2020-12-22 18:24:23 +01:00
return metrics
}
// Collect metrics
func (exporter *Exporter) Collect(metrics []*metric.Metric) (err error) {
bridge := newBridge(exporter.BaseURL)
err = bridge.Login(exporter.Username)
if err != nil {
2020-12-22 20:09:25 +01:00
return fmt.Errorf("[error Login()] '%v'", err)
2020-12-22 18:24:23 +01:00
}
2020-12-28 08:49:18 +01:00
sensorData, err := collectSensors(bridge)
if err != nil {
return err
}
2020-12-22 18:24:23 +01:00
2020-12-28 08:49:18 +01:00
lightData, err := collectLights(bridge)
if err != nil {
return err
}
2020-12-22 18:24:23 +01:00
2020-12-28 08:49:18 +01:00
for _, metric := range metrics {
2020-12-22 18:24:23 +01:00
switch metric.HueType {
case TypeLight:
2020-12-28 08:49:18 +01:00
metric.MetricResult = lightData
case TypeSensor:
metric.MetricResult = sensorData
2020-12-22 18:24:23 +01:00
}
}
return nil
}
2020-12-28 08:49:18 +01:00
func collectSensors(bridge *hue.Bridge) (sensorData []map[string]interface{}, err error) {
2020-12-27 14:58:21 +01:00
sensors, err := bridge.GetAllSensors()
if err != nil {
2020-12-28 08:49:18 +01:00
return nil, fmt.Errorf("[error GetAllSensors()] '%v'", err)
2020-12-27 14:58:21 +01:00
}
for _, sensor := range sensors {
result := make(map[string]interface{})
2020-12-28 08:49:18 +01:00
result[LabelName] = sensor.Name
result[LabelType] = sensor.Type
result[LabelModelID] = sensor.ModelID
result[LabelManufacturerName] = sensor.ManufacturerName
result[LabelSWVersion] = sensor.SWVersion
result[LabelUniqueID] = sensor.UniqueID
result[LabelStateButtonEvent] = float64(sensor.State.ButtonEvent)
result[LabelStateDaylight] = sensor.State.Daylight
result[LabelStateLastUpdated] = sensor.State.LastUpdated.Unix()
result[LabelStateLastUpdatedTime] = sensor.State.LastUpdated.Time.Unix()
result[LabelConfigBattery] = sensor.Config.Battery
result[LabelConfigOn] = sensor.Config.On
result[LabelConfigReachable] = sensor.Config.Reachable
if sensor.Type == "ZLLTemperature" {
result[LabelStateTemperature] = float64(sensor.State.Temperature)
2020-12-27 14:58:21 +01:00
}
2020-12-28 08:49:18 +01:00
sensorData = append(sensorData, result)
2020-12-27 14:58:21 +01:00
}
2020-12-28 08:49:18 +01:00
return sensorData, nil
2020-12-27 14:58:21 +01:00
}
2020-12-28 08:49:18 +01:00
func collectLights(bridge *hue.Bridge) (lightData []map[string]interface{}, err error) {
2020-12-22 18:24:23 +01:00
lights, err := bridge.GetAllLights()
if err != nil {
2020-12-28 08:49:18 +01:00
return nil, fmt.Errorf("[error GetAllLights()] '%v'", err)
2020-12-22 18:24:23 +01:00
}
for _, light := range lights {
result := make(map[string]interface{})
2020-12-28 08:49:18 +01:00
result[LabelName] = light.Name
result[LabelType] = light.Type
result[LabelModelID] = light.ModelID
result[LabelManufacturerName] = light.ManufacturerName
result[LabelSWVersion] = light.SWVersion
result[LabelUniqueID] = light.UniqueID
result[LabelStateOn] = light.State.On
result[LabelStateAlert] = light.State.Alert
result[LabelStateBri] = light.State.Bri
result[LabelStateCT] = light.State.CT
result[LabelStateReachable] = light.State.Reachable
result[LabelStateSaturation] = light.State.Saturation
lightData = append(lightData, result)
2020-12-22 18:24:23 +01:00
}
2020-12-28 08:49:18 +01:00
return lightData, nil
2020-12-22 18:24:23 +01:00
}
func newBridge(ipAddr string) *hue.Bridge {
bridge, err := hue.NewBridge(ipAddr)
if err != nil {
2020-12-22 20:09:25 +01:00
log.Fatalf("Error connecting to Hue bridge with '%v': '%v'\n", ipAddr, err)
2020-12-22 18:24:23 +01:00
}
return bridge
}