-
Notifications
You must be signed in to change notification settings - Fork 0
/
heating_manager.go
107 lines (92 loc) · 2.75 KB
/
heating_manager.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
package main
import (
"fmt"
"log"
"os"
"time"
)
type HeatingManager struct {
Config Config
TemperatureExceeded bool
CheckInterval time.Duration
LastCheckFile string
Token string
TokenExpiry time.Time
}
// NewHeatingManager creates a new HeatingManager instance.
func NewHeatingManager() (*HeatingManager, error) {
config, err := loadConfig()
if err != nil {
return nil, err
}
return &HeatingManager{
Config: config,
CheckInterval: time.Duration(config.CheckInterval) * time.Minute,
LastCheckFile: "lastCheck.txt",
}, nil
}
// StartTemperatureMonitoring starts the temperature monitoring loop.
func (hm *HeatingManager) StartTemperatureMonitoring() {
ticker := time.NewTicker(hm.CheckInterval)
defer ticker.Stop()
for range ticker.C {
hm.checkTemperature()
}
}
// StartWeeklyCheck starts the weekly check loop.
func (hm *HeatingManager) StartWeeklyCheck() {
weeklyCheckTimer := time.NewTimer(hm.nextWeeklyCheckDuration())
defer weeklyCheckTimer.Stop()
for range weeklyCheckTimer.C {
hm.weeklyCheck()
weeklyCheckTimer.Reset(hm.nextWeeklyCheckDuration())
}
}
// weeklyCheck checks if the temperature threshold has been exceeded and turns on the heating if necessary.
func (hm *HeatingManager) weeklyCheck() {
if !hm.TemperatureExceeded {
if err := hm.turnHeatingOn(); err != nil {
log.Printf("Failed to turn on heating: %v", err)
}
// Schedule to turn off after a certain duration
time.AfterFunc(4*time.Hour, func() {
if err := hm.turnHeatingOff(); err != nil {
log.Printf("Failed to turn off heating: %v", err)
}
})
}
hm.TemperatureExceeded = false
hm.saveLastCheckTime()
}
// saveLastCheckTime saves the last check time to a file.
func (hm *HeatingManager) saveLastCheckTime() {
now := time.Now()
err := os.WriteFile(hm.LastCheckFile, []byte(now.Format(time.RFC3339)), 0644)
if err != nil {
log.Printf("Failed to save last check time: %v", err)
}
}
// nextWeeklyCheckDuration calculates the duration until the next weekly check.
func (hm *HeatingManager) nextWeeklyCheckDuration() time.Duration {
lastCheck, err := hm.readLastCheckTime()
if err != nil {
return 0
}
nextCheck := lastCheck.Add(time.Duration(hm.Config.WeeklyCheckInterval) * time.Hour)
if time.Now().After(nextCheck) {
return 0
}
return time.Until(nextCheck)
}
// readLastCheckTime reads the last check time from a file.
func (hm *HeatingManager) readLastCheckTime() (time.Time, error) {
data, err := os.ReadFile(hm.LastCheckFile)
if err != nil {
return time.Time{}, fmt.Errorf("failed to read last check time: %w", err)
}
lastCheck, err := time.Parse(time.RFC3339, string(data))
if err != nil {
return time.Time{}, fmt.Errorf("failed to parse last check time: %w", err)
}
return lastCheck, nil
}