package controllers import ( "ColdP_server/controllers/lib" "ColdP_server/logs" "ColdP_server/models/Company" "ColdP_server/models/Device" "encoding/json" "fmt" "github.com/beego/beego/v2/adapter/orm" beego "github.com/beego/beego/v2/server/web" "github.com/xuri/excelize/v2" "io" "strconv" "strings" "time" ) type DataGeneratorController struct { beego.Controller } // GeneratorHtml 获取页面 func (c *DataGeneratorController) GeneratorHtml() { b_, admin := 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 } classList := Company.Read_CompanyClass_All(admin.T_pid, "") c.Data["Class_List"] = classList //确认状态为登录状态后 c.TplName = "Data/GeneratorData2.html" } // DeviceSensorData 获取对应设备探头数据 func (c *DataGeneratorController) DeviceSensorData() { 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 } startTime := c.GetString("startTime") endTime := c.GetString("endTime") sns := make([][]string, 0) json.Unmarshal([]byte(c.GetString("sns")), &sns) type Temp struct { Sn []string `json:"sn"` Data []Device.DeviceSensorData `json:"data"` } datas := make([]Temp, 0) for _, sn := range sns { //sn = [sn,探头id] id, _ := strconv.ParseInt(sn[1], 10, 63) v, err := Device.ReadDeviceSensorByTsnTidTimeRange(sn[0], int(id), startTime, endTime) if err != nil { c.Data["json"] = lib.JSONS{Code: 500, Msg: "读取设备探头错误!"} c.ServeJSON() return } datas = append(datas, Temp{sn, v}) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取数据成功", Data: datas} c.ServeJSON() } // UpdateFix 更新固定值 func (c *DataGeneratorController) UpdateFix() { 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 } //1.解析数据 var body = c.Ctx.Request.Body defer body.Close() type T struct { FixTemperature string `json:"fixTemperature"` FixHumidity string `json:"fixHumidity"` Sns [][2]string `json:"sns"` Data []int64 `json:"data"` } var temp = T{} bytes, _ := io.ReadAll(body) json.Unmarshal(bytes, &temp) fmt.Println("解析后:", temp.Data[0]) //2.得到数据进行统一设置访问修改 humidity, _ := strconv.ParseFloat(temp.FixHumidity, 64) temperature, _ := strconv.ParseFloat(temp.FixTemperature, 64) //开始时间到结束时间 startTime := time.UnixMilli(temp.Data[0]).Format("2006-01-02 15:04:05") endTime := time.UnixMilli(temp.Data[1]).Format("2006-01-02 15:04:05") //3.循环更新数据 for _, v := range temp.Sns { sn := v[0] tId := v[1] Device.UpdateDeviceSensorDataTemperatureAndHumidity(sn, tId, startTime, endTime, temperature, humidity) } //4.反馈成功 c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil} c.ServeJSON() } // Delete 删除 func (c *DataGeneratorController) Delete() { 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 } //1.解析数据 var body = c.Ctx.Request.Body defer body.Close() type T struct { Sns [][2]string `json:"sns"` Data []int64 `json:"data"` } var temp = T{} bytes, _ := io.ReadAll(body) json.Unmarshal(bytes, &temp) fmt.Println("解析后:", temp.Data[0]) //开始时间到结束时间 startTime := time.UnixMilli(temp.Data[0]).Format("2006-01-02 15:04:05") endTime := time.UnixMilli(temp.Data[1]).Format("2006-01-02 15:04:05") //3.循环更新数据 for _, v := range temp.Sns { sn := v[0] tId := v[1] Device.DeleteDeviceSensorDataByTimeRange(sn, tId, startTime, endTime) } //4.反馈成功 c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil} c.ServeJSON() } // UpdateRand 更新随机值 func (c *DataGeneratorController) UpdateRand() { 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 } //解析请求参数 body := c.Ctx.Request.Body defer body.Close() bytes, _ := io.ReadAll(body) type T struct { TemperatureMin int `json:"temperatureMin"` TemperatureMax int `json:"temperatureMax"` HumidityMax int `json:"humidityMax"` HumidityMin int `json:"humidityMin"` Sns [][2]string `json:"sns"` Data []int64 `json:"data"` } var t = T{} json.Unmarshal(bytes, &t) fmt.Println("requestJSON: ", t, "原始json:", string(bytes)) //开始时间到结束时间 startTime := time.UnixMilli(t.Data[0]).Format("2006-01-02 15:04:05") endTime := time.UnixMilli(t.Data[1]).Format("2006-01-02 15:04:05") for _, v := range t.Sns { sn := v[0] tId := v[1] Device.UpdateDeviceSensorDataTemperatureAndHumidityRandom(sn, tId, startTime, endTime, t.TemperatureMax, t.TemperatureMin, t.HumidityMax, t.HumidityMin) } //反馈成功 c.Data["json"] = lib.JSONS{200, "调整随机偏移值成功!", nil} c.ServeJSON() return } // CopyFromPosition 数据拷贝 func (c *DataGeneratorController) CopyFromPosition() { b_, admin := 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 } var body = c.Ctx.Request.Body defer body.Close() bytes, _ := io.ReadAll(body) type T struct { CopyPosition string `json:"copyPosition"` Sns [][2]string `json:"sns"` Data []int64 `json:"data"` } t := T{} json.Unmarshal(bytes, &t) //开始时间 和结束时间,插入的时间范围 fmt.Println(t) copyTime, _ := time.Parse("2006-01-02 15:04:05", t.CopyPosition) startTime := time.UnixMilli(t.Data[0]).Format("2006-01-02 15:04:05") endTime := time.UnixMilli(t.Data[1]).Format("2006-01-02 15:04:05") for _, v := range t.Sns { sn := v[0] tId := v[1] list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, startTime, endTime) saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT ct := copyTime go func(list []Device.DeviceSensorData, sn string, saveTime int) { for _, d := range list { d.T_time = ct.Format("2006-01-02 15:04:05") d.CreateTime = ct.Format("2006-01-02 15:04:05") Device.InsertDeviceSensorData(sn, d, admin) ct = ct.Add(time.Second * time.Duration(saveTime)) } }(list, sn, saveTime) } c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil} c.ServeJSON() return } // RepairSensorData 数据补漏 func (c *DataGeneratorController) RepairSensorData() { b_, admin := 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 } sns := make([][2]string, 0) timeRange := make([]int64, 0) json.Unmarshal([]byte(c.GetString("sns")), &sns) json.Unmarshal([]byte(c.GetString("data")), &timeRange) start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05") end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05") num := 0 for _, v := range sns { sn := v[0] tId := v[1] saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end) for i := 0; i < len(list)-1; i++ { current := list[i].T_time next := list[i+1].T_time ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current) //format := cts.Format("2006-01-02 15:04:05") //ct, _ := time.Parse("2006-01-02 15:04:05", format) n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next) //nsfmt := ns.Format("2006-01-02 15:04:05") //n, _ := time.Parse("2006-01-02 15:04:05", nsfmt) interval := n.Unix() - ct.Unix() logs.Debug("时间间隔:", interval) fmt.Println("当前:", current, "下一个:", next) if int(interval) > saveTime { ttInterval := list[i+1].T_t - list[i].T_t ttt := list[i].T_t // 温度临时变量 trhInterval := list[i+1].T_rh - list[i].T_rh trht := list[i].T_rh //湿度临时变量 count := int(interval) / saveTime num += count for k := 0; k < count; k++ { t := ct.Format("2006-01-02 15:04:05") //时间临时变量 ttt += ttInterval / float64(count) trht += trhInterval / float64(count) Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{ list[i].T_id, list[i].T_sp, t, lib.Decimal(ttt), lib.Decimal(trht), list[i].T_site, list[i].CreateTime, }, admin) ct = ct.Add(time.Second * time.Duration(saveTime)) } } } } c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil} c.ServeJSON() return } // RepairAllSensorData 补漏所有缺失数据 // // func (c *DataGeneratorController) RepairAllSensorData() { // 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 // } // o := orm.NewOrm() // // 查询设备 BX200GSE MD100 MD200G BX100 KF100 MD300G // deviceList := []Device.Device{} // // var err error // _, err = o.QueryTable(new(Device.Device)). // Filter("T_model__in", []string{"BX200GSE", "MD100", "MD200G", "BX100", "KF100", "MD300G"}). // Filter("T_state", 1). // //Filter("T_sn", "2023388677995151"). // All(&deviceList) // if err != nil { // logs.Error("获取设备列表失败", err) // return // } // start := "2024-07-01 00:00:00" // end := time.Now().Format("2006-01-02 15:04:05") // num := 0 // for _, device := range deviceList { // // 获取传感器列表 // deviceSensorList := []Device.DeviceSensor{} // _, err = o.QueryTable(new(Device.DeviceSensor)).Filter("T_sn", device.T_sn).All(&deviceSensorList) // if err != nil { // logs.Error(lib.FuncName(), err) // } // counts := 0 // for _, v := range deviceSensorList { // sn := v.T_sn // //tId := v.T_id // itoa := strconv.Itoa(v.T_id) // saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT // list := Device.SelectDeviceSensorDataListByTimeRange(sn, itoa, start, end) // // for i := 0; i < len(list)-1; i++ { // current := list[i].T_time // next := list[i+1].T_time // ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current) // n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next) // interval := n.Unix() - ct.Unix() // if int(interval) > saveTime { // ttInterval := list[i+1].T_t - list[i].T_t // ttt := list[i].T_t // 温度临时变量 // trhInterval := list[i+1].T_rh - list[i].T_rh // trht := list[i].T_rh //湿度临时变量 // count := int(interval) / saveTime // num += count // counts += count // for k := 0; k < count; k++ { // t := ct.Format("2006-01-02 15:04:05") //时间临时变量 // ttt += ttInterval / float64(count) // trht += trhInterval / float64(count) // //Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{ // // list[i].T_id, // // list[i].T_sp, // // t, // // lib.Decimal(ttt), // // lib.Decimal(trht), // // list[i].T_site, // // list[i].CreateTime, // //}, admin) // ct = ct.Add(time.Second * time.Duration(saveTime)) // log.Print(t) // } // } // } // } // // 每个设备补漏完成后睡眠一秒 // time.Sleep(time.Second * 1) // logs.Info(fmt.Sprintf("设备 %s 补漏完成!共补漏 %d 条数据", device.T_sn, counts)) // } // c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil} // c.ServeJSON() // return // } //func (c *DataGeneratorController) RepairAllSensorData() { // b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) // if !b_ { // // 用户验证逻辑省略 // } // o := orm.NewOrm() // deviceList := []Device.Device{} // // var err error // _, err = o.QueryTable(new(Device.Device)). // Filter("T_model__in", []string{"BX200GSE", "MD100", "MD200G", "BX100", "KF100", "MD300G"}). // Filter("T_state", 1). // //Filter("T_sn", "2023388677995151"). // All(&deviceList) // if err != nil { // logs.Error("获取设备列表失败", err) // return // } // // start := "2024-07-01 00:00:00" // end := time.Now().Format("2006-01-02 15:04:05") // // // 创建或打开日志文件 // logFile, err := os.OpenFile("repair_log.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) // if err != nil { // logs.Error("打开日志文件失败", err) // return // } // defer logFile.Close() // // // 使用 log 包的 Writer 将日志输出到文件 // fileLogger := log.New(logFile, "", log.LstdFlags) // for _, device := range deviceList { // deviceSensorList := []Device.DeviceSensor{} // _, err = o.QueryTable(new(Device.DeviceSensor)).Filter("T_sn", device.T_sn).All(&deviceSensorList) // if err != nil { // logs.Error(lib.FuncName(), err) // continue // } // // num := 0 // 统计当前设备补漏的数据条数 // for _, v := range deviceSensorList { // sn := v.T_sn // itoa := strconv.Itoa(v.T_id) // saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT // list := Device.SelectDeviceSensorDataListByTimeRange(sn, itoa, start, end) // // for i := 0; i < len(list)-1; i++ { // current := list[i].T_time // next := list[i+1].T_time // ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current) // n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next) // interval := n.Unix() - ct.Unix() // if int(interval) > saveTime { // ttInterval := list[i+1].T_t - list[i].T_t // ttt := list[i].T_t // 温度临时变量 // trhInterval := list[i+1].T_rh - list[i].T_rh // trht := list[i].T_rh //湿度临时变量 // count := int(interval) / saveTime // num += count // for k := 0; k < count; k++ { // t := ct.Format("2006-01-02 15:04:05") //时间临时变量 // ttt += ttInterval / float64(count) // trht += trhInterval / float64(count) // Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{ // list[i].T_id, // list[i].T_sp, // t, // lib.Decimal(ttt), // lib.Decimal(trht), // list[i].T_site, // list[i].CreateTime, // }, admin) // ct = ct.Add(time.Second * time.Duration(saveTime)) // log.Print(t) // } // } // } // } // // 每个设备补漏完成后记录到文件 // fileLogger.Printf("设备 %s 补漏完成!共补漏 %d 条数据\n", device.T_sn, num) // time.Sleep(time.Second * 1) // } // // c.Data["json"] = lib.JSONS{200, fmt.Sprintf("所有设备补漏完成!"), nil} // c.ServeJSON() // return //} func (c *DataGeneratorController) RepairAllSensorData() { b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey")) if !b_ { // 用户验证逻辑省略 } o := orm.NewOrm() deviceList := []Device.Device{} var err error _, err = o.QueryTable(new(Device.Device)). Filter("T_model__in", []string{"BX200GSE", "MD100", "MD200G", "BX100", "KF100", "MD300G"}). Filter("T_state", 1). //Filter("T_pid", 80). Filter("T_sn", "kf861693223539284"). All(&deviceList) if err != nil { logs.Error("获取设备列表失败", err) return } start := "2024.05.14 00:00:00" end := time.Now().Format("2006-01-02 15:04:05") // 初始化Excel文件 f := excelize.NewFile() // 创建工作表 sheetName := "RepairLog" f.NewSheet(sheetName) // 设置列标题 f.SetCellValue(sheetName, "A1", "设备序列号") f.SetCellValue(sheetName, "B1", "传感器ID") f.SetCellValue(sheetName, "C1", "补漏起始时间") f.SetCellValue(sheetName, "D1", "补漏结束时间") f.SetCellValue(sheetName, "E1", "补漏数据量") row := 2 // 从第二行开始记录数据 totalRows := 0 // 总的补漏条数 for _, device := range deviceList { deviceSensorList := []Device.DeviceSensor{} _, err = o.QueryTable(new(Device.DeviceSensor)).Filter("T_sn", device.T_sn).All(&deviceSensorList) if err != nil { logs.Error(lib.FuncName(), err) continue } num := map[int]int{} // 用于存储每个传感器ID补漏的数量 startTime := map[int]string{} // 用于存储每个传感器ID补漏的起始时间 endTime := map[int]string{} // 用于存储每个传感器ID补漏的结束时间 for _, v := range deviceSensorList { sn := v.T_sn itoa := strconv.Itoa(v.T_id) saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT list := Device.SelectDeviceSensorDataListByTimeRange(sn, itoa, start, end) // 确定每个传感器ID补漏的起始时间和结束时间 for i := 0; i < len(list)-1; i++ { current := list[i].T_time next := list[i+1].T_time ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current) n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next) interval := n.Unix() - ct.Unix() if int(interval) > saveTime { ttInterval := list[i+1].T_t - list[i].T_t ttt := list[i].T_t // 温度临时变量 trhInterval := list[i+1].T_rh - list[i].T_rh trht := list[i].T_rh //湿度临时变量 count := int(interval) / saveTime num[v.T_id] += count for k := 0; k < count; k++ { t := ct.Add(time.Duration(k) * time.Duration(saveTime) * time.Second).Format("2006-01-02 15:04:05") //时间临时变量 ttt += ttInterval / float64(count) trht += trhInterval / float64(count) Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{ list[i].T_id, list[i].T_sp, t, lib.Decimal(ttt), lib.Decimal(trht), list[i].T_site, list[i].CreateTime, }, admin) logs.Println(t) } // 更新起始时间和结束时间 if _, ok := startTime[v.T_id]; !ok { startTime[v.T_id] = ct.Format("2006-01-02 15:04:05") } endTime[v.T_id] = ct.Add(time.Duration(count-1) * time.Duration(saveTime) * time.Second).Format("2006-01-02 15:04:05") } } } // 记录每个设备及其每个传感器ID的数据 for sensorID, count := range num { if count > 0 { // 只记录补漏数量大于0的数据 f.SetCellValue(sheetName, fmt.Sprintf("A%d", row), device.T_sn) f.SetCellValue(sheetName, fmt.Sprintf("B%d", row), sensorID) f.SetCellValue(sheetName, fmt.Sprintf("C%d", row), startTime[sensorID]) f.SetCellValue(sheetName, fmt.Sprintf("D%d", row), endTime[sensorID]) f.SetCellValue(sheetName, fmt.Sprintf("E%d", row), count) row++ totalRows += count } } //time.Sleep(time.Second * 1) } // 在最后一行记录总的补漏条数 f.SetCellValue(sheetName, fmt.Sprintf("A%d", row), "总补漏条数") f.SetCellValue(sheetName, fmt.Sprintf("B%d", row), totalRows) // 保存Excel文件 if err := f.SaveAs("repair_log.xlsx"); err != nil { logs.Error("保存Excel文件失败", err) return } c.Data["json"] = lib.JSONS{200, fmt.Sprintf("所有设备补漏完成!"), nil} c.ServeJSON() return } // DataSensorDataSmooth 数据平滑 func (c *DataGeneratorController) DataSensorDataSmooth() { 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 } var ( sns [][2]string timeRange []int64 tRange float64 hRange float64 ) json.Unmarshal([]byte(c.GetString("sns")), &sns) json.Unmarshal([]byte(c.GetString("data")), &timeRange) tRange, _ = c.GetFloat("tRange") hRange, _ = c.GetFloat("hRange") start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05") end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05") fmt.Println(tRange, hRange) var count int for _, v := range sns { sn := v[0] id := v[1] list := Device.SelectDeviceSensorDataListByTimeRange(sn, id, start, end) for i := 1; i < len(list); i++ { n := list[i-1] old := list[i] newO := list[i] //变化差 var tInterval = old.T_t - n.T_t var hInterval = old.T_rh - n.T_rh fmt.Println("温度:", n.T_t, "温度next:", old.T_t, "差值:", n.T_t-old.T_t) if tRange != 0 { if tInterval > tRange { newO.T_t = n.T_t + tRange } else if tInterval < -tRange { newO.T_t = n.T_t - tRange } } if hRange != 0 { if hInterval > hRange { newO.T_rh = n.T_rh + hRange } else if hInterval < -hRange { newO.T_rh = n.T_rh - hRange } } if old != newO { fmt.Println("原始数据:", old, "新数据:", newO) list[i] = newO newO.T_t = lib.Decimal(newO.T_t) newO.T_rh = lib.Decimal(newO.T_rh) Device.UpdateDeviceSensorData(sn, id, old, newO) count++ } } } c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil} c.ServeJSON() } // DataSensorDataTrend 数据趋势 func (c *DataGeneratorController) DataSensorDataTrend() { 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 } //获取数据 timeRange := make([]int64, 0) sns := make([][2]string, 0) json.Unmarshal([]byte(c.GetString("data")), &timeRange) json.Unmarshal([]byte(c.GetString("sns")), &sns) start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05") end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05") for _, v := range sns { sn := v[0] tId := v[1] list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end) first := list[0] last := list[len(list)-1] ttInterval := (last.T_t - first.T_t) / float64(len(list)-2) trhInterval := (last.T_rh - first.T_rh) / float64(len(list)-2) if len(list) < 3 { continue } for i, d := range list[1 : len(list)-1] { old := list[i] if ttInterval != 0 { d.T_t = list[0].T_t + float64(i+1)*ttInterval } if trhInterval != 0 { d.T_rh = list[0].T_rh + float64(i+1)*trhInterval } if d != old { d.T_t = lib.Decimal(d.T_t) d.T_rh = lib.Decimal(d.T_rh) Device.UpdateDeviceSensorData(sn, tId, old, d) } } } c.Data["json"] = lib.JSONS{200, "设置平滑操作成功", nil} c.ServeJSON() return } // ImportSensorData 导入数据 func (c *DataGeneratorController) ImportSensorData() { b_, admin := 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 } //读取文件 file, _, err := c.GetFile("file") if err != nil { fmt.Println("读取form文件失败", err.Error()) } //解析文件 read, err := excelize.OpenReader(file) if err != nil { fmt.Println("解析错误:", err.Error()) } rows, err := read.GetRows("data") if err != nil { fmt.Println("解析excel错误", err.Error()) } values := make([][]string, 0) for _, row := range rows[1:] { //t_id t_sp t_time t_t t_rh t_site create_time values = append(values, row) } //添加操作 sns := strings.Split(c.GetString("sn"), "|") for _, v := range sns { for _, ev := range values { temperature, _ := strconv.ParseFloat(ev[4], 64) humidty, _ := strconv.ParseFloat(ev[5], 64) temperature = lib.Decimal(temperature) humidty = lib.Decimal(humidty) data := Device.ToSensorData(strings.Split(v, ",")[1], ev[1], ev[2], ev[3], temperature, humidty, ev[6]) Device.InsertDeviceSensorData(strings.Split(v, ",")[0], data, admin) } } c.Data["json"] = lib.JSONS{200, "导入数据成功!", nil} c.ServeJSON() return }