DataGeneratorController.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package controllers
  2. import (
  3. "ColdP_server/controllers/lib"
  4. "ColdP_server/logs"
  5. "ColdP_server/models/Company"
  6. "ColdP_server/models/Device"
  7. "encoding/json"
  8. "fmt"
  9. beego "github.com/beego/beego/v2/server/web"
  10. "github.com/xuri/excelize/v2"
  11. "io"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. type DataGeneratorController struct {
  17. beego.Controller
  18. }
  19. // GeneratorHtml 获取页面
  20. func (c *DataGeneratorController) GeneratorHtml() {
  21. b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  22. if !b_ {
  23. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  24. c.ServeJSON()
  25. return
  26. }
  27. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  28. c.Data["Class_List"] = classList
  29. //确认状态为登录状态后
  30. c.TplName = "Data/GeneratorData2.html"
  31. }
  32. // DeviceSensorData 获取对应设备探头数据
  33. func (c *DataGeneratorController) DeviceSensorData() {
  34. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  35. if !b_ {
  36. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  37. c.ServeJSON()
  38. return
  39. }
  40. startTime := c.GetString("startTime")
  41. endTime := c.GetString("endTime")
  42. sns := make([][]string, 0)
  43. json.Unmarshal([]byte(c.GetString("sns")), &sns)
  44. type Temp struct {
  45. Sn []string `json:"sn"`
  46. Data []Device.DeviceSensorData `json:"data"`
  47. }
  48. datas := make([]Temp, 0)
  49. for _, sn := range sns {
  50. //sn = [sn,探头id]
  51. id, _ := strconv.ParseInt(sn[1], 10, 63)
  52. v, err := Device.ReadDeviceSensorByTsnTidTimeRange(sn[0], int(id), startTime, endTime)
  53. if err != nil {
  54. c.Data["json"] = lib.JSONS{Code: 500, Msg: "读取设备探头错误!"}
  55. c.ServeJSON()
  56. return
  57. }
  58. datas = append(datas, Temp{sn, v})
  59. }
  60. c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取数据成功", Data: datas}
  61. c.ServeJSON()
  62. }
  63. // UpdateFix 更新固定值
  64. func (c *DataGeneratorController) UpdateFix() {
  65. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  66. if !b_ {
  67. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  68. c.ServeJSON()
  69. return
  70. }
  71. //1.解析数据
  72. var body = c.Ctx.Request.Body
  73. defer body.Close()
  74. type T struct {
  75. FixTemperature string `json:"fixTemperature"`
  76. FixHumidity string `json:"fixHumidity"`
  77. Sns [][2]string `json:"sns"`
  78. Data []int64 `json:"data"`
  79. }
  80. var temp = T{}
  81. bytes, _ := io.ReadAll(body)
  82. json.Unmarshal(bytes, &temp)
  83. fmt.Println("解析后:", temp.Data[0])
  84. //2.得到数据进行统一设置访问修改
  85. humidity, _ := strconv.ParseFloat(temp.FixHumidity, 64)
  86. temperature, _ := strconv.ParseFloat(temp.FixTemperature, 64)
  87. //开始时间到结束时间
  88. startTime := time.UnixMilli(temp.Data[0]).Format("2006-01-02 15:04:05")
  89. endTime := time.UnixMilli(temp.Data[1]).Format("2006-01-02 15:04:05")
  90. //3.循环更新数据
  91. for _, v := range temp.Sns {
  92. sn := v[0]
  93. tId := v[1]
  94. Device.UpdateDeviceSensorDataTemperatureAndHumidity(sn, tId, startTime, endTime, temperature, humidity)
  95. }
  96. //4.反馈成功
  97. c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil}
  98. c.ServeJSON()
  99. }
  100. // Delete 删除
  101. func (c *DataGeneratorController) Delete() {
  102. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  103. if !b_ {
  104. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  105. c.ServeJSON()
  106. return
  107. }
  108. //1.解析数据
  109. var body = c.Ctx.Request.Body
  110. defer body.Close()
  111. type T struct {
  112. Sns [][2]string `json:"sns"`
  113. Data []int64 `json:"data"`
  114. }
  115. var temp = T{}
  116. bytes, _ := io.ReadAll(body)
  117. json.Unmarshal(bytes, &temp)
  118. fmt.Println("解析后:", temp.Data[0])
  119. //开始时间到结束时间
  120. startTime := time.UnixMilli(temp.Data[0]).Format("2006-01-02 15:04:05")
  121. endTime := time.UnixMilli(temp.Data[1]).Format("2006-01-02 15:04:05")
  122. //3.循环更新数据
  123. for _, v := range temp.Sns {
  124. sn := v[0]
  125. tId := v[1]
  126. Device.DeleteDeviceSensorDataByTimeRange(sn, tId, startTime, endTime)
  127. }
  128. //4.反馈成功
  129. c.Data["json"] = lib.JSONS{200, "调整固定偏移值成功!", nil}
  130. c.ServeJSON()
  131. }
  132. // UpdateRand 更新随机值
  133. func (c *DataGeneratorController) UpdateRand() {
  134. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  135. if !b_ {
  136. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. //解析请求参数
  141. body := c.Ctx.Request.Body
  142. defer body.Close()
  143. bytes, _ := io.ReadAll(body)
  144. type T struct {
  145. TemperatureMin int `json:"temperatureMin"`
  146. TemperatureMax int `json:"temperatureMax"`
  147. HumidityMax int `json:"humidityMax"`
  148. HumidityMin int `json:"humidityMin"`
  149. Sns [][2]string `json:"sns"`
  150. Data []int64 `json:"data"`
  151. }
  152. var t = T{}
  153. json.Unmarshal(bytes, &t)
  154. fmt.Println("requestJSON: ", t, "原始json:", string(bytes))
  155. //开始时间到结束时间
  156. startTime := time.UnixMilli(t.Data[0]).Format("2006-01-02 15:04:05")
  157. endTime := time.UnixMilli(t.Data[1]).Format("2006-01-02 15:04:05")
  158. for _, v := range t.Sns {
  159. sn := v[0]
  160. tId := v[1]
  161. Device.UpdateDeviceSensorDataTemperatureAndHumidityRandom(sn, tId, startTime, endTime, t.TemperatureMax, t.TemperatureMin, t.HumidityMax, t.HumidityMin)
  162. }
  163. //反馈成功
  164. c.Data["json"] = lib.JSONS{200, "调整随机偏移值成功!", nil}
  165. c.ServeJSON()
  166. return
  167. }
  168. // CopyFromPosition 数据拷贝
  169. func (c *DataGeneratorController) CopyFromPosition() {
  170. b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  171. if !b_ {
  172. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  173. c.ServeJSON()
  174. return
  175. }
  176. var body = c.Ctx.Request.Body
  177. defer body.Close()
  178. bytes, _ := io.ReadAll(body)
  179. type T struct {
  180. CopyPosition string `json:"copyPosition"`
  181. Sns [][2]string `json:"sns"`
  182. Data []int64 `json:"data"`
  183. }
  184. t := T{}
  185. json.Unmarshal(bytes, &t)
  186. //开始时间 和结束时间,插入的时间范围
  187. fmt.Println(t)
  188. copyTime, _ := time.Parse("2006-01-02 15:04:05", t.CopyPosition)
  189. startTime := time.UnixMilli(t.Data[0]).Format("2006-01-02 15:04:05")
  190. endTime := time.UnixMilli(t.Data[1]).Format("2006-01-02 15:04:05")
  191. for _, v := range t.Sns {
  192. sn := v[0]
  193. tId := v[1]
  194. list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, startTime, endTime)
  195. saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT
  196. ct := copyTime
  197. go func(list []Device.DeviceSensorData, sn string, saveTime int) {
  198. for _, d := range list {
  199. d.T_time = ct.Format("2006-01-02 15:04:05")
  200. d.CreateTime = ct.Format("2006-01-02 15:04:05")
  201. Device.InsertDeviceSensorData(sn, d, admin)
  202. ct = ct.Add(time.Second * time.Duration(saveTime))
  203. }
  204. }(list, sn, saveTime)
  205. }
  206. c.Data["json"] = lib.JSONS{200, "数据复制已提交后台处理!", nil}
  207. c.ServeJSON()
  208. return
  209. }
  210. // RepairSensorData 数据补漏
  211. func (c *DataGeneratorController) RepairSensorData() {
  212. b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  213. if !b_ {
  214. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  215. c.ServeJSON()
  216. return
  217. }
  218. sns := make([][2]string, 0)
  219. timeRange := make([]int64, 0)
  220. json.Unmarshal([]byte(c.GetString("sns")), &sns)
  221. json.Unmarshal([]byte(c.GetString("data")), &timeRange)
  222. start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05")
  223. end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05")
  224. num := 0
  225. for _, v := range sns {
  226. sn := v[0]
  227. tId := v[1]
  228. saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT
  229. list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end)
  230. for i := 0; i < len(list)-1; i++ {
  231. current := list[i].T_time
  232. next := list[i+1].T_time
  233. c, _ := time.Parse("2006-01-02 15:04:05", current)
  234. n, _ := time.Parse("2006-01-02 15:04:05", next)
  235. interval := n.Unix() - c.Unix()
  236. logs.Debug("时间间隔:", interval)
  237. fmt.Println("当前:", current, "下一个:", next)
  238. if int(interval) > saveTime {
  239. ttInterval := list[i+1].T_t - list[i].T_t
  240. ttt := list[i].T_t // 温度临时变量
  241. trhInterval := list[i+1].T_rh - list[i].T_rh
  242. trht := list[i].T_rh //湿度临时变量
  243. count := int(interval) / saveTime
  244. num += count
  245. for k := 0; k < count; k++ {
  246. t := c.Format("2006-01-02 15:04:05") //时间临时变量
  247. ttt += ttInterval / float64(count)
  248. trht += trhInterval / float64(count)
  249. Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{
  250. list[i].T_id,
  251. list[i].T_sp,
  252. t,
  253. lib.Decimal(ttt),
  254. lib.Decimal(trht),
  255. list[i].T_site,
  256. list[i].CreateTime,
  257. }, admin)
  258. c = c.Add(time.Second * time.Duration(saveTime))
  259. }
  260. }
  261. }
  262. }
  263. c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
  264. c.ServeJSON()
  265. return
  266. }
  267. // DataSensorDataSmooth 数据平滑
  268. func (c *DataGeneratorController) DataSensorDataSmooth() {
  269. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  270. if !b_ {
  271. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  272. c.ServeJSON()
  273. return
  274. }
  275. var (
  276. sns [][2]string
  277. timeRange []int64
  278. tRange float64
  279. hRange float64
  280. )
  281. json.Unmarshal([]byte(c.GetString("sns")), &sns)
  282. json.Unmarshal([]byte(c.GetString("data")), &timeRange)
  283. tRange, _ = c.GetFloat("tRange")
  284. hRange, _ = c.GetFloat("hRange")
  285. start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05")
  286. end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05")
  287. fmt.Println(tRange, hRange)
  288. var count int
  289. for _, v := range sns {
  290. sn := v[0]
  291. id := v[1]
  292. list := Device.SelectDeviceSensorDataListByTimeRange(sn, id, start, end)
  293. for i := 1; i < len(list); i++ {
  294. n := list[i-1]
  295. old := list[i]
  296. newO := list[i]
  297. //变化差
  298. var tInterval = old.T_t - n.T_t
  299. var hInterval = old.T_rh - n.T_rh
  300. fmt.Println("温度:", n.T_t, "温度next:", old.T_t, "差值:", n.T_t-old.T_t)
  301. if tRange != 0 {
  302. if tInterval > tRange {
  303. newO.T_t = n.T_t + tRange
  304. } else if tInterval < -tRange {
  305. newO.T_t = n.T_t - tRange
  306. }
  307. }
  308. if hRange != 0 {
  309. if hInterval > hRange {
  310. newO.T_rh = n.T_rh + hRange
  311. } else if hInterval < -hRange {
  312. newO.T_rh = n.T_rh - hRange
  313. }
  314. }
  315. if old != newO {
  316. fmt.Println("原始数据:", old, "新数据:", newO)
  317. list[i] = newO
  318. newO.T_t = lib.Decimal(newO.T_t)
  319. newO.T_rh = lib.Decimal(newO.T_rh)
  320. Device.UpdateDeviceSensorData(sn, id, old, newO)
  321. count++
  322. }
  323. }
  324. }
  325. c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil}
  326. c.ServeJSON()
  327. }
  328. // DataSensorDataTrend 数据趋势
  329. func (c *DataGeneratorController) DataSensorDataTrend() {
  330. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  331. if !b_ {
  332. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  333. c.ServeJSON()
  334. return
  335. }
  336. //获取数据
  337. timeRange := make([]int64, 0)
  338. sns := make([][2]string, 0)
  339. json.Unmarshal([]byte(c.GetString("data")), &timeRange)
  340. json.Unmarshal([]byte(c.GetString("sns")), &sns)
  341. start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05")
  342. end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05")
  343. for _, v := range sns {
  344. sn := v[0]
  345. tId := v[1]
  346. list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end)
  347. first := list[0]
  348. last := list[len(list)-1]
  349. ttInterval := (last.T_t - first.T_t) / float64(len(list)-2)
  350. trhInterval := (last.T_rh - first.T_rh) / float64(len(list)-2)
  351. if len(list) < 3 {
  352. continue
  353. }
  354. for i, d := range list[1 : len(list)-1] {
  355. old := list[i]
  356. if ttInterval != 0 {
  357. d.T_t = list[0].T_t + float64(i+1)*ttInterval
  358. }
  359. if trhInterval != 0 {
  360. d.T_rh = list[0].T_rh + float64(i+1)*trhInterval
  361. }
  362. if d != old {
  363. d.T_t = lib.Decimal(d.T_t)
  364. d.T_rh = lib.Decimal(d.T_rh)
  365. Device.UpdateDeviceSensorData(sn, tId, old, d)
  366. }
  367. }
  368. }
  369. c.Data["json"] = lib.JSONS{200, "设置平滑操作成功", nil}
  370. c.ServeJSON()
  371. return
  372. }
  373. // ImportSensorData 导入数据
  374. func (c *DataGeneratorController) ImportSensorData() {
  375. b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  376. if !b_ {
  377. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  378. c.ServeJSON()
  379. return
  380. }
  381. //读取文件
  382. file, _, err := c.GetFile("file")
  383. if err != nil {
  384. fmt.Println("读取form文件失败", err.Error())
  385. }
  386. //解析文件
  387. read, err := excelize.OpenReader(file)
  388. if err != nil {
  389. fmt.Println("解析错误:", err.Error())
  390. }
  391. rows, err := read.GetRows("data")
  392. if err != nil {
  393. fmt.Println("解析excel错误", err.Error())
  394. }
  395. values := make([][]string, 0)
  396. for _, row := range rows[1:] {
  397. //t_id t_sp t_time t_t t_rh t_site create_time
  398. values = append(values, row)
  399. }
  400. //添加操作
  401. sns := strings.Split(c.GetString("sn"), "|")
  402. for _, v := range sns {
  403. for _, ev := range values {
  404. temperature, _ := strconv.ParseFloat(ev[4], 64)
  405. humidty, _ := strconv.ParseFloat(ev[5], 64)
  406. temperature = lib.Decimal(temperature)
  407. humidty = lib.Decimal(humidty)
  408. data := Device.ToSensorData(strings.Split(v, ",")[1], ev[1], ev[2], ev[3], temperature, humidty, ev[6])
  409. Device.InsertDeviceSensorData(strings.Split(v, ",")[0], data, admin)
  410. }
  411. }
  412. c.Data["json"] = lib.JSONS{200, "导入数据成功!", nil}
  413. c.ServeJSON()
  414. return
  415. }