/
zone_actions.go
45 lines (38 loc) · 1.36 KB
/
zone_actions.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
package worker
import (
"encoding/json"
"fmt"
"github.com/calvinmclean/automated-garden/garden-app/pkg"
"github.com/calvinmclean/automated-garden/garden-app/pkg/action"
)
// ExecuteZoneAction will execute a ZoneAction
func (w *Worker) ExecuteZoneAction(g *pkg.Garden, z *pkg.Zone, input *action.ZoneAction) error {
if input.Water != nil {
err := w.ExecuteWaterAction(g, z, input.Water)
if err != nil {
return fmt.Errorf("unable to execute WaterAction: %w", err)
}
}
return nil
}
// ExecuteWaterAction sends the message over MQTT to the embedded garden controller. This is used for a directly-requested
// WaterAction and does not perform any of the watering checks that are usuall done for a scheduled watering
func (w *Worker) ExecuteWaterAction(g *pkg.Garden, z *pkg.Zone, input *action.WaterAction) error {
if input.Duration.Duration == 0 {
w.logger.Info("weather control determined that watering should be skipped")
return nil
}
msg, err := json.Marshal(action.WaterMessage{
Duration: input.Duration.Duration.Milliseconds(),
ZoneID: z.GetID(),
Position: *z.Position,
})
if err != nil {
return fmt.Errorf("unable to marshal WaterMessage to JSON: %w", err)
}
topic, err := w.mqttClient.WaterTopic(g.TopicPrefix)
if err != nil {
return fmt.Errorf("unable to fill MQTT topic template: %w", err)
}
return w.mqttClient.Publish(topic, msg)
}