|
@@ -27,18 +27,26 @@ public class ServerGs {
|
|
|
private ServerSocket serverSocket;
|
|
|
|
|
|
private ISysConfigService configService;
|
|
|
+
|
|
|
/**
|
|
|
* 监听端口
|
|
|
*
|
|
|
* @param port
|
|
|
*/
|
|
|
- public ServerGs(int port,ISysConfigService configService) {
|
|
|
+ public ServerGs(int port, ISysConfigService configService) {
|
|
|
try {
|
|
|
this.serverSocket = new ServerSocket(port);
|
|
|
this.configService = configService;
|
|
|
- System.out.println("服务端启动成功,端口:" + port);
|
|
|
+ System.out.println("光栅服务端启动成功,端口:" + port);
|
|
|
} catch (IOException e) {
|
|
|
- System.out.println("服务端启动失败");
|
|
|
+ System.out.println("光栅服务端启动失败");
|
|
|
+ if (serverSocket != null) {
|
|
|
+ try {
|
|
|
+ serverSocket.close();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -61,12 +69,14 @@ public class ServerGs {
|
|
|
while (true) {
|
|
|
try {
|
|
|
Socket client = serverSocket.accept();
|
|
|
+ log.info("----->有光栅客户端新连接:ip," + client.getInetAddress() + ",p:" + client.getPort());
|
|
|
|
|
|
new Timer().schedule(new TimerTask() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
+ OutputStream os = null;
|
|
|
try {
|
|
|
- OutputStream os = client.getOutputStream();
|
|
|
+ os = client.getOutputStream();
|
|
|
|
|
|
//根据参数判定哪个光栅进行超高检测 > condifvalue为Y开启光栅检测,N关闭光栅检测
|
|
|
SysConfig config = new SysConfig();
|
|
@@ -74,41 +84,49 @@ public class ServerGs {
|
|
|
config.setConfigValue("Y");
|
|
|
List<SysConfig> list = configService.selectConfigList(config);
|
|
|
|
|
|
- for (int i=0; i<list.size(); i++){
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
|
//发送检测信号到光栅PLC
|
|
|
switch (list.get(i).getConfigKey()) {
|
|
|
case "sys.guangshan.no1":
|
|
|
// 光栅1 发送检测信号
|
|
|
- log.info("------------光栅1 发送检测信号------------>");
|
|
|
+// log.info("------------光栅1 发送检测信号------------>");
|
|
|
client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00010D0A"));
|
|
|
break;
|
|
|
case "sys.guangshan.no2":
|
|
|
// 光栅2 发送检测信号
|
|
|
- log.info("------------光栅2 发送检测信号------------>");
|
|
|
+// log.info("------------光栅2 发送检测信号------------>");
|
|
|
client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00020D0A"));
|
|
|
break;
|
|
|
case "sys.guangshan.no3":
|
|
|
// 光栅3 发送检测信号
|
|
|
- log.info("------------光栅3 发送检测信号------------>");
|
|
|
+// log.info("------------光栅3 发送检测信号------------>");
|
|
|
client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00030D0A"));
|
|
|
break;
|
|
|
case "sys.guangshan.no4":
|
|
|
// 光栅4 发送检测信号
|
|
|
- log.info("------------光栅4 发送检测信号------------>");
|
|
|
+// log.info("------------光栅4 发送检测信号------------>");
|
|
|
client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00040D0A"));
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
os.flush();
|
|
|
} catch (Exception e) {
|
|
|
- log.error(e.getMessage());
|
|
|
+ log.error("光栅客户端断开:" + e.getMessage());
|
|
|
+ //关闭连接
|
|
|
+ try {
|
|
|
+ os.close();
|
|
|
+ client.close();
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ throw new RuntimeException("光栅客户端断开,释放线程资源:" + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- }, 10000, 5000);
|
|
|
- new ClientHandler(client,configService).start();
|
|
|
+ }, 6000, 3000);
|
|
|
+ new ClientHandler(client, configService).start();
|
|
|
} catch (IOException e) {
|
|
|
- System.out.println("服务端异常");
|
|
|
+ System.out.println("光栅服务端异常:" + e);
|
|
|
}
|
|
|
}
|
|
|
}
|