This commit is contained in:
@@ -93,6 +93,9 @@ func handleEvent(event models.Event) (responses []models.Event) {
|
|||||||
if handler.EventName != event.Name {
|
if handler.EventName != event.Name {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if !handler.IsWithinTimeRange() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
devices, err := config.DeviceRepository().FetchByReference(ctx, handler.TargetKind, handler.TargetValue)
|
devices, err := config.DeviceRepository().FetchByReference(ctx, handler.TargetKind, handler.TargetValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+32
-9
@@ -22,6 +22,28 @@ func (p *Param) String() *string {
|
|||||||
return &p.Value
|
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 {
|
func (p *Param) StringOr(fallback string) string {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return fallback
|
return fallback
|
||||||
@@ -190,6 +212,16 @@ func (p Params) EventConditions() map[string]models.EventCondition {
|
|||||||
return ecMap
|
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 {
|
type Command struct {
|
||||||
Name string
|
Name string
|
||||||
Params Params
|
Params Params
|
||||||
@@ -231,12 +263,3 @@ func parseCommand(args []string) Command {
|
|||||||
|
|
||||||
return cmd
|
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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -39,6 +39,13 @@ func handlerCmd(
|
|||||||
}
|
}
|
||||||
model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr)
|
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
|
// One shot
|
||||||
model.OneShot = cmd.Params.Get("one-shot").StringOr("false") == "true"
|
model.OneShot = cmd.Params.Get("one-shot").StringOr("false") == "true"
|
||||||
|
|
||||||
@@ -49,7 +56,7 @@ func handlerCmd(
|
|||||||
model.Conditions = cmd.Params.Subset("conditions").EventConditions()
|
model.Conditions = cmd.Params.Subset("conditions").EventConditions()
|
||||||
|
|
||||||
// Power, Color, Intensity, Temperature
|
// Power, Color, Intensity, Temperature
|
||||||
nds := cmd.NewDeviceState("set-")
|
nds := cmd.Params.DeviceState("set-")
|
||||||
model.Actions.SetPower = nds.Power
|
model.Actions.SetPower = nds.Power
|
||||||
model.Actions.SetColor = nds.Color
|
model.Actions.SetColor = nds.Color
|
||||||
model.Actions.SetIntensity = nds.Intensity
|
model.Actions.SetIntensity = nds.Intensity
|
||||||
@@ -64,7 +71,7 @@ func handlerCmd(
|
|||||||
firePayloadParams := fireParams.Subset("payload")
|
firePayloadParams := fireParams.Subset("payload")
|
||||||
|
|
||||||
model.Actions.FireEvent = &models.Event{
|
model.Actions.FireEvent = &models.Event{
|
||||||
Name: *fireName,
|
Name: *fireName,
|
||||||
Payload: make(map[string]string, len(firePayloadParams)),
|
Payload: make(map[string]string, len(firePayloadParams)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ EVENT HANDLER COMMANDS
|
|||||||
handler add <event> <search> \
|
handler add <event> <search> \
|
||||||
<one-shot=B> \
|
<one-shot=B> \
|
||||||
<priority=N> \
|
<priority=N> \
|
||||||
|
<from=TOD to=TOD> \
|
||||||
<conditions.[any.*|all.*|*][<|<=|=|=>|>]*> \
|
<conditions.[any.*|all.*|*][<|<=|=|=>|>]*> \
|
||||||
<set-power=B> <set-color=C> <set-intensity=F> <set-temperature=N> \
|
<set-power=B> <set-color=C> <set-intensity=F> <set-temperature=N> \
|
||||||
<add-intensity=F> \
|
<add-intensity=F> \
|
||||||
|
|||||||
+1
-1
@@ -45,7 +45,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
case "set":
|
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 {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ type eventHandlerRecord struct {
|
|||||||
Priority int `db:"priority"`
|
Priority int `db:"priority"`
|
||||||
TargetKind string `db:"target_kind"`
|
TargetKind string `db:"target_kind"`
|
||||||
TargetValue string `db:"target_value"`
|
TargetValue string `db:"target_value"`
|
||||||
|
From *string `db:"from_tod"`
|
||||||
|
To *string `db:"to_tod"`
|
||||||
ConditionsJSON json.RawMessage `db:"conditions"`
|
ConditionsJSON json.RawMessage `db:"conditions"`
|
||||||
ActionsJSON json.RawMessage `db:"actions"`
|
ActionsJSON json.RawMessage `db:"actions"`
|
||||||
}
|
}
|
||||||
@@ -61,12 +63,14 @@ func (r *EventHandlerRepo) Save(ctx context.Context, handler *models.EventHandle
|
|||||||
TargetValue: handler.TargetValue,
|
TargetValue: handler.TargetValue,
|
||||||
ConditionsJSON: conditionsJson,
|
ConditionsJSON: conditionsJson,
|
||||||
ActionsJSON: actionsJson,
|
ActionsJSON: actionsJson,
|
||||||
|
From: handler.From.StringPtr(),
|
||||||
|
To: handler.To.StringPtr(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if record.ID == 0 {
|
if record.ID == 0 {
|
||||||
res, err := r.DBX.NamedExecContext(ctx, `
|
res, err := r.DBX.NamedExecContext(ctx, `
|
||||||
INSERT INTO event_handler (event_name, one_shot, priority, target_kind, target_value, conditions, actions)
|
INSERT INTO event_handler (event_name, one_shot, priority, target_kind, target_value, conditions, from_tod, to_tod, actions)
|
||||||
VALUES (:event_name, :one_shot, :priority, :target_kind, :target_value, :conditions, :actions);
|
VALUES (:event_name, :one_shot, :priority, :target_kind, :target_value, :conditions, :from_tod, :to_tod, :actions);
|
||||||
`, record)
|
`, record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dbErr(err)
|
return dbErr(err)
|
||||||
@@ -87,6 +91,8 @@ func (r *EventHandlerRepo) Save(ctx context.Context, handler *models.EventHandle
|
|||||||
target_kind = :target_kind,
|
target_kind = :target_kind,
|
||||||
target_value = :target_value,
|
target_value = :target_value,
|
||||||
conditions = :conditions,
|
conditions = :conditions,
|
||||||
|
from_tod = :from_tod,
|
||||||
|
to_tod = :to_tod,
|
||||||
actions = :actions
|
actions = :actions
|
||||||
WHERE id = :id
|
WHERE id = :id
|
||||||
`, record)
|
`, record)
|
||||||
@@ -139,6 +145,15 @@ func (r *EventHandlerRepo) populate(records []eventHandlerRecord) ([]models.Even
|
|||||||
return nil, dbErr(err)
|
return nil, dbErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handler.From, err = models.ParseTimeOfDayPtr(record.From)
|
||||||
|
if err != nil {
|
||||||
|
return nil, dbErr(err)
|
||||||
|
}
|
||||||
|
handler.To, err = models.ParseTimeOfDayPtr(record.To)
|
||||||
|
if err != nil {
|
||||||
|
return nil, dbErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
res = append(res, handler)
|
res = append(res, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+22
-4
@@ -16,6 +16,8 @@ type EventHandler struct {
|
|||||||
TargetKind ReferenceKind `json:"targetKind"`
|
TargetKind ReferenceKind `json:"targetKind"`
|
||||||
TargetValue string `json:"targetValue"`
|
TargetValue string `json:"targetValue"`
|
||||||
Actions EventAction `json:"actions"`
|
Actions EventAction `json:"actions"`
|
||||||
|
From TimeOfDay `json:"from"`
|
||||||
|
To TimeOfDay `json:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventHandlerRepository interface {
|
type EventHandlerRepository interface {
|
||||||
@@ -42,6 +44,8 @@ type EventHandlerUpdate struct {
|
|||||||
SetConditions map[string]EventCondition `json:"setConditions"`
|
SetConditions map[string]EventCondition `json:"setConditions"`
|
||||||
PatchConditions map[string]*EventCondition `json:"patchConditions"`
|
PatchConditions map[string]*EventCondition `json:"patchConditions"`
|
||||||
SetActions *EventAction `json:"setActions"`
|
SetActions *EventAction `json:"setActions"`
|
||||||
|
SetFrom *TimeOfDay `json:"setFrom"`
|
||||||
|
SetTo *TimeOfDay `json:"setTo"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *EventHandler) ApplyUpdate(update EventHandlerUpdate) {
|
func (h *EventHandler) ApplyUpdate(update EventHandlerUpdate) {
|
||||||
@@ -63,6 +67,12 @@ func (h *EventHandler) ApplyUpdate(update EventHandlerUpdate) {
|
|||||||
if update.SetOneShot != nil {
|
if update.SetOneShot != nil {
|
||||||
h.OneShot = *update.SetOneShot
|
h.OneShot = *update.SetOneShot
|
||||||
}
|
}
|
||||||
|
if update.SetFrom != nil {
|
||||||
|
h.From = *update.SetFrom
|
||||||
|
}
|
||||||
|
if update.SetFrom != nil {
|
||||||
|
h.To = *update.SetFrom
|
||||||
|
}
|
||||||
|
|
||||||
if update.SetConditions != nil {
|
if update.SetConditions != nil {
|
||||||
h.Conditions = update.SetConditions
|
h.Conditions = update.SetConditions
|
||||||
@@ -100,6 +110,14 @@ func (h *EventHandler) MatchesEvent(event Event, targets []Device) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *EventHandler) IsWithinTimeRange() bool {
|
||||||
|
if h.From.IsNever() || h.To.IsNever() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return CurrentTimeOfDay().IsBetween(h.From, h.To)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *EventCondition) check(key, value string, targets []Device) bool {
|
func (c *EventCondition) check(key, value string, targets []Device) bool {
|
||||||
any := strings.HasPrefix(key, "any.")
|
any := strings.HasPrefix(key, "any.")
|
||||||
all := strings.HasPrefix(key, "all.")
|
all := strings.HasPrefix(key, "all.")
|
||||||
@@ -225,19 +243,19 @@ func (action *EventAction) Apply(other EventAction) {
|
|||||||
func (c EventCondition) String() string {
|
func (c EventCondition) String() string {
|
||||||
str := make([]string, 0, 5)
|
str := make([]string, 0, 5)
|
||||||
if len(c.LT) > 0 {
|
if len(c.LT) > 0 {
|
||||||
str = append(str, "lt:" + c.LT)
|
str = append(str, "lt:"+c.LT)
|
||||||
}
|
}
|
||||||
if len(c.LTE) > 0 {
|
if len(c.LTE) > 0 {
|
||||||
str = append(str, "lte:" + c.LTE)
|
str = append(str, "lte:"+c.LTE)
|
||||||
}
|
}
|
||||||
if len(c.EQ) > 0 {
|
if len(c.EQ) > 0 {
|
||||||
str = append(str, c.EQ)
|
str = append(str, c.EQ)
|
||||||
}
|
}
|
||||||
if len(c.GTE) > 0 {
|
if len(c.GTE) > 0 {
|
||||||
str = append(str, "gte:" + c.GTE)
|
str = append(str, "gte:"+c.GTE)
|
||||||
}
|
}
|
||||||
if len(c.GT) > 0 {
|
if len(c.GT) > 0 {
|
||||||
str = append(str, "gte:" + c.GT)
|
str = append(str, "gte:"+c.GT)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(str, ";")
|
return strings.Join(str, ";")
|
||||||
|
|||||||
@@ -0,0 +1,138 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TimeOfDay int
|
||||||
|
|
||||||
|
func (t *TimeOfDay) UnmarshalJSON(v []byte) error {
|
||||||
|
if bytes.Equal(v, []byte("null")) {
|
||||||
|
*t = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
var str string
|
||||||
|
err := json.Unmarshal(v, &str)
|
||||||
|
if err != nil {
|
||||||
|
var n int
|
||||||
|
err = json.Unmarshal(v, &n)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n < -1 && n >= 86400 {
|
||||||
|
return fmt.Errorf("value outside range 0..86400: %d", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
t2, err := ParseTimeOfDay(str)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*t = t2
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TimeOfDay) MarshalJSON() ([]byte, error) {
|
||||||
|
if t.IsNever() {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(t.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
const Never = TimeOfDay(-1)
|
||||||
|
|
||||||
|
func (t *TimeOfDay) String() string {
|
||||||
|
if *t < 0 {
|
||||||
|
return "n/a"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%02d:%02d:%02d", *t/3600, (*t/60)%60, *t%60)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TimeOfDay) StringPtr() *string {
|
||||||
|
if *t < 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s := t.String()
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TimeOfDay) IsNever() bool {
|
||||||
|
return t == Never
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TimeOfDay) IsBetween(from TimeOfDay, to TimeOfDay) bool {
|
||||||
|
if from == to {
|
||||||
|
return t == from
|
||||||
|
} else if from > to {
|
||||||
|
return t >= to || t <= from
|
||||||
|
} else {
|
||||||
|
return t >= from && t <= to
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CurrentTimeOfDay() TimeOfDay {
|
||||||
|
return TimeOfDayFromDate(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimeOfDayFromDate(date time.Time) TimeOfDay {
|
||||||
|
return NewTimeOfDay(date.Hour(), date.Minute(), date.Second())
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTimeOfDay(hours, minutes, seconds int) TimeOfDay {
|
||||||
|
return TimeOfDay(((hours % 24) * 3600) + ((minutes % 60) * 60) + seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseTimeOfDayPtr(str *string) (TimeOfDay, error) {
|
||||||
|
if str == nil {
|
||||||
|
return Never, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ParseTimeOfDay(*str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseTimeOfDay(str string) (TimeOfDay, error) {
|
||||||
|
if str == "" || str == "n/a" || str == "N/A" {
|
||||||
|
return -1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
split := strings.SplitN(str, ":", 4)
|
||||||
|
res := 0
|
||||||
|
|
||||||
|
n, err := strconv.Atoi(split[0])
|
||||||
|
if err != nil {
|
||||||
|
return Never, err
|
||||||
|
}
|
||||||
|
res += n * 3600
|
||||||
|
|
||||||
|
if len(split) >= 2 {
|
||||||
|
n, err := strconv.Atoi(split[1])
|
||||||
|
if err != nil {
|
||||||
|
return Never, err
|
||||||
|
}
|
||||||
|
res += n * 60
|
||||||
|
}
|
||||||
|
if len(split) >= 3 {
|
||||||
|
n, err := strconv.Atoi(split[2])
|
||||||
|
if err != nil {
|
||||||
|
return Never, err
|
||||||
|
}
|
||||||
|
res += n
|
||||||
|
}
|
||||||
|
|
||||||
|
if res >= 86400 || res < 0 {
|
||||||
|
return Never, fmt.Errorf("invalid time of day string %s (=%d)", str, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
return TimeOfDay(res), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE event_handler
|
||||||
|
ADD COLUMN from_tod VARCHAR(255),
|
||||||
|
ADD COLUMN to_tod VARCHAR(255);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
ALTER TABLE event_handler
|
||||||
|
DROP COLUMN IF EXISTS from_tod,
|
||||||
|
DROP COLUMN IF EXISTS to_tod;
|
||||||
|
-- +goose StatementEnd
|
||||||
Reference in New Issue
Block a user