|
@@ -0,0 +1,261 @@
|
|
|
+package com.persagy.ibms.data.sdk.service.websocket.server;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.persagy.ibms.core.data.SceneDataObject;
|
|
|
+import com.persagy.ibms.core.data.SceneDataSet;
|
|
|
+import com.persagy.ibms.core.data.SceneDataValue;
|
|
|
+import com.persagy.ibms.core.util.RWDUtil;
|
|
|
+import com.persagy.ibms.data.sdk.util.ObjectInfo;
|
|
|
+import com.persagy.ibms.data.sdk.util.RepositoryContainer;
|
|
|
+import com.persagy.ibms.data.sdk.util.RepositoryImpl;
|
|
|
+
|
|
|
+import cn.hutool.core.thread.ExecutorBuilder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class WebSocketUtil {
|
|
|
+ public static ExecutorService executor = ExecutorBuilder.create().setCorePoolSize(4).setMaxPoolSize(10)
|
|
|
+ .setWorkQueue(new LinkedBlockingQueue<>(102400)).setHandler(new ThreadPoolExecutor.AbortPolicy()).build();
|
|
|
+ public static Map<String, Object> idMap = new ConcurrentHashMap<String, Object>();
|
|
|
+
|
|
|
+ public static Map<String, Map<String, Boolean>> objId2idList = new ConcurrentHashMap<String, Map<String, Boolean>>();
|
|
|
+ public static Map<String, Map<String, Boolean>> id2objIdList = new ConcurrentHashMap<String, Map<String, Boolean>>();
|
|
|
+
|
|
|
+ public static Map<String, Map<String, Boolean>> objInfoId2idList = new ConcurrentHashMap<String, Map<String, Boolean>>();
|
|
|
+ public static Map<String, Map<String, Boolean>> id2objInfoIdList = new ConcurrentHashMap<String, Map<String, Boolean>>();
|
|
|
+
|
|
|
+ public static synchronized void ProcessConnected(String id, Object ContentJSON) {
|
|
|
+ idMap.put(id, ContentJSON);
|
|
|
+ Map<String, Boolean> objIdList = new ConcurrentHashMap<String, Boolean>();
|
|
|
+ id2objIdList.put(id, objIdList);
|
|
|
+ Map<String, Boolean> objInfoIdList = new ConcurrentHashMap<String, Boolean>();
|
|
|
+ id2objInfoIdList.put(id, objInfoIdList);
|
|
|
+ JSONArray objArray = (JSONArray) ContentJSON;
|
|
|
+ for (int i = 0; i < objArray.size(); i++) {
|
|
|
+ JSONObject objJSON = objArray.getJSONObject(i);
|
|
|
+ String objId = (String) objJSON.get("objId");
|
|
|
+ if (objJSON.containsKey("infoCodeArray")) {
|
|
|
+ JSONArray infoCodeArray = (JSONArray) objJSON.get("infoCodeArray");
|
|
|
+ for (int ii = 0; ii < infoCodeArray.size(); ii++) {
|
|
|
+ String infoCode = (String) infoCodeArray.get(ii);
|
|
|
+ String objInfoId = objId + "-" + infoCode;
|
|
|
+ if (!objInfoId2idList.containsKey(objInfoId)) {
|
|
|
+ objInfoId2idList.put(objInfoId, new ConcurrentHashMap<String, Boolean>());
|
|
|
+ }
|
|
|
+ objInfoId2idList.get(objInfoId).put(id, true);
|
|
|
+ objInfoIdList.put(objInfoId, true);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!objId2idList.containsKey(objId)) {
|
|
|
+ objId2idList.put(objId, new ConcurrentHashMap<String, Boolean>());
|
|
|
+ }
|
|
|
+ objId2idList.get(objId).put(id, true);
|
|
|
+ objIdList.put(objId, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ProcessFirstSend(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static synchronized void ProcessDisconnected(String id) {
|
|
|
+ for (String key : id2objIdList.keySet()) {
|
|
|
+ Map<String, Boolean> objIdList = id2objIdList.get(key);
|
|
|
+ for (String objId : objIdList.keySet()) {
|
|
|
+ Map<String, Boolean> idList = objId2idList.get(objId);
|
|
|
+ if (idList.containsKey(id)) {
|
|
|
+ idList.remove(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ id2objIdList.remove(id);
|
|
|
+
|
|
|
+ for (String key : id2objInfoIdList.keySet()) {
|
|
|
+ Map<String, Boolean> objInfoIdList = id2objInfoIdList.get(key);
|
|
|
+ for (String objInfoId : objInfoIdList.keySet()) {
|
|
|
+ Map<String, Boolean> idList = objInfoId2idList.get(objInfoId);
|
|
|
+ if (idList.containsKey(id)) {
|
|
|
+ idList.remove(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ id2objInfoIdList.remove(id);
|
|
|
+
|
|
|
+ idMap.remove(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void ProcessFirstSend(String id) {
|
|
|
+ try {
|
|
|
+ Runnable runnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ RepositoryImpl Repository = RepositoryContainer.instance;
|
|
|
+
|
|
|
+ JSONArray sendArray = new JSONArray();
|
|
|
+ Map<String, JSONObject> sendObj = new HashMap<String, JSONObject>();
|
|
|
+ Map<String, Boolean> objIdList = id2objIdList.get(id);
|
|
|
+ Map<String, Boolean> objInfoIdList = id2objInfoIdList.get(id);
|
|
|
+ for (String objId : objIdList.keySet()) {
|
|
|
+ if (Repository.id2sdv.containsKey(objId)) {
|
|
|
+ SceneDataObject sdo = Repository.id2sdv.get(objId);
|
|
|
+ String classCode = (String) sdo.get("classCode").value_prim.value;
|
|
|
+ SceneDataSet infoArray = Repository.infoArrayDic.get(classCode);
|
|
|
+ String[] infoCodes = sdo.keySet().toArray(new String[0]);
|
|
|
+ for (String infoCode : infoCodes) {
|
|
|
+ SceneDataValue infoValue = sdo.get(infoCode);
|
|
|
+ if ((RWDUtil.isRunParam(infoArray.set, infoCode) || RWDUtil.isSetParam(infoArray.set, infoCode))
|
|
|
+ && infoValue.value_prim != null && infoValue.value_prim.value != null) {
|
|
|
+ if (!sendObj.containsKey(objId)) {
|
|
|
+ sendObj.put(objId, new JSONObject());
|
|
|
+ }
|
|
|
+ JSONObject sendItem = sendObj.get(objId);
|
|
|
+ sendItem.put(infoCode, infoValue.value_prim.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String objInfoId : objInfoIdList.keySet()) {
|
|
|
+ int index_ = objInfoId.indexOf('-');
|
|
|
+ String objId = objInfoId.substring(0, index_);
|
|
|
+ String infoCode = objInfoId.substring(index_ + 1);
|
|
|
+ if (Repository.id2sdv.containsKey(objId)) {
|
|
|
+ SceneDataObject sdo = Repository.id2sdv.get(objId);
|
|
|
+ String classCode = (String) sdo.get("classCode").value_prim.value;
|
|
|
+ SceneDataSet infoArray = Repository.infoArrayDic.get(classCode);
|
|
|
+ {
|
|
|
+ SceneDataValue infoValue = sdo.get(infoCode);
|
|
|
+ if ((RWDUtil.isRunParam(infoArray.set, infoCode) || RWDUtil.isSetParam(infoArray.set, infoCode))
|
|
|
+ && infoValue.value_prim != null && infoValue.value_prim.value != null) {
|
|
|
+ if (!sendObj.containsKey(objId)) {
|
|
|
+ sendObj.put(objId, new JSONObject());
|
|
|
+ }
|
|
|
+ JSONObject sendItem = sendObj.get(objId);
|
|
|
+ sendItem.put(infoCode, infoValue.value_prim.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String objId : sendObj.keySet()) {
|
|
|
+ JSONObject sendItem = sendObj.get(objId);
|
|
|
+ sendItem.put("id", objId);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ }
|
|
|
+ if (sendArray.size() > 0) {
|
|
|
+ WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ WebSocketUtil.executor.execute(new Thread(runnable));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("handlerSubMessage", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void ProcessIOTReceived(JSONObject json) {
|
|
|
+ try {
|
|
|
+ Runnable runnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ RepositoryImpl Repository = RepositoryContainer.instance;
|
|
|
+ String type = json.getString("type");
|
|
|
+ if (type.equals("iot") || type.equals("text")) {
|
|
|
+ String message = json.getString("data");
|
|
|
+ String[] splits = message.split(";");
|
|
|
+ for (String id : idMap.keySet()) {
|
|
|
+ try {
|
|
|
+ Map<String, Boolean> objIdList = id2objIdList.get(id);
|
|
|
+ Map<String, Boolean> objInfoIdList = id2objInfoIdList.get(id);
|
|
|
+ JSONArray sendArray = new JSONArray();
|
|
|
+ Map<String, JSONObject> sendObj = new HashMap<String, JSONObject>();
|
|
|
+ for (int i = 0; i < splits.length; i += 4) {
|
|
|
+ // String time = splits[i + 0];
|
|
|
+ String meter = splits[i + 1];
|
|
|
+ String funcid = splits[i + 2];
|
|
|
+ String value = splits[i + 3];
|
|
|
+ String point = meter + "-" + funcid;
|
|
|
+ if (!Repository.point2ObjectInfoList.containsKey(point)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<ObjectInfo> ObjectInfoList = Repository.point2ObjectInfoList.get(point);
|
|
|
+ for (ObjectInfo ObjectInfo : ObjectInfoList) {
|
|
|
+ if (objIdList.containsKey(ObjectInfo.objId)
|
|
|
+ || objInfoIdList.containsKey(ObjectInfo.objId + "" + ObjectInfo.infoCode)) {
|
|
|
+ if (!sendObj.containsKey(ObjectInfo.objId)) {
|
|
|
+ sendObj.put(ObjectInfo.objId, new JSONObject());
|
|
|
+ }
|
|
|
+ JSONObject sendItem = sendObj.get(ObjectInfo.objId);
|
|
|
+ sendItem.put(ObjectInfo.infoCode, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String objId : sendObj.keySet()) {
|
|
|
+ JSONObject sendItem = sendObj.get(objId);
|
|
|
+ sendItem.put("id", objId);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ }
|
|
|
+ if (sendArray.size() > 0) {
|
|
|
+ WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (type.equals("pointset")) {
|
|
|
+ String message = json.getString("data");
|
|
|
+ String[] splits = message.split(";");
|
|
|
+ for (String id : idMap.keySet()) {
|
|
|
+ try {
|
|
|
+ Map<String, Boolean> objIdList = id2objIdList.get(id);
|
|
|
+ Map<String, Boolean> objInfoIdList = id2objInfoIdList.get(id);
|
|
|
+ JSONArray sendArray = new JSONArray();
|
|
|
+ Map<String, JSONObject> sendObj = new HashMap<String, JSONObject>();
|
|
|
+ for (int i = 0; i < splits.length; i += 4) {
|
|
|
+ // String time = splits[i + 0];
|
|
|
+ String meter = splits[i + 1];
|
|
|
+ String funcid = splits[i + 2];
|
|
|
+ String value = splits[i + 3];
|
|
|
+ String point = meter + "-" + funcid;
|
|
|
+ if (!Repository.set2ObjectInfoList.containsKey(point)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<ObjectInfo> ObjectInfoList = Repository.set2ObjectInfoList.get(point);
|
|
|
+ for (ObjectInfo ObjectInfo : ObjectInfoList) {
|
|
|
+ if (objIdList.containsKey(ObjectInfo.objId)
|
|
|
+ || objInfoIdList.containsKey(ObjectInfo.objId + "" + ObjectInfo.infoCode)) {
|
|
|
+ if (!sendObj.containsKey(ObjectInfo.objId)) {
|
|
|
+ sendObj.put(ObjectInfo.objId, new JSONObject());
|
|
|
+ }
|
|
|
+ JSONObject sendItem = sendObj.get(ObjectInfo.objId);
|
|
|
+ sendItem.put(ObjectInfo.infoCode, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String objId : sendObj.keySet()) {
|
|
|
+ JSONObject sendItem = sendObj.get(objId);
|
|
|
+ sendItem.put("id", objId);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ }
|
|
|
+ if (sendArray.size() > 0) {
|
|
|
+ WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ WebSocketUtil.executor.execute(new Thread(runnable));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("handlerSubMessage", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|