123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- package controllers
- import (
- "ColdP_server/controllers/lib"
- "ColdP_server/logs"
- "ColdP_server/models/Company"
- "ColdP_server/models/Device"
- "encoding/json"
- "fmt"
- 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
- }
- // 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
- }
|