| | |
| | | package cn.stylefeng.guns.opcua.client; |
| | | |
| | | import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
| | | import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo; |
| | | import cn.stylefeng.guns.opcua.controller.OpcuaConfController; |
| | | import cn.stylefeng.guns.opcua.entity.OpcuaConf; |
| | | import cn.stylefeng.guns.opcua.mapper.OpcuaConfMapper; |
| | | import cn.stylefeng.guns.opcua.model.params.OpcuaConfParam; |
| | | import cn.stylefeng.guns.opcua.model.result.OpcuaConfResult; |
| | | import cn.stylefeng.guns.opcua.service.OpcuaConfService; |
| | | import cn.stylefeng.roses.kernel.model.response.ResponseData; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.google.common.collect.ImmutableList; |
| | | import cn.stylefeng.guns.opcua.entity.NodeEntity; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.formula.functions.T; |
| | | import org.eclipse.milo.opcua.sdk.client.OpcUaClient; |
| | | import org.eclipse.milo.opcua.sdk.client.api.nodes.VariableNode; |
| | | import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription; |
| | |
| | | // 客户端实例 |
| | | private OpcUaClient client = null; |
| | | |
| | | public List<OpcuaConfResult> b = null; |
| | | |
| | | @Autowired |
| | | private ClientRunner clientRunner; |
| | | |
| | | @Autowired |
| | | private OpcuaConfController opcuaConfController; |
| | | |
| | | |
| | | /** |
| | | * |
| | |
| | | return "找不到客户端,操作失败"; |
| | | } |
| | | |
| | | // List<Node> ns = client.getAddressSpace().browse(new NodeId(2, "模拟通道一.模拟设备一")).get(); |
| | | |
| | | // 查询订阅对象,没有则创建 |
| | | UaSubscription subscription = null; |
| | | ImmutableList<UaSubscription> subscriptionList = client.getSubscriptionManager().getSubscriptions(); |
| | |
| | | // 创建监控项,并且注册变量值改变时候的回调函数 |
| | | subscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id) -> { |
| | | item.setValueConsumer((i, v) -> { |
| | | handle(i.getReadValueId().getNodeId(), v.getValue()); |
| | | handle2(i.getReadValueId().getNodeId(), v.getValue()); |
| | | }); |
| | | }).get(); |
| | | |
| | |
| | | * @CreateTime 2023年10月13日 |
| | | */ |
| | | |
| | | public void handle(NodeId id, Variant value){ |
| | | String className = "cn.stylefeng.guns.opcua.cert.MethodName"; |
| | | String methodName = "a"; |
| | | /*public void handle(NodeId id, Variant value){ |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | OpcuaConfParam opcuaConfParam=new OpcuaConfParam(); |
| | | opcuaConfParam.setNode(id.getIdentifier().toString()); |
| | | List<OpcuaConfResult> a=opcuaConfController.mylist(opcuaConfParam); |
| | | log.info("数据库访问代码执行时间:" + (System.currentTimeMillis() - startTime) + "毫秒"); |
| | | String str1 = id.getIdentifier().toString()+":"+value.getValue().toString(); |
| | | try { |
| | | Class<?> clazz = Class.forName(className); |
| | | Method method = clazz.getMethod(methodName, String.class); |
| | | Class<?> clazz = Class.forName(a.get(0).getRModule()); |
| | | Method method = clazz.getMethod(a.get(0).getRFunction(), String.class); |
| | | |
| | | method.invoke(clazz.newInstance(), str1); |
| | | } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | |
| | | InvocationTargetException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //if(id.getIdentifier().toString().equals("my.device.x1")){ |
| | | // log.info("item={}, value={},{}", id.getIdentifier().toString(), value,"返回一个SN号"); |
| | | //} |
| | | log.info("代码执行时间:" + (System.currentTimeMillis() - startTime) + "毫秒"); |
| | | }*/ |
| | | public void handle2(NodeId id, Variant value){ |
| | | long sTime = System.currentTimeMillis(); |
| | | if (b == null || 0 > b.size()) { |
| | | OpcuaConfParam opcuaConfParam=new OpcuaConfParam(); |
| | | opcuaConfParam.setSubscribe(1); |
| | | b=opcuaConfController.mylist(opcuaConfParam); |
| | | } |
| | | log.info("数据库访问代码执行时间:" + (System.currentTimeMillis() - sTime) + "毫秒"); |
| | | String str1 = id.getIdentifier().toString()+":"+value.getValue().toString(); |
| | | |
| | | OpcuaConfResult opcuaConfResult = b.stream() |
| | | .filter(customer ->id.getIdentifier().toString().equals(customer.getNode())) |
| | | .findAny() |
| | | .orElse(null); |
| | | try { |
| | | Class<?> clazz = Class.forName(opcuaConfResult.getRModule()); |
| | | Method method = clazz.getMethod(opcuaConfResult.getRFunction(), String.class); |
| | | |
| | | method.invoke(clazz.newInstance(), str1); |
| | | } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | |
| | | InvocationTargetException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | log.info("代码执行时间:" + (System.currentTimeMillis() - sTime) + "毫秒"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @MethodName: write |
| | | * @Description: 变节点量写入 |