actions.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package main
  2. import (
  3. "encoding/json"
  4. "sparrow/pkg/productconfig"
  5. "sparrow/pkg/rpcs"
  6. "net/http"
  7. "sparrow/pkg/models"
  8. "sparrow/pkg/server"
  9. "github.com/go-martini/martini"
  10. "github.com/martini-contrib/render"
  11. )
  12. const (
  13. ErrOK = 0
  14. ErrSystemFault = 10001
  15. ErrProductNotFound = 10002
  16. ErrDeviceNotFound = 10003
  17. ErrDeviceNotOnline = 10004
  18. ErrWrongRequestFormat = 10005
  19. ErrWrongProductConfig = 10006
  20. ErrWrongQueryFormat = 10007
  21. ErrAccessDenied = 10008
  22. )
  23. const (
  24. defaultTimeOut = 3 // seconds
  25. )
  26. func renderError(code int, err error) Common {
  27. result := Common{}
  28. result.Code = code
  29. result.Message = err.Error()
  30. server.Log.Error(err.Error())
  31. return result
  32. }
  33. func GetDeviceInfoByKey(params martini.Params, req *http.Request, r render.Render) {
  34. key := req.URL.Query().Get("device_key")
  35. server.Log.Printf("ACTION GetDeviceInfoByKey, key:: %v", key)
  36. device := &models.Device{}
  37. err := server.RPCCallByName("registry", "Registry.ValidateDevice", key, device)
  38. if err != nil {
  39. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  40. return
  41. }
  42. result := DeviceInfoResponse{
  43. Data: DeviceInfoData{
  44. Identifier: device.DeviceIdentifier,
  45. Name: device.DeviceName,
  46. Description: device.DeviceDescription,
  47. Version: device.DeviceVersion,
  48. },
  49. }
  50. r.JSON(http.StatusOK, result)
  51. return
  52. }
  53. func GetDeviceInfoByIdentifier(urlparams martini.Params, r render.Render) {
  54. identifier := urlparams["identifier"]
  55. server.Log.Printf("ACTION GetDeviceInfoByIdentifier, identifier:: %v", identifier)
  56. device := &models.Device{}
  57. err := server.RPCCallByName("registry", "Registry.FindDeviceByIdentifier", identifier, device)
  58. if err != nil {
  59. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  60. return
  61. }
  62. result := DeviceInfoResponse{
  63. Data: DeviceInfoData{
  64. Identifier: device.DeviceIdentifier,
  65. Name: device.DeviceName,
  66. Description: device.DeviceDescription,
  67. Version: device.DeviceVersion,
  68. },
  69. }
  70. r.JSON(http.StatusOK, result)
  71. return
  72. }
  73. func GetDeviceCurrentStatus(device *models.Device, config *productconfig.ProductConfig,
  74. urlparams martini.Params, r render.Render) {
  75. server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v", device.DeviceIdentifier)
  76. statusargs := rpcs.ArgsGetStatus{
  77. Id: uint64(device.ID),
  78. }
  79. statusreply := rpcs.ReplyGetStatus{}
  80. err := server.RPCCallByName("controller", "Controller.GetStatus", statusargs, &statusreply)
  81. if err != nil {
  82. server.Log.Errorf("get devie status error: %v", err)
  83. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  84. return
  85. }
  86. status, err := config.StatusToMap(statusreply.Status)
  87. if err != nil {
  88. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  89. return
  90. }
  91. result := DeviceStatusResponse{
  92. Data: status,
  93. }
  94. r.JSON(http.StatusOK, result)
  95. return
  96. }
  97. func GetDeviceLatestStatus() {
  98. }
  99. func SetDeviceStatus(device *models.Device, config *productconfig.ProductConfig,
  100. urlparams martini.Params, req *http.Request, r render.Render) {
  101. server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v,request: %v", device.DeviceIdentifier, req.Body)
  102. var args interface{}
  103. decoder := json.NewDecoder(req.Body)
  104. err := decoder.Decode(&args)
  105. if err != nil {
  106. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  107. return
  108. }
  109. m, ok := args.(map[string]interface{})
  110. if !ok {
  111. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  112. return
  113. }
  114. status, err := config.MapToStatus(m)
  115. if err != nil {
  116. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  117. return
  118. }
  119. statusargs := rpcs.ArgsSetStatus{
  120. DeviceId: uint64(device.ID),
  121. Status: status,
  122. }
  123. statusreply := rpcs.ReplySetStatus{}
  124. err = server.RPCCallByName("controller", "Controller.SetStatus", statusargs, &statusreply)
  125. if err != nil {
  126. server.Log.Errorf("set devie status error: %v", err)
  127. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  128. return
  129. }
  130. r.JSON(http.StatusOK, Common{})
  131. return
  132. }
  133. func SendCommandToDevice(device *models.Device, config *productconfig.ProductConfig,
  134. urlparams martini.Params, req *http.Request, r render.Render) {
  135. timeout := req.URL.Query().Get("timeout")
  136. server.Log.Printf("ACTION SendCommandToDevice, identifier:: %v, request: %v, timeout: %v",
  137. device.DeviceIdentifier, req.Body, timeout)
  138. var args interface{}
  139. decoder := json.NewDecoder(req.Body)
  140. err := decoder.Decode(&args)
  141. if err != nil {
  142. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  143. return
  144. }
  145. m, ok := args.(map[string]interface{})
  146. if !ok {
  147. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  148. return
  149. }
  150. command, err := config.MapToCommand(m)
  151. if err != nil {
  152. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  153. return
  154. }
  155. cmdargs := rpcs.ArgsSendCommand{
  156. DeviceId: uint64(device.ID),
  157. SubDevice: uint16(command.Head.SubDeviceid),
  158. No: uint16(command.Head.No),
  159. WaitTime: uint32(defaultTimeOut),
  160. Params: command.Params,
  161. }
  162. cmdreply := rpcs.ReplySendCommand{}
  163. err = server.RPCCallByName("controller", "Controller.SendCommand", cmdargs, &cmdreply)
  164. if err != nil {
  165. server.Log.Errorf("send devie command error: %v", err)
  166. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  167. return
  168. }
  169. r.JSON(http.StatusOK, Common{})
  170. return
  171. }
  172. func AddRule(device *models.Device, req *http.Request, r render.Render) {
  173. var ruleReq CreateRuleRequest
  174. decoder := json.NewDecoder(req.Body)
  175. err := decoder.Decode(&ruleReq)
  176. if err != nil {
  177. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  178. return
  179. }
  180. rule := &models.Rule{
  181. DeviceID: int64(device.ID),
  182. RuleType: ruleReq.Type,
  183. Trigger: ruleReq.Trigger,
  184. Target: ruleReq.Target,
  185. Action: ruleReq.Action,
  186. }
  187. reply := &rpcs.ReplyEmptyResult{}
  188. err = server.RPCCallByName("registry", "Registry.CreateRule", rule, reply)
  189. if err != nil {
  190. server.Log.Errorf("create device rule error: %v", err)
  191. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  192. return
  193. }
  194. r.JSON(http.StatusOK, Common{})
  195. return
  196. }