-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrowenta_status.go
55 lines (47 loc) · 1.28 KB
/
rowenta_status.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
package main
import (
"time"
)
type StatusResponse struct {
Voltage int `json:"voltage"`
Mode string `json:"mode"`
BatteryLevel int `json:"battery_level"`
Charging string `json:"charging"`
StartupTime struct {
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Hour int `json:"hour"`
Min int `json:"min"`
Sec int `json:"sec"`
} `json:"startup_time"`
}
type Status struct {
BatteryPercentage float64
BatteryVoltageVolts float64
Charging float64
UptimeSeconds float64
}
func GetStatus() (*Status, error) {
result, err := ParseUrl[StatusResponse](endpoint + "/get/status")
if err != nil {
return nil, err
}
var charging float64
if result.Charging == "charging" {
charging = 1
} else {
charging = 0
}
startupTime := result.StartupTime
startupDate := time.Date(startupTime.Year, time.Month(startupTime.Month), startupTime.Day, startupTime.Hour, startupTime.Min, startupTime.Sec, 0, time.Local)
now := time.Now()
diff := now.Sub(startupDate)
returnValue := &Status{
BatteryPercentage: float64(result.BatteryLevel) / float64(100),
BatteryVoltageVolts: float64(result.Voltage) / float64(1000),
Charging: charging,
UptimeSeconds: diff.Seconds(),
}
return returnValue, nil
}