Bladeren bron

ADD:nats 添加通过sn,id,time 查询设备数据

zoie 10 maanden geleden
bovenliggende
commit
6ad73b3210
2 gewijzigde bestanden met toevoegingen van 54 en 1 verwijderingen
  1. 37 1
      Nats/Nats.go
  2. 17 0
      models/Device/DeviceData.go

+ 37 - 1
Nats/Nats.go

@@ -3,13 +3,13 @@ package Nats
 import (
 import (
 	"Cold_Api/conf"
 	"Cold_Api/conf"
 	"Cold_Api/controllers/lib"
 	"Cold_Api/controllers/lib"
-	"github.com/beego/beego/v2/core/logs"
 	"Cold_Api/models/Account"
 	"Cold_Api/models/Account"
 	"Cold_Api/models/Device"
 	"Cold_Api/models/Device"
 	"Cold_Api/models/System"
 	"Cold_Api/models/System"
 	"encoding/xml"
 	"encoding/xml"
 	"fmt"
 	"fmt"
 	"github.com/astaxie/beego/cache"
 	"github.com/astaxie/beego/cache"
+	"github.com/beego/beego/v2/core/logs"
 	"github.com/nats-io/nats.go"
 	"github.com/nats-io/nats.go"
 	"github.com/vmihailenco/msgpack/v5"
 	"github.com/vmihailenco/msgpack/v5"
 	"strconv"
 	"strconv"
@@ -458,4 +458,40 @@ func NatsInit() {
 		b, _ := msgpack.Marshal(&t_R)
 		b, _ := msgpack.Marshal(&t_R)
 		_ = lib.Nats.Publish(m.Reply, b)
 		_ = lib.Nats.Publish(m.Reply, b)
 	})
 	})
+
+	// 请求-响应 根据时间获取设备数据(单条)
+	_, _ = lib.Nats.Subscribe("Cold_ReadDeviceDataBy_T_snid_T_time", func(m *nats.Msg) {
+		fmt.Printf("Cold_ReadDeviceDataBy_T_snid_T_time message: %s\n", string(m.Data))
+		type T_Req struct {
+			T_sn string `xml:"T_sn"`
+			T_id int    `xml:"T_id"`
+			Time string `xml:"Time"`
+		}
+		type T_R struct {
+			Code int16              `xml:"Code"`
+			Msg  string             `xml:"Msg"`
+			Data Device.DeviceData_ `xml:"Data"` // 泛型
+		}
+		var t_Req T_Req
+		var t_R T_R
+
+		err := msgpack.Unmarshal(m.Data, &t_Req)
+		if err != nil {
+			t_R.Code = 202
+			t_R.Msg = "Unmarshal error"
+			b, _ := msgpack.Marshal(&t_R)
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		deviceData := Device.Read_DeviceData_By_Time(t_Req.T_sn, t_Req.T_id, t_Req.Time)
+
+		t_R.Code = 200
+		t_R.Msg = "ok"
+		t_R.Data = deviceData
+
+		b, _ := msgpack.Marshal(&t_R)
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
 }
 }

+ 17 - 0
models/Device/DeviceData.go

@@ -752,3 +752,20 @@ func Read_DeviceData_BackUp(T_sn string, T_id int, Time_start_ string, Time_end_
 
 
 	return maps, len(maps)
 	return maps, len(maps)
 }
 }
+
+func Read_DeviceData_By_Time(T_sn string, T_id int, Time string) (t DeviceData_) {
+
+	o := orm.NewOrm()
+	var maps DeviceData_
+	sql := "SELECT t_id,t_sp,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_" + T_sn + " WHERE" + " t_id = " + strconv.Itoa(T_id) + " AND t_time <= '" + Time + "'" + " ORDER BY t_time1 DESC LIMIT 0,1"
+
+	fmt.Println(sql)
+	err := o.Raw(sql).QueryRow(&maps)
+
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return t
+	}
+
+	return maps
+}