|
|
@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
import java.net.Socket;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
@@ -31,22 +32,32 @@ public class SocketHandle {
|
|
|
@Autowired
|
|
|
private YkcMsgHandle ykcMsgHandle;
|
|
|
|
|
|
- public synchronized void addDeviceConnection(String ip, int port, String deviceId, String imei, String deviceSn, String ver) throws IOException {
|
|
|
+ public synchronized boolean addDeviceConnection(String ip, int port, String deviceId, String imei, String deviceSn, String ver) throws IOException {
|
|
|
if (deviceConnectionMsgMap.containsKey(deviceId)) {
|
|
|
log.info("已存在登录{}",imei);
|
|
|
- return;
|
|
|
+ return true;
|
|
|
}
|
|
|
log.info("请求登录{},版本:{}",imei,ver);
|
|
|
//思考:这里的Socket的IP和端口从数据库查询到,云快充的device库中加字段保存IP和地址,根据桩后台传的设备SN码确定设备是对接的那个厂家的平台
|
|
|
//Socket socket = new Socket("114.55.7.88", 8781);
|
|
|
//集测
|
|
|
- Socket socket = new Socket(ip, port);
|
|
|
- socket.setKeepAlive(true);
|
|
|
- DeviceConnectionMsg deviceConnectionMsg = new DeviceConnectionMsg(socket, deviceId,imei,deviceSn,ver);
|
|
|
- //每个设备连接后开启监听接收消息
|
|
|
- ykcMsgHandle.startListening(deviceConnectionMsg);
|
|
|
- //并将连接信息等保存再Map中
|
|
|
- deviceConnectionMsgMap.put(deviceId, deviceConnectionMsg);
|
|
|
+ try {
|
|
|
+
|
|
|
+ Socket socket = new Socket();
|
|
|
+ socket.connect(new InetSocketAddress(ip, port), 5000);
|
|
|
+ socket.setKeepAlive(true);
|
|
|
+ DeviceConnectionMsg deviceConnectionMsg = new DeviceConnectionMsg(socket, deviceId,imei,deviceSn,ver);
|
|
|
+ //每个设备连接后开启监听接收消息
|
|
|
+ ykcMsgHandle.startListening(deviceConnectionMsg);
|
|
|
+ //并将连接信息等保存再Map中
|
|
|
+ deviceConnectionMsgMap.put(deviceId, deviceConnectionMsg);
|
|
|
+ return true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 连接失败,捕获IOException异常
|
|
|
+ log.info("{}-{}-{}连接失败: " + e.getMessage(),imei,ip,port);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public void removeDeviceConnection(String deviceId) {
|