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.

66 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package models
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. type ReferenceKind string
  7. var (
  8. RKDeviceID ReferenceKind = "DeviceID"
  9. RKBridgeID ReferenceKind = "BridgeID"
  10. RKTag ReferenceKind = "Tag"
  11. RKAll ReferenceKind = "All"
  12. RKName ReferenceKind = "Name"
  13. )
  14. func (rk ReferenceKind) Matches(device *Device, value string) bool {
  15. switch rk {
  16. case RKName:
  17. if strings.HasPrefix(value, "*") {
  18. return strings.HasSuffix(device.Name, value[1:])
  19. } else if strings.HasSuffix(value, "*") {
  20. return strings.HasPrefix(device.Name, value[:len(value)-1])
  21. } else {
  22. return device.Name == value
  23. }
  24. case RKDeviceID:
  25. idStr := strconv.Itoa(device.ID)
  26. for _, idStr2 := range strings.Split(value, ",") {
  27. if idStr == idStr2 {
  28. return true
  29. }
  30. }
  31. case RKBridgeID:
  32. idStr := strconv.Itoa(device.BridgeID)
  33. for _, idStr2 := range strings.Split(value, ",") {
  34. if idStr == idStr2 {
  35. return true
  36. }
  37. }
  38. case RKTag:
  39. hadAny := false
  40. for _, tag := range strings.Split(value, ",") {
  41. if strings.HasPrefix(tag, "-") {
  42. if device.HasTag(tag[1:]) {
  43. return false
  44. }
  45. } else if strings.HasPrefix(tag, "+") {
  46. if !device.HasTag(tag[1:]) {
  47. return false
  48. }
  49. } else {
  50. if device.HasTag(tag) {
  51. hadAny = true
  52. }
  53. }
  54. }
  55. return hadAny
  56. case RKAll:
  57. return true
  58. }
  59. return false
  60. }