package controllers import ( "Cold_mqtt/lib" "Cold_mqtt/logs" "Cold_mqtt/models/Device" beego "github.com/beego/beego/v2/server/web" "strconv" "strings" "time" ) type MainController struct { beego.Controller } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" } // 列表 - 接口 func (c *MainController) Device_Sensor_Data_More() { // 验证登录 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 } T_sn := c.GetString("T_sn") T_id, _ := c.GetInt("T_id") T_Data := c.GetString("T_Data") // 模板: 温度?湿度?GPS?采集时间| 7.80?53.40?026.6441335,106.7994091?2019-01-08 13:50:30| // 过滤 if len(T_sn) < 4 { c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn Err!"} c.ServeJSON() return } Device_r, err := Device.Read_Device_ByT_sn(T_sn) if err != nil { c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn Err!"} c.ServeJSON() return } // 是否存在传感器 不存在 跳过 DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(T_sn, T_id) if !is { c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn T_id Err!"} c.ServeJSON() return } // 获取传感器参数 DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(Device_r.T_sn, DeviceSensor_r.T_id) if !is { c.Data["json"] = lib.JSONS{Code: 203, Msg: "记录数据 传感器参数不存在 跳过处理"} c.ServeJSON() return } T_Data_list := strings.Split(T_Data, "|") logs.Println("len(T_Data_list)", len(T_Data_list)) for _, v := range T_Data_list { // 7.80?53.40?026.6441335,106.7994091?2019-01-08 13:50:30| if len(v) < 5 { logs.Println(v, "len(v) < 5") continue } v_list := strings.Split(v, "?") if len(v_list) != 4 { logs.Println(v, "len(v_list) != 4") continue } T_t, _ := strconv.ParseFloat(v_list[0], 32) T_rh, _ := strconv.ParseFloat(v_list[1], 32) stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", v_list[3], time.Local) //使用parseInLocation将字符串格式化返回本地时区时间 //// 更新记录 - 缓存 DeviceData_t := Device.DeviceData_R{ T_t: float32(T_t), T_rh: float32(T_rh), T_Site: v_list[4], T_time: stamp, T_sp: DeviceSensorParameter_r.Id, } if r_, DeviceData_old_r := Device.Add_DeviceData(T_sn, T_id, DeviceData_t, true); r_ { // 被替换 Device.Add_DeviceDataOld(Device.DeviceDataOld{ T_sn: T_sn, T_id: T_id, T_t: DeviceData_old_r.T_t, T_rh: DeviceData_old_r.T_rh, T_Site: DeviceData_old_r.T_site, T_time: DeviceData_t.T_time, T_operation: 2, T_uuid: admin_r.T_uuid, }) } } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return }