Skip to content

Commit d11a531

Browse files
authored
Dev (#106)
* fix lacking of bridge sdk * update document * fix code generator
1 parent 824b52d commit d11a531

File tree

32 files changed

+483
-300
lines changed

32 files changed

+483
-300
lines changed

README.md

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -439,35 +439,57 @@ Brdige Demo提供了一个使用TCP设备接入网桥、于云平台进行交互
439439

440440
代码样例:
441441
```java
442-
public void init() {
442+
public void init() {
443443

444-
//网桥启动初始化
445-
BridgeBootstrap bridgeBootstrap = new BridgeBootstrap();
444+
// 网桥启动初始化
445+
BridgeBootstrap bridgeBootstrap = new BridgeBootstrap();
446446

447-
// 从环境变量获取配置进行初始化
448-
bridgeBootstrap.initBridge();
447+
// 从环境变量获取配置进行初始化
448+
bridgeBootstrap.initBridge();
449449

450-
bridgeClient = bridgeBootstrap.getBridgeDevice().getClient();
450+
bridgeClient = bridgeBootstrap.getBridgeDevice().getClient();
451451

452-
// 设置平台下行数据监听器
453-
DownLinkHandler downLinkHandler = new DownLinkHandler();
454-
bridgeClient.setBridgeCommandListener(downLinkHandler) // 设置平台命令下发监听器
455-
.setBridgeDeviceMessageListener(downLinkHandler) // 设置平台消息下发监听器
456-
.setBridgeDeviceDisConnListener(downLinkHandler); // 设置平台通知网桥主动断开设备连接的监听器
457-
}
452+
// 设置平台下行数据监听器
453+
DownLinkHandler downLinkHandler = new DownLinkHandler();
454+
bridgeClient.setBridgeCommandListener(downLinkHandler) // 设置平台命令下发监听器
455+
.setBridgeDeviceMessageListener(downLinkHandler) // 设置平台消息下发监听器
456+
.setBridgeDeviceDisConnListener(downLinkHandler); // 设置平台通知网桥主动断开设备连接的监听器
457+
}
458458
```
459459
#### 2. 设备登录上线
460460
设备登录上线的实现样例如下:
461461
```java
462-
private void login(Channel channel, DeviceLoginMessage message) {
462+
private void login(Channel channel, BaseMessage message) {
463+
if (!(message instanceof DeviceLoginMessage)) {
464+
return;
465+
}
466+
467+
String deviceId = message.getMsgHeader().getDeviceId();
468+
String secret = ((DeviceLoginMessage) message).getSecret();
469+
DeviceSession deviceSession = new DeviceSession();
470+
463471
int resultCode = BridgeService.getBridgeClient().loginSync(deviceId, secret, 5000);
472+
464473
// 登录成功保存会话信息
465474
if (resultCode == 0) {
466-
deviceSession.setDeviceId(deviceId);
467-
deviceSession.setChannel(channel);
468-
DeviceSessionManger.getInstance().createSession(deviceId, deviceSession);
469-
NettyUtils.setDeviceId(channel, deviceId);
475+
deviceSession.setDeviceId(deviceId);
476+
deviceSession.setChannel(channel);
477+
DeviceSessionManger.getInstance().createSession(deviceId, deviceSession);
478+
NettyUtils.setDeviceId(channel, deviceId);
470479
}
480+
481+
// 构造登录响应的消息头
482+
MsgHeader msgHeader = new MsgHeader();
483+
msgHeader.setDeviceId(deviceId);
484+
msgHeader.setFlowNo(message.getMsgHeader().getFlowNo());
485+
msgHeader.setDirect(Constants.DIRECT_CLOUD_RSP);
486+
msgHeader.setMsgType(Constants.MSG_TYPE_DEVICE_LOGIN);
487+
488+
// 调用网桥login接口,向平台发起登录请求
489+
DefaultActionListenerImpl defaultLoginActionListener = new DefaultActionListenerImpl("login");
490+
BridgeService.getBridgeClient()
491+
.loginAsync(deviceId, secret, message.getMsgHeader().getFlowNo(),
492+
defaultLoginActionListener);
471493
}
472494
```
473495
设备上线时,需要从原始设备消息中解析出鉴权信息(设备ID和秘钥),再调用SDK提供的login接口向平台发起登录请求,平台收到设备的login请求后,会对设备的鉴权信息进行认证,认证通过后会通过返回码告知网桥SDK设备的登录结果。您需要根据登录结果对设备进行记录会话信息、给设备返回响应等处理。
@@ -480,21 +502,30 @@ private void login(Channel channel, DeviceLoginMessage message) {
480502
private void reportProperties(Channel channel, BaseMessage message) {
481503
String deviceId = message.getMsgHeader().getDeviceId();
482504
DeviceSession deviceSession = DeviceSessionManger.getInstance().getSession(deviceId);
483-
if (deviceSession == null || !deviceSession.isLoginSuccess()) {
505+
if (deviceSession == null) {
484506
log.warn("device={} is not login", deviceId);
485507
sendResponse(channel, message, 1);
486508
return;
487509
}
510+
511+
ServiceProperty serviceProperty = new ServiceProperty();
512+
serviceProperty.setServiceId("Location");
513+
serviceProperty.setProperties(
514+
JsonUtil.convertJsonStringToObject(JsonUtil.convertObject2String(message), Map.class));
515+
516+
488517
// 调用网桥reportProperties接口,上报设备属性数据
489518
BridgeService.getBridgeClient()
490519
.reportProperties(deviceId, Collections.singletonList(serviceProperty), new ActionListener() {
491520
@Override
492521
public void onSuccess(Object context) {
493522
sendResponse(channel, message, 0);
494523
}
524+
495525
@Override
496526
public void onFailure(Object context, Throwable var2) {
497-
log.warn("device={} reportProperties failed: {}", deviceId, var2.getMessage());
527+
log.warn("device={} reportProperties failed: {}", deviceId, ExceptionUtil.getBriefStackTrace(var2));
528+
498529
sendResponse(channel, message, 1);
499530
}
500531
});
@@ -506,38 +537,45 @@ private void reportProperties(Channel channel, BaseMessage message) {
506537

507538
代码样例参考:
508539
```java
509-
public void onCommand(String deviceId, String requestId, BridgeCommand bridgeCommand) {
510-
log.info("onCommand deviceId={}, requestId={}, bridgeCommand={}", deviceId, requestId, bridgeCommand);
511-
DeviceSession session = DeviceSessionManger.getInstance().getSession(deviceId);
512-
if (session == null) {
513-
log.warn("device={} session is null", deviceId);
514-
return;
515-
}
540+
@Override
541+
public void onCommand(String deviceId, String requestId, BridgeCommand bridgeCommand) {
542+
log.info("onCommand deviceId={}, requestId={}, bridgeCommand={}", deviceId, requestId, bridgeCommand);
543+
DeviceSession session = DeviceSessionManger.getInstance().getSession(deviceId);
544+
if (session == null) {
545+
log.warn("device={} session is null", deviceId);
546+
return;
547+
}
516548

517-
// 设置位置上报的周期
518-
if (Constants.MSG_TYPE_FREQUENCY_LOCATION_SET.equals(bridgeCommand.getCommand().getCommandName())) {
519-
processLocationSetCommand(session, requestId, bridgeCommand);
549+
// 设置位置上报的周期
550+
if (Constants.MSG_TYPE_FREQUENCY_LOCATION_SET.equals(bridgeCommand.getCommand().getCommandName())) {
551+
processLocationSetCommand(session, requestId, bridgeCommand);
552+
}
520553
}
521-
}
522554
```
523555
#### 5. 设备离线
524556
网桥检查到设备到服务端的长连接断开时,需要调用SDK的logout接口通知平台设备离线。
525557

526558
代码样例参考:
527559
```java
528-
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
529-
String deviceId = NettyUtils.getDeviceId(ctx.channel());
530-
DeviceSessionManger.getInstance().getSession(deviceId);
531-
if (deviceId == null) {
532-
return;
533-
}
534-
// 调用网桥的logout接口,通知平台设备离线
535-
DefaultActionListenerImpl defaultLogoutActionListener = new DefaultActionListenerImpl("logout");
536-
BridgeService.getBridgeClient().logout(deviceId, UUID.randomUUID().toString(), defaultLogoutActionListener);
537-
DeviceSessionManger.getInstance().deleteSession(deviceId);
560+
@Override
561+
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
562+
String deviceId = NettyUtils.getDeviceId(ctx.channel());
563+
if (deviceId == null) {
564+
return;
565+
}
566+
DeviceSession deviceSession = DeviceSessionManger.getInstance().getSession(deviceId);
567+
if (deviceSession == null) {
568+
return;
569+
}
538570

539-
ctx.close();
540-
}
571+
// 调用网桥的logout接口,通知平台设备离线
572+
DefaultActionListenerImpl defaultLogoutActionListener = new DefaultActionListenerImpl("logout");
573+
BridgeService.getBridgeClient()
574+
.logoutAsync(deviceId, UUID.randomUUID().toString(), defaultLogoutActionListener);
575+
DeviceSessionManger.getInstance().deleteSession(deviceId);
576+
577+
ctx.close();
578+
}
541579
```
542580
### 测试验证
543581
### 1. 获取网桥接入信息

iot-bridge-demo/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
<artifactId>log4j-core</artifactId>
2727
<version>2.17.1</version>
2828
</dependency>
29-
<dependency>
30-
<groupId>javax.xml.bind</groupId>
31-
<artifactId>jaxb-api</artifactId>
32-
<version>2.3.0</version>
33-
</dependency>
3429
<dependency>
3530
<groupId>org.apache.logging.log4j</groupId>
3631
<artifactId>log4j-api</artifactId>
@@ -60,7 +55,6 @@
6055
<artifactId>iot-bridge-sdk</artifactId>
6156
<version>1.2.0</version>
6257
</dependency>
63-
6458
</dependencies>
6559

6660
<build>

iot-bridge-demo/src/main/java/com/huaweicloud/sdk/iot/device/demo/Bridge.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ private static void createBridge(String serverUri, DeviceIdentityRegistry device
5454
public static void main(String[] args) throws Exception {
5555

5656
// 默认使用北京4的接入地址,其他region的用户请修改
57-
String serverUri = "ssl://iot-mqtts.cn-north-4.myhuaweicloud.com:8883";
57+
String demoServerUri = "ssl://iot-mqtts.cn-north-4.myhuaweicloud.com:8883";
5858

5959
int port = 8080;
6060

61-
Bridge.createBridge(serverUri, null);
61+
Bridge.createBridge(demoServerUri, null);
6262

6363
new TcpServer(port).run();
6464

@@ -88,7 +88,7 @@ void createSession(String nodeId, Channel channel) {
8888
return;
8989
}
9090

91-
//加载iot平台的ca证书,进行服务端校验
91+
// 加载iot平台的ca证书,进行服务端校验
9292
URL resource = Bridge.class.getClassLoader().getResource("ca.jks");
9393
File file = new File(resource.getPath());
9494

iot-bridge-demo/src/main/java/com/huaweicloud/sdk/iot/device/demo/DefaultBridgePropertyListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class DefaultBridgePropertyListener implements PropertyListener {
2828
@Override
2929
public void onPropertiesSet(String requestId, List<ServiceProperty> services) {
3030

31-
//这里可以根据需要进行消息格式转换
31+
// 这里可以根据需要进行消息格式转换
3232
channel.writeAndFlush(services);
3333
ioTDevice.getClient().respondPropsSet(requestId, IotResult.SUCCESS);
3434
}

iot-bridge-demo/src/main/java/com/huaweicloud/sdk/iot/device/demo/TcpServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exceptio
7575
Channel incoming = ctx.channel();
7676
log.info("channelRead0: {}, msg : {}", incoming.remoteAddress(), s);
7777

78-
//如果是首条消息,创建session
78+
// 如果是首条消息,创建session
7979
Session session = Bridge.getInstance().getSessionByChannel(incoming.id().asLongText());
8080
if (session == null) {
8181
Bridge.getInstance().createSession(s, incoming);

iot-bridge-sdk/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<maven.compiler.source>${java.version}</maven.compiler.source>
1818
<maven.compiler.target>${java.version}</maven.compiler.target>
19-
<maven.deploy.skip>true</maven.deploy.skip>
2019
</properties>
2120

2221
<dependencies>

0 commit comments

Comments
 (0)