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.

122 lines
3.1 KiB

4 years ago
  1. package models
  2. import (
  3. "context"
  4. "strings"
  5. )
  6. type Device struct {
  7. ID int `json:"id"`
  8. BridgeID int `json:"bridgeID"`
  9. Icon string `json:"icon"`
  10. Name string `json:"name"`
  11. Capabilities []DeviceCapability `json:"capabilities"`
  12. Properties map[string]string `json:"properties"`
  13. State DeviceState `json:"state"`
  14. Tags []string `json:"tags"`
  15. }
  16. // DeviceState contains optional state values that
  17. // - Power: Whether the device is powered on
  18. // - Color: Color value, if a color setting can be set on the device
  19. // - Intensity: e.g. brightness, from 0-255
  20. // - Temperature: e.g. for thermostats
  21. type DeviceState struct {
  22. Power bool `json:"power"`
  23. Color ColorValue `json:"color,omitempty"`
  24. Intensity int `json:"intensity,omitempty"`
  25. Temperature int `json:"temperature"`
  26. }
  27. type NewDeviceState struct {
  28. Power *bool `json:"power"`
  29. Color *string `json:"color"`
  30. Intensity int `json:"intensity"`
  31. Temperature int `json:"temperature"`
  32. }
  33. type DeviceCapability string
  34. type DeviceRepository interface {
  35. FindByID(ctx context.Context, id int) (*Device, error)
  36. FetchByReference(ctx context.Context, kind ReferenceKind, value string) ([]Device, error)
  37. Save(ctx context.Context, device *Device) error
  38. Delete(ctx context.Context, device *Device) error
  39. }
  40. var (
  41. DCPower DeviceCapability = "Power"
  42. DCColorHS DeviceCapability = "ColorHS"
  43. DCColorKelvin DeviceCapability = "ColorKelvin"
  44. DCButtonDefault DeviceCapability = "ButtonDefault"
  45. DCButtonOn DeviceCapability = "ButtonOn"
  46. DCButtonOff DeviceCapability = "ButtonOff"
  47. DCButtonPlus DeviceCapability = "ButtonPlus"
  48. DCButtonMinus DeviceCapability = "ButtonMinus"
  49. DCButtonToggle DeviceCapability = "ButtonToggle"
  50. DCIntensity DeviceCapability = "Intensity"
  51. DCTemperature DeviceCapability = "Temperature"
  52. )
  53. var Capabilities = []DeviceCapability{
  54. DCPower,
  55. DCColorHS,
  56. DCColorKelvin,
  57. DCButtonDefault,
  58. DCButtonOn,
  59. DCButtonOff,
  60. DCButtonPlus,
  61. DCButtonMinus,
  62. DCButtonToggle,
  63. DCIntensity,
  64. DCTemperature,
  65. }
  66. func (d *Device) Validate() error {
  67. d.Name = strings.Trim(d.Name, " \t\n ")
  68. if d.Name == "" {
  69. return ErrInvalidName
  70. }
  71. newCaps := make([]DeviceCapability, 0, len(d.Capabilities))
  72. for _, currCap := range d.Capabilities {
  73. for _, validCap := range Capabilities {
  74. if currCap == validCap {
  75. newCaps = append(newCaps, currCap)
  76. break
  77. }
  78. }
  79. }
  80. d.Capabilities = newCaps
  81. return nil
  82. }
  83. func (d *Device) HasCapability(capacity DeviceCapability) bool {
  84. for _, c := range d.Capabilities {
  85. if c == capacity {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. func (d *Device) SetState(newState NewDeviceState) error {
  92. if newState.Power != nil && d.HasCapability(DCPower) {
  93. d.State.Power = *newState.Power
  94. }
  95. if newState.Color != nil {
  96. parsed, err := ParseColorValue(*newState.Color)
  97. if err != nil {
  98. return err
  99. }
  100. if (parsed.IsKelvin() && d.HasCapability(DCColorKelvin)) || (parsed.IsHueSat() && d.HasCapability(DCColorHS)) {
  101. d.State.Color = parsed
  102. }
  103. }
  104. return nil
  105. }