parent
9cf368cb1e
commit
c9227526a5
@ -0,0 +1,100 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class Message {
|
||||||
|
private String code;
|
||||||
|
private Msg msg;
|
||||||
|
@JSONField(serialize = false)
|
||||||
|
private CodeEnum codeEx;
|
||||||
|
@JSONField(serialize = false)
|
||||||
|
private Instructions instructions;
|
||||||
|
|
||||||
|
public static Message reg(String id){
|
||||||
|
Message message = new Message();
|
||||||
|
message.setCode(CodeEnum.REG.getCode());
|
||||||
|
message.setMsg(Msg.reg(id));
|
||||||
|
message.formatCodeToEnum(CodeEnum.REG.getCode());
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Message heart(String id){
|
||||||
|
Message message = new Message();
|
||||||
|
message.setCode(CodeEnum.HEART.getCode());
|
||||||
|
message.setMsg(Msg.reg(id));
|
||||||
|
message.formatCodeToEnum(CodeEnum.HEART.getCode());
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void formatInstructions(){
|
||||||
|
if (this.getCodeEx()==CodeEnum.ZX){
|
||||||
|
Instructions.valueOf(this.msg.getResult());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void formatCodeToEnum(@NonNull String code){
|
||||||
|
for (CodeEnum e:CodeEnum.values()){
|
||||||
|
if (e.getCode().equals(code)){
|
||||||
|
this.codeEx=e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public CodeEnum getEnumByCode(@NonNull String code){
|
||||||
|
for (CodeEnum e:CodeEnum.values()){
|
||||||
|
if (e.getCode().equals(code)){
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum CodeEnum{
|
||||||
|
REG("9000"),
|
||||||
|
ZX("3000"),
|
||||||
|
RESULT("9002"),
|
||||||
|
HEART("9003"),
|
||||||
|
;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Instructions{
|
||||||
|
大船大船,开始派工,开始报工,确认完毕
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class Msg{
|
||||||
|
private String erpId;
|
||||||
|
private String orderNumber;
|
||||||
|
private String actionType;
|
||||||
|
private String time;
|
||||||
|
private String deviceId;
|
||||||
|
private String result;
|
||||||
|
|
||||||
|
private static Msg reg(String id){
|
||||||
|
Msg msg = new Msg();
|
||||||
|
msg.setErpId(id);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject toJson(){
|
||||||
|
return (JSONObject) JSONObject.toJSON(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return JSONObject.toJSONString(this);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient;
|
||||||
|
|
||||||
|
|
||||||
|
import com.dsic.gj_erp.wsclient.handler.HeartHandler;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class WebSocketClientComponent {
|
||||||
|
|
||||||
|
@Value("${ws.client.url}")
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
@Value("${ws.client.id}")
|
||||||
|
private String erpId;
|
||||||
|
|
||||||
|
private final WebSocketService webSocketService;
|
||||||
|
private final HeartHandler heartHandler;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
// 发送消息示例
|
||||||
|
try {
|
||||||
|
webSocketService.connect(this.url);
|
||||||
|
webSocketService.sendMessage(Message.reg(this.erpId).toString());
|
||||||
|
heartHandler.execute(Message.heart(erpId));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.client.WebSocketClient;
|
||||||
|
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfig {
|
||||||
|
@Bean
|
||||||
|
public WebSocketClient webSocketClient() {
|
||||||
|
return new StandardWebSocketClient();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.socket.TextMessage;
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
import org.springframework.web.socket.client.WebSocketClient;
|
||||||
|
import org.springframework.web.socket.client.WebSocketConnectionManager;
|
||||||
|
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class WebSocketService extends TextWebSocketHandler {
|
||||||
|
|
||||||
|
private WebSocketSession session;
|
||||||
|
|
||||||
|
private final WebSocketClient client;
|
||||||
|
|
||||||
|
|
||||||
|
public void connect(String uri) {
|
||||||
|
WebSocketConnectionManager manager = new WebSocketConnectionManager(client, this, uri);
|
||||||
|
manager.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect() throws Exception {
|
||||||
|
if (session != null && session.isOpen()) {
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String message) throws Exception {
|
||||||
|
if (session != null && session.isOpen()) {
|
||||||
|
session.sendMessage(new TextMessage(message));
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException("WebSocket is not connected.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||||
|
this.session = session; // 处理连接后保存session
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||||
|
System.out.println("Received: " + message.getPayload());
|
||||||
|
Message msg= JSONObject.parseObject(message.getPayload(),Message.class);
|
||||||
|
switch (msg.getCodeEx()){
|
||||||
|
case REG:
|
||||||
|
break;
|
||||||
|
case ZX:
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient.handler;
|
||||||
|
|
||||||
|
import cn.hutool.cron.CronUtil;
|
||||||
|
import cn.hutool.cron.task.Task;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.dsic.gj_erp.wsclient.Message;
|
||||||
|
import com.dsic.gj_erp.wsclient.WebSocketService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class HeartHandler implements IHandlerService{
|
||||||
|
|
||||||
|
private final WebSocketService webSocketService;
|
||||||
|
|
||||||
|
private boolean scheduleStartFlag=false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Message message) {
|
||||||
|
if (this.scheduleStartFlag){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CronUtil.schedule("*/30 * * * * *", (Task) () -> {
|
||||||
|
try {
|
||||||
|
webSocketService.sendMessage(message.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 支持秒级别定时任务
|
||||||
|
CronUtil.setMatchSecond(true);
|
||||||
|
CronUtil.start();
|
||||||
|
this.scheduleStartFlag=true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient.handler;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.dsic.gj_erp.wsclient.Message;
|
||||||
|
|
||||||
|
public interface IHandlerService {
|
||||||
|
|
||||||
|
void execute(Message message);
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.dsic.gj_erp.wsclient.handler;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.dsic.gj_erp.bean.jhgk.DmYdjh;
|
||||||
|
import com.dsic.gj_erp.service.jhgk.DmYdjhService;
|
||||||
|
import com.dsic.gj_erp.ws.WsHandler;
|
||||||
|
import com.dsic.gj_erp.wsclient.Message;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ZxHandler implements IHandlerService{
|
||||||
|
|
||||||
|
private final DmYdjhService ydjhService;
|
||||||
|
private final WsHandler wsHandler;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Message message) {
|
||||||
|
String djh=message.getMsg().getOrderNumber();
|
||||||
|
DmYdjh one = ydjhService.getOne(Wrappers.<DmYdjh>lambdaQuery().eq(DmYdjh::getDjh, djh));
|
||||||
|
switch (message.getInstructions()){
|
||||||
|
case 开始派工:
|
||||||
|
case 开始报工:
|
||||||
|
JSONObject object=new JSONObject();
|
||||||
|
object.put("zyjh",one);
|
||||||
|
wsHandler.whoIsSelect(object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue