device.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package controllers
  2. import (
  3. "errors"
  4. "fmt"
  5. "sparrow/pkg/rpcs"
  6. "sparrow/pkg/server"
  7. "sparrow/services/knowoapi/services"
  8. "strconv"
  9. "time"
  10. "github.com/kataras/iris/v12"
  11. )
  12. // DeviceController api
  13. type DeviceController struct {
  14. Ctx iris.Context
  15. Service services.DeviceService
  16. Token
  17. }
  18. // Get 获取激活设备列表
  19. // GET /device?pi=&ps=&proid=&device_id=
  20. func (a *DeviceController) Get() {
  21. pi, err := a.Ctx.URLParamInt("pi")
  22. if err != nil {
  23. badRequest(a.Ctx, err)
  24. return
  25. }
  26. ps, err := a.Ctx.URLParamInt("ps")
  27. if err != nil {
  28. badRequest(a.Ctx, err)
  29. return
  30. }
  31. proid := a.Ctx.URLParam("proid")
  32. deviceid := a.Ctx.URLParam("device_id")
  33. vid := a.getVendorID(a.Ctx)
  34. datas, total, err := a.Service.GetDevices(vid, proid, pi, ps, deviceid)
  35. if err != nil {
  36. responseError(a.Ctx, ErrDatabase, err.Error())
  37. return
  38. }
  39. done(a.Ctx, map[string]interface{}{
  40. "list": datas,
  41. "total": total,
  42. })
  43. }
  44. // 设备数量,包含在线数量,离线数量,总数量
  45. // GET /devicecount
  46. func (a *DeviceController) GetDevicecount() {
  47. vid := a.getVendorID(a.Ctx)
  48. data, err := a.Service.GetDevicesCountByVenderId(vid)
  49. if err != nil && err.Error() != "redigo: nil returned" {
  50. responseError(a.Ctx, ErrDatabase, err.Error())
  51. return
  52. }
  53. done(a.Ctx, data)
  54. }
  55. // 获取设备在线状态
  56. // GET /devicestatus?device_id=
  57. func (a *DeviceController) GetDevicestatus() {
  58. deviceid := a.Ctx.URLParam("device_id")
  59. var status int
  60. onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
  61. Id: deviceid,
  62. }
  63. onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
  64. err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
  65. if err != nil && err.Error() != "redigo: nil returned" {
  66. server.Log.Errorf("get devie online status error: %v", err)
  67. responseError(a.Ctx, ErrDatabase, err.Error())
  68. return
  69. }
  70. if int(onlinereply.HeartbeatInterval) > 0 && len(onlinereply.ClientIP) > 0 {
  71. status = 1
  72. }
  73. done(a.Ctx, map[string]interface{}{
  74. "Status": status,
  75. })
  76. }
  77. // GetBannerdata 获取设备激活和活跃数据
  78. // GET /bannerdata?proid=
  79. func (a *DeviceController) GetBannerdata() {
  80. proid, err := a.Ctx.URLParamInt("proid")
  81. if err != nil {
  82. proid = 0
  83. }
  84. var (
  85. //今日激活
  86. tact = 0
  87. // 今日活跃
  88. tlive = 0
  89. // 昨日激活
  90. yact = 0
  91. // 昨日活跃
  92. ylive = 0
  93. // 累计激活
  94. totolact = 0
  95. // 环比昨日激活
  96. comparedWithYesterdayActive = 0.00
  97. //活跃占比
  98. rateOfTodayLive = 0.00
  99. //环比昨日活跃
  100. comparedWithYesterdayLive = 0.00
  101. )
  102. vendorid := a.getVendorID(a.Ctx)
  103. totolact, err = a.Service.GetDeviceCount(vendorid)
  104. if err != nil {
  105. responseError(a.Ctx, ErrDatabase, err.Error())
  106. return
  107. }
  108. today := time.Now().Format("2006-01-02")
  109. yestoday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  110. tact, err = a.Service.GetActiveNumberOfDate(vendorid, today)
  111. if err != nil {
  112. responseError(a.Ctx, ErrDatabase, err.Error())
  113. return
  114. }
  115. tlive, err = a.Service.GetLivelyCountOfDate(vendorid, today)
  116. if err != nil {
  117. responseError(a.Ctx, ErrDatabase, err.Error())
  118. return
  119. }
  120. yact, err = a.Service.GetActiveNumberOfDate(vendorid, yestoday)
  121. if err != nil {
  122. responseError(a.Ctx, ErrDatabase, err.Error())
  123. return
  124. }
  125. ylive, err = a.Service.GetLivelyCountOfDate(vendorid, yestoday)
  126. if err != nil {
  127. responseError(a.Ctx, ErrDatabase, err.Error())
  128. return
  129. }
  130. if tact != 0 && yact != 0 {
  131. comparedWithYesterdayActive = (float64(tact) / float64(yact)) * 100
  132. }
  133. if tlive != 0 && ylive != 0 {
  134. comparedWithYesterdayLive = (float64(tlive) / float64(ylive)) * 100
  135. rateOfTodayLive = float64(tlive) / float64(totolact) * 100
  136. }
  137. //TODO 这里的productid要去掉
  138. result := map[string]interface{}{
  139. "todayAct": tact,
  140. "todayLive": tlive,
  141. "yestodayAct": yact,
  142. "yestodayLive": ylive,
  143. "totalAct": totolact,
  144. "comparedWithYesterdayActive": fmt.Sprintf("%s%%", strconv.FormatFloat(comparedWithYesterdayActive, 'f', 2, 64)),
  145. "comparedWithYesterdayLive": fmt.Sprintf("%s%%", strconv.FormatFloat(comparedWithYesterdayLive, 'f', 2, 64)),
  146. "rateOfTodayLive": fmt.Sprintf("%s%%", strconv.FormatFloat(rateOfTodayLive, 'f', 2, 64)),
  147. "productid": proid,
  148. }
  149. done(a.Ctx, result)
  150. }
  151. // GetActivechart 获取N日的激活数据趋势
  152. func (a *DeviceController) GetActivechart() {
  153. // proid, err := a.Ctx.URLParamInt("proid")
  154. // if err != nil {
  155. // proid = 0
  156. // }
  157. days, err := a.Ctx.URLParamInt("days")
  158. if err != nil {
  159. days = 7
  160. }
  161. if days > 30 {
  162. badRequest(a.Ctx, errors.New("非法参数"))
  163. return
  164. }
  165. vendorid := a.getVendorID(a.Ctx)
  166. datas, err := a.Service.GetActiveOfNumDays(vendorid, days)
  167. if err != nil {
  168. responseError(a.Ctx, ErrDatabase, err.Error())
  169. return
  170. }
  171. done(a.Ctx, map[string]interface{}{
  172. "chart": datas,
  173. })
  174. }
  175. // GetLivechart 获取N日内的活跃数据
  176. func (a *DeviceController) GetLivechart() {
  177. // proid, err := a.Ctx.URLParamInt("proid")
  178. // if err != nil {
  179. // proid = 0
  180. // }
  181. days, err := a.Ctx.URLParamInt("days")
  182. if err != nil {
  183. days = 7
  184. }
  185. if days > 30 {
  186. badRequest(a.Ctx, errors.New("非法参数"))
  187. return
  188. }
  189. vendorid := a.getVendorID(a.Ctx)
  190. datas, err := a.Service.GetLivelyOfNumDays(vendorid, days)
  191. if err != nil {
  192. responseError(a.Ctx, ErrDatabase, err.Error())
  193. return
  194. }
  195. done(a.Ctx, map[string]interface{}{
  196. "chart": datas,
  197. })
  198. }
  199. // Upgrade ota升级
  200. // POST /devices/upgrade
  201. //func (a *DeviceController) Upgrade() {
  202. // params := new(models.UpgradeParams)
  203. // if err := parseBody(a.Ctx, params); err != nil {
  204. // badRequest(a.Ctx, err)
  205. // return
  206. // }
  207. // params.VendorID = a.Token.getVendorID(a.Ctx)
  208. // file, header, err := a.Ctx.FormFile("file")
  209. // if err != nil {
  210. // responseError(a.Ctx, ErrNormal, err.Error())
  211. // return
  212. // }
  213. // fileBytes, err := io.ReadAll(file)
  214. // if err != nil {
  215. // responseError(a.Ctx, ErrNormal, err.Error())
  216. // return
  217. // }
  218. // params.FileSize = header.Size
  219. // params.File = fileBytes
  220. // params.FileName = header.Filename
  221. // err = a.Service.Upgrade(params)
  222. // if err != nil {
  223. // responseError(a.Ctx, ErrNormal, err.Error())
  224. // return
  225. // }
  226. // done(a.Ctx, params)
  227. //}
  228. //// OtaProgress ota升级
  229. //// GET /devices/ota/progress?deviceId=
  230. //func (a *DeviceController) OtaProgress() {
  231. // deviceId := a.Ctx.URLParam("deviceId")
  232. //
  233. // data, err := a.Service.GetUpgradeProgress(deviceId)
  234. // if err != nil {
  235. // responseError(a.Ctx, ErrDatabase, err.Error())
  236. // return
  237. // }
  238. // done(a.Ctx, map[string]interface{}{
  239. // "progress": data.Progress,
  240. // })
  241. //}