|
@@ -0,0 +1,236 @@
|
|
|
|
+package com.huimv.wine.ws;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.huimv.wine.entity.Device;
|
|
|
|
+import com.huimv.wine.entity.vo.WsEvent;
|
|
|
|
+import com.huimv.wine.mapper.DeviceMapper;
|
|
|
|
+import com.huimv.wine.utils.*;
|
|
|
|
+import kotlin.random.Random;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
|
|
+
|
|
|
|
+import javax.websocket.*;
|
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
|
+import javax.xml.crypto.Data;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.time.Duration;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * websocket接口处理类
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@ServerEndpoint(value = "/debugger/{id}")
|
|
|
|
+public class DebuggerController extends TextWebSocketHandler {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
+ /**
|
|
|
|
+ * 连接事件,加入注解
|
|
|
|
+ */
|
|
|
|
+ @OnOpen
|
|
|
|
+ public void onOpen(@PathParam(value = "id") String id, Session session) {
|
|
|
|
+ //需要判断id是否为空
|
|
|
|
+
|
|
|
|
+ // 添加到session的映射关系中
|
|
|
|
+ WebsocketDebuggerUtil.addSession(id, session);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 连接事件,加入注解
|
|
|
|
+ * 用户断开链接
|
|
|
|
+ */
|
|
|
|
+ @OnClose
|
|
|
|
+ public void onClose(@PathParam(value = "id") String id, Session session) {
|
|
|
|
+ // 删除映射关系
|
|
|
|
+ WebsocketWorkerUtil.removeSession(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 当接收到用户上传的消息
|
|
|
|
+ */
|
|
|
|
+ @OnMessage
|
|
|
|
+ public void onMessage(@PathParam(value = "id") String id, Session session, String message) {
|
|
|
|
+ // {event: "fesf", data: }
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(message);
|
|
|
|
+ String event = (String) jsonObject.get("event");
|
|
|
|
+ if ("pin".equals(event)) {
|
|
|
|
+ keepAlive(session);
|
|
|
|
+ } else if ("listDevices".equals(event)) {
|
|
|
|
+ listDevices(session);
|
|
|
|
+ } else if ("setDebug".equals(event)) {
|
|
|
|
+ setDebug(session, jsonObject.get("data"));
|
|
|
|
+ } else if ("openGate".equals(event)) {
|
|
|
|
+ openGate(session, id, jsonObject.get("data"));
|
|
|
|
+ } else if ("setLocation".equals(event)) {
|
|
|
|
+ setLocation(session,jsonObject.get("data"));
|
|
|
|
+ } else if ("setVpp".equals(event)) {
|
|
|
|
+ setVpp(session, jsonObject.get("data"));
|
|
|
|
+ } else {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("__Error_Event__", "unrecognized event");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 处理用户活连接异常
|
|
|
|
+ *
|
|
|
|
+ * @param session
|
|
|
|
+ * @param throwable
|
|
|
|
+ */
|
|
|
|
+ @OnError
|
|
|
|
+ public void onError(Session session, Throwable throwable) {
|
|
|
|
+ try {
|
|
|
|
+ session.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ throwable.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void keepAlive(Session session) {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("pon", null);
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void listDevices(Session session) {
|
|
|
|
+ Map<String, Session> sessions = WebsocketDebuggerUtil.getSessions();
|
|
|
|
+ List<JSONObject> devices = new ArrayList<>();
|
|
|
|
+ for (String s : sessions.keySet()) {
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ DeviceMapper deviceMapper = SpringContextUtil.getBean(DeviceMapper.class);
|
|
|
|
+ Device device = deviceMapper.getByDeviceId(s);
|
|
|
|
+ jsonObject.put("seq", device.getId());
|
|
|
|
+ jsonObject.put("online", true);
|
|
|
|
+ jsonObject.put("location", device.getAddr());
|
|
|
|
+ jsonObject.put("vpp1", device.getVpp1());
|
|
|
|
+ jsonObject.put("vpp2", device.getVpp2());
|
|
|
|
+ jsonObject.put("vpp3", device.getVpp3());
|
|
|
|
+ jsonObject.put("vpp4", device.getVpp4());
|
|
|
|
+ devices.add(jsonObject);
|
|
|
|
+ }
|
|
|
|
+ WsEvent wsEvent = new WsEvent("devices", devices);
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setDebug(Session session,Object data) {
|
|
|
|
+ JSONObject jsonObject = (JSONObject) data;
|
|
|
|
+ String seq = jsonObject.getString("seq");
|
|
|
|
+ Boolean debug = jsonObject.getBoolean("debug");
|
|
|
|
+ Map<String, Session> sessions = WebsocketDebuggerUtil.getSessions();
|
|
|
|
+ boolean key = sessions.containsKey(seq);
|
|
|
|
+ if (key) {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("setDebug", debug);
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(sessions.get("seq"), wsEvent);
|
|
|
|
+ } else {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("__Error_Event__", "device offline/no such device");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void openGate(Session session,String id, Object data) {
|
|
|
|
+ JSONObject jsonObject = (JSONObject) data;
|
|
|
|
+ String seq = jsonObject.getString("seq");
|
|
|
|
+ String kind = jsonObject.getString("kind");
|
|
|
|
+ Map<String, Session> sessions = WebsocketDebuggerUtil.getSessions();
|
|
|
|
+ boolean key = sessions.containsKey(seq);
|
|
|
|
+ if (key) {
|
|
|
|
+ JSONObject jsonObject1 = new JSONObject();
|
|
|
|
+ if (kind.equals("Changing")) {
|
|
|
|
+ kind = "Change";
|
|
|
|
+ } else if (kind.equals("Fixing")) {
|
|
|
|
+ kind = "Fix";
|
|
|
|
+ } else {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("authCode", "error open type");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String key1 = String.format("%s_%s_%s", kind, seq, id);
|
|
|
|
+ Boolean hasKey = redisTemplate.hasKey(key1);
|
|
|
|
+ String formattedNumber = "";
|
|
|
|
+ if (hasKey) {
|
|
|
|
+ redisTemplate.opsForValue().getAndExpire(key1, Duration.ofSeconds(30));
|
|
|
|
+ } else {
|
|
|
|
+ int nextInt = ThreadLocalRandom.current().nextInt(1000000);
|
|
|
|
+ formattedNumber = String.format("%06d", nextInt);
|
|
|
|
+ System.out.println("随机六位数:" + formattedNumber);
|
|
|
|
+ redisTemplate.opsForValue().set(key1,formattedNumber, Duration.ofSeconds(30));
|
|
|
|
+ }
|
|
|
|
+ jsonObject1.put("type", kind);
|
|
|
|
+ jsonObject1.put("user_type", "debugger");
|
|
|
|
+ jsonObject1.put("user_id", id);
|
|
|
|
+ WsEvent wsEvent = new WsEvent("openGate", jsonObject1);
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(sessions.get("seq"), wsEvent);
|
|
|
|
+ WsEvent wsEvent1 = new WsEvent("authCode", new Result(10000, formattedNumber, true));
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent1);
|
|
|
|
+ } else {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("authCode", "device offline");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setLocation(Session session,Object data) {
|
|
|
|
+ JSONObject jsonObject = (JSONObject) data;
|
|
|
|
+ String seq = jsonObject.getString("seq");
|
|
|
|
+ String loc = jsonObject.getString("loc");
|
|
|
|
+ DeviceMapper deviceMapper = SpringContextUtil.getBean(DeviceMapper.class);
|
|
|
|
+ Device device = deviceMapper.getByDeviceId(seq);
|
|
|
|
+ device.setAddr(loc);
|
|
|
|
+ int i = deviceMapper.updateById(device);
|
|
|
|
+ if (i == 0) {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("__Error_Event__", "update location failed");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ } else {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("locationUpdated", null);
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public void setVpp(Session session,Object data) {
|
|
|
|
+ JSONObject jsonObject = (JSONObject) data;
|
|
|
|
+ String seq = jsonObject.getString("seq");
|
|
|
|
+ DeviceMapper deviceMapper = SpringContextUtil.getBean(DeviceMapper.class);
|
|
|
|
+ Device device = deviceMapper.getByDeviceId(seq);
|
|
|
|
+ Integer value = jsonObject.getInteger("value");
|
|
|
|
+ Integer index = jsonObject.getInteger("index");
|
|
|
|
+ switch (index){
|
|
|
|
+ case 0:
|
|
|
|
+ device.setVpp1(value);
|
|
|
|
+ case 1:
|
|
|
|
+ device.setVpp1(value);
|
|
|
|
+ case 2:
|
|
|
|
+ device.setVpp1(value);
|
|
|
|
+ case 3:
|
|
|
|
+ device.setVpp1(value);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int update = deviceMapper.updateById(device);
|
|
|
|
+ if (update == 0) {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("__Error_Event__", "update vpp failed");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Map<String, Session> sessions = WebsocketSellerUtil.getSession();
|
|
|
|
+ boolean key = sessions.containsKey(seq);
|
|
|
|
+ if (key) {
|
|
|
|
+ JSONObject jsonObject1 = new JSONObject();
|
|
|
|
+ jsonObject1.put("value", value);
|
|
|
|
+ jsonObject1.put("index", index);
|
|
|
|
+ WsEvent wsEvent = new WsEvent("vppUpdate", jsonObject1);
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(sessions.get("seq"), wsEvent);
|
|
|
|
+ } else {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("__Error_Event__", "device offline/no such device");
|
|
|
|
+ WebsocketDebuggerUtil.sendMessage(session, wsEvent);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|