123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package Device
- import (
- "Yunlot/conf"
- "Yunlot/logs"
- "context"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- _ "github.com/go-sql-driver/mysql"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- "log"
- "strconv"
- "time"
- )
- // 模板
- type DeviceData_R1 struct {
- T_tab string `orm:"size(256);index" json:"T_Rtab"` //转发 TAB 标识 拼接:AAAA.BBBB. 对象后面加点
- T_type int `orm:"size(200);null"` // 数据类型 (1,'bool'),(2,'u8'),(3,'u16'),(4,'float'),(5,'str'),(6,'object'),(7,'u8'[模式图片]);
- T_value string `orm:"type(text);size(200);null"` // 值
- T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- }
- // 模板
- type DeviceData_R struct {
- T_tab string `orm:"size(256);index" json:"T_Rtab"` //转发 TAB 标识 拼接:AAAA.BBBB. 对象后面加点
- T_type int `orm:"size(200);default(4)"` // 数据类型 (1,'bool'),(2,'int'),(3,'float'),(4,'str'),(5,'time'),(7,'u8'[模式图片]);
- T_bool bool `orm:"type(text);size(200);"` // 值
- T_int int `orm:"type(text);size(200);null"` // 值
- T_float float64 `orm:"type(text);size(200);null"` // 值
- T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- }
- type DeviceData_old struct {
- T_t float32
- T_rh float32
- T_site string
- T_sp int
- }
- var redis_DeviceData cache.Cache
- func init() {
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_DeviceData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redis_DeviceData, err = cache.NewCache("redis", config)
- if err != nil || redis_DeviceData == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- func Connect() (*mongo.Database, error) {
- credential := options.Credential{
- AuthSource: conf.Mongodb_DB,
- Username: conf.Mongodb_Username,
- Password: conf.Mongodb_Password,
- }
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- client, err := mongo.Connect(ctx, options.Client().ApplyURI(conf.Mongodb_Url).SetAuth(credential).SetMaxPoolSize(10))
- if err != nil {
- log.Print(err)
- }
- return client.Database(conf.Mongodb_DB), err
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func RedisDeviceData_Set(T_sn string, T_id int, r DeviceData_R) (err error) {
- key := T_sn + "|" + strconv.Itoa(T_id)
- if redis_DeviceData.IsExist(key) {
- var t DeviceData_R
- v := redis_DeviceData.Get(key)
- json.Unmarshal(v.([]byte), &t)
- // 防止时间溢出
- if time.Now().Unix() <= r.T_time.Unix() {
- r.T_time = time.Now()
- }
- // 提前最新数据
- if t.T_time.Unix() > r.T_time.Unix() {
- // 储存的 是最新数据
- return
- }
- }
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.PrintlnError("RedisDeviceData_Set", err)
- return
- }
- err = redis_DeviceData.Put(key, str, 1*time.Hour)
- if err != nil {
- logs.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- func RedisDeviceData_Get(key string) (r DeviceData_R, is bool) {
- if redis_DeviceData.IsExist(key) {
- v := redis_DeviceData.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- return DeviceData_R{}, false
- }
- // ----------------
- func Data_Add(JointTab string, bson_r *bson.M) {
- // 连接到MongoDB
- DB, err := Connect()
- if err != nil {
- panic(any(err))
- }
- //fmt.Println("JointTab:", JointTab)
- collection := DB.Collection(JointTab)
- _, err = collection.InsertOne(context.TODO(), bson_r)
- if err != nil {
- panic(any(err))
- }
- }
|