|
|
@@ -1,20 +1,20 @@
|
|
|
package com.zhongshu.iot.server.core.service.mqtt;
|
|
|
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoAddParam;
|
|
|
import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoModel;
|
|
|
import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoNameParam;
|
|
|
import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoSearchParam;
|
|
|
import com.zhongshu.iot.client.type.type.MqttUserState;
|
|
|
+import com.zhongshu.iot.server.core.dao.mqtt.GateWay2UserDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.GateWayUserInfoDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.Mqtt2UserDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.MqttInfoDao;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayUserInfo;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.Mqtt2User;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.MqttInfo;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
|
|
|
+import com.zhongshu.iot.server.core.util.CommonUtil;
|
|
|
import com.zhongshu.iot.server.core.util.DateUtils;
|
|
|
import com.zhongshu.iot.server.core.util.bean.BeanUtils;
|
|
|
import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
|
|
|
-import com.github.microservice.net.ResultContent;
|
|
|
import lombok.Cleanup;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl;
|
|
|
@@ -43,16 +43,20 @@ import java.util.List;
|
|
|
public class GateWayUserInfoService {
|
|
|
|
|
|
@Autowired
|
|
|
- GateWayUserInfoDao gateWayUserInfoDao;
|
|
|
+ private GateWayUserInfoDao gateWayUserInfoDao;
|
|
|
|
|
|
@Autowired
|
|
|
- MqttInfoDao mqttInfoDao;
|
|
|
+ private MqttInfoDao mqttInfoDao;
|
|
|
|
|
|
@Autowired
|
|
|
- Mqtt2UserDao mqtt2UserDao;
|
|
|
+ private Mqtt2UserDao mqtt2UserDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GateWay2UserDao gateWay2UserDao;
|
|
|
|
|
|
/**
|
|
|
* 初始mqtt连接账号信息
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public ResultContent initDefaultUser() {
|
|
|
@@ -64,19 +68,56 @@ public class GateWayUserInfoService {
|
|
|
param.setPassWord("admin123");
|
|
|
addGateWayUser(param);
|
|
|
log.info(String.format("gateWayUser [%s] 初始化成功", userName));
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
log.info(String.format("gateWayUser [%s] 已初始化", userName));
|
|
|
}
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
+ public GateWayUserInfo gateWayBindMqttUser(GateWayInfo gateWayInfo) {
|
|
|
+ GateWayUserInfo gateWayUserInfo = null;
|
|
|
+ if (ObjectUtils.isNotEmpty(gateWayInfo)) {
|
|
|
+ GateWay2User gateWay2User = gateWay2UserDao.findTopByGateWayInfo(gateWayInfo);
|
|
|
+ if (ObjectUtils.isEmpty(gateWay2User)) {
|
|
|
+ GateWayUserInfoAddParam userParam = new GateWayUserInfoAddParam();
|
|
|
+ String userName = CommonUtil.generateRandomString(8);
|
|
|
+ String roleName = userName;
|
|
|
+ userParam.setUserName(userName);
|
|
|
+ userParam.setPassWord(CommonUtil.generateRandomString(16));
|
|
|
+ userParam.setRoleName(roleName);
|
|
|
+
|
|
|
+ // 创建 并同步
|
|
|
+ ResultContent<GateWayUserInfo> resultContent = addGateWayUser(userParam);
|
|
|
+ if (resultContent.isSuccess()) {
|
|
|
+ gateWayUserInfo = resultContent.getContent();
|
|
|
+ gateWay2User = new GateWay2User();
|
|
|
+ gateWay2User.setGateWayInfo(gateWayInfo);
|
|
|
+ gateWay2User.setGateWayId(gateWayInfo.getGateWayId());
|
|
|
+ gateWay2User.setGateWayUserInfo(gateWayUserInfo);
|
|
|
+ gateWay2User.setUserName(userName);
|
|
|
+
|
|
|
+ gateWay2User.setBindTime(System.currentTimeMillis());
|
|
|
+ gateWay2User.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
+ gateWay2UserDao.save(gateWay2User);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ gateWayUserInfo = gateWay2User.getGateWayUserInfo();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ syncUserToMQTTService(gateWayUserInfo);
|
|
|
+ return gateWayUserInfo;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加用户
|
|
|
*
|
|
|
* @param param
|
|
|
* @return
|
|
|
*/
|
|
|
- public ResultContent addGateWayUser(GateWayUserInfoAddParam param) {
|
|
|
+ public ResultContent<GateWayUserInfo> addGateWayUser(GateWayUserInfoAddParam param) {
|
|
|
+ if (StringUtils.isEmpty(param.getRoleName())) {
|
|
|
+ return ResultContent.buildFail("roleName不能为空");
|
|
|
+ }
|
|
|
GateWayUserInfo entity = gateWayUserInfoDao.findTopByUserName(param.getUserName());
|
|
|
if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
return ResultContent.buildFail(String.format("%s 用户已存在", param.getUserName()));
|
|
|
@@ -84,11 +125,10 @@ public class GateWayUserInfoService {
|
|
|
entity = new GateWayUserInfo();
|
|
|
BeanUtils.copyProperties(param, entity);
|
|
|
entity.setState(MqttUserState.Enable);
|
|
|
- entity.setRoleName(param.getUserName());
|
|
|
gateWayUserInfoDao.save(entity);
|
|
|
// 同步用户
|
|
|
syncUserToMQTTService(entity);
|
|
|
- return ResultContent.buildSuccess();
|
|
|
+ return ResultContent.buildSuccess(entity);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -178,6 +218,7 @@ public class GateWayUserInfoService {
|
|
|
public ResultContent syncUserToMQTTService(GateWayUserInfo gateWayUserInfo) {
|
|
|
//todo同步用户
|
|
|
List<MqttInfo> list = mqttInfoDao.findAll();
|
|
|
+ log.info("syncUserToMQTTService {}", list.size());
|
|
|
if (ObjectUtils.isNotEmpty(list)) {
|
|
|
for (MqttInfo mqttInfo : list) {
|
|
|
try {
|
|
|
@@ -189,6 +230,8 @@ public class GateWayUserInfoService {
|
|
|
MBeanServerConnection connection = connector.getMBeanServerConnection();
|
|
|
ObjectName addressObjectName = ObjectNameBuilder.create("org.apache.activemq.artemis", mqttInfo.getBrokerName()).getActiveMQServerObjectName();
|
|
|
ActiveMQServerControl addressControl = MBeanServerInvocationHandler.newProxyInstance(connection, addressObjectName, ActiveMQServerControl.class, false);
|
|
|
+
|
|
|
+ // 添加账号
|
|
|
addressControl.addUser(gateWayUserInfo.getUserName(), gateWayUserInfo.getPassWord(), gateWayUserInfo.getRoleName(), false);
|
|
|
|
|
|
Mqtt2User mqtt2User = mqtt2UserDao.findTopByMqttInfoAndGateWayUserInfo(mqttInfo, gateWayUserInfo);
|
|
|
@@ -196,7 +239,11 @@ public class GateWayUserInfoService {
|
|
|
mqtt2User = new Mqtt2User();
|
|
|
}
|
|
|
mqtt2User.setGateWayUserInfo(gateWayUserInfo);
|
|
|
+ mqtt2User.setUserName(gateWayUserInfo.getUserName());
|
|
|
+
|
|
|
mqtt2User.setMqttInfo(mqttInfo);
|
|
|
+ mqtt2User.setMqttInfoName(mqttInfo.getName());
|
|
|
+
|
|
|
mqtt2User.setIsSync(Boolean.TRUE);
|
|
|
mqtt2User.setSyncTime(System.currentTimeMillis());
|
|
|
mqtt2User.setSyncTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|