|
@@ -1,133 +1,143 @@
|
|
|
package com.fjhx.listener;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
-import com.fjhx.constants.InstructTypeConstant;
|
|
|
-import com.fjhx.constants.RabbitConstant;
|
|
|
-import com.fjhx.enums.WebSocketOnMessageTypeEnum;
|
|
|
+import com.fjhx.constants.WebSocketConstant;
|
|
|
+import com.fjhx.entity.MessageEntity;
|
|
|
import com.fjhx.event.WebSocketOnMessageEvent;
|
|
|
+import com.fjhx.event.WebSocketOnOpenEvent;
|
|
|
+import com.fjhx.service.WebSocketServer;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springblade.core.tool.utils.ThreadUtil;
|
|
|
-import org.springframework.amqp.core.Message;
|
|
|
-import org.springframework.amqp.core.MessageBuilder;
|
|
|
-import org.springframework.amqp.core.MessageProperties;
|
|
|
-import org.springframework.amqp.rabbit.connection.CorrelationData;
|
|
|
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class WebSocketEventListener {
|
|
|
|
|
|
- @Resource
|
|
|
- private RabbitTemplate rabbitTemplate;
|
|
|
+// @Resource
|
|
|
+// private RabbitTemplate rabbitTemplate;
|
|
|
|
|
|
- private RabbitTemplate.ConfirmCallback confirmCallback;
|
|
|
+// private RabbitTemplate.ConfirmCallback confirmCallback;
|
|
|
|
|
|
|
|
|
@EventListener
|
|
|
- public void onMessageListener(WebSocketOnMessageEvent event) {
|
|
|
+ public void onMessageListener(WebSocketOnOpenEvent event) {
|
|
|
+ WebSocketServer source = event.getSource();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ source.sendMessage(WebSocketConstant.ON_OPEN_TYPE, jsonObject);
|
|
|
+ }
|
|
|
|
|
|
+
|
|
|
+ @EventListener
|
|
|
+ public void onMessageListener(WebSocketOnMessageEvent event) {
|
|
|
+ WebSocketServer webSocketServer = event.getSource();
|
|
|
String message = event.getMessage();
|
|
|
|
|
|
- JSONObject data = JSONObject.parseObject(message);
|
|
|
+ System.err.println(message);
|
|
|
|
|
|
- switch (WebSocketOnMessageTypeEnum.get(event.getType())) {
|
|
|
- case HANDHELD_MACHINE_RFID_SCANNING:
|
|
|
- handheldMachineRfidScanning(data, event.getUserId(), InstructTypeConstant.INSTRUCT_TYPE_2);
|
|
|
- break;
|
|
|
- case HANDHELD_MACHINE_RFID_CLOSE_SCANNING:
|
|
|
- handheldMachineRfidScanning(data, event.getUserId(), InstructTypeConstant.INSTRUCT_TYPE_3);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ MessageEntity sendEntity = JSONObject.parseObject(message, MessageEntity.class);
|
|
|
|
|
|
- /**
|
|
|
- * 一体机rfid扫描
|
|
|
- */
|
|
|
- private void handheldMachineRfidScanning(JSONObject data, String userId, Integer instructType) {
|
|
|
- Integer plcCode = data.getInteger("plcCode");
|
|
|
- Integer door = data.getInteger("door");
|
|
|
- Integer inOut = data.getInteger("inOut");
|
|
|
-
|
|
|
- JSONObject sendData = new JSONObject();
|
|
|
- List<String> ips = new ArrayList<>();
|
|
|
-
|
|
|
- // 赋值ip地址
|
|
|
- if (plcCode == 1 && door == 1 && inOut == 1) {
|
|
|
- ips.add("192.168.5.11");
|
|
|
- } else if (plcCode == 1 && door == 1 && inOut == 2) {
|
|
|
- ips.add("192.168.5.16");
|
|
|
- } else if (plcCode == 1 && door == 2 && inOut == 1) {
|
|
|
- ips.add("192.168.5.15");
|
|
|
- } else if (plcCode == 2 && door == 1 && inOut == 1) {
|
|
|
- ips.add("192.168.5.41");
|
|
|
- } else if (plcCode == 2 && door == 2 && inOut == 2) {
|
|
|
- ips.add("192.168.5.42");
|
|
|
- } else {
|
|
|
- return;
|
|
|
- }
|
|
|
+ Integer type = sendEntity.getType();
|
|
|
+ JSONObject data = sendEntity.getData();
|
|
|
|
|
|
- sendData.put("ips", ips);
|
|
|
- sendData.put("userId", userId);
|
|
|
- sendData.put("busUuid", userId);
|
|
|
- sendData.put("instructType", instructType);
|
|
|
+ switch (type) {
|
|
|
+ case WebSocketConstant.OPERATING_UPPER_COMPUTER:
|
|
|
|
|
|
- // 封装消息
|
|
|
- Message message = MessageBuilder.withBody(sendData.toJSONString().getBytes())
|
|
|
- .setContentType(MessageProperties.CONTENT_TYPE_JSON)
|
|
|
- .setContentEncoding("UTF-8")
|
|
|
- .build();
|
|
|
+ data.put(WebSocketConstant.FORWARD_USER_ID, webSocketServer.getUserId());
|
|
|
+ data.put(WebSocketConstant.FORWARD_SESSION_ID, webSocketServer.getSessionId());
|
|
|
|
|
|
- sendRabbit(message, 0);
|
|
|
- }
|
|
|
+ WebSocketServer.sendInfo(WebSocketConstant.WEB_STOCK_PROGRAM, type, data);
|
|
|
+ break;
|
|
|
|
|
|
+ case WebSocketConstant.PUSH_RFID:
|
|
|
+ WebSocketServer.sendInfo(sendEntity.getUserId(), sendEntity.getSessionId(), type, data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 发送消息到rabbitMq
|
|
|
- *
|
|
|
- * @param message 消息体
|
|
|
- * @param i 重试次数
|
|
|
- */
|
|
|
- private void sendRabbit(Message message, int i) {
|
|
|
- int j = ++i;
|
|
|
-
|
|
|
- // 发送消息
|
|
|
- this.rabbitTemplate.setMandatory(true);
|
|
|
- this.rabbitTemplate.setConfirmCallback(getConfirmCallback(j, message));
|
|
|
- this.rabbitTemplate.convertAndSend(RabbitConstant.RFID_EXCHANGE, RabbitConstant.INSTRUCTIONS_ROUTING_KEY,
|
|
|
- message, new CorrelationData(IdWorker.getIdStr()));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取回调方法
|
|
|
- */
|
|
|
- private synchronized RabbitTemplate.ConfirmCallback getConfirmCallback(int j, Message message) {
|
|
|
-
|
|
|
- if (confirmCallback == null) {
|
|
|
- confirmCallback = (CorrelationData correlationData, boolean ack, String cause) -> {
|
|
|
- // 重试机制
|
|
|
- if (!ack) {
|
|
|
- if (j < 3) {
|
|
|
- ThreadUtil.sleep(j * 300L);
|
|
|
- sendRabbit(message, j);
|
|
|
- log.error("rabbitMqCallback:一体机开启出入库rfid扫描消息推送重试,次数:" + j);
|
|
|
- } else {
|
|
|
- log.error("rabbitMqCallback:一体机开启出入库rfid扫描消息推送失败");
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.info("rabbitMqCallback:success");
|
|
|
- }
|
|
|
- };
|
|
|
- }
|
|
|
|
|
|
- return confirmCallback;
|
|
|
- }
|
|
|
+// /**
|
|
|
+// * 一体机rfid扫描
|
|
|
+// */
|
|
|
+// private void handheldMachineRfidScanning(JSONObject data, String userId, Integer instructType) {
|
|
|
+// Integer plcCode = data.getInteger("plcCode");
|
|
|
+// Integer door = data.getInteger("door");
|
|
|
+// Integer inOut = data.getInteger("inOut");
|
|
|
+//
|
|
|
+// JSONObject sendData = new JSONObject();
|
|
|
+// List<String> ips = new ArrayList<>();
|
|
|
+//
|
|
|
+// // 赋值ip地址
|
|
|
+// if (plcCode == 1 && door == 1 && inOut == 1) {
|
|
|
+// ips.add("192.168.5.11");
|
|
|
+// } else if (plcCode == 1 && door == 1 && inOut == 2) {
|
|
|
+// ips.add("192.168.5.16");
|
|
|
+// } else if (plcCode == 1 && door == 2 && inOut == 1) {
|
|
|
+// ips.add("192.168.5.15");
|
|
|
+// } else if (plcCode == 2 && door == 1 && inOut == 1) {
|
|
|
+// ips.add("192.168.5.41");
|
|
|
+// } else if (plcCode == 2 && door == 2 && inOut == 2) {
|
|
|
+// ips.add("192.168.5.42");
|
|
|
+// } else {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// sendData.put("ips", ips);
|
|
|
+// sendData.put("userId", userId);
|
|
|
+// sendData.put("busUuid", userId);
|
|
|
+// sendData.put("instructType", instructType);
|
|
|
+//
|
|
|
+// // 封装消息
|
|
|
+// Message message = MessageBuilder.withBody(sendData.toJSONString().getBytes())
|
|
|
+// .setContentType(MessageProperties.CONTENT_TYPE_JSON)
|
|
|
+// .setContentEncoding("UTF-8")
|
|
|
+// .build();
|
|
|
+//
|
|
|
+// sendRabbit(message, 0);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 发送消息到rabbitMq
|
|
|
+// *
|
|
|
+// * @param message 消息体
|
|
|
+// * @param i 重试次数
|
|
|
+// */
|
|
|
+// private void sendRabbit(Message message, int i) {
|
|
|
+// int j = ++i;
|
|
|
+//
|
|
|
+// // 发送消息
|
|
|
+// this.rabbitTemplate.setMandatory(true);
|
|
|
+// this.rabbitTemplate.setConfirmCallback(getConfirmCallback(j, message));
|
|
|
+// this.rabbitTemplate.convertAndSend(RabbitConstant.RFID_EXCHANGE, RabbitConstant.INSTRUCTIONS_ROUTING_KEY,
|
|
|
+// message, new CorrelationData(IdWorker.getIdStr()));
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 获取回调方法
|
|
|
+// */
|
|
|
+// private synchronized RabbitTemplate.ConfirmCallback getConfirmCallback(int j, Message message) {
|
|
|
+//
|
|
|
+// if (confirmCallback == null) {
|
|
|
+// confirmCallback = (CorrelationData correlationData, boolean ack, String cause) -> {
|
|
|
+// // 重试机制
|
|
|
+// if (!ack) {
|
|
|
+// if (j < 3) {
|
|
|
+// ThreadUtil.sleep(j * 300L);
|
|
|
+// sendRabbit(message, j);
|
|
|
+// log.error("rabbitMqCallback:一体机开启出入库rfid扫描消息推送重试,次数:" + j);
|
|
|
+// } else {
|
|
|
+// log.error("rabbitMqCallback:一体机开启出入库rfid扫描消息推送失败");
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// log.info("rabbitMqCallback:success");
|
|
|
+// }
|
|
|
+// };
|
|
|
+// }
|
|
|
+//
|
|
|
+// return confirmCallback;
|
|
|
+// }
|
|
|
|
|
|
|
|
|
}
|