actions.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 done(result interface{}) Common {
  34. return Common{
  35. Code: ErrOK,
  36. Message: "success",
  37. Result: result,
  38. }
  39. }
  40. func GetDeviceInfoByKey(params martini.Params, req *http.Request, r render.Render) {
  41. key := req.URL.Query().Get("device_key")
  42. server.Log.Printf("ACTION GetDeviceInfoByKey, key:: %v", key)
  43. device := &models.Device{}
  44. err := server.RPCCallByName("registry", "Registry.ValidateDevice", key, device)
  45. if err != nil {
  46. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  47. return
  48. }
  49. result := DeviceInfoResponse{
  50. Data: DeviceInfoData{
  51. Identifier: device.DeviceIdentifier,
  52. Name: device.DeviceName,
  53. Description: device.DeviceDescription,
  54. Version: device.DeviceVersion,
  55. },
  56. }
  57. r.JSON(http.StatusOK, result)
  58. return
  59. }
  60. func GetDeviceInfoByIdentifier(urlparams martini.Params, r render.Render) {
  61. identifier := urlparams["identifier"]
  62. server.Log.Printf("ACTION GetDeviceInfoByIdentifier, identifier:: %v", identifier)
  63. device := &models.Device{}
  64. err := server.RPCCallByName("registry", "Registry.FindDeviceByIdentifier", identifier, device)
  65. if err != nil {
  66. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  67. return
  68. }
  69. result := DeviceInfoResponse{
  70. Data: DeviceInfoData{
  71. Identifier: device.DeviceIdentifier,
  72. Name: device.DeviceName,
  73. Description: device.DeviceDescription,
  74. Version: device.DeviceVersion,
  75. },
  76. }
  77. r.JSON(http.StatusOK, result)
  78. return
  79. }
  80. func GetDeviceCurrentStatus(device *models.Device, config *productconfig.ProductConfig,
  81. urlparams martini.Params, r render.Render) {
  82. server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v", device.DeviceIdentifier)
  83. statusargs := rpcs.ArgsGetStatus{
  84. Id: uint64(device.ID),
  85. }
  86. statusreply := rpcs.ReplyGetStatus{}
  87. err := server.RPCCallByName("controller", "Controller.GetStatus", statusargs, &statusreply)
  88. if err != nil {
  89. server.Log.Errorf("get devie status error: %v", err)
  90. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  91. return
  92. }
  93. status, err := config.StatusToMap(statusreply.Status)
  94. if err != nil {
  95. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  96. return
  97. }
  98. result := DeviceStatusResponse{
  99. Data: status,
  100. }
  101. r.JSON(http.StatusOK, result)
  102. return
  103. }
  104. func GetDeviceLatestStatus() {
  105. }
  106. func SetDeviceStatus(device *models.Device, config *productconfig.ProductConfig,
  107. urlparams martini.Params, req *http.Request, r render.Render) {
  108. server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v,request: %v", device.DeviceIdentifier, req.Body)
  109. var args interface{}
  110. decoder := json.NewDecoder(req.Body)
  111. err := decoder.Decode(&args)
  112. if err != nil {
  113. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  114. return
  115. }
  116. m, ok := args.(map[string]interface{})
  117. if !ok {
  118. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  119. return
  120. }
  121. status, err := config.MapToStatus(m)
  122. if err != nil {
  123. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  124. return
  125. }
  126. statusargs := rpcs.ArgsSetStatus{
  127. DeviceId: uint64(device.ID),
  128. Status: status,
  129. }
  130. statusreply := rpcs.ReplySetStatus{}
  131. err = server.RPCCallByName("controller", "Controller.SetStatus", statusargs, &statusreply)
  132. if err != nil {
  133. server.Log.Errorf("set devie status error: %v", err)
  134. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  135. return
  136. }
  137. r.JSON(http.StatusOK, Common{})
  138. return
  139. }
  140. func SendCommandToDevice(device *models.Device, config *productconfig.ProductConfig,
  141. urlparams martini.Params, req *http.Request, r render.Render) {
  142. timeout := req.URL.Query().Get("timeout")
  143. server.Log.Printf("ACTION SendCommandToDevice, identifier:: %v, request: %v, timeout: %v",
  144. device.DeviceIdentifier, req.Body, timeout)
  145. var args interface{}
  146. decoder := json.NewDecoder(req.Body)
  147. err := decoder.Decode(&args)
  148. if err != nil {
  149. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  150. return
  151. }
  152. m, ok := args.(map[string]interface{})
  153. if !ok {
  154. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  155. return
  156. }
  157. command, err := config.MapToCommand(m)
  158. if err != nil {
  159. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  160. return
  161. }
  162. cmdargs := rpcs.ArgsSendCommand{
  163. DeviceId: uint64(device.ID),
  164. SubDevice: uint16(command.Head.SubDeviceid),
  165. No: uint16(command.Head.No),
  166. WaitTime: uint32(defaultTimeOut),
  167. Params: command.Params,
  168. }
  169. cmdreply := rpcs.ReplySendCommand{}
  170. err = server.RPCCallByName("controller", "Controller.SendCommand", cmdargs, &cmdreply)
  171. if err != nil {
  172. server.Log.Errorf("send devie command error: %v", err)
  173. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  174. return
  175. }
  176. r.JSON(http.StatusOK, Common{})
  177. return
  178. }
  179. func AddRule(device *models.Device, req *http.Request, r render.Render) {
  180. var ruleReq CreateRuleRequest
  181. decoder := json.NewDecoder(req.Body)
  182. err := decoder.Decode(&ruleReq)
  183. if err != nil {
  184. r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
  185. return
  186. }
  187. rule := &models.Rule{
  188. DeviceID: int64(device.ID),
  189. RuleType: ruleReq.Type,
  190. Trigger: ruleReq.Trigger,
  191. Target: ruleReq.Target,
  192. Action: ruleReq.Action,
  193. }
  194. reply := &rpcs.ReplyEmptyResult{}
  195. err = server.RPCCallByName("registry", "Registry.CreateRule", rule, reply)
  196. if err != nil {
  197. server.Log.Errorf("create device rule error: %v", err)
  198. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  199. return
  200. }
  201. r.JSON(http.StatusOK, Common{})
  202. return
  203. }