|
|
@@ -4,21 +4,16 @@ import com.github.microservice.net.ResultContent;
|
|
|
import com.github.microservice.types.FunctionType;
|
|
|
import com.github.microservice.types.deviceUse.DeviceCategory;
|
|
|
import com.github.microservice.types.deviceUse.DeviceSpecType;
|
|
|
+import com.github.microservice.types.deviceUse.ThingType;
|
|
|
import com.zhongshu.iot.client.model.baseParam.NameModel;
|
|
|
import com.zhongshu.iot.client.model.iot.*;
|
|
|
import com.zhongshu.iot.client.type.IotDataType;
|
|
|
import com.zhongshu.iot.server.core.dao.UserDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.iot.IotDeviceDataDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.iot.IotMainDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.iot.IotTemplateDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.iot.IotTopicDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.iot.*;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.GateWay2DeviceDao;
|
|
|
import com.zhongshu.iot.server.core.dataConfig.ResultMessage;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.IotDeviceData;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.IotMain;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.IotTemplate;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.IotTopic;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.*;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWay2Device;
|
|
|
import com.zhongshu.iot.server.core.service.base.CommonService;
|
|
|
@@ -81,6 +76,12 @@ public class IotServiceImpl extends SuperService {
|
|
|
@Autowired
|
|
|
private DeviceInfoService deviceInfoService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IotThingDao iotThingDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotThingSyncService iotThingSyncService;
|
|
|
+
|
|
|
//----------------------------- 模版 start----------------------------
|
|
|
|
|
|
/**
|
|
|
@@ -110,6 +111,7 @@ public class IotServiceImpl extends SuperService {
|
|
|
|
|
|
IotTemplate temp = iotTemplateDao.findTopByNameAndIotDataType(param.getName(), IotDataType.IotTemplate);
|
|
|
if (StringUtils.isNotEmpty(param.getId())) {
|
|
|
+ // 编辑
|
|
|
template = iotTemplateDao.findTopById(param.getId());
|
|
|
if (ObjectUtils.isEmpty(template)) {
|
|
|
return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
|
|
|
@@ -117,14 +119,18 @@ public class IotServiceImpl extends SuperService {
|
|
|
if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
|
|
|
return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
|
|
|
}
|
|
|
+ template.setName(param.getName());
|
|
|
+ template.setRemark(param.getRemark());
|
|
|
} else {
|
|
|
+ // 新增加
|
|
|
template = new IotTemplate();
|
|
|
if (ObjectUtils.isNotEmpty(temp)) {
|
|
|
return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
|
|
|
}
|
|
|
template.setIotDataType(IotDataType.IotTemplate);
|
|
|
+ BeanUtils.copyProperties(param, template);
|
|
|
}
|
|
|
- BeanUtils.copyProperties(param, template);
|
|
|
+
|
|
|
String productCode = template.getProductCode();
|
|
|
if (StringUtils.isEmpty(productCode)) {
|
|
|
productCode = String.format("%s%s", template.getDeviceCategory().name(), CommonUtil.randomStr(10));
|
|
|
@@ -381,12 +387,12 @@ public class IotServiceImpl extends SuperService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 添加属性 事件
|
|
|
+ * 添加-编辑 属性 服务 事件
|
|
|
*
|
|
|
* @param param
|
|
|
* @return
|
|
|
*/
|
|
|
- public ResultContent addIotMain(IotMainParam param) {
|
|
|
+ public ResultContent saveIotMain(IotMainParam param) {
|
|
|
initDefaultUser(param);
|
|
|
if (param.getFunctionType() == null) {
|
|
|
return ResultContent.buildFail("functionType 不能为空");
|
|
|
@@ -394,24 +400,63 @@ public class IotServiceImpl extends SuperService {
|
|
|
Assert.hasText(param.getName(), "name不能为空");
|
|
|
Assert.hasText(param.getIdentifier(), "identifier不能为空");
|
|
|
Assert.hasText(param.getIotTopic(), "iotTopic不能为空");
|
|
|
+ IotDataType iotDataType = param.getIotDataType();
|
|
|
+ if (iotDataType == null) {
|
|
|
+ return ResultContent.buildFail("iotDataType 不能为空");
|
|
|
+ }
|
|
|
|
|
|
- String iotTemplateId = param.getIotTemplateId();
|
|
|
- IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
|
|
|
- if (ObjectUtils.isEmpty(iotTemplate)) {
|
|
|
- return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
|
|
|
+ String aboutDataId = "";
|
|
|
+ // 产品信息
|
|
|
+ IotTemplate iotTemplate = null;
|
|
|
+ if (iotDataType == IotDataType.IotTemplate) {
|
|
|
+ if (StringUtils.isEmpty(param.getIotTemplateId())) {
|
|
|
+ return ResultContent.buildFail("产品id不能为空");
|
|
|
+ }
|
|
|
+ String iotTemplateId = param.getIotTemplateId();
|
|
|
+ iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
|
|
|
+ if (ObjectUtils.isEmpty(iotTemplate)) {
|
|
|
+ return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
|
|
|
+ }
|
|
|
+ aboutDataId = param.getIotTemplateId();
|
|
|
+ }
|
|
|
+
|
|
|
+ IotThing iotThing = null;
|
|
|
+ DeviceInfo deviceInfo = null;
|
|
|
+ if (iotDataType == IotDataType.Device) {
|
|
|
+ if (StringUtils.isEmpty(param.getIotThingId())) {
|
|
|
+ return ResultContent.buildFail("物模型id不能为空");
|
|
|
+ }
|
|
|
+ iotThing = iotThingDao.findTopById(param.getIotThingId());
|
|
|
+ if (iotThing == null) {
|
|
|
+ return ResultContent.buildFail(String.format("物模型不存在:%s", param.getIotThingId()));
|
|
|
+ }
|
|
|
+ aboutDataId = param.getIotThingId();
|
|
|
+ if (iotThing.getThingType() == ThingType.Auto) {
|
|
|
+ deviceInfo = deviceInfoDao.findTopByDeviceId(iotThing.getDeviceId());
|
|
|
+ } else if (iotThing.getThingType() == ThingType.Manual) {
|
|
|
+ if (StringUtils.isEmpty(param.getDeviceId())) {
|
|
|
+ return ResultContent.buildFail("手动创建的物模型deviceId不能为空");
|
|
|
+ }
|
|
|
+ deviceInfo = deviceInfoDao.findTopByDeviceId(param.getDeviceId());
|
|
|
+ }
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ return ResultContent.buildFail("物模型管理的设备信息未找到");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
IotMain entity = null;
|
|
|
// 判断名称是否重复
|
|
|
- IotMain nameTemp = iotMainDao.findTopByNameAndIotTemplate(param.getName(), iotTemplate);
|
|
|
- IotMain topicTemp = iotMainDao.findTopByIotTopicAndIotTemplate(param.getIotTopic(), iotTemplate);
|
|
|
- IotMain identifierTemp = iotMainDao.findTopByIdentifierAndIotTemplate(param.getIdentifier(), iotTemplate);
|
|
|
+ IotMain nameTemp = iotMainDao.findTopByNameAndAboutDataId(param.getName(), aboutDataId);
|
|
|
+ IotMain topicTemp = iotMainDao.findTopByIdentifierAndAboutDataId(param.getIotTopic(), aboutDataId);
|
|
|
+ IotMain identifierTemp = iotMainDao.findTopByIotTopicAndAboutDataId(param.getIdentifier(), aboutDataId);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(param.getId())) {
|
|
|
+ // 编辑
|
|
|
entity = iotMainDao.findTopById(param.getId());
|
|
|
if (ObjectUtils.isEmpty(entity)) {
|
|
|
return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
|
|
|
}
|
|
|
+
|
|
|
if (ObjectUtils.isNotEmpty(nameTemp) && !nameTemp.getId().equals(param.getId())) {
|
|
|
return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
|
|
|
}
|
|
|
@@ -422,6 +467,7 @@ public class IotServiceImpl extends SuperService {
|
|
|
return ResultContent.buildFail(String.format("identifier已存在:%s", param.getIdentifier()));
|
|
|
}
|
|
|
} else {
|
|
|
+ // 添加
|
|
|
entity = new IotMain();
|
|
|
initEntity(entity);
|
|
|
if (ObjectUtils.isNotEmpty(nameTemp)) {
|
|
|
@@ -436,15 +482,23 @@ public class IotServiceImpl extends SuperService {
|
|
|
}
|
|
|
|
|
|
BeanUtils.copyProperties(param, entity);
|
|
|
- // 设备所属模型
|
|
|
- entity.setIotTemplate(iotTemplate);
|
|
|
- entity.setIotDataType(iotTemplate.getIotDataType());
|
|
|
- if (iotTemplate.getIotDataType() == IotDataType.Device) {
|
|
|
- entity.setDeviceId(iotTemplate.getDeviceId());
|
|
|
+
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo)) {
|
|
|
+ entity.setDeviceId(deviceInfo.getDeviceId());
|
|
|
+ }
|
|
|
+ if (iotDataType == IotDataType.IotTemplate) {
|
|
|
+ // 产品
|
|
|
+ entity.setIotTemplate(iotTemplate);
|
|
|
+ entity.setProductCode(iotTemplate.getProductCode());
|
|
|
entity.setProjectCode(iotTemplate.getProjectCode());
|
|
|
+ } else if (iotDataType == IotDataType.Device) {
|
|
|
+ // 物模型
|
|
|
+ entity.setProductCode(iotThing.getProductCode());
|
|
|
+ entity.setProjectCode(iotThing.getProjectCode());
|
|
|
initRealTopic(entity);
|
|
|
}
|
|
|
iotMainDao.save(entity);
|
|
|
+ iotThingSyncService.asyncIotThing(iotThing);
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
@@ -530,7 +584,7 @@ public class IotServiceImpl extends SuperService {
|
|
|
if (ObjectUtils.isEmpty(entity)) {
|
|
|
return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
}
|
|
|
- return ResultContent.buildSuccess(toModel(entity));
|
|
|
+ return ResultContent.buildSuccess(toAttributeModel(entity));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -542,15 +596,12 @@ public class IotServiceImpl extends SuperService {
|
|
|
*/
|
|
|
public ResultContent<Page<IotMainModel>> pageIotMain(Pageable pageable, IotMainSearch param) {
|
|
|
initSearchParam(param);
|
|
|
- if (StringUtils.isEmpty(param.getIotTemplateId())) {
|
|
|
- return ResultContent.buildFail("iotTemplateId不能为空");
|
|
|
+ // 从物模型查看 或 从产品查看
|
|
|
+ if (StringUtils.isEmpty(param.getIotTemplateId()) && StringUtils.isEmpty(param.getIotThingId())) {
|
|
|
+ return ResultContent.buildFail("iotTemplateId和iotThingId不能同时为空");
|
|
|
}
|
|
|
Page<IotMain> page = iotMainDao.page(pageable, param);
|
|
|
- if (param.getFunctionType() != null && param.getFunctionType() == FunctionType.Attribute) {
|
|
|
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toAttributeModel));
|
|
|
- } else {
|
|
|
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
|
|
|
- }
|
|
|
+ return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toAttributeModel));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -563,7 +614,7 @@ public class IotServiceImpl extends SuperService {
|
|
|
List<IotMain> list = iotMainDao.findByIotTemplateAndFunctionTypeOrderByCreateTimeAsc(IotTemplate.build(templateId), functionType);
|
|
|
List<IotMainModel> models = new ArrayList<>();
|
|
|
if (ObjectUtils.isNotEmpty(list)) {
|
|
|
- models = list.stream().map(this::toModel).collect(Collectors.toList());
|
|
|
+ models = list.stream().map(this::toAttributeModel).collect(Collectors.toList());
|
|
|
}
|
|
|
return ResultContent.buildSuccess(models);
|
|
|
}
|
|
|
@@ -575,7 +626,7 @@ public class IotServiceImpl extends SuperService {
|
|
|
if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
model = new IotTemplateModel();
|
|
|
BeanUtils.copyProperties(entity, model);
|
|
|
- // 设备数量
|
|
|
+ // (统计)设备数量
|
|
|
model.setDeviceCount(deviceInfoService.countByProductCode(entity.getProductCode()));
|
|
|
}
|
|
|
return model;
|
|
|
@@ -590,21 +641,6 @@ public class IotServiceImpl extends SuperService {
|
|
|
return model;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * IOTMain模型
|
|
|
- *
|
|
|
- * @param entity
|
|
|
- * @return
|
|
|
- */
|
|
|
- public IotMainModel toModel(IotMain entity) {
|
|
|
- IotMainModel model = null;
|
|
|
- if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
- model = new IotMainModel();
|
|
|
- BeanUtils.copyProperties(entity, model);
|
|
|
- }
|
|
|
- return model;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 属性 类型的model,包含值
|
|
|
*
|
|
|
@@ -616,11 +652,13 @@ public class IotServiceImpl extends SuperService {
|
|
|
if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
model = new IotMainAttributeModel();
|
|
|
BeanUtils.copyProperties(entity, model);
|
|
|
- IotDeviceData iotDeviceData = iotDeviceDataDao.findTopByIotMain(entity);
|
|
|
- if (ObjectUtils.isNotEmpty(iotDeviceData)) {
|
|
|
- IotDeviceDataModel deviceData = new IotDeviceDataModel();
|
|
|
- BeanUtils.copyProperties(iotDeviceData, deviceData);
|
|
|
- model.setDeviceData(deviceData);
|
|
|
+ if (entity.getFunctionType() != FunctionType.Attribute) {
|
|
|
+ IotDeviceData iotDeviceData = iotDeviceDataDao.findTopByIotMain(entity);
|
|
|
+ if (ObjectUtils.isNotEmpty(iotDeviceData)) {
|
|
|
+ IotDeviceDataModel deviceData = new IotDeviceDataModel();
|
|
|
+ BeanUtils.copyProperties(iotDeviceData, deviceData);
|
|
|
+ model.setDeviceData(deviceData);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return model;
|