|
|
@@ -12,11 +12,16 @@ import com.github.microservice.models.iot.IotSendParam;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.github.microservice.types.FunctionType;
|
|
|
import com.google.common.collect.Lists;
|
|
|
+import com.zhongshu.card.client.utils.DateUtils;
|
|
|
import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
|
|
|
+import com.zhongshu.card.server.core.dao.devices.DevicePermissSendLogDao;
|
|
|
+import com.zhongshu.card.server.core.dataConfig.TtlConfig;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.DevicePermissSendLog;
|
|
|
import com.zhongshu.card.server.core.event.DevicePermissChangeEvent;
|
|
|
import com.zhongshu.card.server.core.event.UserInfoChangeSyncEvent;
|
|
|
import com.zhongshu.card.server.core.service.openAPI.OpenApiRequestService;
|
|
|
+import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
@@ -27,9 +32,7 @@ import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -63,6 +66,12 @@ public class DevicePermissEventService {
|
|
|
@Autowired
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DevicePermissSendLogDao devicePermissSendLogDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevicePermissSendLogService devicePermissSendLogService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private void init(ApplicationContext applicationContext) {
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
@@ -81,6 +90,12 @@ public class DevicePermissEventService {
|
|
|
applicationContext.publishEvent(event);
|
|
|
}
|
|
|
|
|
|
+ public void sendUserInfoChangeEvent(String userId, String projectOid, Map<String, Object> map) {
|
|
|
+ UserInfoChangeSyncEvent event = new UserInfoChangeSyncEvent(
|
|
|
+ this, List.of(userId), projectOid, map);
|
|
|
+ applicationContext.publishEvent(event);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 通知用户关联信息发送变化
|
|
|
*
|
|
|
@@ -153,20 +168,22 @@ public class DevicePermissEventService {
|
|
|
log.info("userInfoChange; {} {}", projectOid);
|
|
|
if (StringUtils.isNotEmpty(projectOid) && ObjectUtils.isNotEmpty(userIds[0])) {
|
|
|
executorService.execute(() -> {
|
|
|
+ String groupId = event.getGroupId();
|
|
|
userIds[0] = userIds[0].stream().distinct().collect(Collectors.toList());
|
|
|
// 循环用户数据
|
|
|
for (String userId : userIds[0]) {
|
|
|
List<String> deviceIds = devicePermissService.getUserAllDeviceIds(
|
|
|
projectOid, List.of(userId));
|
|
|
+ // 网关 对应的设备
|
|
|
HashMap<String, List<String>> map = deviceGroupByGateWayId(deviceIds);
|
|
|
if (!map.isEmpty()) {
|
|
|
// 遍历发送
|
|
|
map.keySet().stream().forEach(gateWayId -> {
|
|
|
DeviceUsersInfo deviceUsersInfo = new DeviceUsersInfo();
|
|
|
+ deviceUsersInfo.setGroupId(groupId);
|
|
|
deviceUsersInfo.setGateWayId(gateWayId);
|
|
|
// 用户数据 (去重)
|
|
|
List<ProjectUserPermiss> userPermiss = devicePermissIotService.getUserPermiss(projectOid, userIds[0]);
|
|
|
-
|
|
|
deviceUsersInfo.setUsers(userPermiss);
|
|
|
// 下发用户数据
|
|
|
requestDevicesUsersData(deviceUsersInfo, projectOid);
|
|
|
@@ -233,6 +250,28 @@ public class DevicePermissEventService {
|
|
|
IotSendParam param = new IotSendParam();
|
|
|
param.setGateWayId(gateWayId);
|
|
|
param.setIdentifier(IotIdentifierConfig.queryDeviceBindUsers);
|
|
|
+
|
|
|
+ String groupId = gateWayPermissModel.getGroupId();
|
|
|
+ if (ObjectUtils.isNotEmpty(gateWayPermissModel.getDevices())
|
|
|
+ && StringUtils.isNotEmpty(groupId)) {
|
|
|
+ List<DevicePermissSendLog> list = new ArrayList<>();
|
|
|
+ for (DeviceAboutPermiss deviceAboutPermiss : gateWayPermissModel.getDevices()) {
|
|
|
+ deviceAboutPermiss.setDataId(CommonUtil.UUID());
|
|
|
+
|
|
|
+ DevicePermissSendLog sendLog = new DevicePermissSendLog();
|
|
|
+ sendLog.setDataId(deviceAboutPermiss.getDataId());
|
|
|
+ sendLog.setGateWayId(gateWayId);
|
|
|
+ sendLog.setDeviceId(deviceAboutPermiss.getDeviceId());
|
|
|
+ sendLog.setGroupId(groupId);
|
|
|
+ sendLog.setData(gateWayPermissModel);
|
|
|
+ sendLog.setSendTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
+ sendLog.setEvent(IotIdentifierConfig.queryDeviceBindUsers);
|
|
|
+ sendLog.setTtl(new Date(System.currentTimeMillis() + TtlConfig.permissLogTTl));
|
|
|
+ list.add(sendLog);
|
|
|
+ }
|
|
|
+ devicePermissSendLogDao.saveAll(list);
|
|
|
+ }
|
|
|
+
|
|
|
// 组装消息内容
|
|
|
JSONObject jsonObject = gateWayPermissModel.toJson();
|
|
|
param.setData(jsonObject);
|
|
|
@@ -242,6 +281,7 @@ public class DevicePermissEventService {
|
|
|
} else {
|
|
|
log.error("设备权限下发失败 网关ID:{} {}", gateWayId, resultContent.getMsg());
|
|
|
}
|
|
|
+ devicePermissSendLogService.markPermissSend(groupId, resultContent.isSuccess(), resultContent.getMsg());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -270,6 +310,28 @@ public class DevicePermissEventService {
|
|
|
IotSendParam param = new IotSendParam();
|
|
|
param.setGateWayId(gateWayId);
|
|
|
param.setIdentifier(IotIdentifierConfig.queryDeviceUsersInfo);
|
|
|
+
|
|
|
+ String groupId = deviceUsersInfo.getGroupId();
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceUsersInfo.getUsers())
|
|
|
+ && StringUtils.isNotEmpty(groupId)) {
|
|
|
+ List<DevicePermissSendLog> list = new ArrayList<>();
|
|
|
+ for (ProjectUserPermiss projectUserPermiss : deviceUsersInfo.getUsers()) {
|
|
|
+ projectUserPermiss.setDataId(CommonUtil.UUID());
|
|
|
+ DevicePermissSendLog sendLog = new DevicePermissSendLog();
|
|
|
+ sendLog.setDataId(projectUserPermiss.getDataId());
|
|
|
+ sendLog.setGateWayId(gateWayId);
|
|
|
+ sendLog.setUserId(projectUserPermiss.getUid());
|
|
|
+ sendLog.setUserId(projectUserPermiss.getUn());
|
|
|
+ sendLog.setGroupId(groupId);
|
|
|
+ sendLog.setData(projectUserPermiss);
|
|
|
+ sendLog.setSendTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
+ sendLog.setEvent(IotIdentifierConfig.queryDeviceUsersInfo);
|
|
|
+ sendLog.setTtl(new Date(System.currentTimeMillis() + TtlConfig.permissLogTTl));
|
|
|
+ list.add(sendLog);
|
|
|
+ }
|
|
|
+ devicePermissSendLogDao.saveAll(list);
|
|
|
+ }
|
|
|
+
|
|
|
// 组装消息内容
|
|
|
JSONObject jsonObject = deviceUsersInfo.toJson();
|
|
|
param.setData(jsonObject);
|
|
|
@@ -279,6 +341,7 @@ public class DevicePermissEventService {
|
|
|
} else {
|
|
|
log.error("用户信息下发失败 网关ID:{} {}", gateWayId, resultContent.getMsg());
|
|
|
}
|
|
|
+ devicePermissSendLogService.markPermissSend(groupId, resultContent.isSuccess(), resultContent.getMsg());
|
|
|
}
|
|
|
|
|
|
/**
|