siked 1 年之前
父節點
當前提交
5bcbf21ae3

+ 30 - 27
Handle/MqttServer/Server.go

@@ -100,11 +100,16 @@ func Run_MqttServer() {
 	if err != nil {
 		logs.Println("MqttServer", "订阅消息 [cli.Subscribe]", "")
 		logs.Println("err!!!!!! Run_MqttServer:", "连接MQTT失败:", err)
-
 	}
 
 	fmt.Println("MQTT ok!")
 
+	go MQTT_Gin_Run() // 初始化 MQTT ACL
+
+}
+
+func MQTT_Gin_Run() {
+
 	// 创建一个Gin路由器
 	r := gin.Default()
 
@@ -117,16 +122,16 @@ func Run_MqttServer() {
 		}
 		if err := c.Bind(&json); err != nil {
 			c.JSON(200, gin.H{
-				"result": "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
-				"is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
+				"result":       "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
+				"is_superuser": false,  // 超级用户 可选 true | false,该项为空时默认为 false
 			})
 			return
 		}
 		// 保证 Clientid唯一,否则会 下线
 		if json.Clientid != json.Username {
 			c.JSON(200, gin.H{
-				"result": "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
-				"is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
+				"result":       "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
+				"is_superuser": false,  // 超级用户 可选 true | false,该项为空时默认为 false
 			})
 			return
 		}
@@ -136,42 +141,42 @@ func Run_MqttServer() {
 		// 判断是否存在
 		if !Device_r.Read_Tidy() {
 			c.JSON(200, gin.H{
-				"result": "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
-				"is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
+				"result":       "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
+				"is_superuser": false,  // 超级用户 可选 true | false,该项为空时默认为 false
 			})
 			return
 		}
 		// 判断密码是否正确
 		if Device_r.T_password != json.Password {
 			c.JSON(200, gin.H{
-				"result": "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
-				"is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
+				"result":       "deny", //  认证结果 可选 "allow" | "deny" | "ignore"
+				"is_superuser": false,  // 超级用户 可选 true | false,该项为空时默认为 false
 			})
 			return
 		}
 
 		c.JSON(200, gin.H{
-			"result": "allow", // 可选 "allow" | "deny" | "ignore"
-			"is_superuser": false, // 可选 true | false,该项为空时默认为 false
+			"result":       "allow", // 可选 "allow" | "deny" | "ignore"
+			"is_superuser": false,   // 可选 true | false,该项为空时默认为 false
 		})
 	})
 	r.POST("/Acl", func(c *gin.Context) {
 		logs.Println("Acl!!")
 		var json struct {
-			Clientid string `json:"clientid"`//${clientid} — 客户端的 ID。
-			Username string `json:"username"`//${username} — 客户端登录是用的用户名。
-			Peerhost string `json:"peerhost"`//${peerhost} — 客户端的源 IP 地址。
-			Proto_name string `json:"proto_name"`//${proto_name} — 客户端使用的协议名称。例如 MQTT,CoAP 等。
-			Mountpoint string `json:"mountpoint"`//${mountpoint} — 网关监听器的挂载点(主题前缀)。
-			Action string `json:"action"`//${action} — 当前执行的动作请求,例如 publish,subscribe。
-			Topic string `json:"topic"`//${topic} — 当前请求想要发布或订阅的主题(或主题过滤器)
+			Clientid   string `json:"clientid"`   //${clientid} — 客户端的 ID。
+			Username   string `json:"username"`   //${username} — 客户端登录是用的用户名。
+			Peerhost   string `json:"peerhost"`   //${peerhost} — 客户端的源 IP 地址。
+			Proto_name string `json:"proto_name"` //${proto_name} — 客户端使用的协议名称。例如 MQTT,CoAP 等。
+			Mountpoint string `json:"mountpoint"` //${mountpoint} — 网关监听器的挂载点(主题前缀)。
+			Action     string `json:"action"`     //${action} — 当前执行的动作请求,例如 publish,subscribe。
+			Topic      string `json:"topic"`      //${topic} — 当前请求想要发布或订阅的主题(或主题过滤器)
 		}
 		if err := c.Bind(&json); err != nil {
 			c.JSON(200, gin.H{
 				"result": "deny", // 可选 "allow" | "deny" | "ignore"
 			})
 		}
-		fmt.Println("json:",json)
+		fmt.Println("json:", json)
 
 		if json.Username == "admin" {
 			c.JSON(200, gin.H{"result": "allow"})
@@ -197,10 +202,9 @@ func Run_MqttServer() {
 			return
 		}
 
-
 		//topic  /sub/SN  /pub/SN
-		topic_list := strings.Split(json.Topic,"/")
-		for _,v := range topic_list{
+		topic_list := strings.Split(json.Topic, "/")
+		for _, v := range topic_list {
 			if len(v) < 7 {
 				continue // 不是有效SN
 			}
@@ -210,12 +214,11 @@ func Run_MqttServer() {
 			}
 		}
 
-		logs.Println("MQTT Acl E!topic_list:",topic_list)
+		logs.Println("MQTT Acl E!topic_list:", topic_list)
 
 		c.JSON(200, gin.H{"result": "deny"})
 		return
 
-
 		//
 		//Clientid_list := strings.Split(username+"_s", "_")
 		//username = Clientid_list[0]
@@ -273,15 +276,15 @@ func messagePubHandler(topicName string, message []byte) {
 	// 设备协议
 	ProductProt_r := Product.ProductProt{Id: ProductType_r.T_prot}
 	if !ProductProt_r.Read() {
-		logs.Println("MqttServer", Device_r.T_sn+"|"+Device_r.T_ProductID+"-"+ fmt.Sprintf("%d",ProductType_r.T_prot)+" 设备协议找不到!")
+		logs.Println("MqttServer", Device_r.T_sn+"|"+Device_r.T_ProductID+"-"+fmt.Sprintf("%d", ProductType_r.T_prot)+" 设备协议找不到!")
 		return
 	}
 
 	// 根据库的存放路径加载库
-	p, err := plugin.Open(conf.Analysis_Dir + ProductProt_r.T_analysis)
+	p, err := plugin.Open(conf.Analysis_Dir + ProductProt_r.T_analysis + ".so")
 	if err != nil {
 		println(err)
-		panic(any(err))
+		logs.PrintlnError("打开 SO 失败:", err)
 	}
 
 	if topicName[len(topicName)-6:] == "_reply" {

+ 30 - 0
Nats/Nats.go

@@ -0,0 +1,30 @@
+package Nats
+
+import (
+	"Yunlot/Nats/NatsServer"
+	"Yunlot/conf"
+	"Yunlot/lib"
+	"Yunlot/logs"
+	"github.com/nats-io/nats.go"
+	"time"
+)
+
+func init() {
+	go NatsInit()
+}
+
+func NatsInit() {
+	time.Sleep(time.Second * 3)
+	var err error
+	// 连接Nats服务器
+	lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
+	if err != nil {
+		logs.Println("nats 连接失败!")
+		panic(any(err))
+	}
+	logs.Println("nats OK!")
+
+	go NatsServer.NatsServer_Device()
+	go NatsServer.NatsServer_Product()
+
+}

+ 53 - 0
Nats/NatsServer/Device.go

@@ -0,0 +1,53 @@
+package NatsServer
+
+import (
+	"Yunlot/lib"
+	"Yunlot/logs"
+	"Yunlot/models/Device"
+	"github.com/nats-io/nats.go"
+	"github.com/vmihailenco/msgpack/v5"
+)
+
+func NatsServer_Device() {
+	// 请求-响应
+	_, _ = lib.Nats.Subscribe("/Device/Device/Get", func(m *nats.Msg) {
+		logs.Println("Nats /Device/Device/Get: %s\n", string(m.Data))
+
+		var Device_r Device.Device
+		err := msgpack.Unmarshal(m.Data, &Device_r)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		if !Device_r.Read_Tidy() {
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "SN E!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_r})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+	// 请求-响应
+	_, _ = lib.Nats.Subscribe("/Device/Device/List", func(m *nats.Msg) {
+		logs.Println("Nats /Device/Device/List: %s\n", string(m.Data))
+
+		var Device_r Device.Device
+		err := msgpack.Unmarshal(m.Data, &Device_r)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		Device_list := Device_r.Lists_All()
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_list})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+}

+ 148 - 0
Nats/NatsServer/Product.go

@@ -0,0 +1,148 @@
+package NatsServer
+
+import (
+	"Yunlot/lib"
+	"Yunlot/logs"
+	"Yunlot/models/Product"
+	"github.com/nats-io/nats.go"
+	"github.com/vmihailenco/msgpack/v5"
+)
+
+func NatsServer_Product() {
+	//  产品类型 - 获取
+	_, _ = lib.Nats.Subscribe("/Product/ProductType/Get", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductType/List: %s\n", string(m.Data))
+
+		var ProductType_r Product.ProductType
+		err := msgpack.Unmarshal(m.Data, &ProductType_r)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		if !ProductType_r.Read() {
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "ProductID E!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+	// 产品类型 - 列表
+	_, _ = lib.Nats.Subscribe("/Product/ProductType/List", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductType/List: %s\n", string(m.Data))
+
+		var ProductType_r Product.ProductType
+		err := msgpack.Unmarshal(m.Data, &ProductType_r)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		ProductType_list := ProductType_r.Lists_All()
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_list})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+	// 网关 - 列表
+	_, _ = lib.Nats.Subscribe("/Product/ProductModeLists/List", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductModeLists/List: %s\n", string(m.Data))
+
+		ProductModeLists_list, _ := Product.ProductModeLists()
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductModeLists_list})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+	//  产品协议 - 获取
+	_, _ = lib.Nats.Subscribe("/Product/ProductProt/Get", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductProt/Get: %s\n", string(m.Data))
+
+		var ProductProt Product.ProductProt
+		err := msgpack.Unmarshal(m.Data, &ProductProt)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		if !ProductProt.Read() {
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "ID E!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+	// 产品协议 - 列表
+	_, _ = lib.Nats.Subscribe("/Product/ProductType/List", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductType/List: %s\n", string(m.Data))
+
+		var ProductProt Product.ProductProt
+		err := msgpack.Unmarshal(m.Data, &ProductProt)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		ProductProt_list := ProductProt.Lists_All()
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_list})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+	//  产品协议 - 获取
+	_, _ = lib.Nats.Subscribe("/Product/ProductProt/Get", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductProt/Get: %s\n", string(m.Data))
+
+		var ProductProt Product.ProductProt
+		err := msgpack.Unmarshal(m.Data, &ProductProt)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		if !ProductProt.Read() {
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "ID E!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+	// 产品协议 - 列表
+	_, _ = lib.Nats.Subscribe("/Product/ProductType/List", func(m *nats.Msg) {
+		logs.Println("Nats /Product/ProductType/List: %s\n", string(m.Data))
+
+		var ProductProt Product.ProductProt
+		err := msgpack.Unmarshal(m.Data, &ProductProt)
+		if err != nil {
+			logs.PrintlnError("Nats:", err)
+			b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
+			_ = lib.Nats.Publish(m.Reply, b)
+			return
+		}
+
+		ProductProt_list := ProductProt.Lists_All()
+
+		b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_list})
+		_ = lib.Nats.Publish(m.Reply, b)
+	})
+
+}

+ 1 - 1
RunCode/config/docker_config.json

@@ -1,5 +1,5 @@
 {
-  "timeout": 3,
+  "timeout": 30,
   "docker_base": "run -i --cpus=1 -m=512M --rm --network none --mount source=yunlotso,target=/usr/src/myapp/so -v {tmp_file}:/usr/src/myapp/{runner_filename} -w /usr/src/myapp {image} sh -c",
   "docker_runner": {
     "go": {

+ 1 - 1
RunCode/config/main.txt

@@ -28,4 +28,4 @@ func main() {
 		fmt.Print("|-=&" + "无参数")
 	}
 	// 返回数据: 1、 设备发送数据解析到平台json     2、topic     3、平台处理后返回给设备数据
-}
+}

+ 4 - 2
RunCode/ctl/controller.go

@@ -69,7 +69,7 @@ func RunController(writer http.ResponseWriter, request *http.Request) {
 		content, err := service.DockerRunner.Exec(cancelCtx, lang, code)
 		if err == nil {
 			content_list := strings.Split(string(content), "|-=&")
-			fmt.Println("len(content_list):",len(content_list))
+			fmt.Println("content_list:", content_list)
 			if len(content_list) != 4 {
 				ctx.Error("解析错误!!!" + string(content))
 				return
@@ -109,12 +109,14 @@ func BuildController(writer http.ResponseWriter, request *http.Request) {
 			return
 		}
 
-		code := "package main\n" + string(body) + strings.Replace(Go_Main, "func main()", "func mainx()", 1)
+		code := "package main\n" + string(body)
 		cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
 		defer cancelFn()
 
 		name_so := time.Now().UnixMilli()
+		//fmt.Println("code:",code)
 		content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code)
+		fmt.Println("content:", content)
 		if len(content) > 0 {
 			ctx.Error(string(content))
 			return

+ 2 - 2
conf/app.conf

@@ -4,10 +4,10 @@ runmode = dev
 
 
 # 数据解析加载目录
-Analysis_Dir = "/home/bzd/GolandProjects/Yunlot_Plugin/"
+Analysis_Dir = "/var/lib/docker/volumes/yunlotso/_data/"
 
 # Nats
-NatsServer_Url = "172.0.0.1:43422"
+NatsServer_Url = "127.0.0.1:4222"
 
 
 # Mysql

+ 6 - 14
controllers/Device.go

@@ -30,7 +30,7 @@ func (c *DeviceController) Add() {
 	ProductKey := c.GetString("ProductKey")
 	ProductID := c.GetString("ProductID")
 	ProductType_r := Product.ProductType{T_ProductID: ProductID}
-	if !ProductType_r.Read(){
+	if !ProductType_r.Read() {
 		c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID!"}
 		c.ServeJSON()
 		return
@@ -41,18 +41,12 @@ func (c *DeviceController) Add() {
 		return
 	}
 
-
 	// 创建sn
-	T_name := c.GetString("T_name")
 	T_sn := c.GetString("T_sn")
-	Device_r := Device.Device{T_name:T_name,T_ProductID: ProductID,T_sn: T_sn}
-	// 名称为空,默认用设备类型名称
-	if len(Device_r.T_name) == 0 {
-		Device_r.T_name = ProductType_r.T_name
-	}
+	Device_r := Device.Device{T_ProductID: ProductID, T_sn: T_sn}
 
 	//
-	CreateNum,_ := c.GetInt("CreateNum",1)
+	CreateNum, _ := c.GetInt("CreateNum", 1)
 	if CreateNum == 1 || len(Device_r.T_sn) >= 10 {
 		// 单个
 		if !Device_r.CreateSn(1) {
@@ -65,7 +59,7 @@ func (c *DeviceController) Add() {
 		c.ServeJSON()
 		return
 
-	}else {
+	} else {
 		// 多个
 		var Device_List []Device.Device
 		for i := 1; i <= CreateNum; i++ {
@@ -111,19 +105,17 @@ func (c *DeviceController) Delete() {
 	return
 }
 
-
 func (c *DeviceController) Get() {
 	Devicer := Device.Device{}
 	c.ParseForm(&Devicer)
 
-	if !Devicer.Read_Tidy(){
+	if !Devicer.Read_Tidy() {
 		c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"}
 		c.ServeJSON()
 		return
 	}
 
-	c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
+	c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Devicer}
 	c.ServeJSON()
 	return
 }
-

+ 16 - 3
controllers/ProductProt.go

@@ -18,6 +18,21 @@ func (c *ProductProtController) ProductModeLists() {
 	c.ServeJSON()
 	return
 }
+func (c *ProductProtController) Get() {
+	ProductProt := Product.ProductProt{}
+	c.ParseForm(&ProductProt)
+
+	if !ProductProt.Read() {
+		c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
+		c.ServeJSON()
+		return
+	}
+
+	c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt}
+	c.ServeJSON()
+	return
+}
+
 func (c *ProductProtController) List() {
 	PageIndex, _ := c.GetInt("PageIndex", 0)
 	PageSize, _ := c.GetInt("PageSize", 10)
@@ -47,7 +62,7 @@ func (c *ProductProtController) Update() {
 	ProductProtr := Product.ProductProt{}
 	c.ParseForm(&ProductProtr)
 
-	if !ProductProtr.Update("T_name", "T_img", "T_prot") {
+	if !ProductProtr.Update("T_name", "T_mode", "T_lang", "T_analysis", "T_text", "T_describe") {
 		c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
 		c.ServeJSON()
 		return
@@ -72,5 +87,3 @@ func (c *ProductProtController) Delete() {
 	c.ServeJSON()
 	return
 }
-
-

+ 15 - 2
controllers/ProductType.go

@@ -24,6 +24,21 @@ func (c *ProductTypeController) List() {
 	return
 }
 
+func (c *ProductTypeController) Get() {
+	ProductTyper := Product.ProductType{}
+	c.ParseForm(&ProductTyper)
+
+	if !ProductTyper.Read() {
+		c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
+		c.ServeJSON()
+		return
+	}
+
+	c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductTyper}
+	c.ServeJSON()
+	return
+}
+
 func (c *ProductTypeController) Add() {
 	ProductType_r := Product.ProductType{}
 	c.ParseForm(&ProductType_r)
@@ -67,5 +82,3 @@ func (c *ProductTypeController) Delete() {
 	c.ServeJSON()
 	return
 }
-
-

+ 4 - 1
go.mod

@@ -11,11 +11,14 @@ require (
 	github.com/nats-io/nats.go v1.16.0
 	github.com/satori/go.uuid v1.2.0
 	github.com/thinkeridea/go-extend v1.3.2
+	github.com/vmihailenco/msgpack/v5 v5.4.0
 	github.com/yosssi/gmq v0.0.1
 	go.mongodb.org/mongo-driver v1.12.0
 	golang.org/x/text v0.13.0
 )
 
+require github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
+
 require (
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/bytedance/sonic v1.10.0 // indirect
@@ -50,7 +53,7 @@ require (
 	github.com/prometheus/client_golang v1.7.0 // indirect
 	github.com/prometheus/client_model v0.2.0 // indirect
 	github.com/prometheus/common v0.10.0 // indirect
-	github.com/prometheus/procfs v0.1.3 // indirectS
+	github.com/prometheus/procfs v0.1.3 // indirect; indirectS
 	github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect

+ 4 - 0
go.sum

@@ -237,6 +237,10 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2
 github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
 github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
 github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
+github.com/vmihailenco/msgpack/v5 v5.4.0 h1:hRM0digJwyR6vll33NNAwCFguy5JuBD6jxDmQP3l608=
+github.com/vmihailenco/msgpack/v5 v5.4.0/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
+github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
+github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
 github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
 github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
 github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=

+ 6 - 8
main.go

@@ -4,6 +4,7 @@ import (
 	"Yunlot/Handle/HttpServer"
 	"Yunlot/Handle/MqttServer"
 	"Yunlot/Handle/TcpServer"
+	_ "Yunlot/Nats"
 	"Yunlot/RunCode"
 	"Yunlot/TimeTask"
 	"Yunlot/conf"
@@ -14,9 +15,7 @@ import (
 	orm2 "github.com/beego/beego/v2/client/orm"
 	beego "github.com/beego/beego/v2/server/web"
 	_ "github.com/go-sql-driver/mysql"
-
 	"strconv"
-
 )
 
 func init() {
@@ -43,9 +42,9 @@ func main() {
 	//go MqttServer.DeviceMqttMap_go() //// 缓存数据发送-确保设备在休眠后 能收到数据
 
 	// MQTT 通讯
-	if conf.MqttServer_Open == 1{
+	if conf.MqttServer_Open == 1 {
 		mode := Product.ProductMode{
-			Id:         0,
+			Id:         1,
 			T_name:     "MQTT",
 			T_address:  "mqtt://bj-3-mqtt.iot-api.com:1883",
 			T_describe: "MQTT 接入方式为设备和云平台提供双向连接,设备既可上报属性数据,也可接收云端的消息下发。",
@@ -55,9 +54,9 @@ func main() {
 		go MqttServer.Run_MqttServer()
 	}
 	// TCP 通讯
-	if conf.TCPServer_Open == 1{
+	if conf.TCPServer_Open == 1 {
 		mode := Product.ProductMode{
-			Id:         1,
+			Id:         2,
 			T_name:     "TCP",
 			T_address:  "TCP://bj-3-mqtt.iot-api.com:1883",
 			T_describe: "TCP 接入方式为设备和云平台提供双向连接,设备既可上报属性数据,也可接收云端的消息下发。",
@@ -67,7 +66,7 @@ func main() {
 		go TcpServer.TcpServer()
 	}
 	// HTTP 通讯
-	if conf.HTTPServer_Open == 1{
+	if conf.HTTPServer_Open == 1 {
 		mode := Product.ProductMode{
 			Id:         6,
 			T_name:     "HTTP",
@@ -79,7 +78,6 @@ func main() {
 		go HttpServer.Run()
 	}
 
-
 	//go Nats.NatsInit() // Nats 通讯
 
 	go TimeTask.Init()        // 时间任务

+ 20 - 13
models/Device/Device.go

@@ -4,7 +4,6 @@ import (
 	"Yunlot/conf"
 	"Yunlot/lib"
 	"Yunlot/logs"
-	"Yunlot/models/Product"
 	"encoding/json"
 	"fmt"
 	"github.com/astaxie/beego/cache"
@@ -21,24 +20,12 @@ type Device struct {
 	T_password  string `orm:"size(256);" json:"T_password" form:"T_password"`           // 密码
 	T_online    int    `orm:"size(1);index;default(0)" json:"T_online" form:"T_online"` // 在线状态  0 离线  1 在线   3 无效
 	T_ProductID string `orm:"size(8);index" json:"T_ProductID" form:"T_ProductID"`      // 产品类型
-	T_name      string `orm:"size(256);" json:"T_name" form:"T_name"`                   // 标题
 
 	T_state    int       `orm:"size(2);default(0);index" json:"T_state" form:"T_state"` // 0 未激活   1 正常  2 禁用\删除
 	CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"`
 	UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"`
 }
 
-// 设备
-type Device_R struct {
-	T_sn        string  // Sn
-	T_password  string  // 密码
-	T_online    int     // 在线状态  0 离线  1 在线   3 无效
-	T_ProductID Product.ProductType  // 产品类型
-	T_name      string  // 标题
-	T_state     int     // 0 未激活   1 正常  2 禁用\删除
-
-}
-
 func (t *Device) TableName() string {
 	return "Device" // 数据库名称   // ************** 替换 FormulaList **************
 }
@@ -222,3 +209,23 @@ func (t *Device) Lists(PageIndex int, PageSize int) (r []Device, Total int64) {
 
 	return r, Total
 }
+
+// 获取列表
+func (t *Device) Lists_All() (r []Device) {
+
+	o := orm.NewOrm()
+
+	// 也可以直接使用 Model 结构体作为表名
+	qs := o.QueryTable(new(Device))
+
+	// 筛选参数
+	cond := orm.NewCondition()
+	if len(t.T_ProductID) > 0 {
+		cond = cond.And("T_ProductID", t.T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
+	}
+
+	// 执行
+	qs.SetCond((*orm2.Condition)(cond)).All(&r)
+
+	return r
+}

+ 6 - 6
models/Product/ProductMode.go

@@ -1,12 +1,12 @@
 package Product
 
-// 产品协议 0:Mqtt  1:tcp  3:CoAP   6:http  7:https  8:websocket
+// 产品协议 1:Mqtt  2:tcp  3:CoAP   6:http  7:https  8:websocket
 type ProductMode struct {
-	Id     int    `json:"Id"`
-	T_name string `json:"T_name"`         // 协议名称  Mqtt
-	T_address  string    `json:"T_address"` //接入地址    bj-3-mqtt.iot-api.com:1883
-	T_describe    string `json:"T_describe"`                       // 描述内容
-	T_state     int    `json:"T_state"`    //状态
+	Id         int    `json:"Id"`
+	T_name     string `json:"T_name"`     // 协议名称  Mqtt
+	T_address  string `json:"T_address"`  //接入地址    bj-3-mqtt.iot-api.com:1883
+	T_describe string `json:"T_describe"` // 描述内容
+	T_state    int    `json:"T_state"`    //状态
 }
 
 var ProductModeList = []*ProductMode{}

+ 32 - 9
models/Product/ProductProt.go

@@ -15,14 +15,14 @@ import (
 
 // 产品协议
 type ProductProt struct {
-	Id         int    `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
-	T_name     string `orm:"size(256);" json:"T_name" form:"T_name"`                                      // 协议名称
-	T_mode     int    `orm:"size(1);default(0)" json:"T_Mode" form:"T_mode"`                              //接入方式  0:Mqtt  1:http  2:tcp  3:CoAP  4:websocket
+	Id     int    `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
+	T_name string `orm:"size(256);" json:"T_name" form:"T_name"`         // 协议名称
+	T_mode int    `orm:"size(1);default(0)" json:"T_Mode" form:"T_mode"` //接入方式  0:Mqtt  1:http  2:tcp  3:CoAP  4:websocket
 	//T_prot     int    `orm:"size(1);default(0)" json:"T_Prot" form:"T_prot"`                              //接入协议ID  0:统一协议
-	T_lang     int    `orm:"size(1);default(1)" json:"T_lang" form:"T_lang"`                              //编程语言  0: C  1: go
+	T_lang     int    `orm:"size(1);default(1)" json:"T_lang" form:"T_lang"`                                  //编程语言  0: C  1: go
 	T_analysis string `orm:"size(100);default(yunlot-pxxxx-v1.19-01.so)" json:"T_analysis" form:"T_analysis"` //数据解析
-	T_text    string `orm:"type(text);default('')" json:"T_text" form:"T_text"`                       // 代码内容
-	T_describe    string `orm:"type(text);default('')" json:"T_describe" form:"T_describe"`                       // 描述内容
+	T_text     string `orm:"type(text);default('')" json:"T_text" form:"T_text"`                              // 代码内容
+	T_describe string `orm:"type(text);default('')" json:"T_describe" form:"T_describe"`                      // 描述内容
 	//T_reply    string `orm:"size(100);default(_reply)" json:"T_reply" form:"T_reply"`                       //返回后缀  _reply
 
 }
@@ -57,7 +57,7 @@ func (t *ProductProt) redis_Set() (err error) {
 		return
 	}
 
-	err = redis_ProductProt.Put(fmt.Sprintf("%d",t.Id), str, 24*time.Hour)
+	err = redis_ProductProt.Put(fmt.Sprintf("%d", t.Id), str, 24*time.Hour)
 	if err != nil {
 		logs.Println("set key:", t.Id, ",value:", str, err)
 	}
@@ -77,7 +77,7 @@ func (t *ProductProt) redis_Get(key string) (is bool) {
 	return false
 }
 func (t *ProductProt) redis_DelK() (err error) {
-	err = redis_ProductProt.Delete(fmt.Sprintf("%d",t.Id))
+	err = redis_ProductProt.Delete(fmt.Sprintf("%d", t.Id))
 	return
 }
 
@@ -98,7 +98,7 @@ func (t *ProductProt) Add() (is bool) {
 
 // 获取
 func (t *ProductProt) Read() (bool bool) {
-	if t.redis_Get(fmt.Sprintf("%d",t.Id)) {
+	if t.redis_Get(fmt.Sprintf("%d", t.Id)) {
 		return true
 	}
 	o := orm.NewOrm()
@@ -161,3 +161,26 @@ func (t *ProductProt) Lists(PageIndex int, PageSize int) (r []ProductProt, Total
 
 	return r, Total
 }
+
+// 获取列表
+func (t *ProductProt) Lists_All() (r []ProductProt) {
+
+	o := orm.NewOrm()
+
+	// 也可以直接使用 Model 结构体作为表名
+	qs := o.QueryTable(new(ProductProt))
+
+	// 筛选参数
+	cond := orm.NewCondition()
+	if t.T_mode > 0 {
+		cond = cond.And("T_mode", t.T_mode) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
+	}
+	if len(t.T_name) > 0 {
+		cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
+	}
+
+	// 执行
+	qs.SetCond((*orm2.Condition)(cond)).All(&r)
+
+	return r
+}

+ 20 - 21
models/Product/ProductTab.go

@@ -15,33 +15,33 @@ import (
 
 // 模板
 type ProductTab struct {
-	Id         int    `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
+	Id          int    `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
 	T_ProductID string `orm:"size(8);index" json:"T_ProductID" form:"T_ProductID"` // 产品型号 随机生成(8位)
-	T_fid         int    `orm:"size(11);index" json:"T_fid" form:"T_fid"`     // 父级ID,0为根
-
-	T_name        string `orm:"size(256);" json:"T_name" form:"T_name"`    // 名称
-	T_tab  string `orm:"size(32);" json:"T_tab" form:"T_tab"`  // 标签
-	T_type int    `orm:"size(6);" json:"T_type" form:"T_type"` // 数据类型  1 数值  2 文本  3 枚举
-	T_attr string `orm:"size(200);" json:"T_attr" form:"T_attr"` // 属性
-	T_text string `orm:"size(200);" json:"T_text" form:"T_text"` // 说明
+	T_fid       int    `orm:"size(11);index" json:"T_fid" form:"T_fid"`            // 父级ID,0为根
 
+	T_name string `orm:"size(256);" json:"T_name" form:"T_name"`      // 名称
+	T_tab  string `orm:"size(32);" json:"T_tab" form:"T_tab"`         // 标签
+	T_type int    `orm:"size(6);" json:"T_type" form:"T_type"`        // 数据类型  1 数值  2 文本  3 枚举
+	T_attr string `orm:"size(200);" json:"T_attr" form:"T_attr"`      // 属性
+	T_text string `orm:"size(200);" json:"T_text" form:"T_text"`      // 说明
+	T_read bool   `orm:"default(false);" json:"T_read" form:"T_read"` // 只读
 
 	// T_attr属性  定义
 	/*
-	1 数值
-		unit 单位
-		dvalue 默认值
-		accuracy 精度
-		min 最小值
-		max 最大值
+		1 数值
+			unit 单位
+			dvalue 默认值
+			accuracy 精度
+			min 最小值
+			max 最大值
 
-	2 文本
-		dvalue 默认值
+		2 文本
+			dvalue 默认值
 
-	3 枚举
-		map [] 枚举
-			value 值
-			text 描述
+		3 枚举
+			map [] 枚举
+				value 值
+				text 描述
 
 	*/
 }
@@ -177,7 +177,6 @@ func (t *ProductTab) Lists(PageIndex int, PageSize int) (r []ProductTab, Total i
 
 	cond = cond.And("T_fid", t.T_fid) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
 
-
 	// 执行
 	qs.Limit(PageSize, offset).SetCond((*orm2.Condition)(cond)).All(&r)
 	Total, _ = qs.SetCond((*orm2.Condition)(cond)).Count()

+ 29 - 6
models/Product/ProductType.go

@@ -17,10 +17,10 @@ import (
 type ProductType struct {
 	//Id      int64    `orm:"column(ID);size(11);auto;pk" `
 	T_ProductID string `orm:"size(8);pk" json:"T_ProductID" form:"T_ProductID"` // 产品型号 随机生成(8位)
-	T_name        string `orm:"size(256);" json:"T_name" form:"T_name"`               // 产品名称
-	T_img         string `orm:"size(200);" json:"T_img" form:"T_img"`                 // 图片
-	T_akey        string `orm:"size(56);" json:"T_akey" `                                            // 授权密钥
-	T_prot        int    `orm:"size(10);default(0)" json:"T_prot" form:"T_prot"`                     //接入协议ID  0:统一协议
+	T_name      string `orm:"size(256);" json:"T_name" form:"T_name"`           // 产品名称
+	T_img       string `orm:"size(200);" json:"T_img" form:"T_img"`             // 图片
+	T_akey      string `orm:"size(56);" json:"T_akey" `                         // 授权密钥
+	T_prot      int    `orm:"size(10);default(0)" json:"T_prot" form:"T_prot"`  //接入协议ID  0:统一协议
 
 	CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"`
 	UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"`
@@ -100,7 +100,7 @@ func (t *ProductType) Read() (bool bool) {
 		return true
 	}
 	o := orm.NewOrm()
-	err := o.Read(t,"T_ProductID") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
+	err := o.Read(t, "T_ProductID") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
 	if err != nil {
 		return false
 	}
@@ -111,7 +111,7 @@ func (t *ProductType) Read() (bool bool) {
 // 修改
 func (t *ProductType) Update(cols ...string) bool {
 	o := orm.NewOrm()
-	num, err := o.Update(t, cols...);
+	num, err := o.Update(t, cols...)
 	if err == nil {
 		logs.Println("Number of records updated in database:", num)
 		t.redis_Set() // Redis 更新缓存
@@ -163,3 +163,26 @@ func (t *ProductType) Lists(PageIndex int, PageSize int) (r []ProductType, Total
 
 	return r, Total
 }
+
+// 获取列表
+func (t *ProductType) Lists_All() (r []ProductType) {
+
+	o := orm.NewOrm()
+
+	// 也可以直接使用 Model 结构体作为表名
+	qs := o.QueryTable(new(ProductType))
+
+	// 筛选参数
+	cond := orm.NewCondition()
+	if len(t.T_name) > 0 {
+		cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
+	}
+	if len(t.T_ProductID) > 0 {
+		cond = cond.And("T_ProductID", t.T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
+	}
+
+	// 执行
+	qs.SetCond((*orm2.Condition)(cond)).All(&r)
+
+	return r
+}

+ 3 - 2
routers/Device.go

@@ -8,10 +8,11 @@ import (
 func init() {
 	prefix := "/Device"
 
-	beego.Router(prefix+"/Device/Create", &controllers.DeviceController{}, "*:Add")       // 获取未读消息
+	beego.Router(prefix+"/Device/Create", &controllers.DeviceController{}, "*:Add") // 获取未读消息
+
+	beego.Router(prefix+"/Device/Get", &controllers.DeviceController{}, "*:Get")       // 获取未读消息
 	beego.Router(prefix+"/Device/List", &controllers.DeviceController{}, "*:List")     // 获取未读消息
 	beego.Router(prefix+"/Device/Update", &controllers.DeviceController{}, "*:Update") // 获取未读消息
 	beego.Router(prefix+"/Device/Delete", &controllers.DeviceController{}, "*:Delete") // 获取未读消息
 
-
 }

+ 3 - 1
routers/Product.go

@@ -8,16 +8,18 @@ import (
 func init() {
 	prefix := "/Product"
 	// 接入网关
-	beego.Router(prefix+"/ProductModeLists/List", &controllers.ProductProtController{}, "*:ProductModeLists")       // 获取未读消息
+	beego.Router(prefix+"/ProductModeLists/List", &controllers.ProductProtController{}, "*:ProductModeLists") // 获取未读消息
 
 	// 产品类型
 	beego.Router(prefix+"/ProductType/Add", &controllers.ProductTypeController{}, "*:Add")       // 获取未读消息
+	beego.Router(prefix+"/ProductType/Get", &controllers.ProductTypeController{}, "*:Get")       // 获取未读消息
 	beego.Router(prefix+"/ProductType/List", &controllers.ProductTypeController{}, "*:List")     // 获取未读消息
 	beego.Router(prefix+"/ProductType/Update", &controllers.ProductTypeController{}, "*:Update") // 获取未读消息
 	beego.Router(prefix+"/ProductType/Delete", &controllers.ProductTypeController{}, "*:Delete") // 获取未读消息
 
 	// 产品协议
 	beego.Router(prefix+"/ProductProt/Add", &controllers.ProductProtController{}, "*:Add")       // 获取未读消息
+	beego.Router(prefix+"/ProductProt/Get", &controllers.ProductProtController{}, "*:Get")       // 获取未读消息
 	beego.Router(prefix+"/ProductProt/List", &controllers.ProductProtController{}, "*:List")     // 获取未读消息
 	beego.Router(prefix+"/ProductProt/Update", &controllers.ProductProtController{}, "*:Update") // 获取未读消息
 	beego.Router(prefix+"/ProductProt/Delete", &controllers.ProductProtController{}, "*:Delete") // 获取未读消息

+ 35 - 0
tests/maspac_test.go

@@ -0,0 +1,35 @@
+package test
+
+import (
+	"fmt"
+	"github.com/vmihailenco/msgpack/v5"
+	"testing"
+)
+
+func TestName12(t *testing.T) {
+	type Item struct {
+		Foo1 string // 这里 必须大写,不然 不会解析
+		Foo  string // 这里 必须大写,不然 不会解析
+	}
+	b, err := msgpack.Marshal(&Item{Foo: "bar1"})
+	if err != nil {
+		panic(any(err))
+	}
+
+	type Item_a struct {
+		Foo2 string // 这里 必须大写,不然 不会解析
+		Foo  string // 这里 必须大写,不然 不会解析
+		Foo1 string // 这里 必须大写,不然 不会解析
+	}
+
+	var item Item_a
+	err = msgpack.Unmarshal(b, &item)
+	if err != nil {
+		panic(any(err))
+	}
+	fmt.Println(item)
+	fmt.Println("Foo:", item.Foo)
+	// Output:
+	//{bar1}
+	//bar1
+}