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.

88 lines
2.6 KiB

  1. package events
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer3/server/device"
  5. "git.aiterp.net/lucifer3/server/internal/gentools"
  6. "strings"
  7. )
  8. type DeviceHardwareStateChange struct {
  9. ID int64 `json:"id,omitempty"`
  10. InternalID string `json:"internalId"`
  11. DeviceFlags device.SupportFlags `json:"deviceFlags"`
  12. ColorFlags device.ColorFlag `json:"colorFlags"`
  13. State device.State `json:"state"`
  14. }
  15. func (d DeviceHardwareStateChange) EventName() string {
  16. idStr := ""
  17. if d.ID != 0 {
  18. idStr = fmt.Sprintf("id:%d, ", d.ID)
  19. }
  20. return fmt.Sprintf("DeviceHardwareStateChange(%siid:%s, dflags:%d, cflags:%d, state:%s)",
  21. idStr, d.InternalID, d.DeviceFlags, d.ColorFlags, d.State,
  22. )
  23. }
  24. // DeviceRegister must be sent directly to the device hub that took it.
  25. type DeviceRegister struct {
  26. ID int64 `json:"id"`
  27. InternalID string `json:"internalId"`
  28. }
  29. func (d DeviceRegister) EventName() string {
  30. return fmt.Sprintf("DeviceRegister(id:%d, iid:%s)", d.ID, d.InternalID)
  31. }
  32. type DeviceDesiredStateChange struct {
  33. ID int64 `json:"id"`
  34. NewState device.State `json:"newState"`
  35. }
  36. func (d DeviceDesiredStateChange) EventName() string {
  37. return fmt.Sprintf("DeviceDesiredStateChange(id:%d, state:%s)",
  38. d.ID, d.NewState,
  39. )
  40. }
  41. type DeviceAssignment struct {
  42. IDs []int64 `json:"ids"`
  43. Version int64 `json:"version"` // DeviceManager sets the version.
  44. State *device.State `json:"state"` // DeviceManager sets the state in a follow-up event. Others still need to see it to kick the device.
  45. Effect *int64 `json:"effect"` // An effect will pick this up. A scene may defer to own effects
  46. Scene *int64 `json:"scene"` // A scene will take it and handle it. Might move it out a level so scenes are just filters that create assignments
  47. }
  48. func (d DeviceAssignment) EventName() string {
  49. s := "(!!no change!!)"
  50. switch {
  51. case d.State != nil:
  52. s = fmt.Sprintf("state:%s", *d.State)
  53. case d.Effect != nil:
  54. s = fmt.Sprintf("effect:%d", *d.Effect)
  55. case d.Scene != nil:
  56. s = fmt.Sprintf("scene:%d", *d.Scene)
  57. }
  58. return fmt.Sprintf("DeviceAssignment(ids:%s, version:%d, %s)",
  59. strings.Join(gentools.FmtSprintArray(d.IDs), ","),
  60. d.Version,
  61. s,
  62. )
  63. }
  64. // DeviceRestore attempts to restore devices to a previous version. It may not be
  65. // successful.
  66. type DeviceRestore struct {
  67. IDs []int64 `json:"ids"`
  68. Version int64 `json:"version"`
  69. }
  70. func (d DeviceRestore) EventName() string {
  71. return fmt.Sprintf("DeviceRestore(ids:%s, version:%d)",
  72. strings.Join(gentools.FmtSprintArray(d.IDs), ","),
  73. d.Version,
  74. )
  75. }