Search in sources :

Example 1 with KeyLoader

use of com.jbm.framework.opcua.key.KeyLoader in project JBM by numen06.

the class OpcUaTemplate method getOpcUaClient.

/**
 * Get Opc Ua Client
 *
 * @param deviceId   Device Id
 * @param driverInfo Driver Info
 * @return OpcUaClient
 * @throws UaException UaException
 */
public OpcUaClient getOpcUaClient(String deviceId, OpcUaSource driverInfo) {
    OpcUaClient opcUaClient = null;
    if (clientMap.containsKey(deviceId))
        return clientMap.get(deviceId).getOpcUaClient();
    try {
        KeyLoader loader = new KeyLoader().load(Paths.get(FileUtil.getTmpDirPath()));
        if (null == opcUaClient) {
            try {
                List<EndpointDescription> remoteEndpoints = DiscoveryClient.getEndpoints(driverInfo.getUrl()).get();
                EndpointDescription configPoint = EndpointUtil.updateUrl(remoteEndpoints.get(0), driverInfo.getHost(), driverInfo.getPort());
                opcUaClient = OpcUaClient.create(driverInfo.getUrl(), endpoints -> remoteEndpoints.stream().findFirst(), configBuilder -> configBuilder.setIdentityProvider(new AnonymousProvider()).setCertificate(loader.getClientCertificate()).setKeepAliveInterval(uint(3000)).setRequestTimeout(uint(5000)).setEndpoint(configPoint).build());
            // clientMap.put(deviceId, new OpcUaClientBean(deviceId, opcUaClient));
            } catch (UaException e) {
                log.error("get opc ua client error: {}", e.getMessage());
            // clientMap.entrySet().removeIf(next -> next.getKey().equals(deviceId));
            }
        }
    } catch (Exception e) {
        log.error("get opc ua client error: {}", e.getMessage());
    }
    // return clientMap.get(deviceId).getOpcUaClient();
    return opcUaClient;
}
Also used : MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) PointSubscribeEvent(com.jbm.framework.opcua.event.PointSubscribeEvent) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) Autowired(org.springframework.beans.factory.annotation.Autowired) ValueChanageEvent(com.jbm.framework.opcua.event.ValueChanageEvent) CompletableFuture(java.util.concurrent.CompletableFuture) Lists(com.google.common.collect.Lists) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Service(org.springframework.stereotype.Service) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) Map(java.util.Map) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) org.eclipse.milo.opcua.stack.core.types.builtin(org.eclipse.milo.opcua.stack.core.types.builtin) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) GuardSubscriptionListener(com.jbm.framework.opcua.listener.GuardSubscriptionListener) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) SessionActivityListener(org.eclipse.milo.opcua.sdk.client.SessionActivityListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Maps(com.google.common.collect.Maps) ApplicationContext(org.springframework.context.ApplicationContext) KeyLoader(com.jbm.framework.opcua.key.KeyLoader) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) org.eclipse.milo.opcua.stack.core.types.structured(org.eclipse.milo.opcua.stack.core.types.structured) List(java.util.List) JSON(com.alibaba.fastjson.JSON) OpcPointsRead(com.jbm.framework.opcua.attribute.OpcPointsRead) Paths(java.nio.file.Paths) ReflectUtil(cn.hutool.core.util.ReflectUtil) UaException(org.eclipse.milo.opcua.stack.core.UaException) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) FileUtil(cn.hutool.core.io.FileUtil) ValueType(com.jbm.framework.opcua.attribute.ValueType) DriverUtils(com.jbm.framework.opcua.util.DriverUtils) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) UaException(org.eclipse.milo.opcua.stack.core.UaException) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) KeyLoader(com.jbm.framework.opcua.key.KeyLoader) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Aggregations

CollUtil (cn.hutool.core.collection.CollUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 ReflectUtil (cn.hutool.core.util.ReflectUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 JSON (com.alibaba.fastjson.JSON)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 OpcPoint (com.jbm.framework.opcua.attribute.OpcPoint)1 OpcPointsRead (com.jbm.framework.opcua.attribute.OpcPointsRead)1 ValueType (com.jbm.framework.opcua.attribute.ValueType)1 PointSubscribeEvent (com.jbm.framework.opcua.event.PointSubscribeEvent)1 ValueChanageEvent (com.jbm.framework.opcua.event.ValueChanageEvent)1 KeyLoader (com.jbm.framework.opcua.key.KeyLoader)1 GuardSubscriptionListener (com.jbm.framework.opcua.listener.GuardSubscriptionListener)1 DriverUtils (com.jbm.framework.opcua.util.DriverUtils)1 Paths (java.nio.file.Paths)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1