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.
87 lines
1.7 KiB
87 lines
1.7 KiB
package scene
|
|
|
|
import (
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
"time"
|
|
)
|
|
|
|
type Scene struct {
|
|
startTime time.Time
|
|
duration time.Duration
|
|
tag string
|
|
data models.Scene
|
|
roleList [][]models.Device
|
|
deviceMap map[int]*models.Device
|
|
roleMap map[int]int
|
|
changes map[int]models.NewDeviceState
|
|
}
|
|
|
|
func (s *Scene) UpdateScene(scene models.Scene) {
|
|
s.data = scene
|
|
s.roleMap = make(map[int]int)
|
|
|
|
for _, device := range s.deviceMap {
|
|
s.moveDevice(*device)
|
|
}
|
|
}
|
|
|
|
func (s *Scene) UpsertDevice(device models.Device) {
|
|
s.deviceMap[device.ID] = &device
|
|
s.moveDevice(device)
|
|
}
|
|
|
|
func (s *Scene) RemoveDevice(device models.Device) {
|
|
delete(s.deviceMap, device.ID)
|
|
delete(s.roleMap, device.ID)
|
|
}
|
|
|
|
func (s *Scene) runRole(index int) {
|
|
|
|
}
|
|
|
|
func (s *Scene) moveDevice(device models.Device, run bool) {
|
|
oldRole, hasOldRole := s.roleMap[device.ID]
|
|
newRole := s.data.RoleIndex(&device)
|
|
|
|
if hasOldRole && oldRole == newRole {
|
|
return
|
|
}
|
|
|
|
if hasOldRole {
|
|
for i, device2 := range s.roleList[oldRole] {
|
|
if device2.ID == device.ID {
|
|
s.roleList[oldRole] = append(s.roleList[oldRole][:i], s.roleList[oldRole][i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
|
|
s.runRole(oldRole)
|
|
}
|
|
|
|
s.roleMap[device.ID] = newRole
|
|
s.runRole(newRole)
|
|
}
|
|
|
|
func (s *Scene) intervalNumber() int64 {
|
|
intervalDuration := time.Duration(s.data.IntervalMS) * time.Millisecond
|
|
|
|
return int64(time.Since(s.startTime) / intervalDuration)
|
|
}
|
|
|
|
func (s *Scene) intervalFac() float64 {
|
|
return clamp(float64(time.Since(s.startTime)) / float64(s.duration))
|
|
}
|
|
|
|
func (s *Scene) positionFac(i, len int) float64 {
|
|
return clamp(float64(i) / float64(len))
|
|
}
|
|
|
|
func clamp(fac float64) float64 {
|
|
if fac > 1.00 {
|
|
return 1.00
|
|
} else if fac < 0.00 {
|
|
return 0.00
|
|
} else {
|
|
return fac
|
|
}
|
|
}
|