package controllers import ( conf "ColdP_server/conf" "ColdP_server/controllers/MqttServer" "ColdP_server/controllers/lib" "ColdP_server/models/Company" "ColdP_server/models/Device" "ColdP_server/models/Warning" "encoding/json" "fmt" beego "github.com/beego/beego/v2/server/web" "io" "io/ioutil" "math" "strings" "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" } // DeviceList 获取设备列表 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++ } 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, _ := ioutil.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) //发送MQTT for k, v := range t.Sns { topic := 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: v}} msg, _ := json.Marshal(repeatPub) mqttId := strings.Split(Device.ReadDeviceMqttId(k), "\"")[0] client := MqttServer.GetMqttClient(mqttId) for i := 0; i < 3; i++ { time.Sleep(time.Second * time.Duration(i+1)) MqttServer.PubMqttMessage(client, topic, msg) } client.Disconnect() client.Terminate() } c.Data["json"] = lib.JSONS{200, "数据重传成功", nil} c.ServeJSON() return } // ReadDeviation 读取偏差值 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, _ := ioutil.ReadAll(c.Ctx.Request.Body) json.Unmarshal(bytes, &t) fmt.Println("浏览器接收数据:", t) //MQTT发送 fmt.Println("发送MQTT t:", t) deviation := make(chan string, 10) var count = 0 for k, v := range t { topicSub := fmt.Sprintf("/sub/%s", k) fmt.Println(v) topicPub := fmt.Sprintf("/pub/%s", k) mqttId := Device.ReadDeviceMqttId(k) client := MqttServer.GetMqttClient(mqttId) MqttServer.Subscript(client, topicSub, deviation, "\"type\":7,") pubData, _ := json.Marshal(MqttServer.Deviation_Pub{ Sn: k, Type: 7, Mid: time.Now().Unix(), Data: v, }) MqttServer.PubMqttMessage(client, topicPub, pubData) count++ } deviceRepeatData := make([]string, 0) select { case v := <-deviation: fmt.Println("channel收到数据:", v) fmt.Println("count:", count) deviceRepeatData = append(deviceRepeatData, v) count-- if count <= 0 { close(deviation) break } } fmt.Println("响应数据:", deviceRepeatData) c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData} 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) fmt.Println("请求json:", string(bytes)) json.Unmarshal(bytes, &data) for _, v := range data { go func(v MqttServer.Deviation_Sub) { v.Type = 8 v.Mid = int(time.Now().Unix()) mqttid := Device.ReadDeviceMqttId(v.Sn) cli := MqttServer.GetMqttClient(mqttid) bytes, _ := json.Marshal(v) for i := 0; i < 3; i++ { time.Sleep(time.Second * time.Duration(i+1)) MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes) } cli.Disconnect() cli.Terminate() }(v) } c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil} c.ServeJSON() return } // 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, _ := ioutil.ReadAll(c.Ctx.Request.Body) json.Unmarshal(bytes, &t) fmt.Println("浏览器接收数据:", t) //MQTT发送 fmt.Println("发送MQTT t:", t) deviation := make(chan string, 10) var count = 0 for k, v := range t { topicSub := fmt.Sprintf("/sub/%s", k) fmt.Println(v) topicPub := fmt.Sprintf("/pub/%s", k) mqttId := Device.ReadDeviceMqttId(k) client := MqttServer.GetMqttClient(mqttId) MqttServer.Subscript(client, topicSub, deviation, "\"type\":5,") pubData, _ := json.Marshal(MqttServer.Deviation_Pub{ Sn: k, Type: 5, Mid: time.Now().Unix(), Data: v, }) MqttServer.PubMqttMessage(client, topicPub, pubData) count++ } deviceRepeatData := make([]string, 0) select { case v := <-deviation: fmt.Println("channel收到数据:", v) fmt.Println("count:", count) deviceRepeatData = append(deviceRepeatData, v) count-- if count <= 0 { close(deviation) break } } fmt.Println("响应数据:", deviceRepeatData) c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData} c.ServeJSON() return } // WriteDeviation 设置偏差值 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, _ := io.ReadAll(c.Ctx.Request.Body) data := make([]MqttServer.Sensor_Sub, 0) fmt.Println("请求json:", string(bytes)) json.Unmarshal(bytes, &data) for _, v := range data { go func(v MqttServer.Sensor_Sub) { v.Type = 6 v.Mid = int(time.Now().Unix()) mqttid := Device.ReadDeviceMqttId(v.Sn) cli := MqttServer.GetMqttClient(mqttid) bytes, _ := json.Marshal(v) for i := 0; i < 3; i++ { time.Sleep(time.Second * time.Duration(i+1)) MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes) } cli.Disconnect() cli.Terminate() }(v) } c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil} 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 //} var cnt int64 DeviceWarning_List, cnt := Warning.Read_Warning_List(admin_r.T_pid, bindSN, tpList, Time_start, Time_end, page, 10) c.Data["List"] = DeviceWarning_List page_size := math.Ceil(float64(cnt) / float64(conf.Page_size)) 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 c.TplName = "Device/DeviceWarning.html" } 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" } 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 } 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 }