hdsv2.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package hdsv2
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "imuslab.com/arozos/mod/iot"
  11. "imuslab.com/arozos/mod/network/mdns"
  12. )
  13. /*
  14. Home Dynamic 2 Controller
  15. This is a module that handles HDSv2 protocol devices scannings
  16. */
  17. type Handler struct {
  18. scanner *mdns.MDNSHost
  19. historyList []*iot.Device
  20. lastScanTime int64
  21. }
  22. //Create a new HDSv2 Protocol Handler
  23. func NewProtocolHandler(scanner *mdns.MDNSHost) *Handler {
  24. //Create a new MDNS Host
  25. return &Handler{
  26. scanner,
  27. []*iot.Device{},
  28. 0,
  29. }
  30. }
  31. func (h *Handler) Start() error {
  32. log.Println("*IoT* Home Dynamic v2 Loaded")
  33. return nil
  34. }
  35. //Scan the devices within the LAN
  36. func (h *Handler) Scan() ([]*iot.Device, error) {
  37. foundDevices := []*iot.Device{}
  38. hosts := h.scanner.Scan(3, "hds.arozos.com")
  39. for _, host := range hosts {
  40. thisDevice := iot.Device{
  41. Name: host.HostName,
  42. Port: host.Port,
  43. Model: host.Model,
  44. Version: host.BuildVersion + "-" + host.MinorVersion,
  45. Manufacturer: host.Vendor,
  46. DeviceUUID: host.UUID,
  47. IPAddr: host.IPv4[0].String(),
  48. RequireAuth: false,
  49. RequireConnect: false,
  50. Status: map[string]interface{}{},
  51. }
  52. //Try to get the device status
  53. status, err := getStatusForDevice(&thisDevice)
  54. if err != nil {
  55. //This might be not a valid HDSv2 device. Skip this
  56. log.Println("*HDSv2* Get status failed for device: ", host.HostName, err.Error())
  57. continue
  58. }
  59. thisDevice.Status = status
  60. //Get the device content endpoints
  61. eps, err := getEndpoints(&thisDevice)
  62. if err != nil {
  63. //This might be not a valid HDSv2 device. Skip this
  64. log.Println("*HDSv2* Get endpoints failed for device: ", host.HostName, err.Error())
  65. continue
  66. }
  67. thisDevice.ControlEndpoints = eps
  68. //Push this host into found device list
  69. foundDevices = append(foundDevices, &thisDevice)
  70. }
  71. h.historyList = foundDevices
  72. return foundDevices, nil
  73. }
  74. //Return the history scan list from the handler
  75. func (h *Handler) List() ([]*iot.Device, error) {
  76. return h.historyList, nil
  77. }
  78. //Home Dynamic system's devices no need to established conenction before executing anything
  79. func (h *Handler) Connect(device *iot.Device, authInfo *iot.AuthInfo) error {
  80. return nil
  81. }
  82. //Same rules also apply to disconnect
  83. func (h *Handler) Disconnect(device *iot.Device) error {
  84. return nil
  85. }
  86. //Get the status of the device
  87. func (h *Handler) Status(device *iot.Device) (map[string]interface{}, error) {
  88. return getStatusForDevice(device)
  89. }
  90. //Get the status of the device
  91. func (h *Handler) Execute(device *iot.Device, endpoint *iot.Endpoint, payload interface{}) (interface{}, error) {
  92. var result interface{}
  93. return result, nil
  94. }
  95. func (h *Handler) Stats() iot.Stats {
  96. return iot.Stats{
  97. Name: "Home Dynamic v2",
  98. Desc: "A basic IoT communication protocol for ESP8266 made by Makers",
  99. Version: "2.0",
  100. ProtocolVer: "2.0",
  101. Author: "tobychui",
  102. AuthorWebsite: "http://arozos.com",
  103. AuthorEmail: "[email protected]",
  104. ReleaseDate: 1614524498,
  105. }
  106. }
  107. //Get endpoint of the given device object
  108. func getEndpoints(device *iot.Device) ([]*iot.Endpoint, error) {
  109. //Parse the URL of the endpoint apis location (eps)
  110. requestURL := "http://" + device.IPAddr + ":" + strconv.Itoa(device.Port) + "/eps"
  111. resp, err := http.Get(requestURL)
  112. if err != nil {
  113. return nil, err
  114. }
  115. //Get the body content
  116. content, err := ioutil.ReadAll(resp.Body)
  117. if err != nil {
  118. return nil, err
  119. }
  120. //Convert the results to Endpoints
  121. endpoints := []iot.Endpoint{}
  122. err = json.Unmarshal(content, &endpoints)
  123. if err != nil {
  124. return nil, err
  125. }
  126. //Convert the structure to array pointers
  127. results := []*iot.Endpoint{}
  128. for _, ep := range endpoints {
  129. thisEp := ep
  130. results = append(results, &thisEp)
  131. }
  132. return results, nil
  133. }
  134. //Get status given the device object.
  135. func getStatusForDevice(device *iot.Device) (map[string]interface{}, error) {
  136. //Parse the URL for its status api endpoint
  137. requestURL := "http://" + device.IPAddr + ":" + strconv.Itoa(device.Port) + "/status"
  138. resp, err := http.Get(requestURL)
  139. if err != nil {
  140. return map[string]interface{}{}, err
  141. }
  142. //Get the body content
  143. content, err := ioutil.ReadAll(resp.Body)
  144. if err != nil {
  145. return map[string]interface{}{}, err
  146. }
  147. //Check if the resp is json
  148. if !isJSON(strings.TrimSpace(string(content))) {
  149. return map[string]interface{}{}, errors.New("Invalid HDSv2 protocol")
  150. }
  151. //Ok. Parse it
  152. status := map[string]interface{}{}
  153. err = json.Unmarshal(content, &status)
  154. if err != nil {
  155. return map[string]interface{}{}, err
  156. }
  157. return status, nil
  158. }