DeviceData.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package Device
  2. import (
  3. "Cold_Data/conf"
  4. "Cold_Data/lib"
  5. "Cold_Data/logs"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "strconv"
  13. "time"
  14. )
  15. // 模板
  16. type DeviceData_R struct {
  17. T_t float32 // 温度
  18. T_rh float32 // 湿度
  19. T_Site string // GPS
  20. T_time time.Time // 采集时间
  21. T_sp int // 传感器参数ID
  22. //create_time
  23. }
  24. type DeviceData_old struct {
  25. T_t float32
  26. T_rh float32
  27. T_site string
  28. T_sp int
  29. }
  30. // 模板
  31. type DeviceData_RR struct {
  32. T_name string // 传感器名称
  33. T_t float32 // 温度
  34. T_rh float32 // 湿度
  35. T_site string // GPS
  36. T_Tlower float32 // 温度下限
  37. T_Tupper float32 // 温度上限
  38. T_RHlower float32 // 湿度下限
  39. T_RHupper float32 // 湿度上限
  40. T_time string // 采集时间
  41. T_ist int // 温度 1开启 2关闭
  42. T_ish int // 湿度 1开启 2关闭
  43. }
  44. type DeviceData_ struct {
  45. T_t float32 // 温度
  46. T_rh float32 // 湿度
  47. T_site string // GPS
  48. T_time time.Time // 采集时间
  49. }
  50. var redis_DeviceData cache.Cache
  51. func init() {
  52. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  53. "redis_DeviceData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  54. logs.Println(config)
  55. var err error
  56. redis_DeviceData, err = cache.NewCache("redis", config)
  57. if err != nil || redis_DeviceData == nil {
  58. errMsg := "failed to init redis"
  59. logs.Println(errMsg, err)
  60. }
  61. }
  62. // ---------------- Redis -------------------
  63. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  64. func RedisDeviceData_Set(T_sn string, T_id int, r DeviceData_R) (err error) {
  65. key := T_sn + "|" + strconv.Itoa(T_id)
  66. if redis_DeviceData.IsExist(key) {
  67. var t DeviceData_R
  68. v := redis_DeviceData.Get(key)
  69. json.Unmarshal(v.([]byte), &t)
  70. // 防止时间溢出
  71. if time.Now().Unix() <= r.T_time.Unix() {
  72. r.T_time = time.Now()
  73. }
  74. // 提前最新数据
  75. if t.T_time.Unix() > r.T_time.Unix() {
  76. // 储存的 是最新数据
  77. return
  78. }
  79. }
  80. //json序列化
  81. str, err := json.Marshal(r)
  82. if err != nil {
  83. logs.PrintlnError("RedisDeviceData_Set", err)
  84. return
  85. }
  86. err = redis_DeviceData.Put(key, str, 1*time.Hour)
  87. if err != nil {
  88. logs.Println("set key:", key, ",value:", str, err)
  89. }
  90. return
  91. }
  92. func RedisDeviceData_Get(key string) (r DeviceData_R, is bool) {
  93. if redis_DeviceData.IsExist(key) {
  94. v := redis_DeviceData.Get(key)
  95. json.Unmarshal(v.([]byte), &r)
  96. return r, true
  97. }
  98. return DeviceData_R{}, false
  99. }
  100. func Read_DeviceData_List(SN string, T_id int, Time_start_ string, Time_end_ string) (maps []DeviceData_) {
  101. o := orm.NewOrm()
  102. sql_time := ""
  103. if len(Time_start_) > 1 {
  104. sql_time += " t_time >= '" + Time_start_ + "' AND "
  105. }
  106. if len(Time_end_) > 1 {
  107. sql_time += " t_time <= '" + Time_end_ + "' AND "
  108. }
  109. sql := "SELECT t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time1 DESC LIMIT 1000"
  110. fmt.Println(sql)
  111. _, err := o.Raw(sql).QueryRows(&maps)
  112. if err != nil {
  113. logs.PrintlnError(lib.FuncName(), err)
  114. return
  115. }
  116. return
  117. }