123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package controllers
- import (
- "AIOTCOER/Handle"
- "AIOTCOER/lib"
- "AIOTCOER/logs"
- "AIOTCOER/models/Device"
- "AIOTCOER/models/Product"
- "encoding/json"
- beego "github.com/beego/beego/v2/server/web"
- )
- type ProductOTAController struct {
- beego.Controller
- }
- func (c *ProductOTAController) List() {
- PageIndex, _ := c.GetInt("PageIndex", 0)
- PageSize, _ := c.GetInt("PageSize", 10)
- ProductTyper := Product.ProductType{
- T_ProductID:c.GetString("T_ProductID"),
- }
- if !ProductTyper.Read() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
- c.ServeJSON()
- return
- }
- // 验证 T_uuid
- T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
- if ProductTyper.T_uuid != T_uuid {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- ProductOTAr := Product.ProductOTA{
- T_ProductID: ProductTyper.T_ProductID,
- }
- ProductOTA_r, Total := ProductOTAr.Lists(PageIndex, PageSize)
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductOTA_r, PageIndex, PageSize, Total)}
- c.ServeJSON()
- return
- }
- func (c *ProductOTAController) Add() {
- ProductTyper := Product.ProductType{
- T_ProductID:c.GetString("T_ProductID"),
- }
- if !ProductTyper.Read() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
- c.ServeJSON()
- return
- }
- // 验证 T_uuid
- T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
- if ProductTyper.T_uuid != T_uuid {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- ProductOTA_r := Product.ProductOTA{}
- c.ParseForm(&ProductOTA_r)
- go func() {
- device := Device.Device{T_ProductID: ProductOTA_r.T_ProductID}
- Device_list := device.Lists_All()
- var T_datajson map[string]interface{}
- T_datajson = make(map[string]interface{})
- T_datajson["name"] = ProductOTA_r.T_name
- T_datajson["version"] = ProductOTA_r.T_version
- T_datajson["file"] = ProductOTA_r.T_file
- T_datajson["md5"] = ProductOTA_r.T_file
- T_datajson["size"] = ProductOTA_r.T_file
- T_datajson["publish"] = ProductOTA_r.T_publish
- T_datajson["describe"] = ProductOTA_r.T_describe
- b, _ := json.Marshal(map[string]interface{}{"upgrade": T_datajson})
- for _, v := range Device_list {
- logs.Println("全量发布:", v.T_sn, string(b))
- Handle.PushHandle(&v, v.T_sn, string(b))
- }
- }()
- ProductOTA_r.Id = 0
- ProductOTA_r.Add()
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductOTA_r}
- c.ServeJSON()
- return
- }
- func (c *ProductOTAController) Delete() {
- ProductOTAr := Product.ProductOTA{}
- c.ParseForm(&ProductOTAr)
- if !ProductOTAr.Read() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
- c.ServeJSON()
- return
- }
- ProductTyper := Product.ProductType{
- T_ProductID:ProductOTAr.T_ProductID,
- }
- if !ProductTyper.Read() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
- c.ServeJSON()
- return
- }
- // 验证 T_uuid
- T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
- if ProductTyper.T_uuid != T_uuid {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- if !ProductOTAr.Delete() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
- c.ServeJSON()
- return
- }
|