| | |
| | | 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; |
| | |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | 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; |
| | |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | this.connect(); |
| | | deviceLogCollectTime = Utils.getDeviceLogCollectTime(); |
| | | mainThread = Thread.currentThread(); |
| | | |
| | | //设备读取 |
| | | Thread readThread = new Thread(() -> { |
| | | while (true) { |
| | | 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 (true) { |
| | | 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(); |
| | | } |
| | |
| | | } |
| | | |
| | | 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; |
| | | } |
| | |
| | | |
| | | 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)); |
| | |
| | | @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 |