trigger editor
This commit is contained in:
@@ -116,6 +116,7 @@ func New(addr string) (lucifer3.Service, error) {
|
||||
Devices map[string]*uistate.Device `json:"devices"`
|
||||
Assignments map[uuid.UUID]*uistate.Assignment `json:"assignments"`
|
||||
Scripts map[string][]script.Line `json:"scripts"`
|
||||
Triggers map[uuid.UUID]*script.Trigger `json:"triggers"`
|
||||
Full *uistate.Data `json:"full,omitempty"`
|
||||
}
|
||||
|
||||
@@ -131,8 +132,10 @@ func New(addr string) (lucifer3.Service, error) {
|
||||
defer ws.Close()
|
||||
|
||||
svc.mu.Lock()
|
||||
err = ws.WriteJSON(ChangedPatch{Full: &svc.data})
|
||||
patch := ChangedPatch{Full: gentools.Ptr(svc.data.Copy())}
|
||||
svc.mu.Unlock()
|
||||
|
||||
err = ws.WriteJSON(patch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -157,34 +160,44 @@ func New(addr string) (lucifer3.Service, error) {
|
||||
Devices: make(map[string]*uistate.Device),
|
||||
Assignments: make(map[uuid.UUID]*uistate.Assignment),
|
||||
Scripts: make(map[string][]script.Line),
|
||||
Triggers: make(map[uuid.UUID]*script.Trigger),
|
||||
Full: nil,
|
||||
}
|
||||
|
||||
svc.mu.Lock()
|
||||
data := svc.data.Copy()
|
||||
svc.mu.Unlock()
|
||||
|
||||
for _, patch := range patches {
|
||||
if patch.Device != nil {
|
||||
if patch.Device.Delete {
|
||||
statePatch.Devices[patch.Device.ID] = nil
|
||||
} else {
|
||||
statePatch.Devices[patch.Device.ID] = gentools.Ptr(svc.data.Devices[patch.Device.ID])
|
||||
statePatch.Devices[patch.Device.ID] = gentools.Ptr(data.Devices[patch.Device.ID])
|
||||
}
|
||||
}
|
||||
if patch.Assignment != nil {
|
||||
if patch.Assignment.Delete {
|
||||
statePatch.Assignments[patch.Assignment.ID] = nil
|
||||
} else {
|
||||
statePatch.Assignments[patch.Assignment.ID] = gentools.Ptr(svc.data.Assignments[patch.Assignment.ID])
|
||||
statePatch.Assignments[patch.Assignment.ID] = gentools.Ptr(data.Assignments[patch.Assignment.ID])
|
||||
}
|
||||
}
|
||||
if patch.Script != nil {
|
||||
if len(patch.Script.Lines) > 0 {
|
||||
statePatch.Scripts[patch.Script.Name] = svc.data.Scripts[patch.Script.Name]
|
||||
statePatch.Scripts[patch.Script.Name] = data.Scripts[patch.Script.Name]
|
||||
} else {
|
||||
statePatch.Scripts[patch.Script.Name] = nil
|
||||
}
|
||||
}
|
||||
if patch.Trigger != nil {
|
||||
if patch.Trigger.Delete {
|
||||
statePatch.Triggers[patch.Trigger.ID] = nil
|
||||
} else {
|
||||
statePatch.Triggers[patch.Trigger.ID] = gentools.Ptr(data.Triggers[patch.Trigger.ID])
|
||||
}
|
||||
}
|
||||
}
|
||||
svc.mu.Unlock()
|
||||
|
||||
err := ws.WriteJSON(statePatch)
|
||||
if err != nil {
|
||||
@@ -302,12 +315,15 @@ func (s *service) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
|
||||
|
||||
func (s *service) addSub(ch chan uistate.Patch) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.subs = append(s.subs, ch)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *service) removeSub(ch chan uistate.Patch) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.subs = append([]chan uistate.Patch{}, s.subs...)
|
||||
for i, sub := range s.subs {
|
||||
if sub == ch {
|
||||
@@ -315,7 +331,6 @@ func (s *service) removeSub(ch chan uistate.Patch) {
|
||||
break
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
type commandInput struct {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,10 @@ import (
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
Devices map[string]Device `json:"devices"`
|
||||
Assignments map[uuid.UUID]Assignment `json:"assignments"`
|
||||
Scripts map[string][]script.Line `json:"scripts"`
|
||||
Devices map[string]Device `json:"devices"`
|
||||
Assignments map[uuid.UUID]Assignment `json:"assignments"`
|
||||
Scripts map[string][]script.Line `json:"scripts"`
|
||||
Triggers map[uuid.UUID]script.Trigger `json:"triggers"`
|
||||
}
|
||||
|
||||
func (d *Data) WithPatch(patches ...Patch) Data {
|
||||
@@ -121,6 +122,14 @@ func (d *Data) WithPatch(patches ...Patch) Data {
|
||||
delete(newData.Scripts, patch.Script.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if patch.Trigger != nil {
|
||||
if !patch.Trigger.Delete {
|
||||
newData.Triggers[patch.Trigger.ID] = patch.Trigger.Trigger
|
||||
} else {
|
||||
delete(newData.Triggers, patch.Trigger.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newData
|
||||
@@ -131,6 +140,7 @@ func (d *Data) Copy() Data {
|
||||
Devices: gentools.CopyMap(d.Devices),
|
||||
Assignments: gentools.CopyMap(d.Assignments),
|
||||
Scripts: gentools.CopyMap(d.Scripts),
|
||||
Triggers: gentools.CopyMap(d.Triggers),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ type Patch struct {
|
||||
Assignment *AssignmentPatch `json:"assignment,omitempty"`
|
||||
Device *DevicePatch `json:"device,omitempty"`
|
||||
Script *ScriptPatch `json:"script,omitempty"`
|
||||
Trigger *TriggerPatch `json:"trigger,omitempty"`
|
||||
}
|
||||
|
||||
func (e Patch) VerboseKey() string {
|
||||
@@ -83,3 +84,8 @@ type ScriptPatch struct {
|
||||
Name string
|
||||
Lines []script.Line
|
||||
}
|
||||
|
||||
type TriggerPatch struct {
|
||||
script.Trigger
|
||||
Delete bool `json:"delete,omitempty"`
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ func NewService() lucifer3.ActiveService {
|
||||
Devices: map[string]Device{},
|
||||
Assignments: map[uuid.UUID]Assignment{},
|
||||
Scripts: map[string][]script.Line{},
|
||||
Triggers: map[uuid.UUID]script.Trigger{},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -66,6 +67,17 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command
|
||||
patches = append(patches, Patch{
|
||||
Script: &ScriptPatch{Name: command.Name, Lines: command.Lines},
|
||||
})
|
||||
case script.UpdateTrigger:
|
||||
patches = append(patches, Patch{
|
||||
Trigger: &TriggerPatch{Trigger: script.Trigger(command)},
|
||||
})
|
||||
case script.DeleteTrigger:
|
||||
patches = append(patches, Patch{
|
||||
Trigger: &TriggerPatch{
|
||||
Trigger: script.Trigger{ID: command.ID},
|
||||
Delete: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if len(patches) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user