69fbaaa1a5fd16b05953e750ea7fcc3e18e3a27c..4f58ba24b9a7e24dc38aa8eb9ca0b92c83e161ac
2023-10-20 yantian yue
OPCUA更新
4f58ba 对比 | 目录
2023-10-20 yantian yue
OPCUA更新
a7c91f 对比 | 目录
已修改2个文件
已添加1个文件
87 ■■■■■ 文件已修改
guns-vip-main/src/main/java/cn/stylefeng/guns/opcua/client/ClientHandler.java 36 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
guns-vip-main/src/main/java/cn/stylefeng/guns/opcua/init/OpcusConfigInit.java 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
guns-vip-main/src/main/java/cn/stylefeng/guns/opcua/mapper/mapping/OpcuaConfMapper.xml 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
guns-vip-main/src/main/java/cn/stylefeng/guns/opcua/client/ClientHandler.java
@@ -51,6 +51,8 @@
    // å®¢æˆ·ç«¯å®žä¾‹
    private OpcUaClient client = null;
    public List<OpcuaConfResult> b = null;
    @Autowired
    private ClientRunner clientRunner;
@@ -113,8 +115,6 @@
            return "找不到客户端,操作失败";
        }
//        List<Node> ns = client.getAddressSpace().browse(new NodeId(2, "模拟通道一.模拟设备一")).get();
        // æŸ¥è¯¢è®¢é˜…对象,没有则创建
        UaSubscription subscription = null;
        ImmutableList<UaSubscription> subscriptionList = client.getSubscriptionManager().getSubscriptions();
@@ -148,7 +148,7 @@
        // åˆ›å»ºç›‘控项,并且注册变量值改变时候的回调函数
        subscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id) -> {
            item.setValueConsumer((i, v) -> {
                handle(i.getReadValueId().getNodeId(), v.getValue());
                handle2(i.getReadValueId().getNodeId(), v.getValue());
            });
        }).get();
@@ -161,13 +161,13 @@
     * @CreateTime 2023å¹´10月13日
     */
    public void handle(NodeId id, Variant value){
    /*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(a.get(0).getRModule());
@@ -179,8 +179,34 @@
            e.printStackTrace();
        }
        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: å˜èŠ‚点量写入
guns-vip-main/src/main/java/cn/stylefeng/guns/opcua/init/OpcusConfigInit.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,48 @@
package cn.stylefeng.guns.opcua.init;
import cn.stylefeng.guns.opcua.client.ClientHandler;
import cn.stylefeng.guns.opcua.controller.OpcuaConfController;
import cn.stylefeng.guns.opcua.entity.NodeEntity;
import cn.stylefeng.guns.opcua.model.params.OpcuaConfParam;
import cn.stylefeng.guns.opcua.model.result.OpcuaConfResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
@Slf4j
public class OpcusConfigInit implements CommandLineRunner {
    @Autowired
    private ClientHandler clientHandler;
    @Autowired
    private OpcuaConfController opcuaConfController;
    @Override
    public void run(String... args) throws Exception {
        try {
            OpcuaConfParam opcuaConfParam=new OpcuaConfParam();
            opcuaConfParam.setSubscribe(1); //设置查询条件,是否订阅状态为1的所有数据.
            List<OpcuaConfResult> nodeslist=opcuaConfController.mylist(opcuaConfParam);
             clientHandler.connect();
            if (nodeslist != null && nodeslist.size() > 0) {
                for (OpcuaConfResult opcuaConfResult : nodeslist) {
                   List<NodeEntity> nodes = Stream.of(opcuaConfResult.getNode())
                            .map(id -> NodeEntity.builder().index(2).identifier(id).build()).collect(Collectors.toList());
                    clientHandler.subscribe(nodes);
                }
                log.info("初始化OPC订阅" + nodeslist.size() + "条!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("骚货,我启动失败了!");
        }
        log.info("骚货,我启动成功了!");
    }
}
guns-vip-main/src/main/java/cn/stylefeng/guns/opcua/mapper/mapping/OpcuaConfMapper.xml
@@ -29,6 +29,9 @@
        <if test="paramCondition.node != null and paramCondition.node != ''">
            and node like CONCAT('%',#{paramCondition.node},'%')
        </if>
        <if test="paramCondition.subscribe != null and paramCondition.subscribe != ''">
            and subscribe like CONCAT('%',#{paramCondition.subscribe},'%')
        </if>
    </select>
    <select id="customMapList" resultType="map" parameterType="cn.stylefeng.guns.opcua.model.params.OpcuaConfParam">