-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.go
65 lines (53 loc) · 1.71 KB
/
temperature.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
// temperature.go
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// checkTemperature checks the temperature and sets the TemperatureExceeded flag accordingly.
func (hm *HeatingManager) checkTemperature() {
temperature, err := hm.getTemperature()
if err != nil {
log.Printf("Failed to get temperature: %v", err)
return
}
if temperature > hm.Config.TemperatureThreshold {
fmt.Printf("Temperature has exceeded %.1f°C! Legionella heating will be rescheduled.\n", hm.Config.TemperatureThreshold)
hm.TemperatureExceeded = true
} else {
fmt.Printf("Temperature is OK. Actual temperature: %.1f°C\n", temperature)
hm.TemperatureExceeded = false
}
}
// getTemperature gets the temperature from the Solar Manager API.
func (hm *HeatingManager) getTemperature() (float64, error) {
if err := hm.getAuthToken(); err != nil {
return 0, err
}
url := fmt.Sprintf("%s/%s", hm.Config.SolarManagerURL, hm.Config.SolarManagerSensorID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, fmt.Errorf("error creating request: %v", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", hm.Token))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, fmt.Errorf("failed to get temperature: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("failed to get temperature: status code %d", resp.StatusCode)
}
var result struct {
Data struct {
CurrentWaterTemp float64 `json:"currentWaterTemp"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return 0, fmt.Errorf("failed to unmarshal temperature response: %v", err)
}
return result.Data.CurrentWaterTemp, nil
}