application.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package business
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "github.com/gin-gonic/gin/binding"
  8. //"device-manage/app/admin/models"
  9. "device-manage/app/admin/models/busmodels"
  10. "device-manage/common/log"
  11. mqtthandler "device-manage/common/mqttcli"
  12. "device-manage/tools"
  13. "device-manage/tools/app"
  14. "device-manage/tools/config"
  15. )
  16. func GetApplicationList(c *gin.Context) {
  17. var data busmodels.BusApplication
  18. var err error
  19. var params busmodels.QueryAppParams
  20. err = c.MustBindWith(&params, binding.JSON)
  21. pageSize := params.PageSize
  22. pageIndex := params.PageIndex
  23. gids := params.Gids
  24. data.AppName = params.AppName
  25. data.DestObject = params.DestObject
  26. result, count, err := data.GetPage(pageSize, pageIndex, gids)
  27. tools.HasError(err, "", -1)
  28. app.PageOK(c, result, count, pageIndex, pageSize, "")
  29. }
  30. func InsertApplication(c *gin.Context) {
  31. var appInfo busmodels.BusApplication
  32. err := c.MustBindWith(&appInfo, binding.JSON)
  33. tools.HasError(err, "非法数据格式", 500)
  34. user := tools.GetUserIdStr(c)
  35. appInfo.CreateBy = user
  36. appInfo.UpdateBy = user
  37. fmt.Println(user)
  38. times := time.Now()
  39. appInfo.CreatedAt = times
  40. appInfo.UpdatedAt = times
  41. id, err := appInfo.Insert()
  42. fmt.Println(id)
  43. tools.HasError(err, "添加失败", 500)
  44. app.OK(c, id, "添加成功")
  45. }
  46. func GetApplication(c *gin.Context) {
  47. var data busmodels.BusApplication
  48. data.AppId, _ = tools.StringToInt(c.Param("appId"))
  49. fmt.Println(data.AppId)
  50. result, err := data.Get()
  51. tools.HasError(err, "抱歉未找到相关信息", -1)
  52. var res app.Response
  53. res.Data = result
  54. c.JSON(http.StatusOK, res.ReturnOK())
  55. }
  56. //更新之前,应该先获取当前选项的数据, 得到 id以后才好i修改.
  57. func UpdateApplication(c *gin.Context) {
  58. var data busmodels.BusApplication
  59. err := c.Bind(&data)
  60. fmt.Println(data)
  61. tools.HasError(err, "数据解析失败", -1)
  62. data.UpdateBy = tools.GetUserIdStr(c)
  63. fmt.Println(data)
  64. result, err := data.Update(data.AppId)
  65. tools.HasError(err, "修改失败", 500)
  66. app.OK(c, result, "修改成功")
  67. }
  68. func DeleteApplication(c *gin.Context) {
  69. var data busmodels.BusApplication
  70. data.UpdateBy = tools.GetUserIdStr(c)
  71. IDS := tools.IdsStrToIdsIntGroup("appId", c)
  72. fmt.Println(IDS)
  73. result, err := data.BatchDelete(IDS)
  74. tools.HasError(err, "删除失败", 500)
  75. app.OK(c, result, "删除成功")
  76. }
  77. func PublishApp2Devices(c *gin.Context) {
  78. var applicatoin busmodels.BusApplication
  79. var upgradefile busmodels.BusUpgradeFile
  80. var record busmodels.BusUpgradeRecord
  81. var dev busmodels.BusDevice
  82. var devInfo busmodels.DeviceInfo
  83. var data busmodels.PublishAppDevice
  84. err := c.MustBindWith(&data, binding.JSON)
  85. tools.HasError(err, "非法数据格式", 500)
  86. fmt.Println(data.AppId)
  87. applicatoin.AppId = data.AppId
  88. upgradefile.AppId = data.AppId
  89. userId := tools.GetUserIdStr(c)
  90. appInfo, _ := applicatoin.Get()
  91. upgradefile, _ = upgradefile.GetFirstFile()
  92. // /api/v1/device/usual/upgradefile/download
  93. url := "http://" + tools.GetLocaHonst() + ":" + config.ApplicationConfig.Port + "/"
  94. fmt.Println(url)
  95. //todo publish app to devices
  96. for _, devid := range data.DeviceId {
  97. fmt.Println(devid)
  98. dev.DeviceId = devid
  99. if devInfo, err = dev.Get(); err != nil {
  100. continue
  101. }
  102. record.RecordId = 0
  103. record.DeviceSn = devInfo.DeviceSn
  104. record.Uid = upgradefile.Uid
  105. if record, err = record.GetUpgradeRecord(); err == nil {
  106. fmt.Println("app version is old.")
  107. continue
  108. }
  109. if devInfo.Status > 0 {
  110. if _, ok := mqtthandler.MqttClientList[devInfo.ServerIp]; ok {
  111. go func(devInfo busmodels.DeviceInfo) {
  112. mqtthandler.MqttClientList[devInfo.ServerIp].FileUpgrade(userId,
  113. devInfo.DeviceSn, appInfo.AppKey, url, upgradefile.Uid)
  114. }(devInfo)
  115. }
  116. } else {
  117. log.Info("[%v] publish app device offline", devInfo.DeviceSn)
  118. }
  119. }
  120. //result, err := data.BatchDelete(IDS)
  121. //tools.HasError(err, "删除失败", 500)
  122. app.OK(c, "", "发布成功")
  123. }
  124. func PublishApp2Groups(c *gin.Context) {
  125. var applicatoin busmodels.BusApplication
  126. var upgradefile busmodels.BusUpgradeFile
  127. var dev busmodels.BusDevice
  128. var devs []busmodels.DeviceInfo
  129. var data busmodels.PublishAppGroup
  130. err := c.MustBindWith(&data, binding.JSON)
  131. tools.HasError(err, "非法数据格式", 500)
  132. fmt.Println(data.AppId)
  133. userId := tools.GetUserIdStr(c)
  134. applicatoin.AppId = data.AppId
  135. appInfo, _ := applicatoin.Get()
  136. upgradefile, _ = upgradefile.GetFirstFile()
  137. // /api/v1/device/usual/upgradefile/download
  138. url := "http://" + tools.GetLocaHonst() + ":" + config.ApplicationConfig.Port + "/"
  139. fmt.Println(url)
  140. //todo publish app to groups
  141. for _, gid := range data.GroupId {
  142. fmt.Println("group id:", gid)
  143. dev.GroupId = gid
  144. if devs, err = dev.GetDevices(); err != nil {
  145. continue
  146. } else {
  147. for _, devInfo := range devs {
  148. dev.DeviceId = devInfo.DeviceId
  149. if devInfo, err = dev.Get(); err != nil {
  150. continue
  151. }
  152. if devInfo.Status > 0 {
  153. if _, ok := mqtthandler.MqttClientList[devInfo.ServerIp]; ok {
  154. go func(devInfo busmodels.DeviceInfo) {
  155. mqtthandler.MqttClientList[devInfo.ServerIp].FileUpgrade(userId,
  156. devInfo.DeviceSn, appInfo.AppKey, url, upgradefile.Uid)
  157. }(devInfo)
  158. }
  159. } else {
  160. log.Info("cmd publish app device offline [%s]", devInfo.DeviceSn)
  161. }
  162. }
  163. }
  164. }
  165. //result, err := data.BatchDelete(IDS)
  166. //tools.HasError(err, "删除失败", 500)
  167. app.OK(c, "", "发布成功")
  168. }