| | |
| | | private ZyStationConnectDriver zyStationConnectDriver; |
| | | private int deviceLogCollectTime = 200; |
| | | private long deviceDataLogTime = System.currentTimeMillis(); |
| | | private volatile boolean closed = false; |
| | | private Thread mainThread; |
| | | private Thread readThread; |
| | | |
| | | public ZyStationThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | |
| | | public void run() { |
| | | this.connect(); |
| | | deviceLogCollectTime = Utils.getDeviceLogCollectTime(); |
| | | mainThread = Thread.currentThread(); |
| | | |
| | | //设备读取 |
| | | Thread readThread = new Thread(() -> { |
| | | while (!Thread.currentThread().isInterrupted()) { |
| | | readThread = new Thread(() -> { |
| | | while (!closed && !Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | readStatus(); |
| | | Thread.sleep(200); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | break; |
| | | } catch (Exception e) { |
| | | log.error("StationThread Fail", e); |
| | | } |
| | |
| | | }); |
| | | readThread.start(); |
| | | |
| | | while (!Thread.currentThread().isInterrupted()) { |
| | | while (!closed && !Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo()); |
| | |
| | | break; |
| | | } |
| | | Thread.sleep(200); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | break; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | @Override |
| | | public boolean connect() { |
| | | zyStationConnectDriver = new ZyStationConnectDriver(deviceConfig); |
| | | new Thread(zyStationConnectDriver).start(); |
| | | zyStationConnectDriver.start(); |
| | | DeviceConnectPool.put(SlaveType.Devp, deviceConfig.getDeviceNo(), zyStationConnectDriver); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | closed = true; |
| | | Thread t = mainThread; |
| | | if (t != null) { |
| | | try { t.interrupt(); } catch (Exception ignore) {} |
| | | } |
| | | Thread rt = readThread; |
| | | if (rt != null) { |
| | | try { rt.interrupt(); } catch (Exception ignore) {} |
| | | } |
| | | if (zyStationConnectDriver != null) { |
| | | zyStationConnectDriver.close(); |
| | | } |
| | | } |
| | | |
| | | @Override |