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.

86 lines
1.7 KiB

  1. package scene
  2. import (
  3. "git.aiterp.net/lucifer/new-server/models"
  4. "time"
  5. )
  6. type Scene struct {
  7. startTime time.Time
  8. duration time.Duration
  9. tag string
  10. data models.Scene
  11. roleList [][]models.Device
  12. deviceMap map[int]*models.Device
  13. roleMap map[int]int
  14. changes map[int]models.NewDeviceState
  15. }
  16. func (s *Scene) UpdateScene(scene models.Scene) {
  17. s.data = scene
  18. s.roleMap = make(map[int]int)
  19. for _, device := range s.deviceMap {
  20. s.moveDevice(*device)
  21. }
  22. }
  23. func (s *Scene) UpsertDevice(device models.Device) {
  24. s.deviceMap[device.ID] = &device
  25. s.moveDevice(device)
  26. }
  27. func (s *Scene) RemoveDevice(device models.Device) {
  28. delete(s.deviceMap, device.ID)
  29. delete(s.roleMap, device.ID)
  30. }
  31. func (s *Scene) runRole(index int) {
  32. }
  33. func (s *Scene) moveDevice(device models.Device, run bool) {
  34. oldRole, hasOldRole := s.roleMap[device.ID]
  35. newRole := s.data.RoleIndex(&device)
  36. if hasOldRole && oldRole == newRole {
  37. return
  38. }
  39. if hasOldRole {
  40. for i, device2 := range s.roleList[oldRole] {
  41. if device2.ID == device.ID {
  42. s.roleList[oldRole] = append(s.roleList[oldRole][:i], s.roleList[oldRole][i+1:]...)
  43. break
  44. }
  45. }
  46. s.runRole(oldRole)
  47. }
  48. s.roleMap[device.ID] = newRole
  49. s.runRole(newRole)
  50. }
  51. func (s *Scene) intervalNumber() int64 {
  52. intervalDuration := time.Duration(s.data.IntervalMS) * time.Millisecond
  53. return int64(time.Since(s.startTime) / intervalDuration)
  54. }
  55. func (s *Scene) intervalFac() float64 {
  56. return clamp(float64(time.Since(s.startTime)) / float64(s.duration))
  57. }
  58. func (s *Scene) positionFac(i, len int) float64 {
  59. return clamp(float64(i) / float64(len))
  60. }
  61. func clamp(fac float64) float64 {
  62. if fac > 1.00 {
  63. return 1.00
  64. } else if fac < 0.00 {
  65. return 0.00
  66. } else {
  67. return fac
  68. }
  69. }