DeviceData.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package Device
  2. import (
  3. "Yunlot/conf"
  4. "Yunlot/logs"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. _ "github.com/go-sql-driver/mysql"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/mongo"
  13. "go.mongodb.org/mongo-driver/mongo/options"
  14. "log"
  15. "strconv"
  16. "time"
  17. )
  18. // 模板
  19. type DeviceData_R1 struct {
  20. T_tab string `orm:"size(256);index" json:"T_Rtab"` //转发 TAB 标识 拼接:AAAA.BBBB. 对象后面加点
  21. T_type int `orm:"size(200);null"` // 数据类型 (1,'bool'),(2,'u8'),(3,'u16'),(4,'float'),(5,'str'),(6,'object'),(7,'u8'[模式图片]);
  22. T_value string `orm:"type(text);size(200);null"` // 值
  23. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  25. }
  26. // 模板
  27. type DeviceData_R struct {
  28. T_tab string `orm:"size(256);index" json:"T_Rtab"` //转发 TAB 标识 拼接:AAAA.BBBB. 对象后面加点
  29. T_type int `orm:"size(200);default(4)"` // 数据类型 (1,'bool'),(2,'int'),(3,'float'),(4,'str'),(5,'time'),(7,'u8'[模式图片]);
  30. T_bool bool `orm:"type(text);size(200);"` // 值
  31. T_int int `orm:"type(text);size(200);null"` // 值
  32. T_float float64 `orm:"type(text);size(200);null"` // 值
  33. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  34. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  35. }
  36. type DeviceData_old struct {
  37. T_t float32
  38. T_rh float32
  39. T_site string
  40. T_sp int
  41. }
  42. var redis_DeviceData cache.Cache
  43. func init() {
  44. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  45. "redis_DeviceData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  46. logs.Println(config)
  47. var err error
  48. redis_DeviceData, err = cache.NewCache("redis", config)
  49. if err != nil || redis_DeviceData == nil {
  50. errMsg := "failed to init redis"
  51. logs.Println(errMsg, err)
  52. }
  53. }
  54. func Connect() (*mongo.Database, error) {
  55. credential := options.Credential{
  56. AuthSource: conf.Mongodb_DB,
  57. Username: conf.Mongodb_Username,
  58. Password: conf.Mongodb_Password,
  59. }
  60. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  61. defer cancel()
  62. client, err := mongo.Connect(ctx, options.Client().ApplyURI(conf.Mongodb_Url).SetAuth(credential).SetMaxPoolSize(10))
  63. if err != nil {
  64. log.Print(err)
  65. }
  66. return client.Database(conf.Mongodb_DB), err
  67. }
  68. // ---------------- Redis -------------------
  69. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  70. func RedisDeviceData_Set(T_sn string, T_id int, r DeviceData_R) (err error) {
  71. key := T_sn + "|" + strconv.Itoa(T_id)
  72. if redis_DeviceData.IsExist(key) {
  73. var t DeviceData_R
  74. v := redis_DeviceData.Get(key)
  75. json.Unmarshal(v.([]byte), &t)
  76. // 防止时间溢出
  77. if time.Now().Unix() <= r.T_time.Unix() {
  78. r.T_time = time.Now()
  79. }
  80. // 提前最新数据
  81. if t.T_time.Unix() > r.T_time.Unix() {
  82. // 储存的 是最新数据
  83. return
  84. }
  85. }
  86. //json序列化
  87. str, err := json.Marshal(r)
  88. if err != nil {
  89. logs.PrintlnError("RedisDeviceData_Set", err)
  90. return
  91. }
  92. err = redis_DeviceData.Put(key, str, 1*time.Hour)
  93. if err != nil {
  94. logs.Println("set key:", key, ",value:", str, err)
  95. }
  96. return
  97. }
  98. func RedisDeviceData_Get(key string) (r DeviceData_R, is bool) {
  99. if redis_DeviceData.IsExist(key) {
  100. v := redis_DeviceData.Get(key)
  101. json.Unmarshal(v.([]byte), &r)
  102. return r, true
  103. }
  104. return DeviceData_R{}, false
  105. }
  106. // ----------------
  107. func Data_Add(JointTab string, bson_r *bson.M) {
  108. // 连接到MongoDB
  109. DB, err := Connect()
  110. if err != nil {
  111. panic(any(err))
  112. }
  113. //fmt.Println("JointTab:", JointTab)
  114. collection := DB.Collection(JointTab)
  115. _, err = collection.InsertOne(context.TODO(), bson_r)
  116. if err != nil {
  117. panic(any(err))
  118. }
  119. }