psh
11 months ago
3 changed files with 237 additions and 64 deletions
@ -0,0 +1,139 @@ |
|||
package org.nl.acs.device_driver.basedriver.agv.utils; |
|||
|
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
|
|||
import java.io.DataInputStream; |
|||
import java.io.DataOutputStream; |
|||
import java.io.IOException; |
|||
import java.net.Socket; |
|||
|
|||
import static org.nl.acs.agv.server.impl.NDCAgvServiceImpl.Bytes2HexString; |
|||
|
|||
@Slf4j |
|||
public class SocketUtil { |
|||
|
|||
public static Socket s; |
|||
public static DataOutputStream dos; |
|||
public static DataInputStream dis; |
|||
|
|||
public static boolean checkConnect(int agvaddr, byte action) { |
|||
try { |
|||
System.out.println("电梯socket链接开始"); |
|||
byte[] b = new byte[1024]; |
|||
byte[] call = new byte[1024]; |
|||
byte[] move = new byte[1024]; |
|||
s = new Socket("172.17.195.242", 102); |
|||
// s = new Socket("127.0.0.1", 20002);
|
|||
dos = new DataOutputStream(s.getOutputStream()); |
|||
dis = new DataInputStream(s.getInputStream()); |
|||
System.out.println("电梯socket链接开始"); |
|||
log.info("电梯socket链接成功"); |
|||
if(agvaddr==2421){ |
|||
call = new byte[]{(byte) 0X1A, (byte) 0X1B, |
|||
(byte) 0X02, (byte) 0X01, |
|||
(byte) 0X02, (byte) 0X04, |
|||
(byte) 0X01, (byte) 0X01, |
|||
(byte) 0X00, (byte) 0XFF, |
|||
(byte) 0XFB, |
|||
}; |
|||
} |
|||
if(agvaddr==2422){ |
|||
call = new byte[]{(byte) 0X1A, (byte) 0X1B, |
|||
(byte) 0X02, (byte) 0X01, |
|||
(byte) 0X02, (byte) 0X04, |
|||
(byte) 0X01, (byte) 0X02, |
|||
(byte) 0X00, (byte) 0XFF, |
|||
(byte) 0XF8, |
|||
}; |
|||
} |
|||
if(action==0X0A) { |
|||
move = new byte[]{(byte) 0X1A, (byte) 0X1B, |
|||
(byte) 0X02, (byte) 0X01, |
|||
(byte) 0X04, (byte) 0X04, |
|||
(byte) 0X01, (byte) 0X01, |
|||
(byte) 0X0A, (byte) 0XFF, |
|||
(byte) 0XF7, |
|||
}; |
|||
} |
|||
if(action==0X00) { |
|||
move = new byte[]{(byte) 0X1A, (byte) 0X1B, |
|||
(byte) 0X02, (byte) 0X01, |
|||
(byte) 0X04, (byte) 0X04, |
|||
(byte) 0X01, (byte) 0X01, |
|||
(byte) 0X00, (byte) 0XFF, |
|||
(byte) 0XFD, |
|||
}; |
|||
} |
|||
write(b); |
|||
int[] arr = packageResult(); |
|||
if (arr[14] * 256 + arr[15] == 0x00) { |
|||
if ((arr[18] * 256 + arr[19] == 0x01 && agvaddr==2421)|| |
|||
(arr[18] * 256 + arr[19] == 0x02 && agvaddr==2422)) { |
|||
log.info("电梯已就位,下发开门指令"); |
|||
write(move); |
|||
packageResult(); |
|||
if (arr[22] * 256 + arr[23] == 0x11 && action == 0X10) { |
|||
log.info("电梯已开门就位,反馈NDC允许进入"); |
|||
return true; |
|||
} else if (arr[22] * 256 + arr[23] == 0x21 && action == 0X00) { |
|||
log.info("电梯已关门就位,反馈NDC"); |
|||
return true; |
|||
} |
|||
} else { |
|||
log.info("电梯未就位,呼叫电梯就位"); |
|||
write(call); |
|||
packageResult(); |
|||
} |
|||
} else { |
|||
log.info("电梯状态不为正常,直接返回"); |
|||
return false; |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("电梯socket链接异常"); |
|||
log.info("电梯socket链接异常"); |
|||
log.error("电梯socket链接出现异常:{}", e); |
|||
if (ObjectUtil.isNotEmpty(s)) { |
|||
try { |
|||
s.close(); |
|||
} catch (IOException ex) { |
|||
throw new RuntimeException(ex); |
|||
} |
|||
} |
|||
System.out.println(e.getMessage()); |
|||
e.printStackTrace(); |
|||
|
|||
} |
|||
return false; |
|||
} |
|||
|
|||
public static void write(byte[] b) { |
|||
try { |
|||
log.info("下发电梯socket数据:" + Bytes2HexString(b)); |
|||
System.out.println("下发电梯socket数据:" + Bytes2HexString(b)); |
|||
dos.write(b); |
|||
dos.flush(); |
|||
} catch (IOException e) { |
|||
// TODO Auto-generated catch block
|
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
public static int[] packageResult() throws IOException { |
|||
byte[] b = new byte[1024]; |
|||
int count = dis.read(b); |
|||
int[] arr = new int[count]; |
|||
StringBuffer bs = new StringBuffer(); |
|||
|
|||
for (int i = 0; i < count; i++) { |
|||
int temp = b[i]; |
|||
if (temp < 0) |
|||
temp += 256; |
|||
arr[i] = temp; |
|||
StringBuffer bs1 = new StringBuffer("0"); |
|||
bs.append(temp < 16 ? bs1.append(Integer.toHexString(temp)) : Integer.toHexString(temp)); |
|||
} |
|||
System.out.println("接收电梯socket上报信息:" + bs); |
|||
return arr; |
|||
} |
|||
} |
Loading…
Reference in new issue