| | |
| | | |
| | | private static final ZyStationFakeSegConnect zyStationFakeSegConnect = new ZyStationFakeSegConnect(); |
| | | private static final ZyStationV4FakeSegConnect zyStationV4FakeSegConnect = new ZyStationV4FakeSegConnect(); |
| | | private static final long SEND_LOCK_WARN_MS = 3_000L; |
| | | private static final long SEND_COST_WARN_MS = 5_000L; |
| | | |
| | | private volatile boolean connected = false; |
| | | private volatile boolean connecting = false; |
| | |
| | | if (deviceConfig.getFake() == 0) { |
| | | if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) { |
| | | connectApi = new ZyStationV3RealConnect(deviceConfig, redisUtil); |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) { |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl()) |
| | | || "ZyStationV5Thread".equals(deviceConfig.getThreadImpl())) { |
| | | connectApi = new ZyStationV4RealConnect(deviceConfig, redisUtil); |
| | | } else { |
| | | connectApi = new ZyStationRealConnect(deviceConfig, redisUtil); |
| | |
| | | if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationFakeSegConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | connectApi = zyStationFakeSegConnect; |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) { |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl()) |
| | | || "ZyStationV5Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationV4FakeSegConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | connectApi = zyStationV4FakeSegConnect; |
| | | } else { |
| | | fakeConfigUnsupported = true; |
| | | zyStationConnectApi = null; |
| | | log.error("旧版输送站 fake 已移除,deviceNo={}, threadImpl={}, 请切换到 ZyStationV3Thread 或 ZyStationV4Thread", |
| | | log.error("旧版输送站 fake 已移除,deviceNo={}, threadImpl={}, 请切换到 ZyStationV3Thread、ZyStationV4Thread 或 ZyStationV5Thread", |
| | | deviceConfig.getDeviceNo(), deviceConfig.getThreadImpl()); |
| | | return false; |
| | | } |
| | |
| | | if (!connected || connecting || connectApi == null) { |
| | | return new CommandResponse(false, "设备未连接,命令下发失败"); |
| | | } |
| | | long lockWaitStart = System.currentTimeMillis(); |
| | | int waitRounds = 0; |
| | | while (true) { |
| | | Object lock = redisUtil.get(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key); |
| | | if(lock != null) { |
| | | waitRounds++; |
| | | try { |
| | | Thread.sleep(500); |
| | | }catch (Exception e) { |
| | |
| | | break; |
| | | } |
| | | } |
| | | CommandResponse commandResponse = connectApi.sendCommand(deviceConfig.getDeviceNo(), command); |
| | | redisUtil.del(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key); |
| | | return commandResponse; |
| | | long lockWaitCost = System.currentTimeMillis() - lockWaitStart; |
| | | if (lockWaitCost >= SEND_LOCK_WARN_MS) { |
| | | log.warn("输送命令等待全局发送锁超时,deviceNo={}, taskNo={}, stationId={}, targetStaNo={}, waitMs={}, waitRounds={}", |
| | | deviceConfig == null ? null : deviceConfig.getDeviceNo(), |
| | | command == null ? null : command.getTaskNo(), |
| | | command == null ? null : command.getStationId(), |
| | | command == null ? null : command.getTargetStaNo(), |
| | | lockWaitCost, |
| | | waitRounds); |
| | | } |
| | | long sendStart = System.currentTimeMillis(); |
| | | try { |
| | | return connectApi.sendCommand(deviceConfig.getDeviceNo(), command); |
| | | } finally { |
| | | redisUtil.del(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key); |
| | | long sendCostMs = System.currentTimeMillis() - sendStart; |
| | | if (sendCostMs >= SEND_COST_WARN_MS) { |
| | | log.warn("输送命令底层发送耗时过长,deviceNo={}, taskNo={}, stationId={}, targetStaNo={}, sendCostMs={}", |
| | | deviceConfig == null ? null : deviceConfig.getDeviceNo(), |
| | | command == null ? null : command.getTaskNo(), |
| | | command == null ? null : command.getStationId(), |
| | | command == null ? null : command.getTargetStaNo(), |
| | | sendCostMs); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public CommandResponse sendOriginCommand(String address, short[] data) { |
| | |
| | | return connectApi.sendOriginCommand(address, data); |
| | | } |
| | | |
| | | public boolean clearTaskBufferSlot(Integer stationId, Integer slotIdx) { |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (!connected || connecting || connectApi == null) { |
| | | return false; |
| | | } |
| | | CommandResponse response = connectApi.clearTaskBufferSlot(deviceConfig.getDeviceNo(), stationId, slotIdx); |
| | | return response != null && Boolean.TRUE.equals(response.getResult()); |
| | | } |
| | | |
| | | public byte[] readOriginCommand(String address, int length) { |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (!connected || connecting || connectApi == null) { |