ProductOTA.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package controllers
  2. import (
  3. "Yunlot/Handle"
  4. "Yunlot/lib"
  5. "Yunlot/logs"
  6. "Yunlot/models/Device"
  7. "Yunlot/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. ProductOTAr := Product.ProductOTA{}
  18. c.ParseForm(&ProductOTAr)
  19. // 验证 TOKEY
  20. if c.Ctx.Input.Host() != "127.0.0.1" {
  21. if len(ProductOTAr.T_uuid) != 8 {
  22. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  23. c.ServeJSON()
  24. return
  25. }
  26. }
  27. ProductOTA_r, Total := ProductOTAr.Lists(PageIndex, PageSize)
  28. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductOTA_r, PageIndex, PageSize, Total)}
  29. c.ServeJSON()
  30. return
  31. }
  32. func (c *ProductOTAController) Get() {
  33. ProductOTAr := Product.ProductOTA{}
  34. c.ParseForm(&ProductOTAr)
  35. if !ProductOTAr.Read() {
  36. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  37. c.ServeJSON()
  38. return
  39. }
  40. // 验证 TOKEY
  41. if ProductOTAr.T_uuid != c.GetString("T_uuid") {
  42. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  43. c.ServeJSON()
  44. return
  45. }
  46. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductOTAr}
  47. c.ServeJSON()
  48. return
  49. }
  50. func (c *ProductOTAController) Add() {
  51. ProductOTA_r := Product.ProductOTA{}
  52. c.ParseForm(&ProductOTA_r)
  53. if len(ProductOTA_r.T_uuid) != 8 {
  54. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  55. c.ServeJSON()
  56. return
  57. }
  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. // 验证 TOKEY
  91. if ProductOTAr.T_uuid != c.GetString("T_uuid") {
  92. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. if !ProductOTAr.Delete() {
  97. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  98. c.ServeJSON()
  99. return
  100. }
  101. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  102. c.ServeJSON()
  103. return
  104. }