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.3 KiB

4 years ago
4 years ago
4 years ago
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. DCColorHSK DeviceCapability = "ColorHSK"
  47. DCColorKelvin DeviceCapability = "ColorKelvin"
  48. DCButtons DeviceCapability = "Buttons"
  49. DCPresence DeviceCapability = "Presence"
  50. DCIntensity DeviceCapability = "Intensity"
  51. DCTemperatureControl DeviceCapability = "TemperatureControl"
  52. DCTemperatureSensor DeviceCapability = "TemperatureSensor"
  53. )
  54. var Capabilities = []DeviceCapability{
  55. DCPower,
  56. DCColorHS,
  57. DCColorKelvin,
  58. DCButtons,
  59. DCPresence,
  60. DCIntensity,
  61. DCTemperatureControl,
  62. DCTemperatureSensor,
  63. }
  64. func (d *Device) Validate() error {
  65. d.Name = strings.Trim(d.Name, " \t\n ")
  66. if d.Name == "" {
  67. return ErrInvalidName
  68. }
  69. newCaps := make([]DeviceCapability, 0, len(d.Capabilities))
  70. for _, currCap := range d.Capabilities {
  71. for _, validCap := range Capabilities {
  72. if currCap == validCap {
  73. newCaps = append(newCaps, currCap)
  74. break
  75. }
  76. }
  77. }
  78. d.Capabilities = newCaps
  79. return nil
  80. }
  81. func (d *Device) HasCapability(capabilities ...DeviceCapability) bool {
  82. for _, c := range d.Capabilities {
  83. for _, c2 := range capabilities {
  84. if c == c2 {
  85. return true
  86. }
  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, DCColorHSK)) || (parsed.IsHueSat() && d.HasCapability(DCColorHS)) {
  101. d.State.Color = parsed
  102. }
  103. }
  104. return nil
  105. }