120 lines
3.5 KiB
Plaintext
120 lines
3.5 KiB
Plaintext
package com.ruoyi.wms.service;
|
||
|
||
import com.ruoyi.wms.util.TcpUtil;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.boot.CommandLineRunner;
|
||
import org.springframework.scheduling.annotation.Scheduled;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import javax.annotation.PostConstruct;
|
||
import java.io.IOException;
|
||
import java.net.*;
|
||
|
||
/**
|
||
* @Verasion:1.0
|
||
* @Author:DZY
|
||
* @Date:2023/7/5
|
||
**/
|
||
@Component
|
||
@Slf4j
|
||
public class TcpService implements CommandLineRunner {
|
||
private ServerSocket serverSocket;
|
||
public DatagramSocket clientSocket;
|
||
private DatagramPacket packet;
|
||
@Autowired
|
||
private AGVService agvService;
|
||
public void start(int port) throws IOException
|
||
{
|
||
|
||
}
|
||
|
||
public void stop() throws IOException
|
||
{
|
||
serverSocket.close();
|
||
}
|
||
|
||
private static AGVService userService;
|
||
|
||
@PostConstruct
|
||
public void init() {
|
||
if (userService == null) {
|
||
setDateSource(agvService);
|
||
}
|
||
}
|
||
|
||
private synchronized static void setDateSource(AGVService userInterfaceService) {
|
||
userService = userInterfaceService;
|
||
}
|
||
|
||
@Override
|
||
public void run(String... args) throws Exception {
|
||
clientSocket = new DatagramSocket(8085);
|
||
//创建字节数组,指定接收的数据包的大小
|
||
byte[] data = new byte[10];
|
||
DatagramPacket packet = new DatagramPacket(data, data.length);
|
||
// 3.接收客户端发送的数据
|
||
while (true) {
|
||
//通过循环不停的向客户端发送数据和接收数据
|
||
try {
|
||
// 此方法在接收到数据报之前会一直阻塞
|
||
clientSocket.receive(packet);
|
||
//创建字符串对象
|
||
final String s = BinaryToHexString(data);
|
||
final String[] s1 = s.split(" ");
|
||
log.info("tcps:" + s);
|
||
//设备信号 02:空车入库 01:出库1 04:出库2
|
||
String status = s1[3];
|
||
if (status.equals("06") || status.equals("04") || status.equals("00") || status.equals("02")) {
|
||
//成品出库
|
||
agvService.outProdStore("04");
|
||
} else if (status.equals("03") || status.equals("01")) {
|
||
//成品出库
|
||
agvService.outProdStore("02");
|
||
}
|
||
}
|
||
catch (Exception ex){
|
||
System.out.println("我是服务器,客户端说:" );
|
||
}
|
||
}
|
||
}
|
||
|
||
@Scheduled(cron = "*/5 * * * * ?")
|
||
private void configureTasks() throws IOException {
|
||
byte[] bytes = new byte[8];
|
||
bytes[0] = 1;
|
||
bytes[1] = 2;
|
||
bytes[2] = 0;
|
||
bytes[3] = 0;
|
||
bytes[4] = 0;
|
||
bytes[5] = 0;
|
||
bytes[6] = 121;
|
||
bytes[7] = -55;
|
||
//log.info("发送数据:" + bytes.toString());
|
||
DatagramPacket packet2 = new DatagramPacket(bytes, 8, InetAddress.getByName("192.168.33.39"),1030);
|
||
try {
|
||
clientSocket.send(packet2);
|
||
} catch (Exception e) {
|
||
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 将字节数组转换成十六进制的字符串
|
||
*
|
||
* @return
|
||
*/
|
||
public static String BinaryToHexString(byte[] bytes) {
|
||
String hexStr = "0123456789ABCDEF";
|
||
String result = "";
|
||
String hex = "";
|
||
for (byte b : bytes) {
|
||
hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
|
||
hex += String.valueOf(hexStr.charAt(b & 0x0F));
|
||
result += hex + " ";
|
||
}
|
||
return result;
|
||
}
|
||
}
|