Skip to content

Commit

Permalink
some fixes and remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
drnoa committed Apr 4, 2024
1 parent 2177291 commit 98bf2be
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lastCheck.txt
__debug_bin*
Binary file removed __debug_bin3460939783
Binary file not shown.
7 changes: 5 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"shellyTempURL": "http://192.168.178.1/rpc/Temperature.GetStatus?id=100",
"shellyTempURL": "http://192.168.178.1/rpc/Temperature.GetStatus?id=102",
"shellyHeatingOnURL": "http://192.168.178.1/rpc/Switch.Set?id=0&on=true",
"temperatureThreshold": 55
"temperatureThreshold": 55,
"checkInterval": 5,
"weeklyCheckInterval": 168

}
27 changes: 13 additions & 14 deletions heating_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand All @@ -15,6 +15,9 @@ type Config struct {
ShellyURL string `json:"shellyTempURL"` // URL of the Shelly device temperature addon.
ShellyHeatingOnURL string `json:"shellyHeatingOnURL"` // URL to turn Shelly heating on.
TemperatureThreshold float64 `json:"temperatureThreshold"` // Temperature threshold in Celsius.
CheckInterval int `json:"checkInterval"` // Check interval in minutes.
WeeklyCheckInterval int `json:"weeklyCheckInterval"` // Weekly check interval in hours.

}

// HeatingManager is the main application struct.
Expand All @@ -40,7 +43,7 @@ func NewHeatingManager() (*HeatingManager, error) {

return &HeatingManager{
Config: config,
CheckInterval: 5 * time.Minute,
CheckInterval: time.Duration(config.CheckInterval) * time.Minute,
LastCheckFile: "lastCheck.txt",
}, nil
}
Expand All @@ -60,12 +63,9 @@ func (hm *HeatingManager) StartWeeklyCheck() {
weeklyCheckTimer := time.NewTimer(hm.nextWeeklyCheckDuration())
defer weeklyCheckTimer.Stop()

for {
select {
case <-weeklyCheckTimer.C:
hm.weeklyCheck(hm.Config.ShellyHeatingOnURL)
weeklyCheckTimer.Reset(hm.nextWeeklyCheckDuration())
}
for range weeklyCheckTimer.C {
hm.weeklyCheck(hm.Config.ShellyHeatingOnURL)
weeklyCheckTimer.Reset(hm.nextWeeklyCheckDuration())
}
}

Expand Down Expand Up @@ -95,11 +95,10 @@ func (hm *HeatingManager) checkTemperature(shellyURL string) {
}

if temperature > hm.Config.TemperatureThreshold {
fmt.Println("Temperature has exceeded %.1f°C! Legionellaheating will be resheduled.", hm.Config.TemperatureThreshold)
fmt.Printf("Temperature has exceeded %.1f°C! Legionella heating will be rescheduled.\n", hm.Config.TemperatureThreshold)
hm.TemperatureExceeded = true
} else {
fmt.Println("Temperature is in order.")
hm.TemperatureExceeded = false
fmt.Printf("Temperature is OK. Actual temperature: %.1f°C\n", temperature)
}
}

Expand All @@ -115,7 +114,7 @@ func getTemperature(shellyTempURL string) (float64, error) {
return 0, fmt.Errorf("failed to get temperature: status code %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("failed to read response body: %v", err)
}
Expand Down Expand Up @@ -170,11 +169,11 @@ func (hm *HeatingManager) nextWeeklyCheckDuration() time.Duration {
if err != nil {
return 0
}
nextCheck := lastCheck.Add(7 * 24 * time.Hour)
nextCheck := lastCheck.Add(time.Duration(hm.Config.WeeklyCheckInterval) * time.Hour)
if time.Now().After(nextCheck) {
return 0
}
return nextCheck.Sub(time.Now())
return time.Until(nextCheck)
}

// readLastCheckTime reads the last check time from a file.
Expand Down

0 comments on commit 98bf2be

Please sign in to comment.