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.
73 lines
1.4 KiB
73 lines
1.4 KiB
package script
|
|
|
|
import (
|
|
lucifer3 "git.aiterp.net/lucifer3/server"
|
|
"git.aiterp.net/lucifer3/server/internal/gentools"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type VariableSet map[string]map[string]string
|
|
|
|
func (v VariableSet) Global(key string) string {
|
|
return v["global"][key]
|
|
}
|
|
|
|
func (v VariableSet) Match(tag string, key string) string {
|
|
return v["match:"+tag][key]
|
|
}
|
|
|
|
func (v VariableSet) Devices(list []string, key string) string {
|
|
return v["devices:"+strings.Join(list, ";")][key]
|
|
}
|
|
|
|
type Variables struct {
|
|
mu sync.Mutex
|
|
vars VariableSet
|
|
}
|
|
|
|
func NewVariables() *Variables {
|
|
return &Variables{
|
|
vars: make(VariableSet, 8),
|
|
}
|
|
}
|
|
|
|
func (s *Variables) Get() VariableSet {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return s.vars
|
|
}
|
|
|
|
func (s *Variables) Active() bool {
|
|
return true
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
s.mu.Lock()
|
|
s.vars = curr
|
|
s.mu.Unlock()
|
|
}
|
|
}
|