hdsv2.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. Handler: h,
  52. }
  53. //Try to get the device status
  54. status, err := getStatusForDevice(&thisDevice)
  55. if err != nil {
  56. //This might be not a valid HDSv2 device. Skip this
  57. log.Println("*HDSv2* Get status failed for device: ", host.HostName, err.Error())
  58. continue
  59. }
  60. thisDevice.Status = status
  61. //Get the device content endpoints
  62. eps, err := getEndpoints(&thisDevice)
  63. if err != nil {
  64. //This might be not a valid HDSv2 device. Skip this
  65. log.Println("*HDSv2* Get endpoints failed for device: ", host.HostName, err.Error())
  66. continue
  67. }
  68. thisDevice.ControlEndpoints = eps
  69. //Push this host into found device list
  70. foundDevices = append(foundDevices, &thisDevice)
  71. }
  72. h.historyList = foundDevices
  73. return foundDevices, nil
  74. }
  75. //Return the history scan list from the handler
  76. func (h *Handler) List() ([]*iot.Device, error) {
  77. return h.historyList, nil
  78. }
  79. //Home Dynamic system's devices no need to established conenction before executing anything
  80. func (h *Handler) Connect(device *iot.Device, authInfo *iot.AuthInfo) error {
  81. return nil
  82. }
  83. //Same rules also apply to disconnect
  84. func (h *Handler) Disconnect(device *iot.Device) error {
  85. return nil
  86. }
  87. //Get the status of the device
  88. func (h *Handler) Status(device *iot.Device) (map[string]interface{}, error) {
  89. return getStatusForDevice(device)
  90. }
  91. //Get the status of the device
  92. func (h *Handler) Execute(device *iot.Device, endpoint *iot.Endpoint, payload interface{}) (interface{}, error) {
  93. var result interface{}
  94. return result, nil
  95. }
  96. func (h *Handler) Stats() iot.Stats {
  97. return iot.Stats{
  98. Name: "Home Dynamic v2",
  99. Desc: "A basic IoT communication protocol for ESP8266 made by Makers",
  100. Version: "2.0",
  101. ProtocolVer: "2.0",
  102. Author: "tobychui",
  103. AuthorWebsite: "http://arozos.com",
  104. AuthorEmail: "[email protected]",
  105. ReleaseDate: 1614524498,
  106. }
  107. }
  108. //Get endpoint of the given device object
  109. func getEndpoints(device *iot.Device) ([]*iot.Endpoint, error) {
  110. //Parse the URL of the endpoint apis location (eps)
  111. requestURL := "http://" + device.IPAddr + ":" + strconv.Itoa(device.Port) + "/eps"
  112. resp, err := http.Get(requestURL)
  113. if err != nil {
  114. return nil, err
  115. }
  116. //Get the body content
  117. content, err := ioutil.ReadAll(resp.Body)
  118. if err != nil {
  119. return nil, err
  120. }
  121. //Convert the results to Endpoints
  122. endpoints := []iot.Endpoint{}
  123. err = json.Unmarshal(content, &endpoints)
  124. if err != nil {
  125. return nil, err
  126. }
  127. //Convert the structure to array pointers
  128. results := []*iot.Endpoint{}
  129. for _, ep := range endpoints {
  130. thisEp := ep
  131. results = append(results, &thisEp)
  132. }
  133. return results, nil
  134. }
  135. //Get status given the device object.
  136. func getStatusForDevice(device *iot.Device) (map[string]interface{}, error) {
  137. //Parse the URL for its status api endpoint
  138. requestURL := "http://" + device.IPAddr + ":" + strconv.Itoa(device.Port) + "/status"
  139. resp, err := http.Get(requestURL)
  140. if err != nil {
  141. return map[string]interface{}{}, err
  142. }
  143. //Get the body content
  144. content, err := ioutil.ReadAll(resp.Body)
  145. if err != nil {
  146. return map[string]interface{}{}, err
  147. }
  148. //Check if the resp is json
  149. if !isJSON(strings.TrimSpace(string(content))) {
  150. return map[string]interface{}{}, errors.New("Invalid HDSv2 protocol")
  151. }
  152. //Ok. Parse it
  153. status := map[string]interface{}{}
  154. err = json.Unmarshal(content, &status)
  155. if err != nil {
  156. return map[string]interface{}{}, err
  157. }
  158. return status, nil
  159. }