| | |
| | | private ZyRgvConnectDriver zyRgvConnectDriver; |
| | | private RgvProtocol rgvProtocol; |
| | | private int deviceLogCollectTime = 200; |
| | | private volatile boolean closed = false; |
| | | private Thread mainThread; |
| | | |
| | | public ZyRgvThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | |
| | | public void run() { |
| | | connect(); |
| | | initRgv(); |
| | | mainThread = Thread.currentThread(); |
| | | while (!closed && !Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | deviceLogCollectTime = Utils.getDeviceLogCollectTime(); |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Rgv, deviceConfig.getDeviceNo()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | |
| | | Thread readThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | deviceLogCollectTime = Utils.getDeviceLogCollectTime(); |
| | | readStatus(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| | | log.error("RgvThread Fail", e); |
| | | } |
| | | switch (step) { |
| | | case 1: |
| | | readStatus(); |
| | | break; |
| | | case 2: |
| | | sendCommand((RgvCommand) task.getData()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | Thread.sleep(200); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | break; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | readThread.start(); |
| | | |
| | | Thread processThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Rgv, deviceConfig.getDeviceNo()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | if (step == 2) { |
| | | sendCommand((RgvCommand) task.getData()); |
| | | } |
| | | |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| | | log.error("RgvProcess Fail", e); |
| | | } |
| | | } |
| | | }); |
| | | processThread.start(); |
| | | } |
| | | |
| | | private void initRgv() { |
| | |
| | | |
| | | @Override |
| | | public void close() { |
| | | closed = true; |
| | | Thread t = mainThread; |
| | | if (t != null) { |
| | | try { t.interrupt(); } catch (Exception ignore) {} |
| | | } |
| | | if (zyRgvConnectDriver != null) { |
| | | zyRgvConnectDriver.close(); |
| | | } |