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.

105 lines
3.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package events
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer3/server/device"
  5. "git.aiterp.net/lucifer3/server/internal/color"
  6. "git.aiterp.net/lucifer3/server/internal/formattools"
  7. "os"
  8. )
  9. type DeviceConnected struct {
  10. Prefix string `json:"prefix"`
  11. }
  12. func (e DeviceConnected) EventDescription() string {
  13. return fmt.Sprintf("DeviceConnected(prefix:%s)", e.Prefix)
  14. }
  15. type DeviceDisconnected struct {
  16. Prefix string `json:"prefix"`
  17. Reason string `json:"reason"`
  18. }
  19. func (e DeviceDisconnected) EventDescription() string {
  20. return fmt.Sprintf("DeviceDisconnected(prefix:%s, reason:%s)", e.Prefix, e.Reason)
  21. }
  22. type HardwareState struct {
  23. ID string `json:"id"`
  24. InternalName string `json:"internalName"`
  25. SupportFlags device.SupportFlags `json:"supportFlags"`
  26. ColorFlags device.ColorFlags `json:"colorFlags"`
  27. ColorGamut *color.Gamut `json:"colorGamut,omitempty"`
  28. ColorKelvinRange *[2]int `json:"colorKelvinRange,omitempty"`
  29. Buttons []string `json:"buttons"`
  30. State device.State `json:"state"`
  31. BatteryPercentage *int `json:"batteryPercentage"`
  32. Unreachable bool `json:"unreachable"`
  33. Stale bool `json:"stale,omitempty"`
  34. }
  35. func (e HardwareState) EventDescription() string {
  36. return fmt.Sprintf("HardwareState(id:%s, iname:%#+v, sflags:%s, cflags:%s, buttons:%v, state:%s, unreachable:%t)",
  37. e.ID, e.InternalName, e.SupportFlags, e.ColorFlags, e.Buttons, e.State, e.Unreachable,
  38. )
  39. }
  40. // HardwareMetadata contains things that has no bearing on the functionality of
  41. // lucifer, but may be interesting to have in the GUI.
  42. type HardwareMetadata struct {
  43. ID string `json:"id"`
  44. X int `json:"x,omitempty"`
  45. Y int `json:"y,omitempty"`
  46. O int `json:"o,omitempty"`
  47. ShapeType string `json:"shapeType,omitempty"`
  48. Icon string `json:"icon,omitempty"`
  49. SerialNumber string `json:"serialNumber,omitempty"`
  50. FirmwareVersion string `json:"firmwareVersion,omitempty"`
  51. Stale bool `json:"stale,omitempty"`
  52. }
  53. func (e HardwareMetadata) EventDescription() string {
  54. return fmt.Sprintf("HardwareMetadata(id:%s, icon:%s, ...)", e.ID, e.Icon)
  55. }
  56. // DeviceReady is triggered to indicate that set-state commands will be heeded for the device
  57. // by this ID. It may be unavailable, however.
  58. type DeviceReady struct {
  59. ID string `json:"id"`
  60. }
  61. func (d DeviceReady) EventDescription() string {
  62. return fmt.Sprintf("DeviceReady(id:%s)", d.ID)
  63. }
  64. type DeviceFailed struct {
  65. ID string `json:"id"`
  66. Error string `json:"error"`
  67. }
  68. func (e DeviceFailed) EventDescription() string {
  69. return fmt.Sprintf("DeviceFailed(id:%s, err:%s)", e.ID, e.Error)
  70. }
  71. type DeviceAccepted struct {
  72. ID string `json:"id"`
  73. APIKey string `json:"apiKey"`
  74. Extras map[string]string `json:"extras"`
  75. }
  76. func (e DeviceAccepted) EventDescription() string {
  77. if os.Getenv("DEV_SHOW_API_KEYS") == "true" {
  78. return fmt.Sprintf("DeviceAccepted(id:%s, apiKey:%s)", e.ID, e.APIKey)
  79. } else {
  80. return fmt.Sprintf("DeviceAccepted(id:%s, apiKey:%s)", e.ID, formattools.Asterisks(e.APIKey))
  81. }
  82. }
  83. type DeviceForgotten struct {
  84. ID string `json:"id"`
  85. }
  86. func (e DeviceForgotten) EventDescription() string {
  87. return fmt.Sprintf("DeviceForgotten(id:%s)", e.ID)
  88. }