123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package controllers
- import (
- "Yunlot/Handle"
- "Yunlot/lib"
- "Yunlot/logs"
- "Yunlot/models/Device"
- "Yunlot/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)
- ProductOTAr := Product.ProductOTA{}
- c.ParseForm(&ProductOTAr)
- // 验证 TOKEY
- if c.Ctx.Input.Host() != "127.0.0.1" {
- if len(ProductOTAr.T_uuid) != 8 {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- }
- 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) Get() {
- ProductOTAr := Product.ProductOTA{}
- c.ParseForm(&ProductOTAr)
- if !ProductOTAr.Read() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
- c.ServeJSON()
- return
- }
- // 验证 TOKEY
- if ProductOTAr.T_uuid != c.GetString("T_uuid") {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductOTAr}
- c.ServeJSON()
- return
- }
- func (c *ProductOTAController) Add() {
- ProductOTA_r := Product.ProductOTA{}
- c.ParseForm(&ProductOTA_r)
- if len(ProductOTA_r.T_uuid) != 8 {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- 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
- }
- // 验证 TOKEY
- if ProductOTAr.T_uuid != c.GetString("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
- }
|