use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.
the class OpcUaTemplate method createItemMonitored.
private List<UaMonitoredItem> createItemMonitored(OpcUaClientBean opcUaClientBean, OpcPoint opcPoint) throws ExecutionException, InterruptedException {
int namespace = opcPoint.getNamespace();
String tag = opcPoint.getTagName();
NodeId nodeId = new NodeId(namespace, tag);
// 创建发布间隔1000ms的订阅对象
UaSubscription subscription = this.getSubscription(opcUaClientBean.getOpcUaClient());
MonitoringParameters parameters = new MonitoringParameters(uint(subscription.getMonitoredItems().size() + 1), 1000.0, null, uint(10), true);
List<MonitoredItemCreateRequest> requests = Lists.newArrayList();
ReadValueId readValueId = new ReadValueId(nodeId, AttributeId.Value.uid(), null, null);
// 创建监控item, 第一个为Reporting mode
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
requests.add(request);
List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id) -> item.setValueConsumer(new UaMonitoredItem.ValueConsumer() {
@Override
public void onValueArrived(UaMonitoredItem item, DataValue value) {
try {
log.debug("OPC数据变化回调:subscription value received: item={}, value={}", item.getReadValueId().getNodeId(), value.getValue());
ValueChanageEvent valueChanageEvent = opcUaClientBean.getSubscriptionPoints().get(opcPoint.getAlias());
valueChanageEvent.putData(item, value);
applicationContext.publishEvent(valueChanageEvent);
} catch (Exception e) {
}
}
})).get();
log.info("添加监听:[{}]到监听器[{}]监听数量:{}", nodeId, subscription.getSubscriptionId(), subscription.getMonitoredItems().size());
return items;
}
use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.
the class OpcUaTemplate method subscribeItem.
public <T extends ValueChanageEvent> void subscribeItem(String deviceId, String pointName, Class<T> callBackEvent) {
OpcUaClientBean opcUaClientBean = clientMap.get(deviceId);
OpcPoint opcPoint = opcUaClientBean.findPoint(pointName);
ValueChanageEvent valueChanageEvent = ReflectUtil.newInstance(null, null, null);
this.subscribeItem(deviceId, opcPoint, valueChanageEvent);
}
use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.
the class OpcUaTemplate method writeItem.
/**
* Write Opc Ua Point Value
*
* @param deviceId Device Id
* @param point OpcPoint Info
* @throws UaException UaException
* @throws ExecutionException ExecutionException
* @throws InterruptedException InterruptedException
*/
public void writeItem(String deviceId, OpcPoint point) {
OpcUaClient client;
try {
log.debug("OPCUA写入点位:{}", JSON.toJSONString(point));
int namespace = point.getNamespace();
String tag = point.getTagName();
NodeId nodeId = new NodeId(namespace, tag);
client = getOpcUaClient(deviceId);
client.connect().get();
StatusCode statusCode = StatusCode.GOOD;
DataValue dataValue = this.convertData(point);
statusCode = client.writeValue(nodeId, dataValue).get();
if (!statusCode.isGood()) {
throw new RuntimeException(statusCode.toString());
}
} catch (Exception e) {
log.error("Opc Ua Point Write Error", e);
}
}
use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.
the class OpcUaTemplate method readItem.
public String readItem(String deviceId, String pointName) throws Exception {
OpcUaClientBean opcUaClientBean = clientMap.get(deviceId);
OpcPoint point = opcUaClientBean.findPoint(pointName);
return this.readItem(deviceId, point);
}
use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.
the class OpcUaTemplate method readItem.
public String readItem(String deviceId, OpcPoint point) throws Exception {
int namespace = point.getNamespace();
String tag = point.getTagName();
NodeId nodeId = new NodeId(namespace, tag);
CompletableFuture<String> value = new CompletableFuture<>();
OpcUaClient client = getOpcUaClient(deviceId);
log.debug("start read point(ns={};s={})", namespace, tag);
client.connect().get();
client.readValue(0.0, TimestampsToReturn.Both, nodeId).thenAccept(dataValue -> {
try {
value.complete(dataValue.getValue().getValue().toString());
} catch (Exception e) {
log.error("accept point(ns={};s={}) value error", namespace, tag, e);
}
});
String rawValue = value.get(3, TimeUnit.SECONDS);
log.debug("end read point(ns={};s={}) value: {}", namespace, tag, rawValue);
return rawValue;
}
Aggregations