|
|
@@ -1,27 +1,37 @@
|
|
|
package com.zhongshu.iot.server.core.service.iot;
|
|
|
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
+import com.github.microservice.net.ResultMessage;
|
|
|
import com.github.microservice.types.deviceUse.DeviceType;
|
|
|
import com.github.microservice.types.deviceUse.ThingType;
|
|
|
+import com.zhongshu.iot.client.model.iot.thing.IotThingModel;
|
|
|
import com.zhongshu.iot.client.model.iot.thing.IotThingParam;
|
|
|
+import com.zhongshu.iot.client.model.iot.thing.IotThingSearch;
|
|
|
import com.zhongshu.iot.client.type.IotDataType;
|
|
|
import com.zhongshu.iot.server.core.dao.iot.IotTemplateDao;
|
|
|
import com.zhongshu.iot.server.core.dao.iot.IotThingDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.mqtt.ProjectInfoDao;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.IotTemplate;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.IotThing;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.IotThing2Device;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayInfo;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.mqtt.ProjectInfo;
|
|
|
import com.zhongshu.iot.server.core.service.base.CommonService;
|
|
|
import com.zhongshu.iot.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.iot.server.core.service.device.DeviceInfoService;
|
|
|
+import com.zhongshu.iot.server.core.service.device.ProjectInfoService;
|
|
|
import com.zhongshu.iot.server.core.util.CommonUtil;
|
|
|
+import com.zhongshu.iot.server.core.util.bean.BeanUtils;
|
|
|
+import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
@@ -60,6 +70,12 @@ public class IotThingService extends SuperService {
|
|
|
@Autowired
|
|
|
private IotTemplateManagerService iotTemplateManagerService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ProjectInfoDao projectInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProjectInfoService projectInfoService;
|
|
|
+
|
|
|
/**
|
|
|
* 添加/编辑设备时 初始物模型
|
|
|
*
|
|
|
@@ -76,6 +92,7 @@ public class IotThingService extends SuperService {
|
|
|
if (ObjectUtils.isEmpty(iotTemplate)) {
|
|
|
return ResultContent.buildFail(String.format("产品code不存在", productCode));
|
|
|
}
|
|
|
+ String projectCode = deviceInfo.getProjectInfoCode();
|
|
|
IotThing iotThing = iotThingDao.findTopByDeviceIdAndThingType(deviceInfo.getDeviceId(), ThingType.Auto);
|
|
|
if (ObjectUtils.isEmpty(iotThing)) {
|
|
|
iotThing = new IotThing();
|
|
|
@@ -88,7 +105,7 @@ public class IotThingService extends SuperService {
|
|
|
iotThing.setThingType(ThingType.Auto);
|
|
|
iotThing.setIotTemplateId(iotTemplate.getId());
|
|
|
// 分组code
|
|
|
- iotThing.setProjectCode(iotTemplate.getProjectCode());
|
|
|
+ iotThing.setProjectCode(projectCode);
|
|
|
|
|
|
iotThing.setDeviceId(deviceInfo.getDeviceId());
|
|
|
iotThing.setDeviceCategory(iotTemplate.getDeviceCategory());
|
|
|
@@ -100,7 +117,9 @@ public class IotThingService extends SuperService {
|
|
|
}
|
|
|
}
|
|
|
iotThingDao.save(iotThing);
|
|
|
- iotThing2DeviceService.initAutoBindDevice(iotThing, deviceInfo);
|
|
|
+ // 物模型 绑定设备
|
|
|
+ iotThing2DeviceService.bindDevice(iotThing, deviceInfo);
|
|
|
+ // 更新物模型 对应的设备数量
|
|
|
updateThingDeviceCount(iotThing);
|
|
|
// 绑定物模型属性信息
|
|
|
iotTemplateManagerService.deviceBindAutoIotTemplate(iotThing, deviceInfo);
|
|
|
@@ -108,51 +127,84 @@ public class IotThingService extends SuperService {
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
- public ResultContent initAutoThingByGateWay(GateWayInfo gateWayInfo) {
|
|
|
- if (ObjectUtils.isNotEmpty(gateWayInfo)) {
|
|
|
- String productCode = gateWayInfo.getProductCode();
|
|
|
- if (StringUtils.isEmpty(productCode)) {
|
|
|
- return ResultContent.buildFail("关联的产品code为空");
|
|
|
- }
|
|
|
- IotTemplate iotTemplate = iotTemplateDao.findTopByProductCodeAndIotDataType(productCode, IotDataType.IotTemplate);
|
|
|
- if (ObjectUtils.isEmpty(iotTemplate)) {
|
|
|
- return ResultContent.buildFail(String.format("产品code不存在", productCode));
|
|
|
+ /**
|
|
|
+ * 保存物模型
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent saveIotThing(IotThingParam param) {
|
|
|
+ IotThing entity = null;
|
|
|
+ if (StringUtils.isEmpty(param.getId())) {
|
|
|
+ entity = iotThingDao.findTopById(param.getId());
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
|
|
|
}
|
|
|
- IotThing iotThing = iotThingDao.findTopByDeviceIdAndThingType(gateWayInfo.getGateWayId(), ThingType.Auto);
|
|
|
- if (ObjectUtils.isEmpty(iotThing)) {
|
|
|
- iotThing = new IotThing();
|
|
|
- initEntity(iotThing);
|
|
|
- } else {
|
|
|
+ if (entity.getThingType() == ThingType.Auto) {
|
|
|
+ return ResultContent.buildFail("该数据不能修改");
|
|
|
}
|
|
|
- iotThing.setName(gateWayInfo.getGateWayName());
|
|
|
- // 产品code
|
|
|
- iotThing.setProductCode(productCode);
|
|
|
- iotThing.setThingType(ThingType.Auto);
|
|
|
- iotThing.setIotTemplateId(iotTemplate.getId());
|
|
|
- // 分组code
|
|
|
- iotThing.setProjectCode(iotTemplate.getProjectCode());
|
|
|
-
|
|
|
- iotThing.setDeviceId(gateWayInfo.getGateWayId());
|
|
|
- iotThing.setDeviceCategory(iotTemplate.getDeviceCategory());
|
|
|
- if (StringUtils.isEmpty(iotThing.getCode())) {
|
|
|
- if (iotThing.getDeviceCategory() != null) {
|
|
|
- iotThing.setCode(String.format("%s%s", iotThing.getDeviceCategory().name(), CommonUtil.randomStr(10)));
|
|
|
- } else {
|
|
|
- iotThing.setCode(String.format("%s%s", CommonUtil.randomStr(10)));
|
|
|
- }
|
|
|
- }
|
|
|
- iotThingDao.save(iotThing);
|
|
|
- iotThing2DeviceService.initAutoBindGateWay(iotThing, gateWayInfo);
|
|
|
- updateThingDeviceCount(iotThing);
|
|
|
+ } else {
|
|
|
+ entity = new IotThing();
|
|
|
+ entity.setThingType(ThingType.Manual);
|
|
|
+ entity.setCode(String.format("ML%s", CommonUtil.randomStr(10)));
|
|
|
}
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
+ ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectCode());
|
|
|
+ if (ObjectUtils.isEmpty(projectInfo)) {
|
|
|
+ return ResultContent.buildFail(String.format("分组不存在"));
|
|
|
+ }
|
|
|
+ entity.setProjectCode(projectInfo.getCode());
|
|
|
+ iotThingDao.save(entity);
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
- public ResultContent saveIotThing(IotThingParam param) {
|
|
|
+ /**
|
|
|
+ * 物模型分页查询
|
|
|
+ *
|
|
|
+ * @param pageable
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<Page<IotThingModel>> page(Pageable pageable, IotThingSearch param) {
|
|
|
+ Page<IotThing> page = iotThingDao.page(pageable, param);
|
|
|
+ return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除物模型
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public ResultContent deleteIotThing(String id) {
|
|
|
+ IotThing entity = iotThingDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ if (entity.getThingType() == ThingType.Auto) {
|
|
|
+ return ResultContent.buildFail("改物模型不能删除");
|
|
|
+ }
|
|
|
+ // 删除物模型和设备的绑定
|
|
|
+ iotThing2DeviceService.deleteByThingId(entity.getId());
|
|
|
+ // 删除物模型的 属性分组
|
|
|
+ iotTemplateManagerService.deleteIotTemplateByThingId(entity.getId());
|
|
|
+ // 删除物模型的属性、方法、服务等信息
|
|
|
+ iotTemplateManagerService.deleteIotMainByThingId(entity.getId());
|
|
|
+ // 删除自己
|
|
|
+ iotThingDao.delete(entity);
|
|
|
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
+ public ResultContent<IotThingModel> getDetail(String id) {
|
|
|
+ IotThing entity = iotThingDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess(toModel(entity));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 更新设备数量
|
|
|
*
|
|
|
@@ -191,4 +243,14 @@ public class IotThingService extends SuperService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public IotThingModel toModel(IotThing entity) {
|
|
|
+ IotThingModel model = null;
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ model = new IotThingModel();
|
|
|
+ BeanUtils.copyProperties(entity, model);
|
|
|
+ model.setProjectInfo(projectInfoService.toModel(projectInfoDao.findTopByCode(entity.getProjectCode())));
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
}
|