客户端模拟器下载
http://free.cmsoft.cn/download/cmsoft/assistant/netassist5.0.3.zip
配置Thingsboard gateway
修改thingsboard.json
打开socket协议
"connectors": [
{
"name": "Socket Connector",
"type": "socket",
"configuration": "socket.json"
}
]
修改socket.json
配置连接地址、端口以及客户端地址、参数等信息
{
"name": "TCP Connector Example",
"type": "TCP",
"address": "192.168.0.133",
"port": 50000,
"bufferSize": 1024,
"devices": [
{
"address": "192.168.0.74:59736",
"deviceName": "TCP Device Example",
"deviceType": "default",
"encoding": "utf-8",
"telemetry": [
{
"key": "temp",
"byteFrom": 0,
"byteTo": 1
},
{
"key": "hum",
"byteFrom": 1,
"byteTo": 2
}
],
"attributes": [
{
"key": "name",
"byteFrom": 2,
"byteTo": 4
},
{
"key": "num",
"byteFrom": 4,
"byteTo": 5
}
],
"attributeRequests": [
{
"type": "shared",
"requestExpression": "${[0:3]==atr}",
"attributeNameExpression": "[3:]"
}
],
"attributeUpdates": [
{
"encoding": "utf-16",
"attributeOnThingsBoard": "sharedName"
}
],
"serverSideRpc": [
{
"methodRPC": "rpcMethod1",
"withResponse": true,
"methodProcessing": "write",
"encoding": "utf-8"
}
]
}
]
}
服务启动
模拟器发送报文
单设备接入限制
运行会发现消息被过滤,原因是代码中对客户端的连接地址与端口都进行了限制
因为客户端的端口是随机的,因此基本上比较难使用上,不知道是否是自己使用的问题,决定进行拓展一个
修改为多设备接入
通过对源码的分析,需要做的调整思路如下:
- 新增一个连接类,网关可以通过配置找到并加载
- 连接类中对客户端的连接不做限制
- 设备需要通过属性动态设置设备名
新建连接类
- 网关的配置加载类tb_gateway_service.py,新增"socket_extends": “SocketExtendsConnector”
DEFAULT_CONNECTORS = {
...
"socket_extends": "SocketExtendsConnector",
...
}
其中SocketExtendsConnector为具体的连接实现类
- 复制原来的socket包,名称调整为socket_extends,类名与上面配置一致
新增配置文件 socket_extends.json
{
"name": "TCP Connector Example",
"type": "TCP",
"address": "192.168.0.133",
"port": 50000,
"bufferSize": 1024,
"devices":
{
"deviceName": "${name}",
"deviceType": "default",
"encoding": "utf-8",
"telemetry": [
{
"key": "temp",
"byteFrom": 0,
"byteTo": 4
},
{
"key": "hum",
"byteFrom": 4,
"byteTo": 5
}
],
"attributes": [
{
"key": "name",
"byteFrom": 5,
"byteTo": 7
},
{
"key": "num",
"byteFrom": 7,
"byteTo": 8
}
],
"attributeRequests": [
{
"type": "shared",
"requestExpression": "${[0:3]==atr}",
"attributeNameExpression": "[3:]"
}
],
"attributeUpdates": [
{
"encoding": "utf-16",
"attributeOnThingsBoard": "sharedName"
}
],
"serverSideRpc": [
{
"methodRPC": "getCurrentTime",
"withResponse": true,
"methodProcessing": "write",
"encoding": "utf-8"
}
]
}
}
其中,主要去除了devices的数组,以及address配置,将名称改为动态标识
连接配置修改
"connectors": [
{
"name": "Socket Connector",
"type": "socket_extends",
"configuration": "socket_extends.json"
}
]
启动测试
-
发送报文 31 30 2E 32 31 68 69 31
-
查看Thingsboard平台是否正常新增设备