DataGeneratorController.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current)
  234. //format := cts.Format("2006-01-02 15:04:05")
  235. //ct, _ := time.Parse("2006-01-02 15:04:05", format)
  236. n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next)
  237. //nsfmt := ns.Format("2006-01-02 15:04:05")
  238. //n, _ := time.Parse("2006-01-02 15:04:05", nsfmt)
  239. interval := n.Unix() - ct.Unix()
  240. logs.Debug("时间间隔:", interval)
  241. fmt.Println("当前:", current, "下一个:", next)
  242. if int(interval) > saveTime {
  243. ttInterval := list[i+1].T_t - list[i].T_t
  244. ttt := list[i].T_t // 温度临时变量
  245. trhInterval := list[i+1].T_rh - list[i].T_rh
  246. trht := list[i].T_rh //湿度临时变量
  247. count := int(interval) / saveTime
  248. num += count
  249. for k := 0; k < count; k++ {
  250. t := ct.Format("2006-01-02 15:04:05") //时间临时变量
  251. ttt += ttInterval / float64(count)
  252. trht += trhInterval / float64(count)
  253. Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{
  254. list[i].T_id,
  255. list[i].T_sp,
  256. t,
  257. lib.Decimal(ttt),
  258. lib.Decimal(trht),
  259. list[i].T_site,
  260. list[i].CreateTime,
  261. }, admin)
  262. ct = ct.Add(time.Second * time.Duration(saveTime))
  263. }
  264. }
  265. }
  266. }
  267. c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
  268. c.ServeJSON()
  269. return
  270. }
  271. // DataSensorDataSmooth 数据平滑
  272. func (c *DataGeneratorController) DataSensorDataSmooth() {
  273. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  274. if !b_ {
  275. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  276. c.ServeJSON()
  277. return
  278. }
  279. var (
  280. sns [][2]string
  281. timeRange []int64
  282. tRange float64
  283. hRange float64
  284. )
  285. json.Unmarshal([]byte(c.GetString("sns")), &sns)
  286. json.Unmarshal([]byte(c.GetString("data")), &timeRange)
  287. tRange, _ = c.GetFloat("tRange")
  288. hRange, _ = c.GetFloat("hRange")
  289. start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05")
  290. end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05")
  291. fmt.Println(tRange, hRange)
  292. var count int
  293. for _, v := range sns {
  294. sn := v[0]
  295. id := v[1]
  296. list := Device.SelectDeviceSensorDataListByTimeRange(sn, id, start, end)
  297. for i := 1; i < len(list); i++ {
  298. n := list[i-1]
  299. old := list[i]
  300. newO := list[i]
  301. //变化差
  302. var tInterval = old.T_t - n.T_t
  303. var hInterval = old.T_rh - n.T_rh
  304. fmt.Println("温度:", n.T_t, "温度next:", old.T_t, "差值:", n.T_t-old.T_t)
  305. if tRange != 0 {
  306. if tInterval > tRange {
  307. newO.T_t = n.T_t + tRange
  308. } else if tInterval < -tRange {
  309. newO.T_t = n.T_t - tRange
  310. }
  311. }
  312. if hRange != 0 {
  313. if hInterval > hRange {
  314. newO.T_rh = n.T_rh + hRange
  315. } else if hInterval < -hRange {
  316. newO.T_rh = n.T_rh - hRange
  317. }
  318. }
  319. if old != newO {
  320. fmt.Println("原始数据:", old, "新数据:", newO)
  321. list[i] = newO
  322. newO.T_t = lib.Decimal(newO.T_t)
  323. newO.T_rh = lib.Decimal(newO.T_rh)
  324. Device.UpdateDeviceSensorData(sn, id, old, newO)
  325. count++
  326. }
  327. }
  328. }
  329. c.Data["json"] = lib.JSONS{200, "操作成功处理" + fmt.Sprint(count) + "条数据", nil}
  330. c.ServeJSON()
  331. }
  332. // DataSensorDataTrend 数据趋势
  333. func (c *DataGeneratorController) DataSensorDataTrend() {
  334. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  335. if !b_ {
  336. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  337. c.ServeJSON()
  338. return
  339. }
  340. //获取数据
  341. timeRange := make([]int64, 0)
  342. sns := make([][2]string, 0)
  343. json.Unmarshal([]byte(c.GetString("data")), &timeRange)
  344. json.Unmarshal([]byte(c.GetString("sns")), &sns)
  345. start := time.UnixMilli(timeRange[0]).Format("2006-01-02 15:04:05")
  346. end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05")
  347. for _, v := range sns {
  348. sn := v[0]
  349. tId := v[1]
  350. list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end)
  351. first := list[0]
  352. last := list[len(list)-1]
  353. ttInterval := (last.T_t - first.T_t) / float64(len(list)-2)
  354. trhInterval := (last.T_rh - first.T_rh) / float64(len(list)-2)
  355. if len(list) < 3 {
  356. continue
  357. }
  358. for i, d := range list[1 : len(list)-1] {
  359. old := list[i]
  360. if ttInterval != 0 {
  361. d.T_t = list[0].T_t + float64(i+1)*ttInterval
  362. }
  363. if trhInterval != 0 {
  364. d.T_rh = list[0].T_rh + float64(i+1)*trhInterval
  365. }
  366. if d != old {
  367. d.T_t = lib.Decimal(d.T_t)
  368. d.T_rh = lib.Decimal(d.T_rh)
  369. Device.UpdateDeviceSensorData(sn, tId, old, d)
  370. }
  371. }
  372. }
  373. c.Data["json"] = lib.JSONS{200, "设置平滑操作成功", nil}
  374. c.ServeJSON()
  375. return
  376. }
  377. // ImportSensorData 导入数据
  378. func (c *DataGeneratorController) ImportSensorData() {
  379. b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  380. if !b_ {
  381. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  382. c.ServeJSON()
  383. return
  384. }
  385. //读取文件
  386. file, _, err := c.GetFile("file")
  387. if err != nil {
  388. fmt.Println("读取form文件失败", err.Error())
  389. }
  390. //解析文件
  391. read, err := excelize.OpenReader(file)
  392. if err != nil {
  393. fmt.Println("解析错误:", err.Error())
  394. }
  395. rows, err := read.GetRows("data")
  396. if err != nil {
  397. fmt.Println("解析excel错误", err.Error())
  398. }
  399. values := make([][]string, 0)
  400. for _, row := range rows[1:] {
  401. //t_id t_sp t_time t_t t_rh t_site create_time
  402. values = append(values, row)
  403. }
  404. //添加操作
  405. sns := strings.Split(c.GetString("sn"), "|")
  406. for _, v := range sns {
  407. for _, ev := range values {
  408. temperature, _ := strconv.ParseFloat(ev[4], 64)
  409. humidty, _ := strconv.ParseFloat(ev[5], 64)
  410. temperature = lib.Decimal(temperature)
  411. humidty = lib.Decimal(humidty)
  412. data := Device.ToSensorData(strings.Split(v, ",")[1], ev[1], ev[2], ev[3], temperature, humidty, ev[6])
  413. Device.InsertDeviceSensorData(strings.Split(v, ",")[0], data, admin)
  414. }
  415. }
  416. c.Data["json"] = lib.JSONS{200, "导入数据成功!", nil}
  417. c.ServeJSON()
  418. return
  419. }