|
@@ -2,7 +2,9 @@ package Device
|
|
|
|
|
|
import (
|
|
|
"Yunlot/conf"
|
|
|
+ "Yunlot/lib"
|
|
|
"Yunlot/logs"
|
|
|
+ "Yunlot/models"
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
@@ -12,68 +14,60 @@ import (
|
|
|
"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 第一次保存时才设置时间
|
|
|
+ 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 models.Time `orm:"type(timestamp);null;"` // 采集时间
|
|
|
+ CreateTime models.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
|
|
|
+// 表单
|
|
|
+type DeviceData_Form struct {
|
|
|
+ T_sn string `json:"T_sn" form:"T_sn"` // Sn
|
|
|
+ T_jointTab string `json:"T_jointTab" form:"T_jointTab"` // 标签路径
|
|
|
+ T_jsonFind string `json:"T_jsonFind" form:"T_jsonFind"` // 条件
|
|
|
+ T_jsonSort string `json:"T_jsonSort" form:"T_jsonSort"` // 排序
|
|
|
+ PageIndex int `json:"PageIndex" form:"PageIndex"` // 当前页码
|
|
|
+ PageSize int `json:"PageSize" form:"PageSize"` // 每页文档数量
|
|
|
}
|
|
|
|
|
|
var redis_DeviceData cache.Cache
|
|
|
+var Mongodb_client *mongo.Client
|
|
|
+var Mongodb_DB *mongo.Database
|
|
|
|
|
|
func init() {
|
|
|
+ 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()
|
|
|
+ var err error
|
|
|
+ Mongodb_client, err = mongo.Connect(ctx, options.Client().ApplyURI(conf.Mongodb_Url).SetAuth(credential).SetMaxPoolSize(200))
|
|
|
+ if err != nil {
|
|
|
+ logs.PrintlnError("Mongodb 连接错误!", err)
|
|
|
+ panic(any(err))
|
|
|
+ }
|
|
|
+ Mongodb_DB = Mongodb_client.Database(conf.Mongodb_DB)
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
@@ -88,7 +82,7 @@ func RedisDeviceData_Set(T_sn string, T_id int, r DeviceData_R) (err error) {
|
|
|
json.Unmarshal(v.([]byte), &t)
|
|
|
// 防止时间溢出
|
|
|
if time.Now().Unix() <= r.T_time.Unix() {
|
|
|
- r.T_time = time.Now()
|
|
|
+ r.T_time.NowDbTime()
|
|
|
}
|
|
|
// 提前最新数据
|
|
|
if t.T_time.Unix() > r.T_time.Unix() {
|
|
@@ -121,17 +115,115 @@ func RedisDeviceData_Get(key string) (r DeviceData_R, is bool) {
|
|
|
|
|
|
// ----------------
|
|
|
func Data_Add(JointTab string, bson_r *bson.M) {
|
|
|
- // 连接到MongoDB
|
|
|
- DB, err := Connect()
|
|
|
+
|
|
|
+ logs.Println("JointTab:", JointTab, *bson_r)
|
|
|
+ collection := Mongodb_DB.Collection(JointTab)
|
|
|
+
|
|
|
+ _, err := collection.InsertOne(context.TODO(), bson_r)
|
|
|
if err != nil {
|
|
|
panic(any(err))
|
|
|
}
|
|
|
- //fmt.Println("JointTab:", JointTab)
|
|
|
- collection := DB.Collection(JointTab)
|
|
|
|
|
|
- _, err = collection.InsertOne(context.TODO(), bson_r)
|
|
|
+}
|
|
|
+
|
|
|
+// db.getCollection("2023445039284316_params_varData").find({ $and : [{"name" : "TempSet"}, {"value" : "26"}] }).limit(1000).skip(0)
|
|
|
+// ----------------
|
|
|
+func Data_Read(JointTab string, bson_r *bson.M) {
|
|
|
+
|
|
|
+ logs.Println("JointTab:", JointTab, *bson_r)
|
|
|
+ collection := Mongodb_DB.Collection(JointTab)
|
|
|
+
|
|
|
+ _, err := collection.InsertOne(context.TODO(), bson_r)
|
|
|
if err != nil {
|
|
|
panic(any(err))
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+/*
|
|
|
+----------------
|
|
|
+// 数据列表
|
|
|
+JointTab:2023445039284316_params_varData
|
|
|
+// 条件 jsonFind := `{"$or":[{"name":"TempSet"},{"value":"26"}]}`
|
|
|
+// 排序 jsonSort := `{"value": 1}`
|
|
|
+// 当前页码 page := 2
|
|
|
+// 每页文档数量 pageSize := 2
|
|
|
+*/
|
|
|
+func Data_List(JointTab, jsonFind, jsonSort string, page, pageSize int) (JSONr lib.JSONR) {
|
|
|
+ var err error
|
|
|
+ // 初始化 Mongodb_DB
|
|
|
+ collection := Mongodb_DB.Collection(JointTab)
|
|
|
+ if page < 1 {
|
|
|
+ page = 1
|
|
|
+ }
|
|
|
+ // 限制 过小、过大
|
|
|
+ if pageSize < 1 || pageSize > 9999 {
|
|
|
+ page = 10
|
|
|
+ }
|
|
|
+ // 分页
|
|
|
+ options := options.Find().SetSkip(int64((page - 1) * pageSize)).SetLimit(int64(pageSize))
|
|
|
+ // 排序
|
|
|
+ if len(jsonSort) != 0 {
|
|
|
+ // 解码JSON
|
|
|
+ var jsonSortMap map[string]interface{}
|
|
|
+ err = json.Unmarshal([]byte(jsonSort), &jsonSortMap)
|
|
|
+ if err != nil {
|
|
|
+ logs.PrintlnError("解码JSON 错误", err)
|
|
|
+ JSONr.Code = lib.Error
|
|
|
+ JSONr.Msg = "排序 解码JSON 错误!"
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ options = options.SetSort(bson.M(jsonSortMap))
|
|
|
+ }
|
|
|
+ // 条件
|
|
|
+ jsonFind_M := bson.M{}
|
|
|
+ if len(jsonFind) != 0 {
|
|
|
+ // 解码JSON
|
|
|
+ var jsonFindMap map[string]interface{}
|
|
|
+ err = json.Unmarshal([]byte(jsonFind), &jsonFindMap)
|
|
|
+ if err != nil {
|
|
|
+ logs.PrintlnError("解码JSON 错误", err)
|
|
|
+ JSONr.Code = lib.Error
|
|
|
+ JSONr.Msg = "条件 解码JSON 错误!"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ jsonFind_M = jsonFindMap
|
|
|
+ }
|
|
|
+
|
|
|
+ cursor, err := collection.Find(context.Background(), jsonFind_M, options)
|
|
|
+ if err != nil {
|
|
|
+ logs.PrintlnError("Find 错误", err)
|
|
|
+ JSONr.Code = lib.Error
|
|
|
+ JSONr.Msg = "Find 错误!"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer cursor.Close(context.Background())
|
|
|
+
|
|
|
+ var results []bson.M
|
|
|
+ if err = cursor.All(context.Background(), &results); err != nil {
|
|
|
+ logs.PrintlnError("cursor.All 错误", err)
|
|
|
+ JSONr.Code = lib.Error
|
|
|
+ JSONr.Msg = "cursor.All 错误!"
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行计数操作
|
|
|
+ count, err := collection.CountDocuments(context.Background(), jsonFind_M)
|
|
|
+ if err != nil {
|
|
|
+ logs.PrintlnError("Count 错误", err)
|
|
|
+ JSONr.Code = lib.Error
|
|
|
+ JSONr.Msg = "Count 错误!"
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONr.Data = lib.JSONS{
|
|
|
+ List: results,
|
|
|
+ Total: int16(count),
|
|
|
+ PageIndex: page,
|
|
|
+ PageSize: pageSize,
|
|
|
+ }
|
|
|
+ JSONr.Code = lib.Success
|
|
|
+ JSONr.Msg = "ok!"
|
|
|
+ return
|
|
|
+}
|