|
|
@@ -1,6 +1,7 @@
|
|
|
package com.zhongshu.iot.server.core.service.device;
|
|
|
|
|
|
import com.github.microservice.models.type.DeviceState;
|
|
|
+import com.github.microservice.models.type.RegistType;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.dataConfig.MqttConfig;
|
|
|
@@ -13,6 +14,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl;
|
|
|
import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -61,6 +63,8 @@ public class JMXSyncService {
|
|
|
@Autowired
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DeviceInfoDao deviceInfoDao;
|
|
|
|
|
|
/**
|
|
|
* 把用户同步到MQTT服务中
|
|
|
@@ -276,39 +280,40 @@ public class JMXSyncService {
|
|
|
ObjectName addressObjectName = ObjectNameBuilder.create("org.apache.activemq.artemis", mqttInfo.getBrokerName()).getActiveMQServerObjectName();
|
|
|
ActiveMQServerControl addressControl = MBeanServerInvocationHandler.newProxyInstance(connection, addressObjectName, ActiveMQServerControl.class, false);
|
|
|
|
|
|
- final String _roleName = MqttConfig.adminRoleName;
|
|
|
for (GateWayInfo gateWayInfo : gateWayInfos) {
|
|
|
// 以前的权限
|
|
|
List<GateWayMqttSecurity> list = gateWayMqttSecurityDao.findByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
|
|
|
gateWayMqttSecurityDao.deleteByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
|
|
|
|
|
|
- // 查询网关关联的用户
|
|
|
- GateWay2User gateWay2User = gateWay2UserDao.findTopByGateWayInfo(gateWayInfo);
|
|
|
- if (ObjectUtils.isEmpty(gateWay2User)) {
|
|
|
+ String userName = gateWayInfo.getMqttUserName();
|
|
|
+ if (StringUtils.isEmpty(userName)) {
|
|
|
log.error("网关对应用户信息为不存在");
|
|
|
continue;
|
|
|
}
|
|
|
- GateWayUserInfo gateWayUserInfo = gateWay2User.getGateWayUserInfo();
|
|
|
+
|
|
|
+ GateWayUserInfo gateWayUserInfo = gateWayUserInfoDao.findTopByUserName(userName);
|
|
|
if (ObjectUtils.isEmpty(gateWayUserInfo)) {
|
|
|
log.error("网关对应用户信息为空");
|
|
|
continue;
|
|
|
}
|
|
|
+ String roleName = JMXUtil.buildSecurityRoleName(gateWayUserInfo.getRoleName());
|
|
|
+
|
|
|
+ boolean gatewayIsUse = true;
|
|
|
+ if (gateWayInfo.getState() == null || gateWayInfo.getState() == DeviceState.Enable) {
|
|
|
+ gatewayIsUse = true;
|
|
|
+ } else {
|
|
|
+ gatewayIsUse = false;
|
|
|
+ }
|
|
|
+
|
|
|
// 查询网关对应的设备
|
|
|
List<GateWay2Device> gateWay2Devices = gateWay2DeviceDao.findByGateWayInfo(gateWayInfo);
|
|
|
List<DeviceInfo> deviceInfos = new ArrayList<>();
|
|
|
if (ObjectUtils.isNotEmpty(gateWay2Devices)) {
|
|
|
deviceInfos = gateWay2Devices.stream().map(it -> it.getDeviceInfo()).collect(Collectors.toList());
|
|
|
}
|
|
|
+
|
|
|
List<String> addressMatchs = JMXUtil.buildAddressMatch(gateWayInfo, deviceInfos);
|
|
|
if (ObjectUtils.isNotEmpty(addressMatchs)) {
|
|
|
- String userName = gateWayUserInfo.getUserName();
|
|
|
- String roleName = gateWayUserInfo.getRoleName();
|
|
|
-
|
|
|
- List<String> roleNames = new ArrayList<>();
|
|
|
- roleNames.add(roleName);
|
|
|
- roleNames.add(_roleName);
|
|
|
-
|
|
|
- roleName = String.join(",", roleNames);
|
|
|
List<GateWayMqttSecurity> securities = new ArrayList<>();
|
|
|
for (String addressMatch : addressMatchs) {
|
|
|
GateWayMqttSecurity gateWayMqttSecurity = new GateWayMqttSecurity();
|
|
|
@@ -320,8 +325,11 @@ public class JMXSyncService {
|
|
|
gateWayMqttSecurity.setMqttName(mqttInfo.getName());
|
|
|
gateWayMqttSecurity.setAddressMatch(addressMatch);
|
|
|
try {
|
|
|
- addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
|
|
|
-
|
|
|
+ if (gatewayIsUse) {
|
|
|
+ addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
|
|
|
+ } else {
|
|
|
+ addressControl.removeSecuritySettings(addressMatch);
|
|
|
+ }
|
|
|
gateWayMqttSecurity.setIsSync(Boolean.TRUE);
|
|
|
gateWayMqttSecurity.setMsg("同步成功");
|
|
|
} catch (Exception e) {
|
|
|
@@ -473,7 +481,7 @@ public class JMXSyncService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 同步所有的用户
|
|
|
+ * 同步所有的用户 -- 用户
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -486,7 +494,7 @@ public class JMXSyncService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 同步所有的数据
|
|
|
+ * 同步所有的数据 -- 网关
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -498,4 +506,16 @@ public class JMXSyncService {
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 同步所有直连设备的权限
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent refreshAllDirectDevice() {
|
|
|
+ List<DeviceInfo> deviceInfos = deviceInfoDao.findByRegistTypeIn(List.of(RegistType.DirectConnection));
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfos)) {
|
|
|
+ syncDevicesSecurity(deviceInfos);
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
}
|