some more stuffs.
This commit is contained in:
@@ -13,6 +13,7 @@ type Line struct {
|
||||
If *LineIf `json:"if,omitempty"`
|
||||
Assign *LineAssign `json:"assign,omitempty"`
|
||||
Set *LineSet `json:"set,omitempty"`
|
||||
Select *LineSelect `json:"select,omitempty"`
|
||||
}
|
||||
|
||||
type LineSet struct {
|
||||
@@ -31,3 +32,8 @@ type LineIf struct {
|
||||
Then []Line `json:"then,omitempty"`
|
||||
Else []Line `json:"else,omitempty"`
|
||||
}
|
||||
|
||||
type LineSelect struct {
|
||||
Match string `json:"match"`
|
||||
Then []Line `json:"then"`
|
||||
}
|
||||
|
||||
+38
-24
@@ -1,6 +1,7 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
lucifer3 "git.aiterp.net/lucifer3/server"
|
||||
"git.aiterp.net/lucifer3/server/commands"
|
||||
"git.aiterp.net/lucifer3/server/device"
|
||||
@@ -37,7 +38,9 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
|
||||
return devices[i].ID < devices[j].ID
|
||||
})
|
||||
|
||||
s.runScript(bus, script.Lines, trig.ScriptTarget, map[string]bool{}, devices, variables)
|
||||
fmt.Println("[TRIGGER] Running", trig.ID, "for", len(devices), "devices:", event.EventDescription())
|
||||
|
||||
s.runScript(bus, lines, trig.ScriptTarget, map[string]bool{}, devices, variables)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,7 +100,7 @@ func (s *service) runScript(bus *lucifer3.EventBus, lines []Line, match string,
|
||||
continue
|
||||
}
|
||||
|
||||
if dev.Matches(matcher) {
|
||||
if line.Assign.Match == "*" || dev.Matches(matcher) {
|
||||
matchedDevices[dev.ID] = true
|
||||
matched = append(matched, dev.ID)
|
||||
}
|
||||
@@ -109,30 +112,41 @@ func (s *service) runScript(bus *lucifer3.EventBus, lines []Line, match string,
|
||||
Effect: line.Assign.Effect.Effect,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if line.Set != nil {
|
||||
switch line.Set.Scope {
|
||||
case "devices":
|
||||
bus.RunCommand(SetVariable{
|
||||
Devices: gentools.Map(devices, func(d device.Pointer) string {
|
||||
return d.ID
|
||||
}),
|
||||
Key: line.Set.Key,
|
||||
Value: line.Set.Value,
|
||||
})
|
||||
case "match":
|
||||
bus.RunCommand(SetVariable{
|
||||
Match: gentools.Ptr(match),
|
||||
Key: line.Set.Key,
|
||||
Value: line.Set.Value,
|
||||
})
|
||||
case "global":
|
||||
bus.RunCommand(SetVariable{
|
||||
Key: line.Set.Key,
|
||||
Value: line.Set.Value,
|
||||
})
|
||||
} else if line.Set != nil {
|
||||
switch line.Set.Scope {
|
||||
case "devices":
|
||||
bus.RunCommand(SetVariable{
|
||||
Devices: gentools.Map(devices, func(d device.Pointer) string {
|
||||
return d.ID
|
||||
}),
|
||||
Key: line.Set.Key,
|
||||
Value: line.Set.Value,
|
||||
})
|
||||
case "match":
|
||||
bus.RunCommand(SetVariable{
|
||||
Match: gentools.Ptr(match),
|
||||
Key: line.Set.Key,
|
||||
Value: line.Set.Value,
|
||||
})
|
||||
case "global":
|
||||
bus.RunCommand(SetVariable{
|
||||
Key: line.Set.Key,
|
||||
Value: line.Set.Value,
|
||||
})
|
||||
}
|
||||
} else if line.Select != nil {
|
||||
matcher := s.resolver.CompileMatcher(line.Select.Match)
|
||||
|
||||
matched := make([]device.Pointer, 0)
|
||||
for _, dev := range devices {
|
||||
if dev.Matches(matcher) {
|
||||
matched = append(matched, dev)
|
||||
}
|
||||
}
|
||||
|
||||
if len(matched) > 0 {
|
||||
s.runScript(bus, line.Select.Then, match, matchedDevices, matched, variables)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user