| | |
| | | import com.zy.core.network.real.ZyStationV3RealConnect; |
| | | import com.zy.core.network.real.ZyStationV4RealConnect; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import java.util.Collections; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.ThreadFactory; |
| | |
| | | private static final ZyStationFakeSegConnect zyStationFakeSegConnect = new ZyStationFakeSegConnect(); |
| | | private static final ZyStationV4FakeSegConnect zyStationV4FakeSegConnect = new ZyStationV4FakeSegConnect(); |
| | | |
| | | private boolean connected = false; |
| | | private volatile boolean connected = false; |
| | | private volatile boolean connecting = false; |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | private ZyStationConnectApi zyStationConnectApi; |
| | | private volatile ZyStationConnectApi zyStationConnectApi; |
| | | private volatile boolean closed = false; |
| | | private ScheduledExecutorService executor; |
| | | private final Object connectLock = new Object(); |
| | | |
| | | public ZyStationConnectDriver(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | if (deviceConfig.getFake() == 0) { |
| | | if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationConnectApi = new ZyStationV3RealConnect(deviceConfig, redisUtil); |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationConnectApi = new ZyStationV4RealConnect(deviceConfig, redisUtil); |
| | | } else { |
| | | zyStationConnectApi = new ZyStationRealConnect(deviceConfig, redisUtil); |
| | | synchronized (connectLock) { |
| | | if (closed) { |
| | | return false; |
| | | } |
| | | } else { |
| | | if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationFakeSegConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | zyStationConnectApi = zyStationFakeSegConnect; |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationV4FakeSegConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | zyStationConnectApi = zyStationV4FakeSegConnect; |
| | | } else { |
| | | zyStationFakeConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | zyStationConnectApi = zyStationFakeConnect; |
| | | if (connected && zyStationConnectApi != null) { |
| | | return true; |
| | | } |
| | | |
| | | connecting = true; |
| | | try { |
| | | ZyStationConnectApi connectApi; |
| | | if (deviceConfig.getFake() == 0) { |
| | | if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) { |
| | | connectApi = new ZyStationV3RealConnect(deviceConfig, redisUtil); |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) { |
| | | connectApi = new ZyStationV4RealConnect(deviceConfig, redisUtil); |
| | | } else { |
| | | connectApi = new ZyStationRealConnect(deviceConfig, redisUtil); |
| | | } |
| | | } else { |
| | | if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationFakeSegConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | connectApi = zyStationFakeSegConnect; |
| | | } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) { |
| | | zyStationV4FakeSegConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | connectApi = zyStationV4FakeSegConnect; |
| | | } else { |
| | | zyStationFakeConnect.addFakeConnect(deviceConfig, redisUtil); |
| | | connectApi = zyStationFakeConnect; |
| | | } |
| | | } |
| | | |
| | | boolean connect = connectApi.connect(); |
| | | connected = connect; |
| | | if (connect) { |
| | | zyStationConnectApi = connectApi; |
| | | } else { |
| | | zyStationConnectApi = null; |
| | | } |
| | | return connect; |
| | | } finally { |
| | | connecting = false; |
| | | } |
| | | } |
| | | |
| | | boolean connect = zyStationConnectApi.connect(); |
| | | connected = connect; |
| | | return connect; |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (ex != null) { |
| | | try { ex.shutdownNow(); } catch (Exception ignore) {} |
| | | } |
| | | if (zyStationConnectApi != null) { |
| | | zyStationConnectApi.disconnect(); |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (connectApi != null) { |
| | | connectApi.disconnect(); |
| | | zyStationConnectApi = null; |
| | | } |
| | | connected = false; |
| | | connecting = false; |
| | | } |
| | | |
| | | public void start() { |
| | |
| | | } |
| | | |
| | | public List<ZyStationStatusEntity> getStatus() { |
| | | if (zyStationConnectApi == null) { |
| | | return null; |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (!connected || connecting || connectApi == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return zyStationConnectApi.getStatus(deviceConfig.getDeviceNo()); |
| | | return connectApi.getStatus(deviceConfig.getDeviceNo()); |
| | | } |
| | | |
| | | public CommandResponse sendCommand(StationCommand command) { |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (!connected || connecting || connectApi == null) { |
| | | return new CommandResponse(false, "设备未连接,命令下发失败"); |
| | | } |
| | | while (true) { |
| | | Object lock = redisUtil.get(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key); |
| | | if(lock != null) { |
| | |
| | | break; |
| | | } |
| | | } |
| | | CommandResponse commandResponse = zyStationConnectApi.sendCommand(deviceConfig.getDeviceNo(), command); |
| | | CommandResponse commandResponse = connectApi.sendCommand(deviceConfig.getDeviceNo(), command); |
| | | redisUtil.del(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key); |
| | | return commandResponse; |
| | | } |
| | | |
| | | public CommandResponse sendOriginCommand(String address, short[] data) { |
| | | return zyStationConnectApi.sendOriginCommand(address, data); |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (!connected || connecting || connectApi == null) { |
| | | return new CommandResponse(false, "设备未连接,原始命令下发失败"); |
| | | } |
| | | return connectApi.sendOriginCommand(address, data); |
| | | } |
| | | |
| | | public byte[] readOriginCommand(String address, int length) { |
| | | return zyStationConnectApi.readOriginCommand(address, length); |
| | | ZyStationConnectApi connectApi = zyStationConnectApi; |
| | | if (!connected || connecting || connectApi == null) { |
| | | return new byte[0]; |
| | | } |
| | | return connectApi.readOriginCommand(address, length); |
| | | } |
| | | } |