monitor.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package business
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gin-gonic/gin/binding"
  6. //"device-manage/app/admin/models"
  7. "device-manage/app/admin/models/busmodels"
  8. "device-manage/common/log"
  9. mqtthandler "device-manage/common/mqttcli"
  10. "device-manage/tools"
  11. "device-manage/tools/app"
  12. "device-manage/tools/config"
  13. )
  14. // @Summary 获取设备列表
  15. // @Description Get JSON
  16. // @Tags 设备列表
  17. // @Param name query string false "name"
  18. // @Param status query string false "status"
  19. // @Param deviceSn query string false "deviceSn"
  20. // @Param pageSize query int false "页条数"
  21. // @Param pageIndex query int false "页码"
  22. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  23. // @Router /api/v1/device/list/devicelist [get]
  24. // @Security Bearer
  25. func GetMonitorList(c *gin.Context) {
  26. var data busmodels.BusDevice
  27. var err error
  28. var params busmodels.QueryParams
  29. err = c.MustBindWith(&params, binding.JSON)
  30. pageSize := params.PageSize
  31. pageIndex := params.PageIndex
  32. gids := params.Gids
  33. data.DeviceName = params.DeviceName
  34. data.DeviceSn = params.DeviceSn
  35. data.Status = params.Status
  36. data.IsRegister = 1
  37. result, count, err := data.GetMonitorPage(pageSize, pageIndex, gids)
  38. tools.HasError(err, "", -1)
  39. app.PageOK(c, result, count, pageIndex, pageSize, "")
  40. }
  41. func GetDeviceLog(c *gin.Context) {
  42. var err error
  43. var dev busmodels.BusDevice
  44. var devInfo busmodels.DeviceInfo
  45. //"/api/v1/device/usual/log/upload"
  46. url := "http://" + tools.GetLocaHonst() + ":" + config.ApplicationConfig.Port + "/"
  47. fmt.Println(url)
  48. IDS := tools.IdsStrToIdsIntGroup("deviceId", c)
  49. userId := tools.GetUserIdStr(c)
  50. fmt.Println(IDS)
  51. //todo get device log
  52. for _, devid := range IDS {
  53. fmt.Println(devid)
  54. dev.DeviceId = devid
  55. if devInfo, err = dev.Get(); err != nil {
  56. continue
  57. }
  58. if devInfo.Status > 0 {
  59. log.Info(devInfo.Status)
  60. log.Info(len(mqtthandler.MqttClientList))
  61. if _, ok := mqtthandler.MqttClientList[devInfo.ServerIp]; ok {
  62. mqtthandler.MqttClientList[devInfo.ServerIp].CollectLog(userId, devInfo.DeviceSn, url)
  63. } else {
  64. log.Info(ok)
  65. }
  66. } else {
  67. log.Info("cmd collect log device offline [%s]", devInfo.DeviceSn)
  68. }
  69. }
  70. app.OK(c, "", "开始获取日志")
  71. }
  72. func RebootDevice(c *gin.Context) {
  73. var err error
  74. var dev busmodels.BusDevice
  75. var devInfo busmodels.DeviceInfo
  76. IDS := tools.IdsStrToIdsIntGroup("deviceId", c)
  77. fmt.Println(IDS)
  78. //todo get device log
  79. for _, devid := range IDS {
  80. fmt.Println(devid)
  81. dev.DeviceId = devid
  82. if devInfo, err = dev.Get(); err != nil {
  83. continue
  84. }
  85. if devInfo.Status > 0 {
  86. if _, ok := mqtthandler.MqttClientList[devInfo.ServerIp]; ok {
  87. mqtthandler.MqttClientList[devInfo.ServerIp].Reboot(devInfo.DeviceSn)
  88. }
  89. } else {
  90. log.Info("cmd reboot offline [%s]", devInfo.DeviceSn)
  91. }
  92. }
  93. app.OK(c, "", "重启成功.")
  94. }