package com.zswl.dataservice.service.iot; import ch.qos.logback.core.model.NamedModel; import com.zswl.dataservice.dao.UserDao; import com.zswl.dataservice.dao.iot.IotMainDao; import com.zswl.dataservice.dao.iot.IotTemplateDao; import com.zswl.dataservice.dao.iot.IotTopicDao; import com.zswl.dataservice.dao.mqtt.DeviceInfoDao; import com.zswl.dataservice.dataConfig.ResultMessage; import com.zswl.dataservice.domain.iot.IotMain; import com.zswl.dataservice.domain.iot.IotTemplate; import com.zswl.dataservice.domain.iot.IotTopic; import com.zswl.dataservice.domain.mqtt.DeviceInfo; import com.zswl.dataservice.model.baseParam.NameModel; import com.zswl.dataservice.model.iot.*; import com.zswl.dataservice.model.mqtt.DeviceInfoModel; import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam; import com.zswl.dataservice.service.base.SuperService; import com.zswl.dataservice.type.FunctionType; import com.zswl.dataservice.type.IotDataType; import com.zswl.dataservice.type.ResultState; import com.zswl.dataservice.utils.bean.BeanUtil; import com.zswl.dataservice.utils.bean.BeanUtils; import com.zswl.dataservice.utils.page.PageEntityUtil; import com.zswl.dataservice.utils.result.ResultContent; 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 org.springframework.util.Assert; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; /** * @author TRX * @date 2024/6/20 */ @Slf4j @Service public class IotServiceImpl extends SuperService { @Autowired private IotTemplateDao iotTemplateDao; @Autowired private IotMainDao iotMainDao; @Autowired private IotTopicDao iotTopicDao; @Autowired UserDao userDao; @Autowired DeviceInfoDao deviceInfoDao; //----------------------------- 模版 start---------------------------- /** * 添加模版 * * @param param * @return */ public ResultContent addIotTemplate(IotTemplateParam param) { IotTemplate template = null; initDefaultUser(param); 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())); } if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) { return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName())); } } 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); iotTemplateDao.save(template); return ResultContent.buildSuccess(); } /** * 删除模版 * * @param id * @return */ public ResultContent deleteTemplate(String id) { IotTemplate template = iotTemplateDao.findTopById(id); if (ObjectUtils.isEmpty(template)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } iotTemplateDao.delete(template); return ResultContent.buildSuccess(); } /** * 查询模版详情 * * @param id * @return */ public ResultContent getIotTemplate(String id) { IotTemplate template = iotTemplateDao.findTopById(id); if (ObjectUtils.isEmpty(template)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } return ResultContent.buildSuccess(toModel(template)); } /** * 模版分页 * * @param pageable * @param param * @return */ public ResultContent> pageTemplate(Pageable pageable, IotTemplateSearch param) { initSearchParam(param); param.setIotDataType(IotDataType.IotTemplate); Page page = iotTemplateDao.page(pageable, param); return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel)); } /** * 查询设备的物模型列表 * * @param deviceId * @return */ public ResultContent> getDeviceTemplateList(String deviceId) { List list = iotTemplateDao.findByDeviceIdAndIotDataTypeOrderByCreateTimeAsc(deviceId, IotDataType.Device); List models = new ArrayList<>(); if (ObjectUtils.isNotEmpty(list)) { models = list.stream().map(this::toModel).collect(Collectors.toList()); } return ResultContent.buildSuccess(models); } /** * 修改名称 * * @param param * @return */ public ResultContent updateIotTemplateName(NameModel param) { Assert.hasText(param.getName(), "name不能为空"); IotTemplate template = iotTemplateDao.findTopById(param.getId()); if (ObjectUtils.isEmpty(template)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId())); } // 检查名称是否重复 if (template.getIotDataType() == IotDataType.IotTemplate) { // 模版 IotTemplate temp = iotTemplateDao.findTopByNameAndIotDataType(param.getName(), IotDataType.IotTemplate); if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(template.getId())) { return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName())); } } else if (template.getIotDataType() == IotDataType.Device) { // 物模型 IotTemplate temp = iotTemplateDao.findTopByNameAndDeviceIdAndIotDataType( param.getName(), template.getDeviceId(), IotDataType.Device); if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(template.getId())) { return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName())); } } BeanUtils.copyProperties(param, template); iotTemplateDao.save(template); return ResultContent.buildSuccess(); } //----------------------------- 模版 end------------------------------ //----------------------------- Topic start---------------------------- public ResultContent addIotTopic(IotTopicParam param) { initDefaultUser(param); String iotTemplateId = param.getIotTemplateId(); IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId); if (ObjectUtils.isEmpty(iotTemplate)) { return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId)); } IotTopic entity = null; if (StringUtils.isNotEmpty(param.getId())) { entity = iotTopicDao.findTopById(param.getId()); if (ObjectUtils.isEmpty(entity)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId())); } IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate); if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) { return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic())); } } else { entity = new IotTopic(); IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate); if (ObjectUtils.isNotEmpty(temp)) { return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic())); } } BeanUtils.copyProperties(param, entity); entity.setIotTemplate(iotTemplate); iotTopicDao.save(entity); return ResultContent.buildSuccess(); } public ResultContent deleteIotTopic(String id) { IotTopic entity = iotTopicDao.findTopById(id); if (ObjectUtils.isEmpty(entity)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } iotTopicDao.delete(entity); return ResultContent.buildSuccess(); } public ResultContent getIotTopic(String id) { IotTopic entity = iotTopicDao.findTopById(id); if (ObjectUtils.isEmpty(entity)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } return ResultContent.buildSuccess(toModel(entity)); } /** * 分页查询 模版的Topic管理数据 * * @param pageable * @param param * @return */ public ResultContent> pageIotTopic(Pageable pageable, IotTopicSearch param) { initSearchParam(param); Page page = iotTopicDao.page(pageable, param); return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel)); } //----------------------------- Topic end------------------------------ //----------------------------- 物模型 start---------------------------- /** * 设备绑定模版 * * @param param * @return */ @Transactional public ResultContent deviceBindIotTemplate(DeviceBindIotTemplate param) { DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceIdAndProjectInfoCode(param.getDeviceId(), param.getProjectInfoCode()); if (ObjectUtils.isEmpty(deviceInfo)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getDeviceId())); } // 模版信息 IotTemplate iotTemplate = iotTemplateDao.findTopById(param.getTemplateId()); if (ObjectUtils.isEmpty(iotTemplate)) { return ResultContent.buildFail(String.format("模版ID不存在", param.getTemplateId())); } // 判断该设备是否已绑定该模版 IotTemplate temp = iotTemplateDao.findTopByDeviceIdAndIotTemplateIdAndIotDataType(param.getDeviceId(), iotTemplate.getId(), IotDataType.Device); if (ObjectUtils.isNotEmpty(temp)) { return ResultContent.buildFail(String.format("设备【%s】已绑定模版%s", deviceInfo.getDeviceName(), iotTemplate.getName())); } IotTemplate newIotTemplate = new IotTemplate(); BeanUtils.copyProperties(param, newIotTemplate, "id", "createTime", "updateTime"); newIotTemplate.setIotDataType(IotDataType.Device); newIotTemplate.setIotTemplateId(param.getTemplateId()); newIotTemplate.setDeviceId(deviceInfo.getDeviceId()); newIotTemplate.setProjectCode(deviceInfo.getProjectInfoCode()); iotTemplateDao.save(newIotTemplate); // 属性等数据 List saveList = new ArrayList<>(); List list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(iotTemplate); if (list != null) { list.stream().forEach(it -> { IotMain main = new IotMain(); BeanUtils.copyProperties(it, main, "id", "createTime", "updateTime"); main.setIotTemplate(newIotTemplate); main.setIotDataType(IotDataType.Device); main.setDeviceId(deviceInfo.getDeviceId()); main.setProjectCode(deviceInfo.getProjectInfoCode()); main.setAddTime(System.currentTimeMillis()); initRealTopic(main); saveList.add(main); }); } iotMainDao.saveAll(saveList); return ResultContent.buildSuccess(); } /** * 添加属性 事件 * * @param param * @return */ public ResultContent addIotMain(IotMainParam param) { initDefaultUser(param); if (param.getFunctionType() == null) { return ResultContent.buildFail("functionType 不能为空"); } Assert.hasText(param.getName(), "name不能为空"); Assert.hasText(param.getIdentifier(), "identifier不能为空"); Assert.hasText(param.getIotTopic(), "iotTopic不能为空"); String iotTemplateId = param.getIotTemplateId(); IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId); if (ObjectUtils.isEmpty(iotTemplate)) { return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId)); } IotMain entity = null; // 判断名称是否重复 IotMain nameTemp = iotMainDao.findTopByNameAndIotTemplate(param.getName(), iotTemplate); IotMain topicTemp = iotMainDao.findTopByIotTopicAndIotTemplate(param.getIotTopic(), iotTemplate); 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())); } if (ObjectUtils.isNotEmpty(topicTemp) && !topicTemp.getId().equals(param.getId())) { return ResultContent.buildFail(String.format("iotTopic已存在:%s", param.getIotTopic())); } } else { entity = new IotMain(); if (ObjectUtils.isNotEmpty(nameTemp)) { return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName())); } if (ObjectUtils.isNotEmpty(topicTemp)) { return ResultContent.buildFail(String.format("iotTopic已存在:%s", param.getIotTopic())); } } BeanUtils.copyProperties(param, entity); // 设备所属模型 entity.setIotTemplate(iotTemplate); entity.setIotDataType(iotTemplate.getIotDataType()); if (iotTemplate.getIotDataType() == IotDataType.Device) { entity.setDeviceId(iotTemplate.getDeviceId()); entity.setProjectCode(iotTemplate.getProjectCode()); initRealTopic(entity); } iotMainDao.save(entity); return ResultContent.buildSuccess(); } private void initRealTopic(IotMain iotMain) { if (iotMain != null && iotMain.getIotDataType() == IotDataType.Device) { // 把 topic有占位符的换为具体的值 String deviceId = iotMain.getDeviceId(); String iotTopic = iotMain.getIotTopic(); String realIotTopic = ""; if (StringUtils.isNotEmpty(iotTopic)) { realIotTopic = iotTopic.replaceAll(Pattern.quote("${deviceId}"), deviceId); } iotMain.setRealIotTopic(realIotTopic); } } public ResultContent deleteIotMain(String id) { IotMain entity = iotMainDao.findTopById(id); if (ObjectUtils.isEmpty(entity)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } iotMainDao.delete(entity); return ResultContent.buildSuccess(); } public ResultContent getIotMain(String id) { IotMain entity = iotMainDao.findTopById(id); if (ObjectUtils.isEmpty(entity)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } return ResultContent.buildSuccess(toModel(entity)); } public ResultContent> pageIotMain(Pageable pageable, IotMainSearch param) { initSearchParam(param); if (StringUtils.isEmpty(param.getIotTemplateId())) { return ResultContent.buildFail("iotTemplateId不能为空"); } Page page = iotMainDao.page(pageable, param); return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel)); } /** * 得到模版物模型的顶用数据 * * @param templateId * @return */ public ResultContent> getTemplateIotMainList(String templateId, FunctionType functionType) { List list = iotMainDao.findByIotTemplateAndFunctionTypeOrderByCreateTimeAsc(IotTemplate.build(templateId), functionType); List models = new ArrayList<>(); if (ObjectUtils.isNotEmpty(list)) { models = list.stream().map(this::toModel).collect(Collectors.toList()); } return ResultContent.buildSuccess(models); } //----------------------------- 物模型 end------------------------------ public IotTemplateModel toModel(IotTemplate entity) { IotTemplateModel model = null; if (ObjectUtils.isNotEmpty(entity)) { model = new IotTemplateModel(); BeanUtils.copyProperties(entity, model); } return model; } public IotTopicModel toModel(IotTopic entity) { IotTopicModel model = null; if (ObjectUtils.isNotEmpty(entity)) { model = new IotTopicModel(); BeanUtils.copyProperties(entity, model); } return model; } public IotMainModel toModel(IotMain entity) { IotMainModel model = null; if (ObjectUtils.isNotEmpty(entity)) { model = new IotMainModel(); BeanUtils.copyProperties(entity, model); } return model; } }