|
|
@@ -0,0 +1,194 @@
|
|
|
+package com.zhongshu.iot.server.core.service.iot;
|
|
|
+
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
+import com.github.microservice.types.deviceUse.DeviceType;
|
|
|
+import com.github.microservice.types.deviceUse.ThingType;
|
|
|
+import com.zhongshu.iot.client.model.iot.thing.IotThingParam;
|
|
|
+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.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.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.util.CommonUtil;
|
|
|
+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.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 物模型 管理服务
|
|
|
+ *
|
|
|
+ * @author TRX
|
|
|
+ * @date 2025/2/25
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class IotThingService extends SuperService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotThingDao iotThingDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotThing2DeviceService iotThing2DeviceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceInfoDao deviceInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceInfoService deviceInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotTemplateDao iotTemplateDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommonService commonService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotTemplateManagerService iotTemplateManagerService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加/编辑设备时 初始物模型
|
|
|
+ *
|
|
|
+ * @param deviceInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent initAutoThingByDevice(DeviceInfo deviceInfo) {
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo)) {
|
|
|
+ String productCode = deviceInfo.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));
|
|
|
+ }
|
|
|
+ IotThing iotThing = iotThingDao.findTopByDeviceIdAndThingType(deviceInfo.getDeviceId(), ThingType.Auto);
|
|
|
+ if (ObjectUtils.isEmpty(iotThing)) {
|
|
|
+ iotThing = new IotThing();
|
|
|
+ initEntity(iotThing);
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ iotThing.setName(deviceInfo.getDeviceName());
|
|
|
+ // 产品code
|
|
|
+ iotThing.setProductCode(productCode);
|
|
|
+ iotThing.setThingType(ThingType.Auto);
|
|
|
+ iotThing.setIotTemplateId(iotTemplate.getId());
|
|
|
+ // 分组code
|
|
|
+ iotThing.setProjectCode(iotTemplate.getProjectCode());
|
|
|
+
|
|
|
+ iotThing.setDeviceId(deviceInfo.getDeviceId());
|
|
|
+ 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.initAutoBindDevice(iotThing, deviceInfo);
|
|
|
+ updateThingDeviceCount(iotThing);
|
|
|
+ // 绑定物模型属性信息
|
|
|
+ iotTemplateManagerService.deviceBindAutoIotTemplate(iotThing, deviceInfo);
|
|
|
+ }
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ IotThing iotThing = iotThingDao.findTopByDeviceIdAndThingType(gateWayInfo.getGateWayId(), ThingType.Auto);
|
|
|
+ if (ObjectUtils.isEmpty(iotThing)) {
|
|
|
+ iotThing = new IotThing();
|
|
|
+ initEntity(iotThing);
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultContent saveIotThing(IotThingParam param) {
|
|
|
+
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新设备数量
|
|
|
+ *
|
|
|
+ * @param thingId
|
|
|
+ */
|
|
|
+ public void updateThingDeviceCount(String thingId) {
|
|
|
+ if (StringUtils.isNotEmpty(thingId)) {
|
|
|
+ IotThing entity = iotThingDao.findTopById(thingId);
|
|
|
+ updateThingDeviceCount(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新物模型绑定设备的数量等信息
|
|
|
+ *
|
|
|
+ * @param entity
|
|
|
+ */
|
|
|
+ public void updateThingDeviceCount(IotThing entity) {
|
|
|
+ if (entity != null) {
|
|
|
+ java.util.Map<String, Object> where = new HashMap<>();
|
|
|
+ where.put("id", entity.getId());
|
|
|
+
|
|
|
+ Map<String, Object> standardData = new HashMap<>();
|
|
|
+ standardData.put("deviceCount", iotThing2DeviceService.countByIotThingId(entity.getId()));
|
|
|
+
|
|
|
+ List<IotThing2Device> list = iotThing2DeviceService.getListByIotThingId(entity.getId());
|
|
|
+ List<String> deviceIds = new ArrayList<>();
|
|
|
+ List<DeviceType> deviceTypes = new ArrayList<>();
|
|
|
+ if (ObjectUtils.isNotEmpty(list)) {
|
|
|
+ deviceIds = list.stream().map(IotThing2Device::getDeviceId).collect(Collectors.toList());
|
|
|
+ deviceTypes = list.stream().map(IotThing2Device::getDeviceType).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ standardData.put("deviceIds", deviceIds);
|
|
|
+ standardData.put("deviceTypes", deviceTypes);
|
|
|
+ commonService.updateData(where, standardData, IotThing.class.getSimpleName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|