You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

71 lines
1.7 KiB

package script
import (
"fmt"
lucifer3 "git.aiterp.net/lucifer3/server"
"git.aiterp.net/lucifer3/server/device"
"git.aiterp.net/lucifer3/server/events"
"github.com/google/uuid"
"strconv"
)
type TriggerEvent string
const (
TEButton TriggerEvent = "Button"
TETime TriggerEvent = "Time"
TETemperatureAbove TriggerEvent = "TemperatureAbove"
TETemperatureBelow TriggerEvent = "TemperatureBelow"
)
type Trigger struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Event TriggerEvent `json:"event"`
DeviceMatch string `json:"deviceMatch"`
Parameter string `json:"parameter"`
ScriptTarget string `json:"scriptTarget"`
ScriptName string `json:"scriptName"`
ScriptPre []Line `json:"scriptPre"`
ScriptPost []Line `json:"scriptPost"`
}
func (t *Trigger) Check(event lucifer3.Event, res device.Resolver) bool {
devices := res.Resolve(t.DeviceMatch)
switch event := event.(type) {
case events.ButtonPressed:
if t.Event != TEButton {
return false
}
for _, dev := range devices {
if dev.ID == event.ID {
return t.Parameter == event.Name
}
}
case events.Time:
if t.Event != TETime {
return false
}
return fmt.Sprintf("%02d:%02d", event.Hour, event.Minute) == t.Parameter
case events.TemperatureChanged:
if t.Event != TETemperatureAbove && t.Event != TETemperatureBelow {
return false
}
num, err := strconv.ParseFloat(t.Parameter, 64)
if err != nil {
return false
}
for _, dev := range devices {
if dev.ID == event.ID {
return (event.Temperature >= num) == (t.Event == TETemperatureAbove)
}
}
}
return false
}