-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
149 lines (131 loc) · 3.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"encoding/json"
"github.com/tess1o/go-tapo/api/types"
"github.com/tess1o/go-tapo/pkg/tapogo"
"io"
"log/slog"
"os"
"strings"
"time"
)
const (
tapoEmail = "TAPO_EMAIL"
tapoPassword = "TAPO_PASSWORD"
tapoConfigLocation = "TAPO_CONFIG_LOCATION"
prometheusPort = "8086"
metricPrefix = "tapo"
fetchIntervalSeconds = 15
maxRetries = 5
delayBetweenRetries = 2
)
func main() {
slog.SetLogLoggerLevel(slog.LevelInfo)
username := os.Getenv(tapoEmail)
password := os.Getenv(tapoPassword)
configLocation := os.Getenv(tapoConfigLocation)
if username == "" || password == "" || configLocation == "" {
slog.Error("TAPO_USERNAME, TAPO_PASSWORD and TAPO_CONFIG_LOCATION must be set")
return
}
devices, err := getDevices(configLocation)
if err != nil {
slog.Error("Error getting devices", "error", err)
return
}
if len(devices) == 0 {
slog.Error("No devices found")
return
}
initTapiClients(devices, username, password)
config := PrometheusConfig{
ServerPort: prometheusPort,
Prefix: metricPrefix,
Devices: devices,
}
exporter := NewPrometheusExporter(&config)
ticker := time.NewTicker(fetchIntervalSeconds * time.Second)
for _ = time.Now(); ; _ = <-ticker.C {
for _, device := range devices {
go handleDevice(device, username, password, exporter)
}
}
}
func getDevices(configLocation string) ([]Device, error) {
configFile, err := os.Open(configLocation)
if err != nil {
slog.Error("Error opening config file: ", err)
return nil, err
}
defer configFile.Close()
configData, err := io.ReadAll(configFile)
if err != nil {
slog.Error("Error reading config file: ", err)
return nil, err
}
devices, err := ReadDevices(configData)
if err != nil {
slog.Error("Error reading devices: ", err)
return nil, err
}
slog.Info("Devices from config", "devices", devices)
return devices, nil
}
func initTapiClients(devices []Device, username string, password string) {
for i := range devices {
client, err := initClient(devices[i].IPAddress, username, password)
if err != nil {
slog.Error("Failed to create Tapo client", "error", err, "device", devices[i].Name)
continue
}
devices[i].Client = client
}
}
func initClient(host, username, password string) (*tapogo.Tapo, error) {
return tapogo.NewTapo(host, username, password, &tapogo.TapoOptions{})
}
func handleDevice(device Device, username string, password string, exporter *PrometheusExporter) {
if device.Client == nil {
client, err := initClient(device.IPAddress, username, password)
if err != nil {
slog.Error("Failed to create Tapo client", "error", err, "device", device.Name)
return
}
device.Client = client
}
r, err := getEnergyUsage(device)
if err != nil {
slog.Error("Error getting energy usage", "device", device.Name, "error", err)
} else {
slog.Info("Successfully received metrics", "device", device.Name)
d, _ := json.Marshal(r.Result)
var params map[string]interface{}
json.Unmarshal(d, ¶ms)
exporter.Handle(context.Background(), device, params)
}
}
func getEnergyUsage(device Device) (*types.ResponseSpec, error) {
var energyUsageResponse *types.ResponseSpec
var energyError error
for i := 0; i < maxRetries; i++ {
r, err := device.Client.GetEnergyUsage()
if err == nil {
energyUsageResponse = r
break
} else {
energyError = err
slog.Error("Error getting energy usage", "attempt", i+1, "device", device.Name, "error", energyError)
if strings.Contains(energyError.Error(), "403") {
slog.Info("Received 403 error, trying to re-authenticate", "device", device.Name)
time.Sleep(time.Second * 2)
err = device.Client.Handshake()
if err != nil {
slog.Error("Failed to re-authenticate", "error", err, "device", device.Name)
}
}
time.Sleep(time.Second * delayBetweenRetries)
}
}
return energyUsageResponse, energyError
}