|
@@ -1,47 +1,29 @@
|
|
|
package com.zhongshu.card.server.core.service.devices;
|
|
package com.zhongshu.card.server.core.service.devices;
|
|
|
|
|
|
|
|
-import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
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.device.DevicePerQuery;
|
|
|
import com.github.microservice.models.device.UserPerQuery;
|
|
import com.github.microservice.models.device.UserPerQuery;
|
|
|
-import com.github.microservice.models.devicePermiss.*;
|
|
|
|
|
-import com.github.microservice.models.iot.IotSendParam;
|
|
|
|
|
|
|
+import com.github.microservice.models.devicePermiss.DeviceUsersInfo;
|
|
|
|
|
+import com.github.microservice.models.devicePermiss.GateWayPermissModel;
|
|
|
|
|
+import com.github.microservice.models.devicePermiss.ProjectUserPermiss;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
import com.github.microservice.net.ResultContent;
|
|
|
-import com.github.microservice.types.FunctionType;
|
|
|
|
|
import com.zhongshu.card.client.model.school.CardInfoModel;
|
|
import com.zhongshu.card.client.model.school.CardInfoModel;
|
|
|
import com.zhongshu.card.client.type.school.CardState;
|
|
import com.zhongshu.card.client.type.school.CardState;
|
|
|
-import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
|
|
|
|
|
-import com.zhongshu.card.server.core.dao.devices.DevicePermissDao;
|
|
|
|
|
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
|
|
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.devices.DevicePermiss;
|
|
|
import com.zhongshu.card.server.core.domain.org.Organization;
|
|
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.org.UserAccount;
|
|
|
import com.zhongshu.card.server.core.domain.projectAbout.OrgUserFace;
|
|
import com.zhongshu.card.server.core.domain.projectAbout.OrgUserFace;
|
|
|
-import com.zhongshu.card.server.core.event.DevicePermissChangeEvent;
|
|
|
|
|
-import com.zhongshu.card.server.core.service.openAPI.OpenApiRequestService;
|
|
|
|
|
import com.zhongshu.card.server.core.service.projectAbout.CardInfoServiceImpl;
|
|
import com.zhongshu.card.server.core.service.projectAbout.CardInfoServiceImpl;
|
|
|
import com.zhongshu.card.server.core.service.projectAbout.OrgUserFaceService;
|
|
import com.zhongshu.card.server.core.service.projectAbout.OrgUserFaceService;
|
|
|
-import com.zhongshu.card.server.core.service.user.UserAccountServiceImpl;
|
|
|
|
|
-import lombok.SneakyThrows;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.kafka.common.protocol.types.Field;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
|
|
-import org.springframework.context.event.EventListener;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
|
|
-import java.util.concurrent.Executors;
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -54,47 +36,18 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
@Service
|
|
|
public class DevicePermissIotService {
|
|
public class DevicePermissIotService {
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private DevicePermissDao devicePermissDao;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private DeviceInfoServiceImpl deviceInfoService;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private UserAccountServiceImpl userAccountService;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private DeviceInfoDao deviceInfoDao;
|
|
|
|
|
-
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private OrganizationDao organizationDao;
|
|
private OrganizationDao organizationDao;
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private OrganizationUserDao organizationUserDao;
|
|
|
|
|
-
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private OrgUserFaceService orgUserFaceService;
|
|
private OrgUserFaceService orgUserFaceService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private CardInfoServiceImpl cardInfoService;
|
|
private CardInfoServiceImpl cardInfoService;
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private OpenApiRequestService openApiRequestService;
|
|
|
|
|
-
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private DevicePermissService devicePermissService;
|
|
private DevicePermissService devicePermissService;
|
|
|
|
|
|
|
|
- //线程池
|
|
|
|
|
- private ExecutorService executorService = Executors.newFixedThreadPool(
|
|
|
|
|
- SystemUtil.getCpuCoreCount() * 2);
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private void init(ApplicationContext applicationContext) {
|
|
|
|
|
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
|
- executorService.shutdownNow();
|
|
|
|
|
- }));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 查询设备的权限数据
|
|
* 查询设备的权限数据
|
|
|
*
|
|
*
|
|
@@ -102,17 +55,27 @@ public class DevicePermissIotService {
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
public ResultContent queryDevicePermiss(DevicePerQuery query) {
|
|
public ResultContent queryDevicePermiss(DevicePerQuery query) {
|
|
|
|
|
+ GateWayPermissModel permissModel = queryDevicePermissInfo(query);
|
|
|
|
|
+ return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public GateWayPermissModel queryDevicePermissInfo(DevicePerQuery query) {
|
|
|
GateWayPermissModel permissModel = devicePermissService.getDeviceAboutUsers(query.getDeviceIds());
|
|
GateWayPermissModel permissModel = devicePermissService.getDeviceAboutUsers(query.getDeviceIds());
|
|
|
permissModel.setSuccess();
|
|
permissModel.setSuccess();
|
|
|
permissModel.setGateWayId(query.getGateWayId());
|
|
permissModel.setGateWayId(query.getGateWayId());
|
|
|
- return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
|
|
|
|
+ return permissModel;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ResultContent getDeviceAboutUserInfos(DevicePerQuery query) {
|
|
public ResultContent getDeviceAboutUserInfos(DevicePerQuery query) {
|
|
|
|
|
+ DeviceUsersInfo permissModel = queryDeviceAboutUsers(query);
|
|
|
|
|
+ return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public DeviceUsersInfo queryDeviceAboutUsers(DevicePerQuery query) {
|
|
|
DeviceUsersInfo permissModel = devicePermissService.getDeviceAboutUserInfos(query.getDeviceIds());
|
|
DeviceUsersInfo permissModel = devicePermissService.getDeviceAboutUserInfos(query.getDeviceIds());
|
|
|
permissModel.setSuccess();
|
|
permissModel.setSuccess();
|
|
|
permissModel.setGateWayId(query.getGateWayId());
|
|
permissModel.setGateWayId(query.getGateWayId());
|
|
|
- return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
|
|
|
|
+ return permissModel;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -139,74 +102,6 @@ public class DevicePermissIotService {
|
|
|
return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
return ResultContent.buildSuccess(JSONUtil.toJsonStr(permissModel));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @EventListener(classes = DevicePermissChangeEvent.class)
|
|
|
|
|
- @SneakyThrows
|
|
|
|
|
- public void devicePermissChange(DevicePermissChangeEvent event) {
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public ProjectUserPermiss toModel(DevicePermiss devicePermiss) {
|
|
public ProjectUserPermiss toModel(DevicePermiss devicePermiss) {
|
|
|
ProjectUserPermiss userPermiss = null;
|
|
ProjectUserPermiss userPermiss = null;
|
|
|
if (ObjectUtils.isNotEmpty(devicePermiss)) {
|
|
if (ObjectUtils.isNotEmpty(devicePermiss)) {
|