ÎļþÃû´Ó src/main/java/com/fzzy/gateway/hx2023/websocket/WebSocketDevice.java ÐÞ¸Ä |
| | |
| | | package com.fzzy.gateway.hx2023.websocket; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.gateway.hx2023.data.WebSocketPacket; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | @Slf4j |
| | | @Component |
| | | @ServerEndpoint(value = "/device/{productId}/{deviceId}/message/property/report") |
| | | public class WebSocketDevice { |
| | | public class WebSocketDeviceReport { |
| | | |
| | | private static Map<String, Session> sessionPool = new ConcurrentHashMap<>(); |
| | | private static Map<String, String> sessionIds = new ConcurrentHashMap<>(); |
| | |
| | | sessionPool.put(key, session); |
| | | sessionIds.put(session.getId(), key); |
| | | |
| | | ScConstant.updateOnline(deviceId); |
| | | GatewayUtils.updateOnline(deviceId); |
| | | |
| | | log.info("new webSocket,clientId={}", key); |
| | | } |
| | |
| | | |
| | | String key = sessionIds.get(session.getId()); |
| | | |
| | | String deviceId = key.substring(0, key.indexOf("-")); |
| | | String deviceId = key.substring(key.indexOf("-")); |
| | | |
| | | ScConstant.updateOffOnline(deviceId); |
| | | GatewayUtils.updateOffOnline(deviceId); |
| | | |
| | | sessionPool.remove(key); |
| | | sessionIds.remove(session.getId()); |
| | |
| | | |
| | | String key = sessionIds.get(session.getId()); |
| | | |
| | | String deviceId = key.substring(0, key.indexOf("-")); |
| | | String deviceId = key.substring(key.indexOf("-")); |
| | | |
| | | ScConstant.updateOffOnline(deviceId); |
| | | GatewayUtils.updateOffOnline(deviceId); |
| | | |
| | | sessionPool.remove(key); |
| | | sessionIds.remove(session.getId()); |
| | |
| | | * |
| | | * @param packet |
| | | */ |
| | | public static void sendByPacket(WebSocketPacket packet) { |
| | | public void sendByPacket(WebSocketPacket packet) { |
| | | if (StringUtils.isEmpty(packet.getDeviceId())) { |
| | | log.error("WebSocketä¿¡æ¯æ¨é失败ï¼è®¾å¤ç¼ç 为空ã"); |
| | | return; |
| | | } |
| | | |
| | | |
| | | String tag = packet.getDeviceId(); |
| | | |
| | | // é忍é |
| | | Session session; |
| | | String productId; |
| | | for (String key : sessionPool.keySet()) { |
| | | |
| | | productId = key.substring(0, key.indexOf("-")); |
| | | |
| | | packet.getHeaders().setProductId(productId); |
| | | |
| | | if (key.indexOf(tag) != -1) { |
| | | session = sessionPool.get(key); |
| | | session.getAsyncRemote().sendText( |