feat:opcua协议读写
This commit is contained in:
@@ -9,6 +9,7 @@ import org.nl.iot.core.driver.entity.RValue;
|
||||
import org.nl.iot.core.driver.entity.WResponse;
|
||||
import org.nl.iot.core.driver.entity.WValue;
|
||||
import org.nl.iot.core.driver.protocol.modbustcp.ModBusProtocolDriverImpl;
|
||||
import org.nl.iot.core.driver.protocol.opcua.OpcUaProtocolDriverImpl;
|
||||
import org.nl.iot.core.driver.protocol.plcs7.PlcS7ProtocolDriverImpl;
|
||||
import org.nl.iot.modular.iot.entity.IotConfig;
|
||||
import org.nl.iot.modular.iot.entity.IotConnect;
|
||||
@@ -31,6 +32,12 @@ public class ApiTest {
|
||||
@Autowired
|
||||
private ModBusProtocolDriverImpl modBusProtocolDriver;
|
||||
|
||||
@Autowired
|
||||
private PlcS7ProtocolDriverImpl plcS7ProtocolDriver;
|
||||
|
||||
@Autowired
|
||||
private OpcUaProtocolDriverImpl opcUaProtocolDriver;
|
||||
|
||||
@Test
|
||||
public void modbusTest() {
|
||||
// 构建连接对象
|
||||
@@ -464,9 +471,6 @@ public class ApiTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private PlcS7ProtocolDriverImpl plcS7ProtocolDriver;
|
||||
|
||||
@Test
|
||||
public void plcS7ReadTest() {
|
||||
// 构建PLC S7连接对象
|
||||
@@ -958,4 +962,516 @@ public class ApiTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void opcUaReadTest() {
|
||||
// 构建OPC UA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("path", "/OPCUA/SimulationServer"); // OPC UA服务器路径
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(3)
|
||||
.code("OPC_UA_001")
|
||||
.host("Lyd-ThinkBook")
|
||||
.port(53530) // OPC UA默认端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-ua")
|
||||
.enabled(true)
|
||||
.description("测试OPC UA连接")
|
||||
.build();
|
||||
|
||||
// 构建配置对象 - 读取不同类型的节点
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(3)
|
||||
.alias("temperature")
|
||||
.aliasName("温度传感器")
|
||||
.registerAddress("ns=3;s=Temperature") // OPC UA节点ID格式
|
||||
.dataType("DOUBLE")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA温度读取")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(3)
|
||||
.alias("pressure")
|
||||
.aliasName("压力传感器")
|
||||
.registerAddress("ns=3;i=1009")
|
||||
.dataType("FLOAT")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA压力读取")
|
||||
.build();
|
||||
|
||||
IotConfig config3 = IotConfig.builder()
|
||||
.id(3)
|
||||
.connectId(3)
|
||||
.alias("status")
|
||||
.aliasName("设备状态")
|
||||
.registerAddress("ns=2;s=DeviceStatus")
|
||||
.dataType("BOOLEAN")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA状态读取")
|
||||
.build();
|
||||
|
||||
// 执行读取操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC UA读取 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("服务器路径: " + properties.getString("path"));
|
||||
|
||||
// 转换为DeviceBO
|
||||
DeviceBO deviceBO = DeviceBO.builder()
|
||||
.id(String.valueOf(connect.getId()))
|
||||
.code(connect.getCode())
|
||||
.properties(connect.getProperties())
|
||||
.host(connect.getHost())
|
||||
.port(connect.getPort())
|
||||
.protocol(connect.getProtocol())
|
||||
.build();
|
||||
//
|
||||
// // 测试读取温度值
|
||||
// System.out.println("\n--- 测试读取温度值 ---");
|
||||
// SiteBO siteBO1 = SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config1.getAlias())
|
||||
// .aliasName(config1.getAliasName())
|
||||
// .registerAddress(config1.getRegisterAddress())
|
||||
// .dataType(config1.getDataType())
|
||||
// .readonly(config1.getReadonly())
|
||||
// .build();
|
||||
//
|
||||
// RValue result1 = opcUaProtocolDriver.read(deviceBO, siteBO1);
|
||||
// System.out.println("节点ID: " + config1.getRegisterAddress());
|
||||
// System.out.println("数据类型: " + config1.getDataType());
|
||||
// System.out.println("读取结果: " + result1.getValue());
|
||||
// if (result1.getExceptionMessage() != null) {
|
||||
// System.out.println("异常信息: " + result1.getExceptionMessage());
|
||||
// }
|
||||
|
||||
// 测试读取压力值
|
||||
System.out.println("\n--- 测试读取压力值 ---");
|
||||
SiteBO siteBO2 = SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build();
|
||||
|
||||
RValue result2 = opcUaProtocolDriver.read(deviceBO, siteBO2);
|
||||
System.out.println("节点ID: " + config2.getRegisterAddress());
|
||||
System.out.println("数据类型: " + config2.getDataType());
|
||||
System.out.println("读取结果: " + result2.getValue());
|
||||
if (result2.getExceptionMessage() != null) {
|
||||
System.out.println("异常信息: " + result2.getExceptionMessage());
|
||||
}
|
||||
|
||||
// 测试读取状态值
|
||||
// System.out.println("\n--- 测试读取状态值 ---");
|
||||
// SiteBO siteBO3 = SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config3.getAlias())
|
||||
// .aliasName(config3.getAliasName())
|
||||
// .registerAddress(config3.getRegisterAddress())
|
||||
// .dataType(config3.getDataType())
|
||||
// .readonly(config3.getReadonly())
|
||||
// .build();
|
||||
//
|
||||
// RValue result3 = opcUaProtocolDriver.read(deviceBO, siteBO3);
|
||||
// System.out.println("节点ID: " + config3.getRegisterAddress());
|
||||
// System.out.println("数据类型: " + config3.getDataType());
|
||||
// System.out.println("读取结果: " + result3.getValue());
|
||||
// if (result3.getExceptionMessage() != null) {
|
||||
// System.out.println("异常信息: " + result3.getExceptionMessage());
|
||||
// }
|
||||
|
||||
System.out.println("\n========== OPC UA读取测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC UA读取测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void opcUaBatchReadTest() {
|
||||
// 构建OPC UA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("path", "/OPCUA/SimulationServer"); // OPC UA服务器路径
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(3)
|
||||
.code("OPC_UA_001")
|
||||
.host("Lyd-ThinkBook")
|
||||
.port(53530) // OPC UA默认端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-ua")
|
||||
.enabled(true)
|
||||
.description("测试OPC UA连接")
|
||||
.build();
|
||||
|
||||
|
||||
// 构建多个配置对象进行批量读取
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(3)
|
||||
.alias("temperature")
|
||||
.aliasName("温度传感器")
|
||||
.registerAddress("ns=3;s=Temperature")
|
||||
.dataType("DOUBLE")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA温度读取")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(3)
|
||||
.alias("pressure")
|
||||
.aliasName("压力传感器")
|
||||
.registerAddress("ns=3;i=1009")
|
||||
.dataType("FLOAT")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA压力读取")
|
||||
.build();
|
||||
|
||||
IotConfig config3 = IotConfig.builder()
|
||||
.id(3)
|
||||
.connectId(3)
|
||||
.alias("humidity")
|
||||
.aliasName("湿度传感器")
|
||||
.registerAddress("ns=2;s=Humidity")
|
||||
.dataType("DOUBLE")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA湿度读取")
|
||||
.build();
|
||||
|
||||
IotConfig config4 = IotConfig.builder()
|
||||
.id(4)
|
||||
.connectId(3)
|
||||
.alias("status")
|
||||
.aliasName("设备状态")
|
||||
.registerAddress("ns=2;s=DeviceStatus")
|
||||
.dataType("BOOLEAN")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC UA状态读取")
|
||||
.build();
|
||||
|
||||
// 执行批量读取操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC UA批量读取 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("服务器路径: " + properties.getString("path"));
|
||||
|
||||
// 转换为DeviceBO
|
||||
DeviceBO deviceBO = DeviceBO.builder()
|
||||
.id(String.valueOf(connect.getId()))
|
||||
.code(connect.getCode())
|
||||
.properties(connect.getProperties())
|
||||
.host(connect.getHost())
|
||||
.port(connect.getPort())
|
||||
.protocol(connect.getProtocol())
|
||||
.build();
|
||||
|
||||
// 转换为SiteBO列表
|
||||
java.util.List<SiteBO> siteBOList = java.util.Arrays.asList(
|
||||
SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config1.getAlias())
|
||||
.aliasName(config1.getAliasName())
|
||||
.registerAddress(config1.getRegisterAddress())
|
||||
.dataType(config1.getDataType())
|
||||
.readonly(config1.getReadonly())
|
||||
.build(),
|
||||
SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build()
|
||||
// SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config3.getAlias())
|
||||
// .aliasName(config3.getAliasName())
|
||||
// .registerAddress(config3.getRegisterAddress())
|
||||
// .dataType(config3.getDataType())
|
||||
// .readonly(config3.getReadonly())
|
||||
// .build(),
|
||||
// SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config4.getAlias())
|
||||
// .aliasName(config4.getAliasName())
|
||||
// .registerAddress(config4.getRegisterAddress())
|
||||
// .dataType(config4.getDataType())
|
||||
// .readonly(config4.getReadonly())
|
||||
// .build()
|
||||
);
|
||||
|
||||
System.out.println("批量读取节点数量: " + siteBOList.size());
|
||||
for (SiteBO site : siteBOList) {
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
site.getRegisterAddress() + " [" + site.getDataType() + "]");
|
||||
}
|
||||
|
||||
// 调用驱动批量读取数据
|
||||
List<RValue> result = opcUaProtocolDriver.batchRead(deviceBO, siteBOList);
|
||||
|
||||
System.out.println("批量读取完成!");
|
||||
System.out.println("读取结果:");
|
||||
|
||||
// 输出每个节点的读取结果
|
||||
for (RValue rValue : result) {
|
||||
SiteBO site = rValue.getSiteBO();
|
||||
String value = rValue.getValue();
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
value + (value == null ? " [" + rValue.getExceptionMessage() + "]" : ""));
|
||||
}
|
||||
|
||||
System.out.println("========== OPC UA批量读取测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC UA批量读取测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void opcUaWriteTest() {
|
||||
// 构建OPC UA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("path", "/OPCUA/SimulationServer"); // OPC UA服务器路径
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(3)
|
||||
.code("OPC_UA_001")
|
||||
.host("Lyd-ThinkBook")
|
||||
.port(53530) // OPC UA默认端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-ua")
|
||||
.enabled(true)
|
||||
.description("测试OPC UA连接")
|
||||
.build();
|
||||
|
||||
// 构建配置对象 - 写入操作,设置为可写
|
||||
IotConfig config = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(3)
|
||||
.alias("output_value")
|
||||
.aliasName("输出值")
|
||||
.registerAddress("ns=3;i=1009") // OPC UA节点ID格式
|
||||
.dataType("FLOAT")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试OPC UA写入操作")
|
||||
.build();
|
||||
|
||||
// 执行写入操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC UA写入 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("服务器路径: " + properties.getString("path"));
|
||||
System.out.println("节点ID: " + config.getRegisterAddress());
|
||||
System.out.println("数据类型: " + config.getDataType());
|
||||
|
||||
// 转换为DeviceBO
|
||||
DeviceBO deviceBO = DeviceBO.builder()
|
||||
.id(String.valueOf(connect.getId()))
|
||||
.code(connect.getCode())
|
||||
.properties(connect.getProperties())
|
||||
.host(connect.getHost())
|
||||
.port(connect.getPort())
|
||||
.protocol(connect.getProtocol())
|
||||
.build();
|
||||
|
||||
// 转换为SiteBO
|
||||
SiteBO siteBO = SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config.getAlias())
|
||||
.aliasName(config.getAliasName())
|
||||
.registerAddress(config.getRegisterAddress())
|
||||
.dataType(config.getDataType())
|
||||
.readonly(config.getReadonly())
|
||||
.build();
|
||||
|
||||
// 构建写入值对象
|
||||
String writeValue = "1.3"; // 要写入的值
|
||||
WValue wValue = WValue.builder()
|
||||
.point(siteBO)
|
||||
.value(writeValue)
|
||||
.type(config.getDataType())
|
||||
.build();
|
||||
|
||||
System.out.println("写入值: " + writeValue);
|
||||
|
||||
// 调用驱动写入数据
|
||||
Boolean result = opcUaProtocolDriver.write(deviceBO, wValue);
|
||||
|
||||
if (result != null && result) {
|
||||
System.out.println("写入成功!");
|
||||
System.out.println("写入结果: " + result);
|
||||
} else {
|
||||
System.out.println("写入失败!");
|
||||
}
|
||||
System.out.println("========== OPC UA写入测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC UA写入测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void opcUaBatchWriteTest() {
|
||||
// 构建OPC UA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("path", "/OPCUA/SimulationServer"); // OPC UA服务器路径
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(3)
|
||||
.code("OPC_UA_001")
|
||||
.host("Lyd-ThinkBook")
|
||||
.port(53530) // OPC UA默认端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-ua")
|
||||
.enabled(true)
|
||||
.description("测试OPC UA连接")
|
||||
.build();
|
||||
|
||||
// 构建多个配置对象进行批量写入
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(3)
|
||||
.alias("output_value1")
|
||||
.aliasName("输出值1")
|
||||
.registerAddress("ns=3;s=Temperature") // OPC UA节点ID格式
|
||||
.dataType("DOUBLE")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试批量写入1")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(3)
|
||||
.alias("output_value2")
|
||||
.aliasName("输出值2")
|
||||
.registerAddress("ns=3;i=1009")
|
||||
.dataType("FLOAT")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试批量写入2")
|
||||
.build();
|
||||
|
||||
IotConfig config3 = IotConfig.builder()
|
||||
.id(3)
|
||||
.connectId(3)
|
||||
.alias("output_status")
|
||||
.aliasName("输出状态")
|
||||
.registerAddress("ns=2;s=OutputStatus")
|
||||
.dataType("BOOLEAN")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试批量写入3")
|
||||
.build();
|
||||
|
||||
// 执行批量写入操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC UA批量写入 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("服务器路径: " + properties.getString("path"));
|
||||
|
||||
// 转换为DeviceBO
|
||||
DeviceBO deviceBO = DeviceBO.builder()
|
||||
.id(String.valueOf(connect.getId()))
|
||||
.code(connect.getCode())
|
||||
.properties(connect.getProperties())
|
||||
.host(connect.getHost())
|
||||
.port(connect.getPort())
|
||||
.protocol(connect.getProtocol())
|
||||
.build();
|
||||
|
||||
// 转换为SiteBO并构建WValue列表
|
||||
java.util.List<WValue> wValueList = java.util.Arrays.asList(
|
||||
WValue.builder()
|
||||
.point(SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config1.getAlias())
|
||||
.aliasName(config1.getAliasName())
|
||||
.registerAddress(config1.getRegisterAddress())
|
||||
.dataType(config1.getDataType())
|
||||
.readonly(config1.getReadonly())
|
||||
.build())
|
||||
.value("100.5") // 写入值1
|
||||
.type(config1.getDataType())
|
||||
.build(),
|
||||
WValue.builder()
|
||||
.point(SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build())
|
||||
.value("9.55") // 写入值2
|
||||
.type(config2.getDataType())
|
||||
.build()
|
||||
// WValue.builder()
|
||||
// .point(SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config3.getAlias())
|
||||
// .aliasName(config3.getAliasName())
|
||||
// .registerAddress(config3.getRegisterAddress())
|
||||
// .dataType(config3.getDataType())
|
||||
// .readonly(config3.getReadonly())
|
||||
// .build())
|
||||
// .value("true") // 写入值3
|
||||
// .type(config3.getDataType())
|
||||
// .build()
|
||||
);
|
||||
|
||||
System.out.println("批量写入节点数量: " + wValueList.size());
|
||||
for (WValue wValue : wValueList) {
|
||||
SiteBO site = wValue.getPoint();
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
site.getRegisterAddress() + " [" + site.getDataType() + "] = " + wValue.getValue());
|
||||
}
|
||||
|
||||
// 调用驱动批量写入数据
|
||||
List<WResponse> result = opcUaProtocolDriver.batchWrite(deviceBO, wValueList);
|
||||
|
||||
System.out.println("批量写入完成!");
|
||||
System.out.println("写入结果:");
|
||||
|
||||
// 输出每个节点的写入结果
|
||||
for (WResponse wResponse : result) {
|
||||
WValue wValue = wResponse.getWValue();
|
||||
SiteBO site = wValue.getPoint();
|
||||
Boolean success = wResponse.getIsOk();
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
"写入值=" + wValue.getValue() + " 结果=" + (success ? "成功" : "失败"));
|
||||
if (!success && wResponse.getExceptionMessage() != null) {
|
||||
System.out.println(" 错误信息: " + wResponse.getExceptionMessage());
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("========== OPC UA批量写入测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC UA批量写入测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user