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.

115 lines
3.0 KiB

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