hds.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package hds
  2. import (
  3. "errors"
  4. "log"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "imuslab.com/arozos/mod/iot"
  9. )
  10. /*
  11. Home Dynamic System
  12. Author: tobychui
  13. This is a compaitbility module for those who are still using HDSv1 protocol
  14. If you are still using HDSv1, you should consider upgrading it to the latest
  15. version of HDS protocols.
  16. */
  17. type Handler struct {
  18. lastScanTime int64
  19. }
  20. //Create a new HDS Protocol Handler
  21. func NewProtocolHandler() *Handler {
  22. //Create a new MDNS Host
  23. return &Handler{}
  24. }
  25. //Start the HDSv1 scanner, which no startup process is required
  26. func (h *Handler) Start() error {
  27. log.Println("*IoT* Home Dynamic System (Legacy) Loaded")
  28. return nil
  29. }
  30. //Scan all the HDS devices in LAN using the legacy ip scanner methods
  31. func (h *Handler) Scan() ([]*iot.Device, error) {
  32. //Get the current local IP address
  33. ip := getLocalIP()
  34. if ip == "" {
  35. //Not connected to a network?
  36. return nil, errors.New("Unable to get ip address of host device")
  37. }
  38. //Only handle a small subset of ip ranges (Last 255 addfress)
  39. scanBase := ""
  40. valid := false
  41. if ip[:8] == "192.168." {
  42. tmp := strings.Split(ip, ".")
  43. tmp = tmp[:len(tmp)-1]
  44. scanBase = strings.Join(tmp, ".") + "." //Return 192.168.0.
  45. valid = true
  46. } else if ip[:4] == "172." {
  47. //Handle 172.16.x.x - 172.31.x.x
  48. tmp := strings.Split(ip, ".")
  49. val, err := strconv.Atoi(tmp[1])
  50. if err == nil {
  51. if val >= 16 && val <= 31 {
  52. //Private addr range
  53. valid = true
  54. scanBase = strings.Join(tmp, ".") + "." //Return 172.16.0.
  55. }
  56. }
  57. }
  58. //Check if the IP range is supported by HDS protocol
  59. if !valid {
  60. log.Println("*IoT* Home Dynamic Protocol requirement not satisfied. Skipping Scan")
  61. return nil, nil
  62. }
  63. //Create a IP scanner
  64. var wg sync.WaitGroup
  65. for i := 1; i < 256; i++ {
  66. wg.Add(1)
  67. go tryGetHDSUUID(&wg, scanBase+strconv.Itoa(i))
  68. }
  69. log.Println("*IoT* Home Dynamic waiting for responses")
  70. wg.Wait()
  71. log.Println("*IoT* Home Dynamic - DONE")
  72. return nil, nil
  73. }
  74. //Home Dynamic system's devices no need to established conenction before executing anything
  75. func (h *Handler) Connect(device *iot.Device, authInfo *iot.AuthInfo) error {
  76. return nil
  77. }
  78. //Same rules also apply to disconnect
  79. func (h *Handler) Disconnect(device *iot.Device) error {
  80. return nil
  81. }
  82. //Get the status of the device
  83. func (h *Handler) Execute(device *iot.Device, endpoint *iot.Endpoint, payload interface{}) (interface{}, error) {
  84. var result interface{}
  85. return result, nil
  86. }
  87. //Get the status of the device
  88. func (h *Handler) Status(device *iot.Device) (map[string]interface{}, error) {
  89. resp, err := tryGet("http://" + device.IPAddr + "/status")
  90. if err != nil {
  91. return map[string]interface{}{}, err
  92. }
  93. //Convert the resp into map string itnerface
  94. result := map[string]interface{}{}
  95. result["status"] = resp
  96. return result, nil
  97. }
  98. //Return the specification of this protocol handler
  99. func (h *Handler) Stats() iot.Stats {
  100. return iot.Stats{
  101. Name: "Home Dynamic",
  102. Desc: "A basic IoT communication protocol for ESP8266 for ArOZ Online Beta",
  103. Version: "1.0",
  104. ProtocolVer: "1.0",
  105. Author: "tobychui",
  106. AuthorWebsite: "https://git.hkwtc.org/TC/HomeDynamic",
  107. AuthorEmail: "",
  108. ReleaseDate: 1576094199,
  109. }
  110. }