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.

116 lines
3.1 KiB

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