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.

172 lines
4.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
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. type DeviceUpdate struct {
  20. Icon *string `json:"icon"`
  21. Name *string `json:"name"`
  22. UserProperties map[string]*string `json:"userProperties"`
  23. }
  24. // DeviceState contains optional state values that
  25. // - Power: Whether the device is powered on
  26. // - Color: Color value, if a color setting can be set on the device
  27. // - Intensity: e.g. brightness, range 0..=1
  28. // - Temperature: e.g. for thermostats
  29. type DeviceState struct {
  30. Power bool `json:"power"`
  31. Color ColorValue `json:"color,omitempty"`
  32. Intensity float64 `json:"intensity,omitempty"`
  33. Temperature float64 `json:"temperature"`
  34. }
  35. type NewDeviceState struct {
  36. Power *bool `json:"power"`
  37. Color *string `json:"color"`
  38. Intensity *float64 `json:"intensity"`
  39. Temperature *int `json:"temperature"`
  40. }
  41. type DeviceCapability string
  42. type DeviceRepository interface {
  43. Find(ctx context.Context, id int) (*Device, error)
  44. FetchByReference(ctx context.Context, kind ReferenceKind, value string) ([]Device, error)
  45. Save(ctx context.Context, device *Device) error
  46. Delete(ctx context.Context, device *Device) error
  47. }
  48. func DeviceCapabilitiesToStrings(caps []DeviceCapability) []string {
  49. res := make([]string, 0, len(caps))
  50. for _, cap := range caps {
  51. res = append(res, string(cap))
  52. }
  53. return res
  54. }
  55. var (
  56. DCPower DeviceCapability = "Power"
  57. DCColorHS DeviceCapability = "ColorHS"
  58. DCColorHSK DeviceCapability = "ColorHSK"
  59. DCColorKelvin DeviceCapability = "ColorKelvin"
  60. DCButtons DeviceCapability = "Buttons"
  61. DCPresence DeviceCapability = "Presence"
  62. DCIntensity DeviceCapability = "Intensity"
  63. DCTemperatureControl DeviceCapability = "TemperatureControl"
  64. DCTemperatureSensor DeviceCapability = "TemperatureSensor"
  65. )
  66. var Capabilities = []DeviceCapability{
  67. DCPower,
  68. DCColorHS,
  69. DCColorKelvin,
  70. DCButtons,
  71. DCPresence,
  72. DCIntensity,
  73. DCTemperatureControl,
  74. DCTemperatureSensor,
  75. }
  76. func (d *Device) ApplyUpdate(update DeviceUpdate) {
  77. if update.Name != nil {
  78. d.Name = *update.Name
  79. }
  80. if update.Icon != nil {
  81. d.Icon = *update.Icon
  82. }
  83. if d.UserProperties == nil {
  84. d.UserProperties = make(map[string]string)
  85. }
  86. for key, value := range update.UserProperties {
  87. if value != nil {
  88. d.UserProperties[key] = *value
  89. } else {
  90. delete(d.UserProperties, key)
  91. }
  92. }
  93. }
  94. func (d *Device) Validate() error {
  95. d.Name = strings.Trim(d.Name, " \t\n ")
  96. if d.Name == "" {
  97. return ErrInvalidName
  98. }
  99. newCaps := make([]DeviceCapability, 0, len(d.Capabilities))
  100. for _, currCap := range d.Capabilities {
  101. for _, validCap := range Capabilities {
  102. if currCap == validCap {
  103. newCaps = append(newCaps, currCap)
  104. break
  105. }
  106. }
  107. }
  108. d.Capabilities = newCaps
  109. return nil
  110. }
  111. func (d *Device) HasTag(tags ...string) bool {
  112. for _, c := range d.Tags {
  113. for _, c2 := range tags {
  114. if c == c2 {
  115. return true
  116. }
  117. }
  118. }
  119. return false
  120. }
  121. func (d *Device) HasCapability(capabilities ...DeviceCapability) bool {
  122. for _, c := range d.Capabilities {
  123. for _, c2 := range capabilities {
  124. if c == c2 {
  125. return true
  126. }
  127. }
  128. }
  129. return false
  130. }
  131. func (d *Device) SetState(newState NewDeviceState) error {
  132. if newState.Power != nil && d.HasCapability(DCPower) {
  133. d.State.Power = *newState.Power
  134. }
  135. if newState.Color != nil {
  136. parsed, err := ParseColorValue(*newState.Color)
  137. if err != nil {
  138. return err
  139. }
  140. if (parsed.IsKelvin() && d.HasCapability(DCColorKelvin, DCColorHSK)) || (parsed.IsHueSat() && d.HasCapability(DCColorHS)) {
  141. d.State.Color = parsed
  142. }
  143. }
  144. if newState.Intensity != nil && d.HasCapability(DCIntensity) {
  145. d.State.Intensity = *newState.Intensity
  146. }
  147. return nil
  148. }