trigger editor

This commit is contained in:
2023-10-12 23:27:04 +02:00
parent e06cf1ae34
commit 16e5726cbc
15 changed files with 410 additions and 45 deletions
+15 -6
View File
@@ -113,26 +113,35 @@ func (s *service) runScript(bus *lucifer3.EventBus, lines []Line, match string,
})
}
} else if line.Set != nil {
var sv *SetVariable
switch line.Set.Scope {
case "devices":
bus.RunCommand(SetVariable{
sv = &SetVariable{
Devices: gentools.Map(devices, func(d device.Pointer) string {
return d.ID
}),
Key: line.Set.Key,
Value: line.Set.Value,
})
}
bus.RunCommand(sv)
case "match":
bus.RunCommand(SetVariable{
sv = &SetVariable{
Match: gentools.Ptr(match),
Key: line.Set.Key,
Value: line.Set.Value,
})
}
case "global":
bus.RunCommand(SetVariable{
sv = &SetVariable{
Match: gentools.Ptr(match),
Key: line.Set.Key,
Value: line.Set.Value,
})
}
}
if sv != nil {
variables = variables.With(*sv)
bus.RunCommand(*sv)
}
} else if line.Select != nil {
matcher := s.resolver.CompileMatcher(line.Select.Match)
+23 -19
View File
@@ -21,6 +21,27 @@ func (v VariableSet) Devices(list []string, key string) string {
return v["devices:"+strings.Join(list, ";")][key]
}
func (v VariableSet) With(sv SetVariable) VariableSet {
curr := gentools.CopyMap(v)
key := "global"
if sv.Match != nil {
key = "match:" + *sv.Match
} else if sv.Devices != nil {
key = "devices:" + strings.Join(sv.Devices, ";")
}
curr[key] = gentools.CopyMap(curr["global"])
if sv.Value == "" {
delete(curr[key], sv.Key)
} else {
curr[key][sv.Key] = sv.Value
}
return curr
}
type Variables struct {
mu sync.Mutex
vars VariableSet
@@ -47,27 +68,10 @@ func (s *Variables) HandleEvent(_ *lucifer3.EventBus, _ lucifer3.Event) {}
func (s *Variables) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command) {
if sv, ok := command.(SetVariable); ok {
s.mu.Lock()
curr := gentools.CopyMap(s.vars)
s.mu.Unlock()
key := "global"
if sv.Match != nil {
key = "match:" + *sv.Match
} else if sv.Devices != nil {
key = "devices:" + strings.Join(sv.Devices, ";")
}
curr[key] = gentools.CopyMap(curr["global"])
if sv.Value == "" {
delete(curr[key], sv.Key)
} else {
curr[key][sv.Key] = sv.Value
}
newVariables := s.Get().With(sv)
s.mu.Lock()
s.vars = curr
s.vars = newVariables
s.mu.Unlock()
}
}