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.

86 lines
1.8 KiB

  1. package events
  2. import "fmt"
  3. type TemperatureChanged struct {
  4. ID string
  5. Temperature float64
  6. }
  7. func (e TemperatureChanged) TriggerKind() string {
  8. return "TemperatureChanged"
  9. }
  10. func (e TemperatureChanged) TriggerValue(key string) (string, bool) {
  11. switch key {
  12. case "temperature":
  13. return fmt.Sprintf("%.2f", e.Temperature), true
  14. case "id":
  15. return e.ID, true
  16. default:
  17. return "", false
  18. }
  19. }
  20. func (e TemperatureChanged) EventDescription() string {
  21. return fmt.Sprintf("TemperatureChanged(id=%s, temperature=%.2f)", e.ID, e.Temperature)
  22. }
  23. type MotionSensed struct {
  24. ID string
  25. SecondsSince float64
  26. }
  27. func (e MotionSensed) TriggerKind() string {
  28. return "MotionSensed"
  29. }
  30. func (e MotionSensed) TriggerValue(key string) (string, bool) {
  31. switch key {
  32. case "secondsSince":
  33. return fmt.Sprintf("%.1f", e.SecondsSince), true
  34. case "id":
  35. return e.ID, true
  36. default:
  37. return "", false
  38. }
  39. }
  40. func (e MotionSensed) EventDescription() string {
  41. return fmt.Sprintf("MotionSensed(id=%s, secondsSince=%.2f)", e.ID, e.SecondsSince)
  42. }
  43. type ButtonPressed struct {
  44. ID string `json:"id"`
  45. SwipedID string `json:"swipedId,omitempty"`
  46. Name string `json:"name"`
  47. }
  48. func (e ButtonPressed) EventDescription() string {
  49. if e.SwipedID != "" {
  50. return fmt.Sprintf("ButtonPressed(name=%s, swipe=%s->%s)", e.Name, e.SwipedID, e.ID)
  51. } else {
  52. return fmt.Sprintf("ButtonPressed(name=%s, id=%s)", e.Name, e.ID)
  53. }
  54. }
  55. func (e ButtonPressed) TriggerKind() string {
  56. return "ButtonPressed:" + e.Name
  57. }
  58. func (e ButtonPressed) TriggerValue(key string) (string, bool) {
  59. switch key {
  60. case "name":
  61. return e.Name, true
  62. case "id":
  63. return e.ID, true
  64. case "swipedFromId", "swipedId":
  65. if e.SwipedID != "" {
  66. return e.SwipedID, true
  67. } else {
  68. return "", false
  69. }
  70. default:
  71. return "", false
  72. }
  73. }