-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (71 loc) · 1.97 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
package main
import (
"context"
"encoding/hex"
"flag"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"thermostat/api"
"thermostat/config"
"thermostat/sensor"
"thermostat/system"
)
func main() {
config.Ready()
ctx := context.Background()
var test bool
var stop bool
flag.BoolVar(&test, "post", false, "Perform a power on self test of systems and sensors")
flag.BoolVar(&stop, "stop", false, "Turn off all relays")
flag.Parse()
if test {
selfTest()
return
}
if stop {
turnOff()
return
}
sys := system.NewHVAC(viper.GetInt("fanPin"), viper.GetInt("acPin"), viper.GetInt("heatPin"))
addr, err := hex.DecodeString(viper.GetString("tempSensor")[2:])
if err != nil {
panic(err)
}
sens := sensor.NewHIH6020(uint16(addr[0]), viper.GetFloat64("tempCorrection"), viper.GetFloat64("temperatureRangeDivider"), viper.GetFloat64("humCorrection"))
zone, err := system.NewZone(ctx, "default", sys, sens)
if err != nil {
panic(err)
}
zone.Startup()
cert, key := loadApiCert()
api.StartApi(cert, key)
}
func loadApiCert() (cert, key []byte) {
cert = []byte(viper.GetString("apiCert"))
key = []byte(viper.GetString("apiKey"))
return
}
func selfTest() {
logrus.SetOutput(os.Stderr)
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.Info("running Power On Self Test...")
addr, err := hex.DecodeString(viper.GetString("tempSensor")[2:])
if err != nil {
panic(err)
}
sens := sensor.NewHIH6020(uint16(addr[0]), 0, 1, 0)
logrus.WithField("temp", sens.Temperature()).Info("current temp")
h := system.NewHVAC(viper.GetInt("fanPin"), viper.GetInt("acPin"), viper.GetInt("heatPin"))
h.Test()
logrus.WithField("temp", sens.Temperature()).Info("current temp")
}
func turnOff() {
logrus.SetOutput(os.Stderr)
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.Info("Powering off...")
h := system.NewHVAC(viper.GetInt("fanPin"), viper.GetInt("acPin"), viper.GetInt("heatPin"))
h.Reset()
}