干燥机配套车间生产管理系统/云平台服务端
zhuguifei
2024-11-29 339515558253d776769dc2e2560bbb4a0450c989
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//package org.jeecg.modules.dry.websocket;
//
//import cn.hutool.core.bean.BeanUtil;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import lombok.extern.slf4j.Slf4j;
//import org.jeecg.common.constant.DrySocketConst;
//import org.jeecg.common.util.SpringContextUtils;
//import org.jeecg.modules.dry.service.IDryRealTimeDataService;
//import org.jeecg.modules.dry.vo.RealTimeDataVo;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.websocket.*;
//import javax.websocket.server.PathParam;
//import javax.websocket.server.ServerEndpoint;
//import java.util.Collection;
//import java.util.HashMap;
//import java.util.Map;
//
//@Slf4j
//@Component
//@ServerEndpoint("/drySocket/{tenantId}/{machineId}")
//public class DrySocket {
//
//    /**
//     * 当前 session
//     */
//    private Session session;
//    /**
//     * 当前租户id
//     */
//    private String tenantId;
//    /**
//     * 设备id,用于标识同一租户,不同设备的数据
//     */
//    private String machineId;
//    /**
//     * 当前socket唯一id
//     */
//    private String socketId;
//
//    /**
//     * 租户连接池,包含单个租户的所有socket连接;
//     * 因为一个租户可能打开多个设备,多个设备就会有多个连接;
//     * key是tenantId,value是Map对象;子Map的key是machineId,value是drySocket对象
//     */
//    private  Map<String, Map<String, DrySocket>> tenantPool = new HashMap<>();
//    /**
//     * 连接池,包含所有WebSocket连接;
//     * key是socketId,value是drySocket对象
//     */
//    private  Map<String, DrySocket> machinePool = new HashMap<>();
//
//    /**
//     * 获取某个租户所有的设备
//     */
//    public  Map<String, DrySocket> getTenantPool(String tenantId) {
//        return tenantPool.computeIfAbsent(tenantId, k -> new HashMap<>(5));
//    }
//
//    /**
//     * 向当前租户发送消息
//     *
//     * @param message 消息内容
//     */
//    public void sendMessage(String message) {
//        try {
//            this.session.getAsyncRemote().sendText(message);
//        } catch (Exception e) {
//            log.error("【drySocket】消息发送失败:" + e.getMessage());
//        }
//    }
//
//    /**
//     * 封装消息json
//     *
//     * @param data 消息内容
//     */
//    public static String packageMessage(String type, Object data) {
//        JSONObject message = new JSONObject();
//        message.put(DrySocketConst.TYPE, type);
//        message.put(DrySocketConst.DATA, data);
//        return message.toJSONString();
//    }
//
//    /**
//     * 向指定租户的所有设备发送消息
//     *
//     * @param tenantId  接收消息的租户ID
//     * @param message 消息内容
//     */
//    public  void sendMessageTo(String tenantId, String message) {
//        Collection<DrySocket> values = getTenantPool(tenantId).values();
//        if (values.size() > 0) {
//            for (DrySocket socketItem : values) {
//                socketItem.sendMessage(message);
//            }
//        } else {
//            log.warn("【drySocket】消息发送失败:tenantId\"" + tenantId + "\"不存在或未在线!");
//        }
//    }
//
//    /**
//     * 向指定租户的指定设备发送消息
//     *
//     * @param tenantId  接收消息的租户ID
//     * @param message 消息内容
//     */
//    public  void sendMessageTo(String tenantId, String machineId, String message) {
//        DrySocket socketItem = getTenantPool(tenantId).get(machineId);
//        if (socketItem != null) {
//            socketItem.sendMessage(message);
//        } else {
//            log.warn("【drySocket】消息发送失败:tenantId\"" + tenantId + "\"的machineId\"" + machineId + "\"不存在或未在线!");
//        }
//    }
//
//    /**
//     * 向多个租户的所有设备发送消息
//     *
//     * @param tenantIds 接收消息的租户ID数组
//     * @param message 消息内容
//     */
//    public  void sendMessageTo(String[] tenantIds, String message) {
//        for (String tenantId : tenantIds) {
//            this.sendMessageTo(tenantId, message);
//        }
//    }
//
//    /**
//     * 向所有租户的所有设备发送消息
//     *
//     * @param message 消息内容
//     */
//    public  void sendMessageToAll(String message) {
//        for (DrySocket socketItem : machinePool.values()) {
//            socketItem.sendMessage(message);
//        }
//    }
//
//    /**
//     * websocket 开启连接
//     */
//    @OnOpen
//    public void onOpen(Session session, @PathParam("tenantId") String tenantId, @PathParam("machineId") String machineId) {
//        try {
//            this.tenantId = tenantId;
//            this.machineId = machineId;
//            this.socketId = tenantId + machineId;
//            this.session = session;
//
//            machinePool.put(this.socketId, this);
//            getTenantPool(tenantId).put(this.machineId, this);
//
//            log.info("【drySocket】有新的连接,总数为:" + machinePool.size());
//            log.info("ssss"+tenantPool.size());
//        } catch (Exception ignored) {
//        }
//    }
//
//    /**
//     * websocket 断开连接
//     */
//    @OnClose
//    public void onClose() {
//        try {
//            machinePool.remove(this.socketId);
//            getTenantPool(this.tenantId).remove(this.machineId);
//
//            log.info("【drySocket】连接断开,总数为:" + machinePool.size());
//        } catch (Exception ignored) {
//        }
//    }
//
//    /**
//     * websocket 收到消息
//     */
//    @OnMessage
//    public void onMessage(String message) {
//        log.info("【drySocket】onMessage:" + message);
//        IDryRealTimeDataService realTimeDataService = SpringContextUtils.getBean(IDryRealTimeDataService.class);
//        JSONObject json;
//        try {
//            json = JSON.parseObject(message);
//        } catch (Exception e) {
//            log.warn("【drySocket】收到不合法的消息:" + message);
//            return;
//        }
//        String type = json.getString(DrySocketConst.TYPE);
//        switch (type) {
//            // 心跳检测
//            case DrySocketConst.TYPE_HB:
//                this.sendMessage(DrySocket.packageMessage(type, true));
//                break;
//            // 实时数据处理
//            case DrySocketConst.TYPE_RDT:
//                Object o = json.get(DrySocketConst.DATA);
//                RealTimeDataVo realTimeDataVo = BeanUtil.toBean(o, RealTimeDataVo.class);
//                realTimeDataService.realTimeDataHandle(realTimeDataVo);
//                break;
//
//            default:
//                log.warn("【drySocket】收到不识别的消息类型:" + type);
//                break;
//        }
//
//
//    }
//
//    /** * 配置错误信息处理 * @param session * @param t */
//    @OnError
//    public void onError(Session session, Throwable t) {
//
////什么都不想打印都去掉就好了
//        log.info("【websocket消息】出现未知错误 ");
////打印错误信息,如果你不想打印错误信息,去掉就好了
////这里打印的也是 java.io.EOFException: null
//        t.printStackTrace();
//    }
//
//
//}