|
|
@@ -3,6 +3,9 @@ package com.zhongshu.card.server.core.service.devices;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.github.microservice.core.util.os.SystemUtil;
|
|
|
import com.github.microservice.dataConfig.IotIdentifierConfig;
|
|
|
+import com.github.microservice.models.device.DevicePerQuery;
|
|
|
+import com.github.microservice.models.devicePermiss.DeviceUsersInfo;
|
|
|
+import com.github.microservice.models.devicePermiss.GateWayPermissModel;
|
|
|
import com.github.microservice.models.iot.IotSendParam;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.github.microservice.types.FunctionType;
|
|
|
@@ -41,9 +44,11 @@ public class DevicePermissEventService {
|
|
|
@Autowired
|
|
|
private OpenApiRequestService openApiRequestService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DevicePermissIotService devicePermissIotService;
|
|
|
+
|
|
|
//线程池
|
|
|
- private ExecutorService executorService = Executors.newFixedThreadPool(
|
|
|
- SystemUtil.getCpuCoreCount() * 2);
|
|
|
+ private ExecutorService executorService = Executors.newFixedThreadPool(SystemUtil.getCpuCoreCount() * 2);
|
|
|
|
|
|
@Autowired
|
|
|
private void init(ApplicationContext applicationContext) {
|
|
|
@@ -60,26 +65,9 @@ public class DevicePermissEventService {
|
|
|
// 通知物联网 设备权限发生变化
|
|
|
List<String> deviceIds = event.getDeviceIds();
|
|
|
if (ObjectUtils.isNotEmpty(deviceIds)) {
|
|
|
- List<DeviceInfo> deviceInfos = deviceInfoDao.findByDeviceIdIn(deviceIds);
|
|
|
- if (ObjectUtils.isNotEmpty(deviceInfos)) {
|
|
|
- // 按网关分组
|
|
|
- HashMap<String, List<String>> map = new HashMap<>();
|
|
|
- deviceInfos.stream().forEach(it -> {
|
|
|
- String deviceId = it.getDeviceId();
|
|
|
- String gateWayId = it.getGateWayId();
|
|
|
- List<String> arr = List.of(gateWayId.split(","));
|
|
|
- if (ObjectUtils.isNotEmpty(arr)) {
|
|
|
- arr.stream().forEach(_gateWayId -> {
|
|
|
- List<String> _list = map.get(_gateWayId);
|
|
|
- if (ObjectUtils.isEmpty(_list)) {
|
|
|
- _list = new ArrayList<>();
|
|
|
- }
|
|
|
- _list.add(deviceId);
|
|
|
- map.put(_gateWayId, _list);
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
+ HashMap<String, List<String>> map = deviceGroupByGateWayId(deviceIds);
|
|
|
+ if (!map.isEmpty()) {
|
|
|
+ String projectOid = event.getProjectOid();
|
|
|
// 遍历发送
|
|
|
map.keySet().stream().forEach(gateWayId -> {
|
|
|
List<String> _deviceIds = map.get(gateWayId);
|
|
|
@@ -90,23 +78,24 @@ public class DevicePermissEventService {
|
|
|
List<String> subList = _deviceIds.subList(0, index);
|
|
|
_deviceIds = _deviceIds.subList(index, _deviceIds.size());
|
|
|
|
|
|
- DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(subList.get(0));
|
|
|
-
|
|
|
- IotSendParam param = new IotSendParam();
|
|
|
- param.setGateWayId(gateWayId);
|
|
|
- param.setIdentifier(IotIdentifierConfig.permissionNotice);
|
|
|
-
|
|
|
- // 组装消息内容
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.set("deviceIds", subList);
|
|
|
- param.setData(jsonObject);
|
|
|
- ResultContent resultContent = openApiRequestService.sendIotMessage(
|
|
|
- param, deviceInfo.getProjectOid());
|
|
|
- if (resultContent.isSuccess()) {
|
|
|
-
|
|
|
- } else {
|
|
|
- log.error("网关设备权限通知失败 {} {}", gateWayId, resultContent.getMsg());
|
|
|
- }
|
|
|
+ sendDeviceAboutUserInfo(gateWayId, subList, projectOid);
|
|
|
+ sendDeviceAboutUsers(gateWayId, subList, projectOid);
|
|
|
+
|
|
|
+// DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(subList.get(0));
|
|
|
+// IotSendParam param = new IotSendParam();
|
|
|
+// param.setGateWayId(gateWayId);
|
|
|
+// param.setIdentifier(IotIdentifierConfig.permissionNotice);
|
|
|
+//
|
|
|
+// // 组装消息内容
|
|
|
+// JSONObject jsonObject = new JSONObject();
|
|
|
+// jsonObject.set("deviceIds", subList);
|
|
|
+// param.setData(jsonObject);
|
|
|
+// ResultContent resultContent = openApiRequestService.sendIotMessage(param, deviceInfo.getProjectOid());
|
|
|
+// if (resultContent.isSuccess()) {
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// log.error("网关设备权限通知失败 {} {}", gateWayId, resultContent.getMsg());
|
|
|
+// }
|
|
|
if (!_deviceIds.isEmpty()) {
|
|
|
TimeUnit.SECONDS.sleep(10);
|
|
|
}
|
|
|
@@ -120,9 +109,88 @@ public class DevicePermissEventService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public void send() {
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 发送设备关联的用户数据
|
|
|
+ *
|
|
|
+ * @param gateWayId
|
|
|
+ * @param deviceIds
|
|
|
+ * @param projectOid
|
|
|
+ */
|
|
|
+ public void sendDeviceAboutUsers(String gateWayId, List<String> deviceIds, String projectOid) {
|
|
|
+ DevicePerQuery query = new DevicePerQuery();
|
|
|
+ query.setDeviceIds(deviceIds);
|
|
|
+ query.setGateWayId(gateWayId);
|
|
|
+ GateWayPermissModel gateWayPermissModel = devicePermissIotService.queryDevicePermissInfo(query);
|
|
|
+
|
|
|
+ IotSendParam param = new IotSendParam();
|
|
|
+ param.setGateWayId(gateWayId);
|
|
|
+ param.setIdentifier(IotIdentifierConfig.queryDeviceBindUsers);
|
|
|
+ // 组装消息内容
|
|
|
+ JSONObject jsonObject = gateWayPermissModel.toJson();
|
|
|
+ param.setData(jsonObject);
|
|
|
+ ResultContent resultContent = openApiRequestService.sendIotMessage(param, projectOid);
|
|
|
+ if (resultContent.isSuccess()) {
|
|
|
+ log.info("设备权限下发成功");
|
|
|
+ } else {
|
|
|
+ log.error("设备权限下发失败 网关ID:{} {}", gateWayId, resultContent.getMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 下发用户信息
|
|
|
+ *
|
|
|
+ * @param gateWayId
|
|
|
+ * @param deviceIds
|
|
|
+ * @param projectOid
|
|
|
+ */
|
|
|
+ public void sendDeviceAboutUserInfo(String gateWayId, List<String> deviceIds, String projectOid) {
|
|
|
+ DevicePerQuery query = new DevicePerQuery();
|
|
|
+ query.setDeviceIds(deviceIds);
|
|
|
+ query.setGateWayId(gateWayId);
|
|
|
+ DeviceUsersInfo deviceUsersInfo = devicePermissIotService.queryDeviceAboutUsers(query);
|
|
|
+
|
|
|
+ IotSendParam param = new IotSendParam();
|
|
|
+ param.setGateWayId(gateWayId);
|
|
|
+ param.setIdentifier(IotIdentifierConfig.queryDeviceUsersInfo);
|
|
|
+ // 组装消息内容
|
|
|
+ JSONObject jsonObject = deviceUsersInfo.toJson();
|
|
|
+ param.setData(jsonObject);
|
|
|
+ ResultContent resultContent = openApiRequestService.sendIotMessage(param, projectOid);
|
|
|
+ if (resultContent.isSuccess()) {
|
|
|
+ log.info("用户信息下发成功");
|
|
|
+ } else {
|
|
|
+ log.error("用户信息下发失败 网关ID:{} {}", gateWayId, resultContent.getMsg());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 设备以网关分组
|
|
|
+ *
|
|
|
+ * @param deviceIds
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public HashMap<String, List<String>> deviceGroupByGateWayId(List<String> deviceIds) {
|
|
|
+ HashMap<String, List<String>> map = new HashMap<>();
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceIds)) {
|
|
|
+ List<DeviceInfo> deviceInfos = deviceInfoDao.findByDeviceIdIn(deviceIds);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfos)) {
|
|
|
+ deviceInfos.stream().forEach(it -> {
|
|
|
+ String deviceId = it.getDeviceId();
|
|
|
+ String gateWayId = it.getGateWayId();
|
|
|
+ List<String> arr = List.of(gateWayId.split(","));
|
|
|
+ if (ObjectUtils.isNotEmpty(arr)) {
|
|
|
+ arr.stream().forEach(_gateWayId -> {
|
|
|
+ List<String> _list = map.get(_gateWayId);
|
|
|
+ if (ObjectUtils.isEmpty(_list)) {
|
|
|
+ _list = new ArrayList<>();
|
|
|
+ }
|
|
|
+ _list.add(deviceId);
|
|
|
+ map.put(_gateWayId, _list);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
}
|