| | |
| | | import com.zy.asrs.entity.DeviceDataLog; |
| | | import com.zy.asrs.service.BasRgvService; |
| | | import com.zy.asrs.service.BasRgvOptService; |
| | | import com.zy.asrs.utils.Utils; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.OutputQueue; |
| | |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.Date; |
| | | import com.zy.asrs.entity.BasRgvErrLog; |
| | | import com.zy.asrs.service.BasRgvErrLogService; |
| | | |
| | | @Data |
| | | @Slf4j |
| | |
| | | private RedisUtil redisUtil; |
| | | private ZyRgvConnectDriver zyRgvConnectDriver; |
| | | private RgvProtocol rgvProtocol; |
| | | private int deviceLogCollectTime = 200; |
| | | |
| | | public ZyRgvThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | |
| | | public void run() { |
| | | connect(); |
| | | initRgv(); |
| | | while (true) { |
| | | try { |
| | | 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 (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 boolean connect() { |
| | | zyRgvConnectDriver = new ZyRgvConnectDriver(deviceConfig); |
| | | new Thread(zyRgvConnectDriver).start(); |
| | | zyRgvConnectDriver.start(); |
| | | DeviceConnectPool.put(SlaveType.Rgv, deviceConfig.getDeviceNo(), zyRgvConnectDriver); |
| | | return true; |
| | | } |
| | |
| | | rgvProtocol.setLastCommandTime(System.currentTimeMillis()); |
| | | } |
| | | |
| | | if (System.currentTimeMillis() - rgvProtocol.getDeviceDataLog() > 200) { |
| | | if (System.currentTimeMillis() - rgvProtocol.getDeviceDataLog() > deviceLogCollectTime) { |
| | | DeviceDataLog deviceDataLog = new DeviceDataLog(); |
| | | deviceDataLog.setOriginData(JSON.toJSONString(s)); |
| | | deviceDataLog.setWcsData(JSON.toJSONString(rgvProtocol)); |
| | |
| | | rgvProtocol.setDeviceDataLog(System.currentTimeMillis()); |
| | | } |
| | | |
| | | BasRgvService basRgvService = SpringUtils.getBean(BasRgvService.class); |
| | | try { |
| | | BasRgvErrLogService errLogService = SpringUtils.getBean(BasRgvErrLogService.class); |
| | | if (errLogService != null) { |
| | | String errFlagKey = RedisKeyType.DEVICE_ERR_ACTIVE_RGV.key + rgvProtocol.getRgvNo(); |
| | | Object active = redisUtil.get(errFlagKey); |
| | | if (rgvProtocol.getAlarm() != null && rgvProtocol.getAlarm() > 0) { |
| | | if (active == null) { |
| | | BasRgvErrLog log = new BasRgvErrLog(); |
| | | log.setTaskNo(rgvProtocol.getTaskNo()); |
| | | log.setStartTime(new Date()); |
| | | log.setRgvNo(rgvProtocol.getRgvNo()); |
| | | log.setErrCode(rgvProtocol.getAlarm()); |
| | | log.setStatus(1); |
| | | log.setCreateTime(new Date()); |
| | | log.setSystemStatus(JSON.toJSONString(rgvProtocol)); |
| | | errLogService.insert(log); |
| | | if (log.getId() != null) { |
| | | redisUtil.set(errFlagKey, log.getId(), 60 * 60 * 24); |
| | | } |
| | | } |
| | | } else { |
| | | if (active != null) { |
| | | Long id = Long.valueOf(String.valueOf(active)); |
| | | BasRgvErrLog update = new BasRgvErrLog(); |
| | | update.setId(id); |
| | | update.setEndTime(new Date()); |
| | | update.setStatus(2); |
| | | update.setUpdateTime(new Date()); |
| | | errLogService.updateById(update); |
| | | redisUtil.del(errFlagKey); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ignore) {} |
| | | |
| | | BasRgvService basRgvService = null; |
| | | try { |
| | | basRgvService = SpringUtils.getBean(BasRgvService.class); |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | if (basRgvService != null) { |
| | | BasRgv basRgv = basRgvService.selectOne(new EntityWrapper<BasRgv>().eq("rgv_no", deviceConfig.getDeviceNo())); |
| | | if(basRgv == null) { |
| | |
| | | |
| | | @Override |
| | | public void close() { |
| | | zyRgvConnectDriver.close(); |
| | | if (zyRgvConnectDriver != null) { |
| | | zyRgvConnectDriver.close(); |
| | | } |
| | | } |
| | | |
| | | @Override |