actions.go 6.2 KB

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