123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970 |
- package controllers
- import (
- "ColdP_server/conf"
- "ColdP_server/controllers/MqttServer"
- "ColdP_server/controllers/lib"
- "ColdP_server/logs"
- "ColdP_server/models/Company"
- "ColdP_server/models/Device"
- "ColdP_server/models/Warning"
- "encoding/json"
- "fmt"
- beego "github.com/beego/beego/v2/server/web"
- "io"
- "log"
- "math"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- // DeviceController 设备管理页面
- type DeviceController struct {
- beego.Controller
- }
- // DeviceManagerHtml 返回页面
- func (c *DeviceController) DeviceManagerHtml() {
- //验证是否登录
- is, admin := lib.VerificationController(&c.Controller)
- if !is {
- return
- }
- //类名列表
- classList := Company.Read_CompanyClass_All(admin.T_pid, "")
- c.Data["Class_List"] = classList
- c.TplName = "Device/Device.html"
- }
- func (c *DeviceController) DeviceList() {
- tName := c.GetString("deviceName") // 设备名称
- page, _ := c.GetInt64("currentPage") // 当前页码
- tClassify, _ := c.GetInt("deviceClass") // 设备分类
- is, admin := lib.VerificationController(&c.Controller)
- fmt.Println("当前用户的PID为:", admin.T_pid)
- if !is {
- // 用户未登录
- c.Data["json"] = lib.JSONS{202, "身份认证失效", nil}
- c.ServeJSON()
- return
- }
- device_list, count := Device.Read_DeviceSensor_List_T_ClassOr(admin.T_pid, tClassify, tName, tName, -1, int(page), conf.Page_size)
- var pageCount int
- if (int(count) % conf.Page_size) != 0 {
- pageCount = int(count) / conf.Page_size
- pageCount++
- }
- var wg sync.WaitGroup
- for i := range device_list {
- wg.Add(1)
- go func(deviceList *Device.DeviceSensor_) {
- defer func() {
- if r := recover(); r != nil {
- logs.Debug("协程出现异常: %v", r)
- }
- }()
- defer wg.Done()
- t := Device.Read_DeviceData(deviceList.T_sn, deviceList.T_id)
- err2, m := Device.ReadDeviceSensorParameter(deviceList.T_Sp)
- if err2 != nil {
- logs.Error("Error getting device sensor parameter for SN: %s, %v", deviceList.T_sn, err2)
- }
- coldp, _ := MqttServer.FindClodpServerBySnAndId(deviceList.T_sn, deviceList.T_id, 8)
- deviceList.T_tDeviation = 1201
- deviceList.T_RhDeviation = 1201
- if len(coldp.Sn) != 0 {
- deviceList.T_tDeviation = coldp.T_t
- deviceList.T_RhDeviation = coldp.T_h
- deviceList.Is_true = coldp.IsTrue
- }
- deviceList.T_Tlower = m.T_Tlower
- deviceList.T_Tupper = m.T_Tupper
- deviceList.T_RHlower = m.T_RHlower
- deviceList.T_RHupper = m.T_RHupper
- deviceList.T_t = t.T_t
- deviceList.T_rh = t.T_rh
- }(&device_list[i])
- }
- wg.Wait()
- c.Data["json"] = lib.PageHelper{int(count), pageCount, int(page), int(page) >= pageCount, page <= 1, device_list}
- c.ServeJSON()
- return
- }
- // CompanyClass 获取公司设备类目
- func (c *DeviceController) CompanyClass() {
- is, admin := lib.VerificationController(&c.Controller)
- if !is {
- c.Data["json"] = lib.JSONS{202, "用户未登录", nil}
- c.ServeJSON()
- return
- }
- //类名列表
- classList := Company.Read_CompanyClass_All(admin.T_pid, "")
- c.Data["json"] = classList
- c.ServeJSON()
- }
- // DataRepeat 数据重传
- func (c *DeviceController) DataRepeat() {
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- t := MqttServer.DataRepeat_C{}
- bytes, _ := io.ReadAll(c.Ctx.Request.Body)
- json.Unmarshal(bytes, &t)
- fmt.Println("浏览器接收数据:", t)
- s, _ := time.Parse("2006-01-02 15:04:05", t.StartTime)
- e, _ := time.Parse("2006-01-02 15:04:05", t.EndTime)
- var wg sync.WaitGroup
- failSn := make([]string, 0)
- // 发送MQTT
- for k, v := range t.Sns {
- wg.Add(1)
- log.Println("传感器参数", v)
- go func(k string, v []string) {
- defer wg.Done()
- if len(v[0]) > 0 {
- atoi, _ := strconv.Atoi(v[0])
- topicPub := fmt.Sprintf("/pub/%s", k)
- repeatPub := MqttServer.DataRepeat_Pub{
- Sn: k, Type: 9, Mid: time.Now().Unix(), Data: MqttServer.DataRepeat_Pub_Data{Start: s.Unix(), End: e.Unix(), Id: []int{atoi}}}
- msg, _ := json.Marshal(repeatPub)
- mqttId := strings.Split(Device.ReadDeviceMqttId(k), "\"")[0]
- topicSub := fmt.Sprintf("/sub/%s", k)
- timeout := 5 * time.Second
- if len(mqttId) != 0 {
- ack, _, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, msg, timeout)
- if ack {
- } else {
- logs.Error("连接失败", err)
- failSn = append(failSn, k)
- }
- }
- }
- }(k, v)
- }
- wg.Wait()
- if len(failSn) > 0 {
- c.Data["json"] = lib.JSONS{Code: 1201, Msg: "部分重传失败", Data: failSn}
- } else {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "重传数据成功!", Data: nil}
- }
- c.ServeJSON()
- }
- func (c *DeviceController) ReadDeviation() {
- // 用户令牌验证
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- t := make(map[string][]int)
- bytes, err := io.ReadAll(c.Ctx.Request.Body)
- if err != nil {
- log.Printf("Error reading request body: %v", err)
- c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
- c.ServeJSON()
- return
- }
- if err := json.Unmarshal(bytes, &t); err != nil {
- log.Printf("Error unmarshalling JSON: %v", err)
- c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
- c.ServeJSON()
- return
- }
- fmt.Println("浏览器接收数据:", t)
- var errors []string
- for k, v := range t {
- if len(v) > 0 {
- coldp, err := MqttServer.FindClodpServerBySnAndId(k, v[0], 8)
- if err != nil || len(coldp.Sn) == 0 {
- //topicSub := fmt.Sprintf("/sub/%s", k)
- topicPub := fmt.Sprintf("/pub/%s", k)
- mqttId := Device.ReadDeviceMqttId(k)
- //timeout := 5 * time.Second
- // 订阅主题操作,设置超时控制
- pubData, err := json.Marshal(MqttServer.Deviation_Pub{
- Sn: k,
- Type: 7,
- Mid: time.Now().Unix(),
- Data: v,
- })
- if err != nil {
- log.Printf("Error marshalling JSON for SN: %s, %v", k, err)
- errors = append(errors, fmt.Sprintf("设备: %s 序列化消息失败 %v", k, err))
- continue
- }
- if len(mqttId) != 0 {
- if client, ok := MqttServer.MqttCon.Get(mqttId); ok {
- err = MqttServer.MqttPublish(client, topicPub, pubData)
- time.Sleep(3 * time.Second)
- coldp, err = MqttServer.FindClodpServerBySnAndId(k, v[0], 8)
- if err != nil || len(coldp.Sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 500, Msg: "同步参数失败", Data: nil}
- c.ServeJSON()
- return
- }
- }
- var sub MqttServer.Deviation_Sub
- sub.Sn = coldp.Sn
- sub.Data = []MqttServer.Deviation_Sub_Data{
- MqttServer.Deviation_Sub_Data{
- Id: coldp.T_id,
- T: coldp.T_t,
- H: coldp.T_h,
- },
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "读取参数成功", Data: sub}
- c.ServeJSON()
- return
- }
- } else {
- var sub MqttServer.Deviation_Sub
- sub.Sn = coldp.Sn
- sub.Data = []MqttServer.Deviation_Sub_Data{
- MqttServer.Deviation_Sub_Data{
- Id: coldp.T_id,
- T: coldp.T_t,
- H: coldp.T_h,
- },
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "读取参数成功", Data: sub}
- c.ServeJSON()
- return
- }
- }
- }
- }
- // WriteDeviation 设置偏差值
- func (c *DeviceController) WriteDeviation() {
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- bytes, _ := io.ReadAll(c.Ctx.Request.Body)
- data := make([]MqttServer.Deviation_Sub, 0)
- if err := json.Unmarshal(bytes, &data); err != nil {
- log.Printf("Error unmarshalling JSON: %v", err)
- c.Data["json"] = lib.JSONS{Code: 400, Msg: "mqtt请求失败"}
- c.ServeJSON()
- return
- }
- var wg sync.WaitGroup
- successfulSns := make(map[string]bool) // 记录成功的SN
- mutex := &sync.Mutex{} // 保护对successfulSns的访问
- failSn := make([]string, 0)
- handleResult := func(sn string, success bool) {
- mutex.Lock()
- defer mutex.Unlock()
- if success {
- successfulSns[sn] = true
- } else {
- failSn = append(failSn, sn)
- }
- }
- // 处理每个设备的数据
- for _, v := range data {
- wg.Add(1)
- go func(v MqttServer.Deviation_Sub) {
- defer wg.Done()
- v.Type = 8
- v.Mid = int64(int(time.Now().Unix()))
- mqttId := Device.ReadDeviceMqttId(v.Sn)
- topicPub := fmt.Sprintf("/pub/%s", v.Sn)
- msgBytes, err := json.Marshal(v)
- if err != nil {
- log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
- handleResult(v.Sn, false)
- return
- }
- if len(mqttId) != 0 {
- clos := make([]string, 0)
- if client, ok := MqttServer.MqttCon.Get(mqttId); ok {
- err = MqttServer.MqttPublish(client, topicPub, msgBytes)
- var cold MqttServer.ClodpServer
- if err != nil {
- cold.Sn = v.Sn
- cold.T_id = v.Data[0].Id
- cold.T_h = v.Data[0].H
- cold.T_t = v.Data[0].T
- cold.IsTrue = 1
- cold.Types = 8
- cold.CreateTime = time.Now()
- cold.UpdateTime = time.Now()
- err := MqttServer.Add_ClodpServer(cold)
- if err != nil {
- logs.Error("设置参数失败", err)
- }
- failSn = append(failSn, v.Sn)
- }
- time.Sleep(3 * time.Second)
- if len(v.Data) > 0 {
- coldp, err := MqttServer.FindClodpServerBySnAndId(v.Sn, v.Data[0].Id, 8)
- if err != nil {
- failSn = append(failSn, v.Sn)
- }
- if len(coldp.Sn) == 0 {
- coldp.Sn = v.Sn
- coldp.T_id = v.Data[0].Id
- coldp.T_h = v.Data[0].H
- coldp.T_t = v.Data[0].T
- coldp.IsTrue = 1
- coldp.Types = 8
- coldp.CreateTime = time.Now()
- coldp.UpdateTime = time.Now()
- err := MqttServer.Add_ClodpServer(coldp)
- if err != nil {
- logs.Error("设置参数失败", err)
- }
- failSn = append(failSn, v.Sn)
- }
- if coldp.T_h != v.Data[0].H || coldp.T_t != v.Data[0].T {
- coldp.T_h = v.Data[0].H
- coldp.T_t = v.Data[0].T
- coldp.IsTrue = 1
- clos = append(clos, "t_t")
- clos = append(clos, "t_h")
- clos = append(clos, "is_true")
- MqttServer.Update_ClodpServer(coldp, clos...)
- }
- }
- }
- }
- }(v)
- }
- wg.Wait()
- if len(failSn) > 0 {
- c.Data["json"] = lib.JSONS{Code: 1201, Msg: "部分设置失败", Data: failSn}
- } else {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
- }
- c.ServeJSON()
- }
- // WriteDeviationAll 批量设置偏差值
- func (c *DeviceController) WriteDeviationAll() {
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- bytes, _ := io.ReadAll(c.Ctx.Request.Body)
- fmt.Println("请求json:", string(bytes))
- var data MqttServer.Deviation_SubAll
- if err := json.Unmarshal(bytes, &data); err != nil {
- log.Printf("Error unmarshalling JSON: %v", err)
- c.Data["json"] = lib.JSONS{Code: 400, Msg: "mqtt请求失败"}
- c.ServeJSON()
- return
- }
- var wg sync.WaitGroup
- successfulSns := make(map[string]bool) // 记录成功的SN
- mutex := &sync.Mutex{} // 保护对successfulSns的访问
- failSn := make([]string, 0)
- handleResult := func(sn string, success bool) {
- mutex.Lock()
- defer mutex.Unlock()
- if success {
- successfulSns[sn] = true
- } else {
- failSn = append(failSn, sn)
- }
- }
- // 处理每个设备的数据
- for i, sn := range data.Sn {
- subData := data.Data[i]
- wg.Add(1)
- go func(sn string, subData MqttServer.Deviation_Sub_Data) {
- defer wg.Done()
- sub_data := make([]MqttServer.Deviation_Sub_Data, 0)
- sub_data = append(sub_data, subData)
- v := MqttServer.Deviation_Sub{
- Type: 8,
- Sn: sn,
- Mid: int64(int(time.Now().Unix())),
- Data: sub_data,
- }
- mqttId := Device.ReadDeviceMqttId(v.Sn)
- topicPub := fmt.Sprintf("/pub/%s", v.Sn)
- msgBytes, err := json.Marshal(v)
- if len(mqttId) != 0 {
- clos := make([]string, 0)
- if client, ok := MqttServer.MqttCon.Get(mqttId); ok {
- err = MqttServer.MqttPublish(client, topicPub, msgBytes)
- var cold MqttServer.ClodpServer
- if err != nil {
- cold.Sn = v.Sn
- cold.T_id = v.Data[0].Id
- cold.T_h = v.Data[0].H
- cold.T_t = v.Data[0].T
- cold.IsTrue = 1
- cold.Types = 8
- cold.CreateTime = time.Now()
- cold.UpdateTime = time.Now()
- err := MqttServer.Add_ClodpServer(cold)
- if err != nil {
- logs.Error("设置参数失败", err)
- }
- handleResult(v.Sn, false)
- }
- time.Sleep(3 * time.Second)
- if len(v.Data) > 0 {
- coldp, err := MqttServer.FindClodpServerBySnAndId(v.Sn, v.Data[0].Id, 8)
- log.Println("得到参数", coldp)
- if err != nil {
- failSn = append(failSn, v.Sn)
- }
- if len(coldp.Sn) == 0 {
- coldp.Sn = v.Sn
- coldp.T_id = v.Data[0].Id
- coldp.T_h = v.Data[0].H
- coldp.T_t = v.Data[0].T
- coldp.IsTrue = 1
- coldp.Types = 8
- coldp.CreateTime = time.Now()
- coldp.UpdateTime = time.Now()
- err := MqttServer.Add_ClodpServer(coldp)
- if err != nil {
- logs.Error("设置参数失败", err)
- }
- handleResult(v.Sn, false)
- }
- if coldp.T_h != v.Data[0].H || coldp.T_t != v.Data[0].T {
- coldp.T_h = v.Data[0].H
- coldp.T_t = v.Data[0].T
- coldp.IsTrue = 1
- clos = append(clos, "t_t")
- clos = append(clos, "t_h")
- clos = append(clos, "is_true")
- MqttServer.Update_ClodpServer(coldp, clos...)
- handleResult(v.Sn, false)
- }
- }
- }
- }
- }(sn, subData)
- }
- wg.Wait()
- if len(failSn) > 0 {
- c.Data["json"] = lib.JSONS{Code: 1201, Msg: "部分设置失败", Data: failSn}
- } else {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
- }
- c.ServeJSON()
- }
- // ReadSensor 读取传感器参数
- func (c *DeviceController) ReadSensor() {
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- t := make(map[string][]int)
- bytes, _ := io.ReadAll(c.Ctx.Request.Body)
- if err := json.Unmarshal(bytes, &t); err != nil {
- log.Printf("Error unmarshalling JSON: %v", err)
- c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
- c.ServeJSON()
- return
- }
- fmt.Println("浏览器接收数据:", t)
- for k, v := range t {
- topicPub := fmt.Sprintf("/pub/%s", k)
- mqttId := Device.ReadDeviceMqttId(k)
- pubData, err := json.Marshal(MqttServer.Deviation_Pub{
- Sn: k,
- Type: 5,
- Mid: time.Now().Unix(),
- Data: v,
- })
- if err != nil {
- logs.Error("json序列化失败")
- }
- if len(mqttId) != 0 {
- if client, ok := MqttServer.MqttCon.Get(mqttId); ok {
- err = MqttServer.MqttPublish(client, topicPub, pubData)
- if err != nil {
- logs.Error("发送失败")
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "读取传感器参数失败", Data: nil}
- c.ServeJSON()
- return
- } else {
- if len(v) > 0 {
- time.Sleep(3 * time.Second)
- coldp, err := MqttServer.FindClodpServerBySnAndId(k, v[0], 6)
- if err != nil {
- logs.Error("发送失败")
- c.Data["json"] = lib.JSONS{Code: 500, Msg: "读取传感器参数失败", Data: nil}
- c.ServeJSON()
- return
- }
- if len(coldp.Sn) == 0 {
- logs.Error("发送失败")
- c.Data["json"] = lib.JSONS{Code: 500, Msg: "读取传感器参数失败", Data: nil}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: coldp}
- c.ServeJSON()
- return
- }
- }
- }
- }
- }
- }
- // WriteSensor 设置传感器参数
- func (c *DeviceController) WriteSensor() {
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- bytes, err := io.ReadAll(c.Ctx.Request.Body)
- if err != nil {
- log.Printf("Error reading request body: %v", err)
- c.Data["json"] = lib.JSONS{Code: 500, Msg: "Internal Server Error"}
- c.ServeJSON()
- return
- }
- fmt.Println("请求json:", string(bytes))
- clos := make([]string, 0)
- data := make([]MqttServer.Sensor_Sub, 0)
- if err := json.Unmarshal(bytes, &data); err != nil {
- log.Printf("Error unmarshalling JSON: %v", err)
- c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
- c.ServeJSON()
- return
- }
- for _, v := range data {
- mqttId := Device.ReadDeviceMqttId(v.Sn)
- topicPub := fmt.Sprintf("/pub/%s", v.Sn)
- v.Type = 6
- v.Mid = int(time.Now().Unix())
- msgBytes, err := json.Marshal(v)
- if err != nil {
- log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
- return
- }
- log.Println("传感器下发参数", v)
- if client, ok := MqttServer.MqttCon.Get(mqttId); ok {
- err = MqttServer.MqttPublish(client, topicPub, msgBytes)
- if err != nil {
- if len(v.Data) > 0 {
- coldpServer := MqttServer.ClodpServer{
- Sn: v.Sn,
- T_id: v.Data[0].Id,
- IsTrue: 1,
- Sense: v.Data[0].Sense,
- Speed: v.Data[0].Speed,
- Types: 6,
- CreateTime: time.Now(),
- UpdateTime: time.Now(),
- }
- MqttServer.Add_ClodpServer(coldpServer)
- }
- c.Data["json"] = lib.JSONS{Code: 500, Msg: "传感器参数失败", Data: nil}
- c.ServeJSON()
- return
- } else {
- if len(v.Data) > 0 {
- time.Sleep(3 * time.Second)
- coldp, err := MqttServer.FindClodpServerBySnAndId(v.Sn, v.Data[0].Id, v.Type)
- if len(coldp.Sn) == 0 || err != nil {
- if len(v.Data) > 0 {
- coldpServer := MqttServer.ClodpServer{
- Sn: v.Sn,
- T_id: v.Data[0].Id,
- IsTrue: 1,
- Sense: v.Data[0].Sense,
- Speed: v.Data[0].Speed,
- Types: 6,
- CreateTime: time.Now(),
- UpdateTime: time.Now(),
- }
- MqttServer.Add_ClodpServer(coldpServer)
- }
- c.Data["json"] = lib.JSONS{Code: 500, Msg: "读取传感器参数失败", Data: nil}
- c.ServeJSON()
- return
- }
- if len(v.Data) > 0 && (v.Data[0].Speed != coldp.Speed || v.Data[0].Sense != coldp.Sense) {
- coldp.Sense = v.Data[0].Sense
- coldp.Speed = v.Data[0].Speed
- coldp.IsTrue = 1
- clos = append(clos, "speed")
- clos = append(clos, "sense")
- clos = append(clos, "is_true")
- MqttServer.Update_ClodpServer(coldp, clos...)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: coldp}
- c.ServeJSON()
- return
- }
- }
- }
- }
- }
- // 列表 -
- func (c *DeviceController) DeviceWarning_List_html() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- c.Data["Admin_r"] = admin_r
- page, _ := c.GetInt("page")
- println(page)
- if page < 1 {
- page = 1
- }
- c.Data["Admin_r"] = admin_r
- bindSN := c.GetStrings("bindSN")
- tpList := c.GetStrings("tpList")
- T_sn := c.GetString("T_sn")
- if len(T_sn) != 0 {
- bindSN = append(bindSN, T_sn)
- c.Data["T_sn"] = T_sn
- }
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- if len(Time_start) == 0 && len(Time_end) == 0 {
- Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
- Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
- }
- c.Data["Time_start"] = Time_start
- c.Data["Time_end"] = Time_end
- //c.Data["Class_List"] = Device.Read_DeviceWarningList_All_1()
- //T_Title := ""
- //if Class_1 > 0 {
- // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
- //}
- pageSize := c.GetString("pageSize", "100")
- pageSizeInt, _ := strconv.Atoi(pageSize)
- //atoi, _ := strconv.Atoi(pageSizes.PageSize)
- getString := c.GetString("t_tp")
- t_tp, _ := strconv.Atoi(getString)
- var cnt int64
- DeviceWarning_List, cnt := Warning.Read_Warning_List(admin_r.T_pid, bindSN, tpList, Time_start, Time_end, t_tp, page, pageSizeInt)
- c.Data["List"] = DeviceWarning_List
- page_size := math.Ceil(float64(cnt) / float64(pageSizeInt))
- c.Data["Page"] = page
- c.Data["Page_size"] = page_size
- c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
- c.Data["cnt"] = cnt
- // 将sync.Map中的数据转存到切片中
- c.TplName = "Device/DeviceWarning.html"
- }
- // GetWarningtype 获取报警类型
- func (c *DeviceController) GetWarningtype() {
- var results []struct {
- Key int
- Value string
- }
- Warning.WarningType_list.Range(func(key, value any) bool {
- // 确保类型断言成功
- if k, ok := key.(int); ok {
- if v, ok := value.(string); ok {
- // 创建匿名结构体实例并添加到切片
- results = append(results, struct {
- Key int
- Value string
- }{Key: k, Value: v})
- } else {
- fmt.Println("Value is not of type string")
- }
- } else {
- fmt.Println("Key is not of type int")
- }
- return true // 继续遍历
- })
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: results}
- c.ServeJSON()
- log.Println(results)
- }
- func (c *DeviceController) DeviceWarning_() {
- // 验证登录
- //b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- // c.ServeJSON()
- // return
- //}
- c.Data["WarningType"] = Warning.Read_WarningType_All()
- c.TplName = "Device/DeviceWarning-.html"
- }
- // DeviceWarning_Post 添加报警
- func (c *DeviceController) DeviceWarning_Post() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- T_tp, _ := c.GetInt("T_tp")
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- T_Ut := c.GetString("T_Ut")
- T_Remark := c.GetString("T_Remark")
- r_Device, err := Device.Read_Device_ByT_sn(T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
- c.ServeJSON()
- return
- }
- // 获取 传感器 参数
- DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, T_id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "编号 错误!!!"}
- c.ServeJSON()
- return
- }
- t1, _ := time.ParseInLocation("2006-01-02 15:04:05", T_Ut, time.Local) // +8 时差
- t_c := Warning.Warning{
- T_pid: admin_r.T_pid,
- T_tp: T_tp,
- T_sn: T_sn,
- T_D_name: r_Device.T_devName,
- T_id: T_id,
- T_DS_name: DeviceSensor_r.T_name,
- T_Ut: t1,
- T_State: 1,
- T_Remark: T_Remark,
- }
- Warning.Add_Warning(t_c)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // DeviceWarning_Del 删除报警
- func (c *DeviceController) DeviceWarning_Del() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- Id, _ := c.GetInt("Id")
- if Id > 1000 {
- Warning.Delete_Warning(admin_r.T_pid, Id)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // DeviceWarning_DelS 批量删除
- func (c *DeviceController) DeviceWarning_DelS() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- type SelectedItem struct {
- Id string `json:"id"`
- Ut string `json:"ut"` // 假设"ut"字段是时间戳字符串
- }
- type SelectedItems struct {
- SelectedIds []SelectedItem `json:"selectedIds"`
- }
- var SelectedIds SelectedItems
- //var selectedIds map[string]any
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &SelectedIds)
- log.Println(SelectedIds.SelectedIds)
- for _, v := range SelectedIds.SelectedIds {
- Warning.Delete_Warning_List(v.Id, v.Ut, admin_r.T_pid)
- }
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "数据格式错误!!!"}
- c.ServeJSON()
- } else {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- }
- }
- // Device_Copy 复制并添加
- func (c *DeviceController) Device_Copy() {
- // 验证登录
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- type T_data struct {
- T_sn string `json:"t_sn"`
- T_id int `json:"t_id"`
- T_Rh any `json:"t_rh"`
- T_Site string `json:"t_site"`
- T_T any `json:"t_t"`
- CreateTime string `json:"createTime"`
- }
- var data T_data
- json.Unmarshal(c.Ctx.Input.RequestBody, &data)
- r := Device.Read_DeviceParameter_SNNo(data.T_sn)
- parse, _ := time.Parse("2006-01-02 15:04:05", data.CreateTime)
- data.CreateTime = parse.Add(time.Duration(r[0].T_saveT) * time.Second).Format("2006-01-02 15:04:05")
- itoa := strconv.Itoa(data.T_id)
- Device.Copy_DeviceData(data.T_sn, itoa, data.T_Rh, data.T_T, data.T_Site, data.CreateTime)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
- c.ServeJSON()
- }
- // DeviceWarningUpdate 修改报警
- func (c *DeviceController) DeviceWarningUpdate() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- var data struct {
- ColumnName string `json:"columnName"`
- NewValue string `json:"newValue"`
- RowId string `json:"rowId"`
- T_Ut string `json:"T_Ut"`
- SN string `json:"sn"`
- }
- json.Unmarshal(c.Ctx.Input.RequestBody, &data)
- log.Println(admin_r.T_pid)
- Warning.Update_DeviceParameter_Warning(data.ColumnName, data.NewValue, data.RowId, data.T_Ut)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
- c.ServeJSON()
- }
- // GetDeviceALLSN 根据pid获取所有sn
- func (c *DeviceController) GetDeviceALLSN() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- r := Device.Read_Device_All_SN(admin_r.T_pid)
- log.Println(r)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
- c.ServeJSON()
- }
- // GetDeviceALLTID 根据SN获取所有探头
- func (c *DeviceController) GetDeviceALLTID() {
- // 验证登录
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- getString := c.GetString("sn")
- r := Device.Get_DeviceSensor_Tid_ByT_sn(getString)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: r}
- c.ServeJSON()
- }
- // DeviceWarningAdd 复制添加报警
- func (c *DeviceController) DeviceWarningAdd() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- var data struct {
- T_tp_name string `json:"column1"`
- T__d_name string `json:"column2"`
- T_DS_name string `json:"column3"`
- T_Remark string `json:"column4"`
- T_State string `json:"column5"`
- T_Ut string `json:"column6"`
- RowId string `json:"rowId"`
- SN string `json:"sn"`
- T_id string `json:"t_id"`
- }
- json.Unmarshal(c.Ctx.Input.RequestBody, &data)
- atoi, _ := strconv.Atoi(data.T_id)
- r_Device, err := Device.Read_Device_ByT_sn(data.SN)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
- c.ServeJSON()
- return
- }
- T_tp := Warning.Read_WarningType(data.T_tp_name)
- t1, _ := time.ParseInLocation("2006-01-02 15:04:05", data.T_Ut, time.Local) // +8 时差
- t_c := Warning.Warning{
- T_pid: admin_r.T_pid,
- T_tp: T_tp,
- T_sn: data.SN,
- T_D_name: r_Device.T_devName,
- T_id: atoi,
- T_DS_name: data.T_DS_name,
- T_Ut: t1,
- T_State: 1,
- T_Remark: data.T_Remark,
- }
- Warning.Add_Warning(t_c)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // SiftWarningType 根据报警类型筛选
- func (c *DeviceController) SiftWarningType() {
- // 验证登录
- b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- }
|