refactorings + basis of script editor.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"git.aiterp.net/lucifer3/server/effects"
|
||||
"git.aiterp.net/lucifer3/server/events"
|
||||
"git.aiterp.net/lucifer3/server/internal/color"
|
||||
"git.aiterp.net/lucifer3/server/internal/gentools"
|
||||
"git.aiterp.net/lucifer3/server/services/script"
|
||||
"git.aiterp.net/lucifer3/server/services/uistate"
|
||||
"github.com/google/uuid"
|
||||
@@ -110,6 +111,86 @@ func New(addr string) (lucifer3.Service, error) {
|
||||
return nil
|
||||
})
|
||||
|
||||
e.GET("/subscribe-simple", func(c echo.Context) error {
|
||||
type ChangedPatch struct {
|
||||
Devices map[string]*uistate.Device `json:"devices"`
|
||||
Assignments map[uuid.UUID]*uistate.Assignment `json:"assignments"`
|
||||
Scripts map[string][]script.Line `json:"scripts"`
|
||||
Full *uistate.Data `json:"full,omitempty"`
|
||||
}
|
||||
|
||||
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sub := make(chan uistate.Patch, 64)
|
||||
svc.addSub(sub)
|
||||
|
||||
defer svc.removeSub(sub)
|
||||
defer ws.Close()
|
||||
|
||||
svc.mu.Lock()
|
||||
err = ws.WriteJSON(ChangedPatch{Full: &svc.data})
|
||||
svc.mu.Unlock()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
patches := make([]uistate.Patch, 0, 128)
|
||||
for {
|
||||
patch := <-sub
|
||||
patches = append(patches[:0], patch)
|
||||
waitCh := time.After(time.Millisecond * 330)
|
||||
waiting := true
|
||||
for waiting {
|
||||
select {
|
||||
case patch, ok := <-sub:
|
||||
patches = append(patches, patch)
|
||||
waiting = ok
|
||||
case <-waitCh:
|
||||
waiting = false
|
||||
}
|
||||
}
|
||||
|
||||
statePatch := ChangedPatch{
|
||||
Devices: make(map[string]*uistate.Device),
|
||||
Assignments: make(map[uuid.UUID]*uistate.Assignment),
|
||||
Scripts: make(map[string][]script.Line),
|
||||
Full: nil,
|
||||
}
|
||||
|
||||
svc.mu.Lock()
|
||||
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])
|
||||
}
|
||||
}
|
||||
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])
|
||||
}
|
||||
}
|
||||
if patch.Script != nil {
|
||||
statePatch.Scripts[patch.Script.Name] = svc.data.Scripts[patch.Script.Name]
|
||||
}
|
||||
}
|
||||
svc.mu.Unlock()
|
||||
|
||||
err := ws.WriteJSON(statePatch)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
e.POST("/command", func(c echo.Context) error {
|
||||
var input commandInput
|
||||
err := c.Bind(&input)
|
||||
|
||||
Reference in New Issue
Block a user