default.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package controllers
  2. import (
  3. "Cold_mqtt/lib"
  4. "Cold_mqtt/logs"
  5. "Cold_mqtt/models/Device"
  6. beego "github.com/beego/beego/v2/server/web"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type MainController struct {
  12. beego.Controller
  13. }
  14. func (c *MainController) Get() {
  15. c.Data["Website"] = "beego.me"
  16. c.Data["Email"] = "astaxie@gmail.com"
  17. c.TplName = "index.tpl"
  18. }
  19. // 列表 - 接口
  20. func (c *MainController) Device_Sensor_Data_More() {
  21. // 验证登录
  22. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  23. if !b_ {
  24. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  25. c.ServeJSON()
  26. return
  27. }
  28. T_sn := c.GetString("T_sn")
  29. T_id, _ := c.GetInt("T_id")
  30. T_Data := c.GetString("T_Data") // 模板: 温度?湿度?GPS?采集时间| 7.80?53.40?026.6441335,106.7994091?2019-01-08 13:50:30|
  31. // 过滤
  32. if len(T_sn) < 4 {
  33. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn Err!"}
  34. c.ServeJSON()
  35. return
  36. }
  37. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  38. if err != nil {
  39. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn Err!"}
  40. c.ServeJSON()
  41. return
  42. }
  43. // 是否存在传感器 不存在 跳过
  44. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(T_sn, T_id)
  45. if !is {
  46. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn T_id Err!"}
  47. c.ServeJSON()
  48. return
  49. }
  50. // 获取传感器参数
  51. DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(Device_r.T_sn, DeviceSensor_r.T_id)
  52. if !is {
  53. c.Data["json"] = lib.JSONS{Code: 203, Msg: "记录数据 传感器参数不存在 跳过处理"}
  54. c.ServeJSON()
  55. return
  56. }
  57. T_Data_list := strings.Split(T_Data, "|")
  58. logs.Println("len(T_Data_list)", len(T_Data_list))
  59. for _, v := range T_Data_list {
  60. // 7.80?53.40?026.6441335,106.7994091?2019-01-08 13:50:30|
  61. if len(v) < 5 {
  62. logs.Println(v, "len(v) < 5")
  63. continue
  64. }
  65. v_list := strings.Split(v, "?")
  66. if len(v_list) != 4 {
  67. logs.Println(v, "len(v_list) != 4")
  68. continue
  69. }
  70. T_t, _ := strconv.ParseFloat(v_list[0], 32)
  71. T_rh, _ := strconv.ParseFloat(v_list[1], 32)
  72. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", v_list[3], time.Local) //使用parseInLocation将字符串格式化返回本地时区时间
  73. //// 更新记录 - 缓存
  74. DeviceData_t := Device.DeviceData_R{
  75. T_t: float32(T_t),
  76. T_rh: float32(T_rh),
  77. T_Site: v_list[4],
  78. T_time: stamp,
  79. T_sp: DeviceSensorParameter_r.Id,
  80. }
  81. if r_, DeviceData_old_r := Device.Add_DeviceData(T_sn, T_id, DeviceData_t, true); r_ {
  82. // 被替换
  83. Device.Add_DeviceDataOld(Device.DeviceDataOld{
  84. T_sn: T_sn,
  85. T_id: T_id,
  86. T_t: DeviceData_old_r.T_t,
  87. T_rh: DeviceData_old_r.T_rh,
  88. T_Site: DeviceData_old_r.T_site,
  89. T_time: DeviceData_t.T_time,
  90. T_operation: 2,
  91. T_uuid: admin_r.T_uuid,
  92. })
  93. }
  94. }
  95. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  96. c.ServeJSON()
  97. return
  98. }