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.

113 lines
3.5 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. func (e HardwareState) VerboseKey() string {
  41. return "HardwareState"
  42. }
  43. // HardwareMetadata contains things that has no bearing on the functionality of
  44. // lucifer, but may be interesting to have in the GUI.
  45. type HardwareMetadata struct {
  46. ID string `json:"id"`
  47. X int `json:"x,omitempty"`
  48. Y int `json:"y,omitempty"`
  49. O int `json:"o,omitempty"`
  50. ShapeType string `json:"shapeType,omitempty"`
  51. Icon string `json:"icon,omitempty"`
  52. SerialNumber string `json:"serialNumber,omitempty"`
  53. FirmwareVersion string `json:"firmwareVersion,omitempty"`
  54. Stale bool `json:"stale,omitempty"`
  55. }
  56. func (e HardwareMetadata) EventDescription() string {
  57. return fmt.Sprintf("HardwareMetadata(id:%s, icon:%s, ...)", e.ID, e.Icon)
  58. }
  59. func (e HardwareMetadata) VerboseKey() string {
  60. return "HardwareMetadata"
  61. }
  62. // DeviceReady is triggered to indicate that set-state commands will be heeded for the device
  63. // by this ID. It may be unavailable, however.
  64. type DeviceReady struct {
  65. ID string `json:"id"`
  66. }
  67. func (d DeviceReady) EventDescription() string {
  68. return fmt.Sprintf("DeviceReady(id:%s)", d.ID)
  69. }
  70. type DeviceFailed struct {
  71. ID string `json:"id"`
  72. Error string `json:"error"`
  73. }
  74. func (e DeviceFailed) EventDescription() string {
  75. return fmt.Sprintf("DeviceFailed(id:%s, err:%s)", e.ID, e.Error)
  76. }
  77. type DeviceAccepted struct {
  78. ID string `json:"id"`
  79. APIKey string `json:"apiKey"`
  80. Extras map[string]string `json:"extras"`
  81. }
  82. func (e DeviceAccepted) EventDescription() string {
  83. if os.Getenv("DEV_SHOW_API_KEYS") == "true" {
  84. return fmt.Sprintf("DeviceAccepted(id:%s, apiKey:%s)", e.ID, e.APIKey)
  85. } else {
  86. return fmt.Sprintf("DeviceAccepted(id:%s, apiKey:%s)", e.ID, formattools.Asterisks(e.APIKey))
  87. }
  88. }
  89. type DeviceForgotten struct {
  90. ID string `json:"id"`
  91. }
  92. func (e DeviceForgotten) EventDescription() string {
  93. return fmt.Sprintf("DeviceForgotten(id:%s)", e.ID)
  94. }