API订阅写入示例
1. 依赖
使用Equipment Client库需要配置maven仓库访问和导入edge-management-client的依赖库
maven settings.xml仓库访问配置:
<servers>
<server>
<id>snapshot</id>
<username>matrix</username>
<password>hVisions!maven</password>
</server>
<server>
<id>release</id>
<username>matrix</username>
<password>hVisions!maven</password>
</server>
<server>
<id>nexus</id>
<username>matrix</username>
<password>hVisions!maven</password>
</server>
</servers>
<mirrors>
<mirror>
<id>release</id>
<mirrorOf>*</mirrorOf>
<name>nexus hvisions</name>
<url>http://58.247.122.126:8022/repository/maven-public/</url>
</mirror>
</mirrors>
gradle:
implementation 'com.hvisions:edge-management-client:2.2.2'
maven pom:
<dependency>
<groupId>com.hvisions</groupId>
<artifactId>edge-management-client</artifactId>
<version>2.2.2</version>
</dependency>
2. 配置RabbitMQ, Redis, Token获取
Equipment Client库基于SpringBoot进行开发,使用了RabbitMQ,Redis进行数据处理,通过集成HttpClient方式实现接口调用。因此在使用时,需要在SpringBoot
(application. yml)对RabbitMQ、Redis、Token获取进行配置。
配置示例如下:
spring:
redis:
host: 127.0.0.1
port: 6379
iot:
broker:
rabbitmq:
host: ${rabbitmq.host:127.0.0.1}
port: ${rabbitmq.port:5672}
username: ${rabbitmq.username:guest}
password: ${rabbitmq.password:guest}
exchangeName: ${rabbitmq.exchangeName:hiot.subscribe}
exchangeType: ${rabbitmq.exchangeType:topic}
queueType: ${rabbitmq.queueType:classic}
maxLength: ${rabbitmq.maxLength:50000}
overflow: ${rabbitmq.overflow:reject-publish}
enabelSsl: ${rabbitmq.enabelSsl:false}
keyStore: ${rabbitmq.keyStore:rabbitmq-client.p12}
keyStorePassword: ${rabbitmq.keyStorePassword:password}
trustStore: ${rabbitmq.trustStore:rabbitmq-server.keystore}
trustStorePassword: ${rabbitmq.trustStorePassword:password}
basicAck: ${rabbitmq.basicAck:true}
api:
setting:
server: http://127.0.0.1:9000
username: admin
password: 111111
enableAuth: true
2.1 Redis配置
Redis采用的是SpringBoot实现的标准的Redis配置,详细配置项参考SpringBoot官方文档Redis部分 ( https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#application-properties.data )
2.2 RabbitMQ配置
RabbitMQ配置说明如下:
名称 | 说明 | 是否必须 | 默认值 |
---|---|---|---|
host | RabbitMQ服务地址 | 是 | 127.0.0.1 |
port | RabbitMQ端口 | 是 | 5672 |
username | 登录到RabbitMQ服务的用户 | 是 | guest |
password | 登录到RabbitMQ服务的密码 | 是 | guest |
exchangeName | iot数据使用的Exchange名称,保持默认hiot不变 | 是 | hiot |
exchangeType | iot数据使用的Exchange类型,保持默认topic不变 | 是 | topic |
enabelSsl | 是否启用SSL连接,在RabbitMQ Server启用了SSL时配置成true,否则设置成false | 否 | false |
keyStore | 采用SSL连接时使用的keystore文件,enabelSsl为True时有效 | 否 | |
keyStorePassword | keystore文件的密码,enabelSsl为True时有效 | 否 | |
trustStore | 采用SSL连接时使用的truststore文件,enabelSsl为True时有效 | 否 | |
trustStorePassword | truststore文件的密码,enabelSsl为True时有效 | 否 | |
basicAck | 是否自动确认 | 否 | true |
2.3 Token获取配置
Equipment Client库通过配置API Token获取方式,实现对Client库提供的API进行调用。
Token配置说明如下:
配置项(api.setting.*) | 说明 | 默认值 |
---|---|---|
server | HiperMatrix平台网关地址 | http://127.0.0.1:9000 |
username | 用户名 | admin |
password | 密码 | 111111 |
enableAuth | 启用token获取失败重试 | true |
3. 启动类中扫描依赖中的Bean
启动类通过注解@ComponentScan将路径为"com.hvisions.edge.equipment"包下的Bean扫描到当前框架中
@ComponentScan(basePackages = {"com.hvisions.edge.equipment"})
4.订阅接口调用
4.1 调用订阅EquipmentService接口
调用订阅接口入参描述:
参数名称 | 说明 |
---|---|
equipmentId | 设备id |
equipmentName | 设备名称 |
equipmentCode | 设备编码 |
fieldName | 设备属性标识符 |
fieldNames | 设备属性标识符列表 |
- equipmentName:指设备名称,对应HiperMatrix中设备管理列表的设备名称。
- fieldName:指设备标识符,对应HiperMatrix中设备管理中的设备属性列表的标识符。
Equipment Client提供了四种不同设备属性订阅接口:
- 根据设备id(equipmentId)订阅设备单个属性标识符(fieldName)和多个属性标识符(fieldNames)采集数据接口
subsById(String equipmentId, String fieldName, EquipmentService.FieldConsumer fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
subsById(String equipmentId, List<String> fieldNames, EquipmentService.FieldConsumer fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
- 根据设备名称(equipmentName)订阅设备单个属性标识符(fieldName)和多个属性标识符(fieldNames)采集数据接口
subsByName(String equipmentName, String fieldName, EquipmentService.FieldConsumer fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
subsByName(String equipmentName, List<String> fieldNames, EquipmentService.FieldConsumer fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
- 根据设备编码(equipmentCode)订阅设备单个属性标识符(fieldName)和多个属性标识符(fieldNames)采集数据接口
subsByCode(String equipmentCode, String fieldName, EquipmentService.FieldConsumer fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
subsByCode(String equipmentCode, List<String> fieldNames, EquipmentService.FieldConsumer fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
- 自定义队列名称(queueName)订阅多组设备的属性标识符List<EquipmentField>, EquipmentField包含设备id(equipmentId),属性标识符(fieldName)采集数据接口
void subsGroup(String subName, List<EquipmentField> equipFieldNames, FieldConsumerTs fieldConsumer,java.util.function.Consumer<BrokerEvent> eventConsumer);
4.2 回调接口FieldConsumer描述
FieldConsumer入参描述:
参数名称 | 说明 |
---|---|
equipmentInfo | 设备数据信息 |
fieldName | 设备属性标识符 |
value | 设备订阅数据 |
public interface FieldConsumer {
void accept(EquipmentData equipmentInfo, String fieldName, Object value);
}
4.3 回调接口状态BrokerEvent描述
BrokerEvent入参描述:
参数名称 | 说明 |
---|---|
name | rabbitmq连接名称 |
status | 连接状态,CONNECTED/DISCONNECTED |
msg | 连接日志内容 |
public interface FieldConsumer {
void accept(EquipmentData equipmentInfo, String fieldName, Object value);
}
5. 订阅接口案列
5.1 引用EquipmentService
声明EquipmentService,并通过注解@Autowired注入到当前使用类中
@Autowired
EquipmentService equipmentService;
5.2 引用注入的EquipmentService,调用订阅函数
如根据设备名称(equipmentName)为海德汉机床,设备属性标识符(fileName) main_axle_load 调用函数subscribeByName,
并将订阅数据写入到回调函数中
equipmentService.subsByName(equipmentName, fileName, (equipmentData,fieldName, value) -> {
log.info("订阅设备数据:{}", equipmentData);
log.info("订阅设备标识符:{}", fileName);
log.info("订阅采集数据:{}", value);
},brokerEvent -> {
log.info("rabbitmq连接名称:{}",brokerEvent.getName());
log.info("连接状态:{}",brokerEvent.getStatus());
log.info("连接日志内容:{}",brokerEvent.getMsg());
});
回调函数返回结果为:设备数据(equipmentData),设备标识符(fieldName), 订阅数据(value)
equipmentData数据格式:
{
"id":"6879310550845624320",
"name":”海德汉机床",
"code":"海德汉机床"
}
fieldName数据格式:
{
fieldName:"main_axle_load"
}
value数据格式:
{
"id":"6879310550845624320",
"timestamp":"2022-02-23T17:53:22.346+08:00",
"_type":"haidehanjichuang",
"main_axle_load":0.8700000047683716
}
5. 数据写入案列
声明HttpBuilderInstance,并通过注解@Autowired注入到当前使用类中
@Autowired
private HttpBuilderInstance httpBuilder;
构建提供API写入的EquipmentClient,httpBuilder为Springboot的Bean在全局变量加载之后,因此不要将EquipmentClient定义为全局变量。
EquipmentClient equipmentClient = httpBuilder.getInstance().getClient(EquipmentClient.class);
如根据设备编码(code)为海德汉机床,设备属性标识符(fileName) main_axle_load 调用函数writeByCode,
将数据写入到函数中
ResultVO<WriteResult> result = equipmentClient.writeByCode(code, fileName, value);
函数返回结果为result:
{
"data": {
"status": "ok",
"error": null,
"duration": null,
"details": [],
"success": true
},
"code": 200,
"message": null
}
status为ok表示数据写入成功