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.

119 lines
3.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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. 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]interface{} `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. DCPresence DeviceCapability = "Presence"
  49. DCIntensity DeviceCapability = "Intensity"
  50. DCTemperatureControl DeviceCapability = "TemperatureControl"
  51. DCTemperatureSensor DeviceCapability = "TemperatureSensor"
  52. )
  53. var Capabilities = []DeviceCapability{
  54. DCPower,
  55. DCColorHS,
  56. DCColorKelvin,
  57. DCButtons,
  58. DCPresence,
  59. DCIntensity,
  60. DCTemperatureControl,
  61. DCTemperatureSensor,
  62. }
  63. func (d *Device) Validate() error {
  64. d.Name = strings.Trim(d.Name, " \t\n ")
  65. if d.Name == "" {
  66. return ErrInvalidName
  67. }
  68. newCaps := make([]DeviceCapability, 0, len(d.Capabilities))
  69. for _, currCap := range d.Capabilities {
  70. for _, validCap := range Capabilities {
  71. if currCap == validCap {
  72. newCaps = append(newCaps, currCap)
  73. break
  74. }
  75. }
  76. }
  77. d.Capabilities = newCaps
  78. return nil
  79. }
  80. func (d *Device) HasCapability(capacity DeviceCapability) bool {
  81. for _, c := range d.Capabilities {
  82. if c == capacity {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. func (d *Device) SetState(newState NewDeviceState) error {
  89. if newState.Power != nil && d.HasCapability(DCPower) {
  90. d.State.Power = *newState.Power
  91. }
  92. if newState.Color != nil {
  93. parsed, err := ParseColorValue(*newState.Color)
  94. if err != nil {
  95. return err
  96. }
  97. if (parsed.IsKelvin() && d.HasCapability(DCColorKelvin)) || (parsed.IsHueSat() && d.HasCapability(DCColorHS)) {
  98. d.State.Color = parsed
  99. }
  100. }
  101. return nil
  102. }