|
|
@@ -2,12 +2,10 @@ package com.zhongshu.card.server.core.service.devices;
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+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.DeviceAboutPermiss;
|
|
|
-import com.github.microservice.models.devicePermiss.GateWayPermissModel;
|
|
|
-import com.github.microservice.models.devicePermiss.GateWayQuery;
|
|
|
-import com.github.microservice.models.devicePermiss.UserPermiss;
|
|
|
+import com.github.microservice.models.devicePermiss.*;
|
|
|
import com.github.microservice.models.iot.IotSendParam;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.github.microservice.types.FunctionType;
|
|
|
@@ -19,7 +17,6 @@ import com.zhongshu.card.server.core.dao.org.OrganizationDao;
|
|
|
import com.zhongshu.card.server.core.dao.org.OrganizationUserDao;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DevicePermiss;
|
|
|
-import com.zhongshu.card.server.core.domain.org.Organization;
|
|
|
import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
import com.zhongshu.card.server.core.domain.projectAbout.OrgUserFace;
|
|
|
import com.zhongshu.card.server.core.event.DevicePermissChangeEvent;
|
|
|
@@ -31,13 +28,15 @@ import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
-import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -78,32 +77,18 @@ public class DevicePermissIotService {
|
|
|
@Autowired
|
|
|
private OpenApiRequestService openApiRequestService;
|
|
|
|
|
|
- /**
|
|
|
- * 查询网关 包含的设备的权限
|
|
|
- *
|
|
|
- * @param query
|
|
|
- * @return
|
|
|
- */
|
|
|
- public GateWayPermissModel queryGateWayPermiss(GateWayQuery query) {
|
|
|
- // 返回模型
|
|
|
- GateWayPermissModel model = new GateWayPermissModel();
|
|
|
- model.setSuccess();
|
|
|
-
|
|
|
- String deviceId = query.getDeviceId();
|
|
|
- DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
|
|
|
- Organization project = organizationDao.findTopByCode(query.getProjectCode());
|
|
|
- if (ObjectUtils.isNotEmpty(deviceInfo) && ObjectUtils.isNotEmpty(project)) {
|
|
|
- DeviceAboutPermiss aboutPermiss = new DeviceAboutPermiss();
|
|
|
- aboutPermiss.setDeviceId(deviceId);
|
|
|
- List<DevicePermiss> devices = devicePermissDao.findByDeviceIdAndProjectOid(deviceId, project.getOid());
|
|
|
- List<UserPermiss> userPermisses = new ArrayList<>();
|
|
|
- if (ObjectUtils.isNotEmpty(devices)) {
|
|
|
- userPermisses = devices.parallelStream().map(this::toModel).collect(Collectors.toUnmodifiableList());
|
|
|
- }
|
|
|
- aboutPermiss.setUserPermisses(userPermisses);
|
|
|
- model.addDevice(aboutPermiss);
|
|
|
- }
|
|
|
- return model;
|
|
|
+ @Autowired
|
|
|
+ private DevicePermissService devicePermissService;
|
|
|
+
|
|
|
+ //线程池
|
|
|
+ private ExecutorService executorService = Executors.newFixedThreadPool(
|
|
|
+ SystemUtil.getCpuCoreCount() * 2);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private void init(ApplicationContext applicationContext) {
|
|
|
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
+ executorService.shutdownNow();
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -113,85 +98,91 @@ public class DevicePermissIotService {
|
|
|
* @return
|
|
|
*/
|
|
|
public ResultContent queryDevicePermiss(DevicePerQuery query) {
|
|
|
- DeviceAboutPermiss deviceAboutPermiss = new DeviceAboutPermiss();
|
|
|
- List<String> deviceIds = query.getDeviceIds();
|
|
|
- if (ObjectUtils.isNotEmpty(deviceIds)) {
|
|
|
+ GateWayPermissModel permissModel = devicePermissService.getDeviceAboutUsers(query.getDeviceIds());
|
|
|
+ permissModel.setSuccess();
|
|
|
+ permissModel.setGateWayId(query.getGateWayId());
|
|
|
+ return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- return ResultContent.buildSuccess(JSONUtil.toJsonStr(deviceAboutPermiss));
|
|
|
+ public ResultContent getDeviceAboutUserInfos(DevicePerQuery query) {
|
|
|
+ DeviceUsersInfo permissModel = devicePermissService.getDeviceAboutUserInfos(query.getDeviceIds());
|
|
|
+ permissModel.setSuccess();
|
|
|
+ permissModel.setGateWayId(query.getGateWayId());
|
|
|
+ return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
}
|
|
|
|
|
|
@EventListener(classes = DevicePermissChangeEvent.class)
|
|
|
- @Async
|
|
|
@SneakyThrows
|
|
|
public void devicePermissChange(DevicePermissChangeEvent event) {
|
|
|
- log.info("设备权限发生变化...");
|
|
|
- // 通知物联网 设备权限发生变化
|
|
|
- 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);
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- // 遍历发送
|
|
|
- map.keySet().stream().forEach(gateWayId -> {
|
|
|
- List<String> _deviceIds = map.get(gateWayId);
|
|
|
- try {
|
|
|
- while (_deviceIds.size() > 0) {
|
|
|
- int index = Math.min(_deviceIds.size(), IotIdentifierConfig.maxNoticeSize);
|
|
|
- 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.setFunctionType(FunctionType.Server);
|
|
|
- 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);
|
|
|
+ executorService.execute(() -> {
|
|
|
+ log.info("设备权限发生变化...");
|
|
|
+ // 通知物联网 设备权限发生变化
|
|
|
+ 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);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 遍历发送
|
|
|
+ map.keySet().stream().forEach(gateWayId -> {
|
|
|
+ List<String> _deviceIds = map.get(gateWayId);
|
|
|
+ try {
|
|
|
+ while (_deviceIds.size() > 0) {
|
|
|
+ int index = Math.min(_deviceIds.size(), IotIdentifierConfig.maxNoticeSize);
|
|
|
+ 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.setFunctionType(FunctionType.Server);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public UserPermiss toModel(DevicePermiss devicePermiss) {
|
|
|
- UserPermiss userPermiss = null;
|
|
|
+ public ProjectUserPermiss toModel(DevicePermiss devicePermiss) {
|
|
|
+ ProjectUserPermiss userPermiss = null;
|
|
|
if (ObjectUtils.isNotEmpty(devicePermiss)) {
|
|
|
- userPermiss = new UserPermiss();
|
|
|
+ userPermiss = new ProjectUserPermiss();
|
|
|
String userId = devicePermiss.getUserId();
|
|
|
String projectOid = devicePermiss.getProjectOid();
|
|
|
UserAccount account = devicePermiss.getUserAccount();
|