add time-of-day limitation to events.
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-10-02 14:43:15 +02:00
parent b83673b2cc
commit 2abb4b1024
9 changed files with 236 additions and 18 deletions
+32 -9
View File
@@ -22,6 +22,28 @@ func (p *Param) String() *string {
return &p.Value
}
func (p *Param) TimeOfDay() *models.TimeOfDay {
if p == nil {
return nil
}
tod, err := models.ParseTimeOfDay(p.Value)
if err != nil {
return nil
}
return &tod
}
func (p *Param) TimeOfDayOr(fallback models.TimeOfDay) models.TimeOfDay {
tod := p.TimeOfDay()
if tod == nil {
return fallback
}
return *tod
}
func (p *Param) StringOr(fallback string) string {
if p == nil {
return fallback
@@ -190,6 +212,16 @@ func (p Params) EventConditions() map[string]models.EventCondition {
return ecMap
}
func (p Params) DeviceState(prefix string) models.NewDeviceState {
return models.NewDeviceState{
Power: p.Get(prefix + "power").Bool(),
Color: p.Get(prefix + "color").String(),
Intensity: p.Get(prefix + "intensity").Float(),
Temperature: p.Get(prefix + "temperature").Int(),
}
}
type Command struct {
Name string
Params Params
@@ -231,12 +263,3 @@ func parseCommand(args []string) Command {
return cmd
}
func (cmd Command) NewDeviceState(prefix string) models.NewDeviceState {
return models.NewDeviceState{
Power: cmd.Params.Get(prefix + "power").Bool(),
Color: cmd.Params.Get(prefix + "color").String(),
Intensity: cmd.Params.Get(prefix + "intensity").Float(),
Temperature: cmd.Params.Get(prefix + "temperature").Int(),
}
}
+9 -2
View File
@@ -39,6 +39,13 @@ func handlerCmd(
}
model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr)
// From / To
model.From = cmd.Params.Get("from").TimeOfDayOr(models.Never)
model.To = cmd.Params.Get("to").TimeOfDayOr(models.Never)
if model.From.IsNever() != model.To.IsNever() {
log.Fatalln("from and to must be specified together!")
}
// One shot
model.OneShot = cmd.Params.Get("one-shot").StringOr("false") == "true"
@@ -49,7 +56,7 @@ func handlerCmd(
model.Conditions = cmd.Params.Subset("conditions").EventConditions()
// Power, Color, Intensity, Temperature
nds := cmd.NewDeviceState("set-")
nds := cmd.Params.DeviceState("set-")
model.Actions.SetPower = nds.Power
model.Actions.SetColor = nds.Color
model.Actions.SetIntensity = nds.Intensity
@@ -64,7 +71,7 @@ func handlerCmd(
firePayloadParams := fireParams.Subset("payload")
model.Actions.FireEvent = &models.Event{
Name: *fireName,
Name: *fireName,
Payload: make(map[string]string, len(firePayloadParams)),
}
+1
View File
@@ -18,6 +18,7 @@ EVENT HANDLER COMMANDS
handler add <event> <search> \
<one-shot=B> \
<priority=N> \
<from=TOD to=TOD> \
<conditions.[any.*|all.*|*][<|<=|=|=>|>]*> \
<set-power=B> <set-color=C> <set-intensity=F> <set-temperature=N> \
<add-intensity=F> \
+1 -1
View File
@@ -45,7 +45,7 @@ func main() {
}
case "set":
{
devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), cmd.NewDeviceState(""))
devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), cmd.Params.DeviceState(""))
if err != nil {
log.Fatalln(err)
}