Skip to content

Commit

Permalink
feat:增加网关默认下发获取网关版本信息功能。优化日志输出。完善说明文档。
Browse files Browse the repository at this point in the history
  • Loading branch information
microrain authored and microrain committed Jun 4, 2024
1 parent 3cc108f commit 629facc
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 25 deletions.
140 changes: 130 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SagooIOT 网关基础服务库代码

开发SagooIOT专属网关时,可以引用此项目,以便快速开发。
开发SagooIOT专属网关时,可以引用此项目,以便快速开发。创建一个空的工程,按下面的步骤完成自己专属网关的开发。


## SagooIOT的网关开发说明
Expand All @@ -9,12 +9,63 @@
go get -u github.com/sagoo-cloud/iotgateway
```

## 实现入口程序

参考如下:

```go

package main

import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/glog"
"github.com/sagoo-cloud/iotgateway"
"github.com/sagoo-cloud/iotgateway/version"
)

// 定义编译时的版本信息
var (
BuildVersion = "0.0"
BuildTime = ""
CommitID = ""
)

func main() {
//初始化日志
glog.SetDefaultLogger(g.Log())
//显示版本信息
version.ShowLogo(BuildVersion, BuildTime, CommitID)
ctx := gctx.GetInitCtx()

//需要解析的协议,可以根据需要添加,如果不需要实现自定义解析协议,可以不添加,可以为nil
chargeProtocol := protocol.ChargeProtocol{}

//创建网关
gateway, err := iotgateway.NewGateway(ctx, chargeProtocol)
if err != nil {
panic(err)
}
//初始化事件
events.Init()

// 初始化个性网关需要实现的其它服务

//启动网关
gateway.Start()

}


```


## 实现protocol接口

实现protocol接口处理接收到的数据。在Decode方法中,需要将接收到的数据进行解析,然后返回解析后的数据。在Encode方法中,需要将需要发送的数据进行编码,然后返回编码后的数据。

```go

type ChargeProtocol struct {
}

Expand All @@ -40,22 +91,19 @@ func (c *ChargeProtocol) Decode(conn net.Conn, buffer []byte) (res []byte, err e

```go

//定义事件返回数据
//准备事件返回数据
var eventData = make(map[string]interface{})
eventData["XXX字段1"] = "XXX值1"
eventData["XXX字段2"] = "XXX值2"

var eventDataList = make(map[string]interface{})
eventDataList["XXX事件标识字串"] = eventData


//推送数据到mqtt
out := g.Map{
"DeviceKey": deviceKey,
"EventDataList": eventDataList,
}

//触发向MQTT服务推送数据事件
//触发属性上报事件
event.MustFire(consts.PushAttributeDataToMQTT, out)

```
Expand All @@ -64,30 +112,102 @@ func (c *ChargeProtocol) Decode(conn net.Conn, buffer []byte) (res []byte, err e
触发的是 `consts.PushAttributeDataToMQTT` 事件

```go

//准备上报的数据
var propertieData = make(map[string]interface{})
propertieData["XXX字段1"] = "XXX值1"
propertieData["XXX字段2"] = "XXX值2"

//推送数据
out := g.Map{
"DeviceKey": deviceKey,
"PropertieDataList": propertieData,
}
//触发属性上报事件
event.MustFire(consts.PushAttributeDataToMQTT, out)

```

## 从SagooIOT平台下发调用回复

在SagooIoT系统中向设备端下发有两种情况,1. 服务下发,2. 属性设置下发。

### 服务下发

如果需要完成SagooIoT端向设备进行服务调用,需要在网关程序中完成订阅服务下发事件。
触发的是 `consts.PushServiceResDataToMQTT` 事件。

一 、在获取到设备key的地方订阅服务下发事件。

```go
//订阅网关设备服务下发事件
iotgateway.ServerGateway.SubscribeServiceEvent(传入获取的设备key)

```
二、在对设备进行处理后,需要回复SagooIOT平台。

由SagooIOT平台端下发后回复:
触发的是 `consts.PushServiceResDataToMQTT` 事件

```go
//准备回复数据
var replyData = make(map[string]interface{})
replyData["XXX字段1"] = "XXX值1"
replyData["XXX字段2"] = "XXX值1"
outData := g.Map{
"DeviceKey": deviceKey,
"ReplyData": replyData,
}
//出发回复的事件
event.MustFire(consts.PushServiceResDataToMQTT, outData)
```
```

### 属性设置下发

如果需要完成SagooIoT端向设备进行服务调用,需要在网关程序中完成订阅服务下发事件。
触发的是 `consts.PropertySetEvent` 事件。

一 、在获取到设备key的地方订阅服务下发事件。

```go
//订阅网关设备服务下发事件
iotgateway.ServerGateway.SubscribeSetEvent(传入获取的设备key)

```
二、在对设备进行处理后,需要回复SagooIOT平台。

由SagooIOT平台端下发后回复:
触发的是 `consts.PushSetResDataToMQTT` 事件

```go
//准备回复数据
var replyData = make(map[string]interface{})
replyData["XXX字段1"] = "XXX值1"
replyData["XXX字段2"] = "XXX值1"
outData := g.Map{
"DeviceKey": deviceKey,
"ReplyData": replyData,
}
//出发回复的事件
event.MustFire(consts.PushSetResDataToMQTT, outData)
```

### SagooIoT平台接收到回复的数据处理

在SagooIoT平台,对服务下发后,会收到回复数据。需要在对应的功能定义设置输入参数。参数标识与数据类型要与回服务回复的数据保持一致。


## 默认服务下发功能

网关中已经有一些默认的服务下发功能。

### 获取网关版本信息

功能标识:`getGatewayVersion`
功能描述:获取网关版本信息
功能输入参数:无
功能输出参数:

| 参数标识 | 参数名称 | 类型 |
| --------- | -------- | ------ |
| Version | 版本 | string |
| BuildTime | 编译时间 | string |
| CommitID | 提交ID | string |
6 changes: 3 additions & 3 deletions consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package consts

const (
PushAttributeDataToMQTT = "PushAttributeDataToMQTT"
PushServiceResDataToMQTT = "PushServiceResDataToMQTT"
PushSetResDataToMQTT = "PushSetResDataToMQTT"
PushAttributeDataToMQTT = "PushAttributeDataToMQTT" //属性上报
PushServiceResDataToMQTT = "PushServiceResDataToMQTT" //服务调用结果上报
PushSetResDataToMQTT = "PushSetResDataToMQTT" //属性设置结果上报

NetTypeTcpServer = "tcp"
NetTypeMqttServer = "mqtt"
Expand Down
6 changes: 4 additions & 2 deletions events/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package events

const (
// PropertySetEvent 属性设置下发事件,SagooIoT平台下发属性设置命令时触发
PropertySetEvent = "property"
PropertySetEvent = "property" // PropertySetEvent 属性设置下发事件,SagooIoT平台下发属性设置命令时触发

GetGatewayVersionEvent = "getGatewayVersion" // ServiceCallEvent 服务调用下发事件,SagooIoT平台下发服务调用getGatewayVersion命令时触发
GetGatewayConfig = "getGatewayConfig" // ServiceCallEvent 服务调用下发事件,SagooIoT平台下发服务调用getGatewayConfig命令时触发
)
36 changes: 36 additions & 0 deletions events/pushEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/guid"
"github.com/gookit/event"
"github.com/sagoo-cloud/iotgateway/consts"
"github.com/sagoo-cloud/iotgateway/lib"
"github.com/sagoo-cloud/iotgateway/log"
"github.com/sagoo-cloud/iotgateway/mqttClient"
"github.com/sagoo-cloud/iotgateway/mqttProtocol"
"github.com/sagoo-cloud/iotgateway/vars"
"github.com/sagoo-cloud/iotgateway/version"
)

// LoadingPublishEvent 加载发布事件
Expand All @@ -25,6 +29,10 @@ func LoadingPublishEvent() {
event.On(consts.PushServiceResDataToMQTT, event.ListenerFunc(pushServiceResDataToMQTT), event.High)
//推送设置属性响应数据到mqtt服务事件
event.On(consts.PushSetResDataToMQTT, event.ListenerFunc(pushSetResDataToMQTT), event.High)

// 服务下发获取网关配置信息事件
event.On(GetGatewayVersionEvent, event.ListenerFunc(getGatewayVersionData), event.Normal)

}

// pushAttributeDataToMQTT 推送属性数据到mqtt服务
Expand Down Expand Up @@ -169,3 +177,31 @@ func pushSetResDataToMQTT(e event.Event) (err error) {
}
return
}

// getGatewayVersionData 获取网关版本信息事件
func getGatewayVersionData(e event.Event) (err error) {
// 获取设备KEY
ok, deviceKey := lib.GetMapValueForKey(e.Data(), "DeviceKey")
if !ok {
glog.Debug(context.Background(), "获取设备KEY失败")
return fmt.Errorf("获取设备KEY失败: %s", e.Data())
}
//==== 平台端下发调用 应答====
ra, err := vars.GetUpMessageMap(deviceKey.(string))
if err == nil {
if ra.MessageID != "" {

var rd = make(map[string]interface{})
rd["Version"] = version.GetVersion()
rd["BuildTime"] = version.GetBuildTime()
rd["CommitID"] = version.CommitID

outData := g.Map{
"DeviceKey": deviceKey,
"ReplyData": rd,
}
event.Async(consts.PushServiceResDataToMQTT, outData)
}
}
return
}
6 changes: 5 additions & 1 deletion gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (gw *gateway) Start() {
if name == "" {
name = "SagooIoT Gateway Server"
}

//订阅网关设备服务下发事件
gw.SubscribeServiceEvent(gw.options.GatewayServerConfig.DeviceKey)

go gw.heartbeat(gw.options.GatewayServerConfig.Duration) //启动心跳
switch gw.options.GatewayServerConfig.NetType {
case consts.NetTypeTcpServer:
Expand Down Expand Up @@ -151,7 +155,7 @@ func (gw *gateway) sendHeartbeat() {
glog.Debugf(context.Background(), "网关向平台发送心跳数据:%s", string(outData))
token := gw.MQTTClient.Publish(topic, 1, false, outData)
if token.Error() != nil {
log.Error("publish error: %s", token.Error())
glog.Errorf(context.Background(), "publish error: %s", token.Error())
}
}

Expand Down
12 changes: 7 additions & 5 deletions service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package iotgateway

import (
"context"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gookit/event"
"github.com/sagoo-cloud/iotgateway/lib"
Expand All @@ -21,10 +23,10 @@ func (gw *gateway) SubscribeServiceEvent(deviceKey string) {
return
}
topic := fmt.Sprintf(serviceTopic, deviceKey)
log.Debug("topic: ", topic)
glog.Debugf(context.Background(), "%s 设备订阅了服务调用监听topic: %s", deviceKey, topic)
token := gw.MQTTClient.Subscribe(topic, 1, onServiceMessage)
if token.Error() != nil {
log.Debug("subscribe error: ", token.Error())
glog.Debug(context.Background(), "subscribe error: ", token.Error())
}
}

Expand All @@ -43,12 +45,12 @@ var onServiceMessage mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Mes
//通过监听到的topic地址获取设备标识
deviceKey := lib.GetTopicInfo("deviceKey", msg.Topic())
var data = mqttProtocol.ServiceCallRequest{}
log.Debug("==111==收到服务下发的topic====", msg.Topic())
log.Debug("====收到服务下发的信息====", msg.Payload())
glog.Debug(context.Background(), "接收到服务下发的topic:", msg.Topic())
glog.Debug(context.Background(), "接收到服务下发的数据:", msg.Payload())

err := gconv.Scan(msg.Payload(), &data)
if err != nil {
log.Debug("解析服务功能数据出错: %s", err)
glog.Debug(context.Background(), "解析服务功能数据出错: %s", err)
return
}

Expand Down
8 changes: 4 additions & 4 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func (gw *gateway) SubscribeSetEvent(deviceKey string) {
return
}
topic := fmt.Sprintf(setTopic, deviceKey)
log.Debug("topic: ", topic)
glog.Debugf(context.Background(), "%s 设备订阅了属性设置监听topic: %s", deviceKey, topic)
token := gw.MQTTClient.Subscribe(topic, 1, onSetMessage)
if token.Error() != nil {
log.Debug("subscribe error: ", token.Error())
glog.Debug(context.Background(), "subscribe error: ", token.Error())
}
}

Expand All @@ -42,8 +42,8 @@ var onSetMessage mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message
//通过监听到的topic地址获取设备标识
deviceKey := lib.GetTopicInfo("deviceKey", msg.Topic())
var data = mqttProtocol.ServiceCallRequest{}
glog.Debug(ctx, "==111==收到属性设置下发的topic====", msg.Topic())
glog.Debug(ctx, "====收到属性设置下发的信息====", msg.Payload())
glog.Debug(ctx, "接收到属性设置下发的topic:", msg.Topic())
glog.Debug(ctx, "接收收到属性设置下发的数据:", msg.Payload())

err := gconv.Scan(msg.Payload(), &data)
if err != nil {
Expand Down
Loading

0 comments on commit 629facc

Please sign in to comment.