|
@@ -3,17 +3,18 @@ package com.persagy.ibms.data.sdk.service.websocket.server;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
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.ComputeUtil;
|
|
|
+import com.persagy.ibms.core.util.KeywordUtil;
|
|
|
import com.persagy.ibms.core.util.PathUtil;
|
|
|
import com.persagy.ibms.core.util.RWDUtil;
|
|
|
import com.persagy.ibms.data.sdk.util.ObjectInfo;
|
|
@@ -38,7 +39,7 @@ public class WebSocketUtil {
|
|
|
public static Map<String, Map<String, Object>> id2objId2Tag = new ConcurrentHashMap<String, Map<String, Object>>();
|
|
|
|
|
|
public static Map<String, Map<String, Boolean>> path2idList = new ConcurrentHashMap<String, Map<String, Boolean>>();
|
|
|
- public static Map<String, Map<String, Boolean>> id2pathList = new ConcurrentHashMap<String, Map<String, Boolean>>();
|
|
|
+ public static Map<String, List<JSONArray>> id2pathList = new ConcurrentHashMap<String, List<JSONArray>>();
|
|
|
|
|
|
public static synchronized void ProcessReceive(String id, Object ContentJSON) {
|
|
|
ProcessDisconnected(id);
|
|
@@ -50,7 +51,7 @@ public class WebSocketUtil {
|
|
|
id2objInfoIdList.put(id, objInfoIdList);
|
|
|
Map<String, Object> objId2Tag = new ConcurrentHashMap<String, Object>();
|
|
|
id2objId2Tag.put(id, objId2Tag);
|
|
|
- Map<String, Boolean> pathList = new ConcurrentHashMap<String, Boolean>();
|
|
|
+ List<JSONArray> pathList = new CopyOnWriteArrayList<JSONArray>();
|
|
|
id2pathList.put(id, pathList);
|
|
|
|
|
|
JSONArray objArray = (JSONArray) ContentJSON;
|
|
@@ -78,7 +79,7 @@ public class WebSocketUtil {
|
|
|
}
|
|
|
} else if (objJSON.containsKey("path")) {
|
|
|
JSONArray pathArray = (JSONArray) objJSON.get("path");
|
|
|
- pathList.putIfAbsent(pathArray.toJSONString(), true);
|
|
|
+ pathList.add(pathArray);
|
|
|
path2idList.putIfAbsent(pathArray.toJSONString(), new ConcurrentHashMap<String, Boolean>());
|
|
|
path2idList.get(pathArray.toJSONString()).putIfAbsent(id, true);
|
|
|
}
|
|
@@ -174,33 +175,15 @@ public class WebSocketUtil {
|
|
|
}
|
|
|
sendArray.add(sendItem);
|
|
|
// 发送
|
|
|
- WebSocketChannelPool.Send(id, sendArray);
|
|
|
- sendArray.clear();
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
}
|
|
|
|
|
|
- Map<String, Boolean> pathList = id2pathList.get(id);
|
|
|
- for (String path : pathList.keySet()) {
|
|
|
- JSONArray pathArray = JSON.parseArray(path);
|
|
|
- Object valueObject = ComputeUtil.getValueObject(Repository, pathArray);
|
|
|
- if (valueObject instanceof SceneDataValue) {
|
|
|
- SceneDataValue currData = (SceneDataValue) valueObject;
|
|
|
- int depth = 1;
|
|
|
- if (currData.rel_property != null) {
|
|
|
- depth = Integer.parseInt(currData.rel_property.read_level);
|
|
|
- }
|
|
|
- Object data = currData.toJSON(true, depth);
|
|
|
- JSONObject sendItem = new JSONObject();
|
|
|
- sendItem.put("path", pathArray);
|
|
|
- sendItem.put("data", data);
|
|
|
- sendArray.add(sendItem);
|
|
|
- // 发送
|
|
|
- WebSocketChannelPool.Send(id, sendArray);
|
|
|
- sendArray.clear();
|
|
|
- }
|
|
|
- }
|
|
|
+ // path
|
|
|
+ // ProcessFirstSend_path(Repository, id, sendArray);
|
|
|
+ ProcessFirstSend_path(Repository.objectData, id, sendArray);
|
|
|
|
|
|
if (sendArray.size() > 0) {
|
|
|
- WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
@@ -210,29 +193,207 @@ public class WebSocketUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void ProcessComputeOccur(SceneDataValue sdv) {
|
|
|
- try {
|
|
|
- JSONArray pathArray = new JSONArray();
|
|
|
- PathUtil.getDataPath(sdv, pathArray);
|
|
|
- if (path2idList.containsKey(pathArray.toString())) {
|
|
|
- Map<String, Boolean> idList = path2idList.get(pathArray.toString());
|
|
|
- JSONArray sendArray = new JSONArray();
|
|
|
+ // 严格匹配路径,已废弃
|
|
|
+ public static void ProcessFirstSend_path(RepositoryImpl Repository, String id, JSONArray sendArray) {
|
|
|
+ List<JSONArray> pathList = id2pathList.get(id);
|
|
|
+ for (JSONArray pathArray : pathList) {
|
|
|
+ Object valueObject = ComputeUtil.getValueObject(Repository, pathArray);
|
|
|
+ if (valueObject instanceof SceneDataValue) {
|
|
|
+ SceneDataValue currData = (SceneDataValue) valueObject;
|
|
|
+ int depth = 1;
|
|
|
+ // if (currData.rel_property != null) {
|
|
|
+ // depth = Integer.parseInt(currData.rel_property.read_level);
|
|
|
+ // }
|
|
|
+ Object data = currData.toJSON(true, depth);
|
|
|
JSONObject sendItem = new JSONObject();
|
|
|
sendItem.put("path", pathArray);
|
|
|
- int depth = 1;
|
|
|
- if (sdv.rel_property != null) {
|
|
|
- depth = Integer.parseInt(sdv.rel_property.read_level);
|
|
|
- }
|
|
|
- Object data = sdv.toJSON(true, depth);
|
|
|
sendItem.put("data", data);
|
|
|
sendArray.add(sendItem);
|
|
|
- for (String id : idList.keySet()) {
|
|
|
- WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ // 发送
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 支持路径通配符*
|
|
|
+ public static void ProcessFirstSend_path(SceneDataObject objectData, String id, JSONArray sendArray) {
|
|
|
+ for (String key : objectData.keySetSelf()) {
|
|
|
+ if (KeywordUtil.containsKey(key)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ SceneDataValue sdv = objectData.get(key);
|
|
|
+ ProcessFirstSend_path(sdv, id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 支持路径通配符*
|
|
|
+ public static void ProcessFirstSend_path(SceneDataValue sdv, String id, JSONArray sendArray) {
|
|
|
+ List<JSONArray> pathList = id2pathList.get(id);
|
|
|
+ try {
|
|
|
+ if (sdv.rel_property != null) {
|
|
|
+ if (sdv.rel_property.propertyValueSchema.equals("JSONObject")) {
|
|
|
+ if (sdv.value_object != null) {
|
|
|
+ JSONArray pathArray = new JSONArray();
|
|
|
+ PathUtil.getDataPath(sdv, pathArray);
|
|
|
+ if (pathMatch(pathList, pathArray)) {
|
|
|
+ Object JSON = sdv.value_object.toJSON(1);
|
|
|
+ JSONObject sendItem = new JSONObject();
|
|
|
+ sendItem.put("path", pathArray);
|
|
|
+ sendItem.put("data", JSON);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ // 发送
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
+ }
|
|
|
+ ProcessFirstSend_path(sdv.value_object, id, sendArray);
|
|
|
+ }
|
|
|
+ } else if (sdv.rel_property.propertyValueSchema.equals("JSONArray")) {
|
|
|
+ if (sdv.value_array != null) {
|
|
|
+ JSONArray pathArray = new JSONArray();
|
|
|
+ PathUtil.getDataPath(sdv, pathArray);
|
|
|
+ if (pathMatch(pathList, pathArray)) {
|
|
|
+ Object JSON = sdv.value_array.toJSON(1);
|
|
|
+ JSONObject sendItem = new JSONObject();
|
|
|
+ sendItem.put("path", pathArray);
|
|
|
+ sendItem.put("data", JSON);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ // 发送
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
+ }
|
|
|
+ if (sdv.value_array.isSingleValueSet) {
|
|
|
+ } else {
|
|
|
+ for (SceneDataObject sdoInner : sdv.value_array.set) {
|
|
|
+ ProcessFirstSend_path(sdoInner, id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (sdv.value_prim != null) {
|
|
|
+ JSONArray pathArray = new JSONArray();
|
|
|
+ PathUtil.getDataPath(sdv, pathArray);
|
|
|
+ if (pathMatch(pathList, pathArray)) {
|
|
|
+ Object JSON = sdv.value_prim.value;
|
|
|
+ JSONObject sendItem = new JSONObject();
|
|
|
+ sendItem.put("path", pathArray);
|
|
|
+ sendItem.put("data", JSON);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ // 发送
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (sdv.value_array != null) {
|
|
|
+ if (sdv.value_array.isSingleValueSet) {
|
|
|
+ JSONArray pathArray = new JSONArray();
|
|
|
+ PathUtil.getDataPath(sdv, pathArray);
|
|
|
+ if (pathMatch(pathList, pathArray)) {
|
|
|
+ Object JSON = sdv.value_array.toJSON(1);
|
|
|
+ JSONObject sendItem = new JSONObject();
|
|
|
+ sendItem.put("path", pathArray);
|
|
|
+ sendItem.put("data", JSON);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ // 发送
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (sdv.value_object != null) {
|
|
|
+ } else if (sdv.value_prim != null) {
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void SendAndClear(String id, JSONArray sendArray) {
|
|
|
+ WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ sendArray.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean pathMatch(List<JSONArray> pathList, JSONArray pathArray) {
|
|
|
+ boolean match = false;
|
|
|
+ for (JSONArray pathArrayInner : pathList) {
|
|
|
+ if (pathArrayInner.size() != pathArray.size()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean matchOne = true;
|
|
|
+ for (int i = 0; i < pathArrayInner.size(); i++) {
|
|
|
+ String item1 = pathArrayInner.getString(i);
|
|
|
+ String item2 = pathArray.getString(i);
|
|
|
+ if (!item1.equals("*") && !item1.equals(item2)) {
|
|
|
+ matchOne = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (matchOne) {
|
|
|
+ match = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return match;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void ProcessComputeOccur(SceneDataValue sdv) {
|
|
|
+ Runnable runnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ JSONArray pathArray = new JSONArray();
|
|
|
+ PathUtil.getDataPath(sdv, pathArray);
|
|
|
+ int[] flagArray = new int[pathArray.size()];
|
|
|
+ while (true) {
|
|
|
+ JSONArray pathArrayInner = pathString(pathArray, flagArray);
|
|
|
+ if (path2idList.containsKey(pathArrayInner.toString())) {
|
|
|
+ Map<String, Boolean> idList = path2idList.get(pathArrayInner.toString());
|
|
|
+ JSONArray sendArray = new JSONArray();
|
|
|
+ JSONObject sendItem = new JSONObject();
|
|
|
+ sendItem.put("path", pathArrayInner);
|
|
|
+ int depth = 1;
|
|
|
+ // if (sdv.rel_property != null) {
|
|
|
+ // depth = Integer.parseInt(sdv.rel_property.read_level);
|
|
|
+ // }
|
|
|
+ Object data = sdv.toJSON(true, depth);
|
|
|
+ sendItem.put("data", data);
|
|
|
+ sendArray.add(sendItem);
|
|
|
+ for (String id : idList.keySet()) {
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < flagArray.length; i++) {
|
|
|
+ if (flagArray[i] == 0) {
|
|
|
+ flagArray[i] = 1;
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ flagArray[i] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean all_0 = true;
|
|
|
+ for (int i = 0; i < flagArray.length; i++) {
|
|
|
+ if (flagArray[i] != 0) {
|
|
|
+ all_0 = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (all_0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ WebSocketUtil.executor.execute(new Thread(runnable));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static JSONArray pathString(JSONArray pathArray, int[] flagArray) {
|
|
|
+ JSONArray result = new JSONArray();
|
|
|
+ for (int i = 0; i < pathArray.size(); i++) {
|
|
|
+ String pathItem = pathArray.getString(i);
|
|
|
+ int flag = flagArray[i];
|
|
|
+ result.add(flag == 0 ? pathItem : "*");
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
public static void ProcessIOTReceived(JSONObject json) {
|
|
@@ -292,7 +453,7 @@ public class WebSocketUtil {
|
|
|
sendArray.add(sendItem);
|
|
|
}
|
|
|
if (sendArray.size() > 0) {
|
|
|
- WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -344,7 +505,7 @@ public class WebSocketUtil {
|
|
|
sendArray.add(sendItem);
|
|
|
}
|
|
|
if (sendArray.size() > 0) {
|
|
|
- WebSocketChannelPool.Send(id, sendArray);
|
|
|
+ SendAndClear(id, sendArray);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|