ProductOTA.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package controllers
  2. import (
  3. "AIOTCOER/Handle"
  4. "AIOTCOER/lib"
  5. "AIOTCOER/logs"
  6. "AIOTCOER/models/Device"
  7. "AIOTCOER/models/Product"
  8. "encoding/json"
  9. beego "github.com/beego/beego/v2/server/web"
  10. )
  11. type ProductOTAController struct {
  12. beego.Controller
  13. }
  14. func (c *ProductOTAController) List() {
  15. PageIndex, _ := c.GetInt("PageIndex", 0)
  16. PageSize, _ := c.GetInt("PageSize", 10)
  17. ProductTyper := Product.ProductType{
  18. T_ProductID:c.GetString("T_ProductID"),
  19. }
  20. if !ProductTyper.Read() {
  21. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
  22. c.ServeJSON()
  23. return
  24. }
  25. // 验证 T_uuid
  26. T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  27. if ProductTyper.T_uuid != T_uuid {
  28. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  29. c.ServeJSON()
  30. return
  31. }
  32. ProductOTAr := Product.ProductOTA{
  33. T_ProductID: ProductTyper.T_ProductID,
  34. }
  35. ProductOTA_r, Total := ProductOTAr.Lists(PageIndex, PageSize)
  36. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductOTA_r, PageIndex, PageSize, Total)}
  37. c.ServeJSON()
  38. return
  39. }
  40. func (c *ProductOTAController) Add() {
  41. ProductTyper := Product.ProductType{
  42. T_ProductID:c.GetString("T_ProductID"),
  43. }
  44. if !ProductTyper.Read() {
  45. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
  46. c.ServeJSON()
  47. return
  48. }
  49. // 验证 T_uuid
  50. T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  51. if ProductTyper.T_uuid != T_uuid {
  52. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  53. c.ServeJSON()
  54. return
  55. }
  56. ProductOTA_r := Product.ProductOTA{}
  57. c.ParseForm(&ProductOTA_r)
  58. go func() {
  59. device := Device.Device{T_ProductID: ProductOTA_r.T_ProductID}
  60. Device_list := device.Lists_All()
  61. var T_datajson map[string]interface{}
  62. T_datajson = make(map[string]interface{})
  63. T_datajson["name"] = ProductOTA_r.T_name
  64. T_datajson["version"] = ProductOTA_r.T_version
  65. T_datajson["file"] = ProductOTA_r.T_file
  66. T_datajson["md5"] = ProductOTA_r.T_file
  67. T_datajson["size"] = ProductOTA_r.T_file
  68. T_datajson["publish"] = ProductOTA_r.T_publish
  69. T_datajson["describe"] = ProductOTA_r.T_describe
  70. b, _ := json.Marshal(map[string]interface{}{"upgrade": T_datajson})
  71. for _, v := range Device_list {
  72. logs.Println("全量发布:", v.T_sn, string(b))
  73. Handle.PushHandle(&v, v.T_sn, string(b))
  74. }
  75. }()
  76. ProductOTA_r.Id = 0
  77. ProductOTA_r.Add()
  78. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductOTA_r}
  79. c.ServeJSON()
  80. return
  81. }
  82. func (c *ProductOTAController) Delete() {
  83. ProductOTAr := Product.ProductOTA{}
  84. c.ParseForm(&ProductOTAr)
  85. if !ProductOTAr.Read() {
  86. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  87. c.ServeJSON()
  88. return
  89. }
  90. ProductTyper := Product.ProductType{
  91. T_ProductID:ProductOTAr.T_ProductID,
  92. }
  93. if !ProductTyper.Read() {
  94. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
  95. c.ServeJSON()
  96. return
  97. }
  98. // 验证 T_uuid
  99. T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  100. if ProductTyper.T_uuid != T_uuid {
  101. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. if !ProductOTAr.Delete() {
  106. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  111. c.ServeJSON()
  112. return
  113. }