package controllers import ( "Yunlot/Handle" "Yunlot/lib" "Yunlot/logs" "Yunlot/models/Device" "Yunlot/models/Product" beego "github.com/beego/beego/v2/server/web" "time" ) type DeviceController struct { beego.Controller } func (c *DeviceController) List() { PageIndex, _ := c.GetInt("PageIndex", 0) PageSize, _ := c.GetInt("PageSize", 10) Devicer := Device.Device{} c.ParseForm(&Devicer) ProductTyper := Product.ProductType{T_ProductID: Devicer.T_ProductID} if !ProductTyper.Read() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"} c.ServeJSON() return } Device_r, Total := Devicer.Lists(PageIndex, PageSize) c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(Device_r, PageIndex, PageSize, Total)} c.ServeJSON() return } func (c *DeviceController) Add() { // 验证秘钥 ProductKey := c.GetString("ProductKey") ProductID := c.GetString("ProductID") ProductType_r := Product.ProductType{T_ProductID: ProductID} if !ProductType_r.Read() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID!"} c.ServeJSON() return } if ProductType_r.T_akey != ProductKey { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductKey!"} c.ServeJSON() return } // 创建sn T_sn := c.GetString("T_sn") Device_r := Device.Device{T_ProductID: ProductID, T_sn: T_sn} // CreateNum, _ := c.GetInt("CreateNum", 1) if CreateNum == 1 || len(Device_r.T_sn) >= 10 { // 单个 if !Device_r.CreateSn(1) { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_sn 重复!"} c.ServeJSON() return } c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_r} c.ServeJSON() return } else { // 多个 var Device_List []Device.Device for i := 1; i <= CreateNum; i++ { Device_rr := Device_r Device_rr.CreateSn(i) Device_List = append(Device_List, Device_rr) } c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_List} c.ServeJSON() return } } func (c *DeviceController) Update() { Devicer := Device.Device{T_sn: c.GetString("T_sn")} Devicer.Read_Tidy() c.ParseForm(&Devicer) if !Devicer.Update("T_state", "T_project") { 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 } // //func (c *DeviceController) Delete() { // Devicer := Device.Device{} // c.ParseForm(&Devicer) // // if !Devicer.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 //} func (c *DeviceController) Get() { Devicer := Device.Device{} c.ParseForm(&Devicer) if !Devicer.Read_Tidy() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"} c.ServeJSON() return } c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Devicer} c.ServeJSON() return } func (c *DeviceController) Push() { Devicer := Device.Device{} c.ParseForm(&Devicer) T_data := c.GetString("T_data") if len(T_data) == 0 { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_data E!"} c.ServeJSON() return } if !Devicer.Read_Tidy() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"} c.ServeJSON() return } Handle.PushHandle(&Devicer, Devicer.T_sn, T_data) c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"} c.ServeJSON() return } func (c *DeviceController) DataList() { DeviceData_Form := Device.DeviceData_Form{} c.ParseForm(&DeviceData_Form) if len(DeviceData_Form.T_sn) < 1 { c.Data["json"] = lib.JSONS{Code: lib.Error, Msg: "T_sn !"} c.ServeJSON() return } if len(DeviceData_Form.T_jointTab) < 1 { c.Data["json"] = lib.JSONS{Code: lib.Error, Msg: "T_jointTab !"} c.ServeJSON() return } JSONR_r := Device.Data_List(DeviceData_Form.T_sn+"_"+DeviceData_Form.T_jointTab, DeviceData_Form.T_jsonFind, DeviceData_Form.T_jsonSort, DeviceData_Form.PageIndex, DeviceData_Form.PageSize) c.Data["json"] = JSONR_r c.ServeJSON() return } func (c *DeviceController) GetLog() { Devicer := Device.Device{} c.ParseForm(&Devicer) if !Devicer.Read_Tidy() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"} c.ServeJSON() return } v, is := logs.DeviceRealLogMap[Devicer.T_sn] if !is { logs.DeviceRealLogMap[Devicer.T_sn] = logs.DeviceRealLogR{Time: time.Now(), Data: []string{}} c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: []string{}} c.ServeJSON() return } v.Time = time.Now() logs.DeviceRealLogMap[Devicer.T_sn] = v c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: v.Data} c.ServeJSON() return }