-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
62 lines (53 loc) · 1.5 KB
/
controller.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
package config
import (
"fmt"
"io/ioutil"
"piaqua/pkg/model"
"gopkg.in/yaml.v2"
)
// ControllerConf controller config
type ControllerConf struct{ model.System }
const controllerConfigFilename = "/controller.yml"
func (conf *ControllerConf) Read(dir string) error {
content, err := ioutil.ReadFile(dir + controllerConfigFilename)
if err != nil {
return err
}
return yaml.Unmarshal(content, conf)
}
func (conf *ControllerConf) Write(dir string) error {
content, err := yaml.Marshal(conf)
if err != nil {
return err
}
return ioutil.WriteFile(dir+controllerConfigFilename, content, 0644)
}
func (conf *ControllerConf) Init(hwConf *HardwareConf) {
conf.Sensors = make(model.Sensors, len(hwConf.Sensors))
conf.Relays = make(model.Relays, len(hwConf.Relays))
conf.Buttons = len(hwConf.Buttons)
}
func (conf *ControllerConf) CheckValid(hwConf *HardwareConf) error {
if slen := len(hwConf.Sensors); slen != len(conf.Sensors) {
return fmt.Errorf("Invalid number of sensors")
}
if rlen := len(hwConf.Relays); rlen != len(conf.Relays) {
return fmt.Errorf("Invalid number of relays")
}
if len(hwConf.Buttons) != conf.Buttons {
return fmt.Errorf("Invalid number of buttons")
}
for i := range conf.Relays {
for j, task := range conf.Relays[i].Tasks {
if !task.IsValid() {
return fmt.Errorf("Invalid relay %d task %d", i, j)
}
}
}
for i, action := range conf.Actions {
if !action.IsValid(len(hwConf.Relays), len(hwConf.Buttons)) {
return fmt.Errorf("Invalid action %d", i)
}
}
return nil
}