|
@@ -11,6 +11,8 @@ import (
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
"io"
|
|
|
+ "log"
|
|
|
+ "math/rand"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -227,7 +229,7 @@ func (c *DataGeneratorController) CopyFromPosition() {
|
|
|
|
|
|
// RepairSensorData 数据补漏
|
|
|
func (c *DataGeneratorController) RepairSensorData() {
|
|
|
- b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
|
|
|
+ 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()
|
|
@@ -242,226 +244,150 @@ func (c *DataGeneratorController) RepairSensorData() {
|
|
|
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)
|
|
|
+ listdevices, lists := Listdevices(v[0], v[1], start, end)
|
|
|
+ num = normal(listdevices, lists.T_save_t, lists.T_warn, lists.Sn, lists.T_tlower, lists.T_tupper, lists.T_r_hlower, lists.T_r_hupper)
|
|
|
+ }
|
|
|
+ //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
|
|
|
+}
|
|
|
+func Listdevices(sn, t_id, startTime, endTime string) ([]Device.DeviceData, Device.DeviceLists) {
|
|
|
+ var deviceslist Device.DeviceLists
|
|
|
+ sql := fmt.Sprintf("SELECT * FROM z_device_data_%s WHERE t_id=%s and t_time BETWEEN '%s' AND '%s';", sn, t_id, startTime, endTime)
|
|
|
+ deviceslist.Sn = sn
|
|
|
+ var devices []Device.DeviceData
|
|
|
+ o := orm.NewOrm()
|
|
|
+ queryRows, err := o.Raw(sql).QueryRows(&devices)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(queryRows, err)
|
|
|
+ return devices, deviceslist
|
|
|
+ }
|
|
|
+ sqls := fmt.Sprintf("SELECT t_save_t,t_warn FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sn)
|
|
|
+ deviceSensorParameter := fmt.Sprintf("SELECT t__tlower,t__tupper,t__r_hlower,t__r_hupper FROM device_sensor_parameter WHERE t_sn='%s' and t__state=1 and t_id = '%s' ORDER BY update_time DESC LIMIT 1", sn, t_id)
|
|
|
+ var save_t string
|
|
|
+ var t_warn string
|
|
|
+ err = o.Raw(sqls).QueryRow(&save_t, &t_warn)
|
|
|
+ deviceslist.T_save_t = save_t
|
|
|
+ deviceslist.T_warn = t_warn
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ return devices, deviceslist
|
|
|
+ }
|
|
|
+ err = o.Raw(deviceSensorParameter).QueryRow(&deviceslist.T_tlower, &deviceslist.T_tupper, &deviceslist.T_r_hlower, &deviceslist.T_r_hupper)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ return devices, deviceslist
|
|
|
+ }
|
|
|
+ return devices, deviceslist
|
|
|
+}
|
|
|
|
|
|
- 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))
|
|
|
+// 正常情况下补漏数据
|
|
|
+func normal(devices []Device.DeviceData, t_save_t, t_warn, sn string, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) int {
|
|
|
+ num := 0
|
|
|
+ timeFormat := "2006-01-02 15:04:05 -0700 MST"
|
|
|
+ Format := "2006-01-02 15:04:05"
|
|
|
+ floatSave, _ := strconv.ParseFloat(t_save_t, 64)
|
|
|
+ newOrm := orm.NewOrm()
|
|
|
+ for i := 0; i < len(devices)-1; i++ {
|
|
|
+ t1, err := time.Parse(timeFormat, devices[i].T_time)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("解析时间失败:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ t2, err := time.Parse(timeFormat, devices[i+1].T_time)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("解析时间失败:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ timeDiff := t2.Sub(t1).Seconds()
|
|
|
+ if timeDiff > floatSave {
|
|
|
+ fmt.Printf("时间差大于系统设置时间:%s 和 %s\n", devices[i].T_time, devices[i+1].T_time)
|
|
|
+ numInserts := int(timeDiff / floatSave)
|
|
|
+ for j := 1; j <= numInserts-1; j++ {
|
|
|
+ t := t1.Truncate(time.Minute)
|
|
|
+ newTime := t.Add(time.Duration(j*int(floatSave)) * time.Second)
|
|
|
+ T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
|
|
|
+ devi := Device.DeviceData{
|
|
|
+ T_id: devices[i].T_id,
|
|
|
+ T_sp: devices[i].T_sp,
|
|
|
+ T_time: newTime.Format(Format),
|
|
|
+ T_t: T_T,
|
|
|
+ T_rh: T_Rh,
|
|
|
+ T_site: devices[i].T_site,
|
|
|
+ Create_time: newTime.Format(Format),
|
|
|
+ }
|
|
|
+ insertSql := fmt.Sprintf("INSERT INTO z_device_data_%s (t_id, t_sp, t_time, t_t, t_rh, t_site, create_time) VALUES (?, ?, ?, ?, ?, ?, ?)", sn)
|
|
|
+ log.Println(insertSql)
|
|
|
+ _, err := newOrm.Raw(insertSql, devi.T_id, devi.T_sp, devi.T_time, devi.T_t, devi.T_rh, devi.T_site.String, devi.Create_time).Exec()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("插入新时间点失败:", err)
|
|
|
+ return 0
|
|
|
}
|
|
|
+ num++
|
|
|
+ log.Println("sn:", sn, "补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
|
|
|
- c.ServeJSON()
|
|
|
- return
|
|
|
+ return num
|
|
|
}
|
|
|
|
|
|
-// 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 IsNotWarn(deviceDatas []Device.DeviceData, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) (T_T, T_Rh float64) {
|
|
|
+ for i, _ := range deviceDatas {
|
|
|
+ if !(deviceDatas[i].T_t < t_tlower || deviceDatas[i].T_t > t_tupper || deviceDatas[i].T_rh < t_r_hlower || deviceDatas[i].T_rh > t_r_hupper) {
|
|
|
+ T_T = deviceDatas[i].T_t + rand.Float64()*0.4 - 0.2
|
|
|
+ T_Rh = deviceDatas[i].T_rh + rand.Float64()*0.4 - 0.2
|
|
|
+ return T_T, T_Rh
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0, 0
|
|
|
+}
|
|
|
|
|
|
func (c *DataGeneratorController) RepairAllSensorData() {
|
|
|
b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
|