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.
77 lines
1.4 KiB
77 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]
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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 {
|
|
newVariables := s.Get().With(sv)
|
|
|
|
s.mu.Lock()
|
|
s.vars = newVariables
|
|
s.mu.Unlock()
|
|
}
|
|
}
|