| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- 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<IotTemplateModel> 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<Page<IotTemplateModel>> pageTemplate(Pageable pageable, IotTemplateSearch param) {
- initSearchParam(param);
- param.setIotDataType(IotDataType.IotTemplate);
- Page<IotTemplate> page = iotTemplateDao.page(pageable, param);
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
- }
- /**
- * 查询设备的物模型列表
- *
- * @param deviceId
- * @return
- */
- public ResultContent<List<IotTemplateModel>> getDeviceTemplateList(String deviceId) {
- List<IotTemplate> list = iotTemplateDao.findByDeviceIdAndIotDataTypeOrderByCreateTimeAsc(deviceId, IotDataType.Device);
- List<IotTemplateModel> 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<IotTopicModel> 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<Page<IotTopicModel>> pageIotTopic(Pageable pageable, IotTopicSearch param) {
- initSearchParam(param);
- Page<IotTopic> 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<IotMain> saveList = new ArrayList<>();
- List<IotMain> 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<IotMainModel> 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<Page<IotMainModel>> pageIotMain(Pageable pageable, IotMainSearch param) {
- initSearchParam(param);
- if (StringUtils.isEmpty(param.getIotTemplateId())) {
- return ResultContent.buildFail("iotTemplateId不能为空");
- }
- Page<IotMain> page = iotMainDao.page(pageable, param);
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
- }
- /**
- * 得到模版物模型的顶用数据
- *
- * @param templateId
- * @return
- */
- public ResultContent<List<IotMainModel>> getTemplateIotMainList(String templateId, FunctionType functionType) {
- 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());
- }
- 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;
- }
- }
|