#
Junjie
19 小时以前 0c5a58771fcf86ad0b562829fbfa440da9392703
src/main/java/com/zy/core/thread/impl/ZyStationThread.java
@@ -1,11 +1,10 @@
package com.zy.core.thread.impl;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.utils.Utils;
import com.zy.core.network.DeviceConnectPool;
import com.zy.core.thread.StationThread;
import com.alibaba.fastjson.JSON;
@@ -13,6 +12,8 @@
import com.core.common.SpringUtils;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.asrs.entity.BasStationOpt;
import com.zy.asrs.service.BasStationOptService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.network.ZyStationConnectDriver;
import com.zy.core.cache.MessageQueue;
@@ -26,9 +27,11 @@
import com.zy.core.network.entity.ZyStationStatusEntity;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -44,7 +47,11 @@
    private DeviceConfig deviceConfig;
    private RedisUtil redisUtil;
    private ZyStationConnectDriver zyStationConnectDriver;
    private int deviceLogCollectTime = 200;
    private long deviceDataLogTime = System.currentTimeMillis();
    private volatile boolean closed = false;
    private ScheduledExecutorService readExecutor;
    private ScheduledExecutorService processExecutor;
    public ZyStationThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
@@ -55,29 +62,55 @@
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        this.connect();
        while (true) {
        deviceLogCollectTime = Utils.getDeviceLogCollectTime();
        readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("DevpReader-" + deviceConfig.getDeviceNo());
                t.setDaemon(true);
                return t;
            }
        });
        readExecutor.scheduleAtFixedRate(() -> {
            if (closed || Thread.currentThread().isInterrupted()) {
                return;
            }
            try {
                deviceLogCollectTime = Utils.getDeviceLogCollectTime();
                readStatus();
            } catch (Exception e) {
                log.error("StationThread Fail", e);
            }
        }, 0, 200, TimeUnit.MILLISECONDS);
        processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("DevpWriter-" + deviceConfig.getDeviceNo());
                t.setDaemon(true);
                return t;
            }
        });
        processExecutor.scheduleAtFixedRate(() -> {
            if (closed || Thread.currentThread().isInterrupted()) {
                return;
            }
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
                        break;
                    case 2:
                        sendCommand((StationCommand) task.getData());
                        break;
                    default:
                        break;
                if (step == 2 && task != null) {
                    sendCommand((StationCommand) task.getData());
                }
                Thread.sleep(200);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        }, 0, 200, TimeUnit.MILLISECONDS);
    }
    private void readStatus() {
@@ -86,7 +119,12 @@
        }
        if(statusList.isEmpty()) {
            BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
            BasDevpService basDevpService = null;
            try {
                basDevpService = SpringUtils.getBean(BasDevpService.class);
            } catch (Exception e) {
            }
            if (basDevpService == null) {
                return;
            }
@@ -126,7 +164,7 @@
        OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), deviceConfig.getDeviceNo()));
        if (System.currentTimeMillis() - deviceDataLogTime > 1000 * 1) {
        if (System.currentTimeMillis() - deviceDataLogTime > deviceLogCollectTime) {
            //保存数据记录
            DeviceDataLog deviceDataLog = new DeviceDataLog();
            deviceDataLog.setOriginData(JSON.toJSONString(zyStationStatusEntities));
@@ -143,15 +181,26 @@
    @Override
    public boolean connect() {
        SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp());
        zyStationConnectDriver = new ZyStationConnectDriver(siemensS7Net, deviceConfig);
        new Thread(zyStationConnectDriver).start();
        zyStationConnectDriver = new ZyStationConnectDriver(deviceConfig);
        zyStationConnectDriver.start();
        DeviceConnectPool.put(SlaveType.Devp, deviceConfig.getDeviceNo(), zyStationConnectDriver);
        return true;
    }
    @Override
    public void close() {
        closed = true;
        ScheduledExecutorService ex = readExecutor;
        if (ex != null) {
            try { ex.shutdownNow(); } catch (Exception ignore) {}
        }
        ScheduledExecutorService px = processExecutor;
        if (px != null) {
            try { px.shutdownNow(); } catch (Exception ignore) {}
        }
        if (zyStationConnectDriver != null) {
            zyStationConnectDriver.close();
        }
    }
    @Override
@@ -171,8 +220,49 @@
    @Override
    public CommandResponse sendCommand(StationCommand command) {
       CommandResponse commandResponse = zyStationConnectDriver.sendCommand(command);
       return commandResponse;
       CommandResponse commandResponse = null;
       try {
           commandResponse = zyStationConnectDriver.sendCommand(command);
           return commandResponse;
       } finally {
           BasStationOptService optService = SpringUtils.getBean(BasStationOptService.class);
           List<ZyStationStatusEntity> statusListEntity = zyStationConnectDriver.getStatus();
           ZyStationStatusEntity matched = null;
           if (statusListEntity != null) {
               for (ZyStationStatusEntity e : statusListEntity) {
                   if (e.getStationId() != null && e.getStationId().equals(command.getStationId())) {
                       matched = e;
                       break;
                   }
               }
           }
           BasStationOpt basStationOpt = new BasStationOpt(
                   command.getTaskNo(),
                   command.getStationId(),
                   new Date(),
                   "MOVE",
                   command.getStationId(),
                   command.getTargetStaNo(),
                   null,
                   null,
                   null,
                   JSON.toJSONString(command),
                   JSON.toJSONString(matched),
                   1,
                   JSON.toJSONString(commandResponse)
           );
           if (optService != null) {
               optService.insert(basStationOpt);
           }
       }
    }
    @Override
    public Map<Integer, StationProtocol> getStatusMap() {
        Map<Integer, StationProtocol> map = new HashMap<>();
        for (StationProtocol stationProtocol : statusList) {
            map.put(stationProtocol.getStationId(), stationProtocol);
        }
        return map;
    }
}