123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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
- }
|