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.

90 lines
1.6 KiB

  1. package mill
  2. import (
  3. "fmt"
  4. lucifer3 "git.aiterp.net/lucifer3/server"
  5. "git.aiterp.net/lucifer3/server/device"
  6. "git.aiterp.net/lucifer3/server/events"
  7. "strings"
  8. )
  9. type Bridge interface {
  10. // SetBus to apply a bus that changes will be sent to
  11. SetBus(bus *lucifer3.EventBus)
  12. // SetState updates device state
  13. SetState(id string, state device.State) bool
  14. // Start triggers the event emitting part, including device discovery
  15. Start()
  16. // IsStarted returns true when it's running
  17. IsStarted() bool
  18. }
  19. func MakeBridge(id, apiKey string) (Bridge, bool) {
  20. parts := strings.Split(id, ":")
  21. if len(parts) < 3 {
  22. return nil, false
  23. }
  24. driver := parts[0]
  25. version := parts[1]
  26. target := parts[2]
  27. if driver != "mill" {
  28. return nil, false
  29. }
  30. id = fmt.Sprintf("%s:%s:%s", driver, version, target)
  31. switch version {
  32. case "2":
  33. return &OnlineBridge{
  34. ID: id,
  35. Email: target,
  36. Password: apiKey,
  37. Gen2: true,
  38. Gen3: false,
  39. }, true
  40. case "23":
  41. return &OnlineBridge{
  42. ID: id,
  43. Email: target,
  44. Password: apiKey,
  45. Gen2: true,
  46. Gen3: true,
  47. }, true
  48. case "3":
  49. if strings.Contains(target, "@") {
  50. return &OnlineBridge{
  51. ID: id,
  52. Email: target,
  53. Password: apiKey,
  54. Gen2: false,
  55. Gen3: true,
  56. }, true
  57. } else {
  58. return &WifiBridge{
  59. ID: id,
  60. IP: target,
  61. }, true
  62. }
  63. }
  64. return nil, false
  65. }
  66. func deviceFailed(id string, err any) events.DeviceFailed {
  67. msg := "(no message)"
  68. switch typed := err.(type) {
  69. case string:
  70. msg = typed
  71. case error:
  72. msg = typed.Error()
  73. }
  74. return events.DeviceFailed{
  75. ID: id,
  76. Error: msg,
  77. }
  78. }