IotServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package com.zswl.dataservice.service.iot;
  2. import ch.qos.logback.core.model.NamedModel;
  3. import com.zswl.dataservice.dao.UserDao;
  4. import com.zswl.dataservice.dao.iot.IotMainDao;
  5. import com.zswl.dataservice.dao.iot.IotTemplateDao;
  6. import com.zswl.dataservice.dao.iot.IotTopicDao;
  7. import com.zswl.dataservice.dao.mqtt.DeviceInfoDao;
  8. import com.zswl.dataservice.dataConfig.ResultMessage;
  9. import com.zswl.dataservice.domain.iot.IotMain;
  10. import com.zswl.dataservice.domain.iot.IotTemplate;
  11. import com.zswl.dataservice.domain.iot.IotTopic;
  12. import com.zswl.dataservice.domain.mqtt.DeviceInfo;
  13. import com.zswl.dataservice.model.baseParam.NameModel;
  14. import com.zswl.dataservice.model.iot.*;
  15. import com.zswl.dataservice.model.mqtt.DeviceInfoModel;
  16. import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam;
  17. import com.zswl.dataservice.service.base.SuperService;
  18. import com.zswl.dataservice.type.FunctionType;
  19. import com.zswl.dataservice.type.IotDataType;
  20. import com.zswl.dataservice.type.ResultState;
  21. import com.zswl.dataservice.utils.bean.BeanUtil;
  22. import com.zswl.dataservice.utils.bean.BeanUtils;
  23. import com.zswl.dataservice.utils.page.PageEntityUtil;
  24. import com.zswl.dataservice.utils.result.ResultContent;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang3.ObjectUtils;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.data.domain.Page;
  30. import org.springframework.data.domain.Pageable;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.transaction.annotation.Transactional;
  33. import org.springframework.util.Assert;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import java.util.regex.Pattern;
  37. import java.util.stream.Collectors;
  38. /**
  39. * @author TRX
  40. * @date 2024/6/20
  41. */
  42. @Slf4j
  43. @Service
  44. public class IotServiceImpl extends SuperService {
  45. @Autowired
  46. private IotTemplateDao iotTemplateDao;
  47. @Autowired
  48. private IotMainDao iotMainDao;
  49. @Autowired
  50. private IotTopicDao iotTopicDao;
  51. @Autowired
  52. UserDao userDao;
  53. @Autowired
  54. DeviceInfoDao deviceInfoDao;
  55. //----------------------------- 模版 start----------------------------
  56. /**
  57. * 添加模版
  58. *
  59. * @param param
  60. * @return
  61. */
  62. public ResultContent addIotTemplate(IotTemplateParam param) {
  63. IotTemplate template = null;
  64. initDefaultUser(param);
  65. IotTemplate temp = iotTemplateDao.findTopByNameAndIotDataType(param.getName(), IotDataType.IotTemplate);
  66. if (StringUtils.isNotEmpty(param.getId())) {
  67. template = iotTemplateDao.findTopById(param.getId());
  68. if (ObjectUtils.isEmpty(template)) {
  69. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  70. }
  71. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
  72. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  73. }
  74. } else {
  75. template = new IotTemplate();
  76. if (ObjectUtils.isNotEmpty(temp)) {
  77. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  78. }
  79. template.setIotDataType(IotDataType.IotTemplate);
  80. }
  81. BeanUtils.copyProperties(param, template);
  82. iotTemplateDao.save(template);
  83. return ResultContent.buildSuccess();
  84. }
  85. /**
  86. * 删除模版
  87. *
  88. * @param id
  89. * @return
  90. */
  91. public ResultContent deleteTemplate(String id) {
  92. IotTemplate template = iotTemplateDao.findTopById(id);
  93. if (ObjectUtils.isEmpty(template)) {
  94. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  95. }
  96. iotTemplateDao.delete(template);
  97. return ResultContent.buildSuccess();
  98. }
  99. /**
  100. * 查询模版详情
  101. *
  102. * @param id
  103. * @return
  104. */
  105. public ResultContent<IotTemplateModel> getIotTemplate(String id) {
  106. IotTemplate template = iotTemplateDao.findTopById(id);
  107. if (ObjectUtils.isEmpty(template)) {
  108. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  109. }
  110. return ResultContent.buildSuccess(toModel(template));
  111. }
  112. /**
  113. * 模版分页
  114. *
  115. * @param pageable
  116. * @param param
  117. * @return
  118. */
  119. public ResultContent<Page<IotTemplateModel>> pageTemplate(Pageable pageable, IotTemplateSearch param) {
  120. initSearchParam(param);
  121. param.setIotDataType(IotDataType.IotTemplate);
  122. Page<IotTemplate> page = iotTemplateDao.page(pageable, param);
  123. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  124. }
  125. /**
  126. * 查询设备的物模型列表
  127. *
  128. * @param deviceId
  129. * @return
  130. */
  131. public ResultContent<List<IotTemplateModel>> getDeviceTemplateList(String deviceId) {
  132. List<IotTemplate> list = iotTemplateDao.findByDeviceIdAndIotDataTypeOrderByCreateTimeAsc(deviceId, IotDataType.Device);
  133. List<IotTemplateModel> models = new ArrayList<>();
  134. if (ObjectUtils.isNotEmpty(list)) {
  135. models = list.stream().map(this::toModel).collect(Collectors.toList());
  136. }
  137. return ResultContent.buildSuccess(models);
  138. }
  139. /**
  140. * 修改名称
  141. *
  142. * @param param
  143. * @return
  144. */
  145. public ResultContent updateIotTemplateName(NameModel param) {
  146. Assert.hasText(param.getName(), "name不能为空");
  147. IotTemplate template = iotTemplateDao.findTopById(param.getId());
  148. if (ObjectUtils.isEmpty(template)) {
  149. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  150. }
  151. // 检查名称是否重复
  152. if (template.getIotDataType() == IotDataType.IotTemplate) {
  153. // 模版
  154. IotTemplate temp = iotTemplateDao.findTopByNameAndIotDataType(param.getName(), IotDataType.IotTemplate);
  155. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(template.getId())) {
  156. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  157. }
  158. } else if (template.getIotDataType() == IotDataType.Device) {
  159. // 物模型
  160. IotTemplate temp = iotTemplateDao.findTopByNameAndDeviceIdAndIotDataType(
  161. param.getName(),
  162. template.getDeviceId(),
  163. IotDataType.Device);
  164. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(template.getId())) {
  165. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  166. }
  167. }
  168. BeanUtils.copyProperties(param, template);
  169. iotTemplateDao.save(template);
  170. return ResultContent.buildSuccess();
  171. }
  172. //----------------------------- 模版 end------------------------------
  173. //----------------------------- Topic start----------------------------
  174. public ResultContent addIotTopic(IotTopicParam param) {
  175. initDefaultUser(param);
  176. String iotTemplateId = param.getIotTemplateId();
  177. IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
  178. if (ObjectUtils.isEmpty(iotTemplate)) {
  179. return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
  180. }
  181. IotTopic entity = null;
  182. if (StringUtils.isNotEmpty(param.getId())) {
  183. entity = iotTopicDao.findTopById(param.getId());
  184. if (ObjectUtils.isEmpty(entity)) {
  185. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  186. }
  187. IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
  188. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
  189. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic()));
  190. }
  191. } else {
  192. entity = new IotTopic();
  193. IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
  194. if (ObjectUtils.isNotEmpty(temp)) {
  195. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic()));
  196. }
  197. }
  198. BeanUtils.copyProperties(param, entity);
  199. entity.setIotTemplate(iotTemplate);
  200. iotTopicDao.save(entity);
  201. return ResultContent.buildSuccess();
  202. }
  203. public ResultContent deleteIotTopic(String id) {
  204. IotTopic entity = iotTopicDao.findTopById(id);
  205. if (ObjectUtils.isEmpty(entity)) {
  206. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  207. }
  208. iotTopicDao.delete(entity);
  209. return ResultContent.buildSuccess();
  210. }
  211. public ResultContent<IotTopicModel> getIotTopic(String id) {
  212. IotTopic entity = iotTopicDao.findTopById(id);
  213. if (ObjectUtils.isEmpty(entity)) {
  214. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  215. }
  216. return ResultContent.buildSuccess(toModel(entity));
  217. }
  218. /**
  219. * 分页查询 模版的Topic管理数据
  220. *
  221. * @param pageable
  222. * @param param
  223. * @return
  224. */
  225. public ResultContent<Page<IotTopicModel>> pageIotTopic(Pageable pageable, IotTopicSearch param) {
  226. initSearchParam(param);
  227. Page<IotTopic> page = iotTopicDao.page(pageable, param);
  228. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  229. }
  230. //----------------------------- Topic end------------------------------
  231. //----------------------------- 物模型 start----------------------------
  232. /**
  233. * 设备绑定模版
  234. *
  235. * @param param
  236. * @return
  237. */
  238. @Transactional
  239. public ResultContent deviceBindIotTemplate(DeviceBindIotTemplate param) {
  240. DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceIdAndProjectInfoCode(param.getDeviceId(), param.getProjectInfoCode());
  241. if (ObjectUtils.isEmpty(deviceInfo)) {
  242. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getDeviceId()));
  243. }
  244. // 模版信息
  245. IotTemplate iotTemplate = iotTemplateDao.findTopById(param.getTemplateId());
  246. if (ObjectUtils.isEmpty(iotTemplate)) {
  247. return ResultContent.buildFail(String.format("模版ID不存在", param.getTemplateId()));
  248. }
  249. // 判断该设备是否已绑定该模版
  250. IotTemplate temp = iotTemplateDao.findTopByDeviceIdAndIotTemplateIdAndIotDataType(param.getDeviceId(),
  251. iotTemplate.getId(),
  252. IotDataType.Device);
  253. if (ObjectUtils.isNotEmpty(temp)) {
  254. return ResultContent.buildFail(String.format("设备【%s】已绑定模版%s", deviceInfo.getDeviceName(), iotTemplate.getName()));
  255. }
  256. IotTemplate newIotTemplate = new IotTemplate();
  257. BeanUtils.copyProperties(param, newIotTemplate, "id", "createTime", "updateTime");
  258. newIotTemplate.setIotDataType(IotDataType.Device);
  259. newIotTemplate.setIotTemplateId(param.getTemplateId());
  260. newIotTemplate.setDeviceId(deviceInfo.getDeviceId());
  261. newIotTemplate.setProjectCode(deviceInfo.getProjectInfoCode());
  262. iotTemplateDao.save(newIotTemplate);
  263. // 属性等数据
  264. List<IotMain> saveList = new ArrayList<>();
  265. List<IotMain> list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(iotTemplate);
  266. if (list != null) {
  267. list.stream().forEach(it -> {
  268. IotMain main = new IotMain();
  269. BeanUtils.copyProperties(it, main, "id", "createTime", "updateTime");
  270. main.setIotTemplate(newIotTemplate);
  271. main.setIotDataType(IotDataType.Device);
  272. main.setDeviceId(deviceInfo.getDeviceId());
  273. main.setProjectCode(deviceInfo.getProjectInfoCode());
  274. main.setAddTime(System.currentTimeMillis());
  275. initRealTopic(main);
  276. saveList.add(main);
  277. });
  278. }
  279. iotMainDao.saveAll(saveList);
  280. return ResultContent.buildSuccess();
  281. }
  282. /**
  283. * 添加属性 事件
  284. *
  285. * @param param
  286. * @return
  287. */
  288. public ResultContent addIotMain(IotMainParam param) {
  289. initDefaultUser(param);
  290. if (param.getFunctionType() == null) {
  291. return ResultContent.buildFail("functionType 不能为空");
  292. }
  293. Assert.hasText(param.getName(), "name不能为空");
  294. Assert.hasText(param.getIdentifier(), "identifier不能为空");
  295. Assert.hasText(param.getIotTopic(), "iotTopic不能为空");
  296. String iotTemplateId = param.getIotTemplateId();
  297. IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
  298. if (ObjectUtils.isEmpty(iotTemplate)) {
  299. return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
  300. }
  301. IotMain entity = null;
  302. // 判断名称是否重复
  303. IotMain nameTemp = iotMainDao.findTopByNameAndIotTemplate(param.getName(), iotTemplate);
  304. IotMain topicTemp = iotMainDao.findTopByIotTopicAndIotTemplate(param.getIotTopic(), iotTemplate);
  305. if (StringUtils.isNotEmpty(param.getId())) {
  306. entity = iotMainDao.findTopById(param.getId());
  307. if (ObjectUtils.isEmpty(entity)) {
  308. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  309. }
  310. if (ObjectUtils.isNotEmpty(nameTemp) && !nameTemp.getId().equals(param.getId())) {
  311. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  312. }
  313. if (ObjectUtils.isNotEmpty(topicTemp) && !topicTemp.getId().equals(param.getId())) {
  314. return ResultContent.buildFail(String.format("iotTopic已存在:%s", param.getIotTopic()));
  315. }
  316. } else {
  317. entity = new IotMain();
  318. if (ObjectUtils.isNotEmpty(nameTemp)) {
  319. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  320. }
  321. if (ObjectUtils.isNotEmpty(topicTemp)) {
  322. return ResultContent.buildFail(String.format("iotTopic已存在:%s", param.getIotTopic()));
  323. }
  324. }
  325. BeanUtils.copyProperties(param, entity);
  326. // 设备所属模型
  327. entity.setIotTemplate(iotTemplate);
  328. entity.setIotDataType(iotTemplate.getIotDataType());
  329. if (iotTemplate.getIotDataType() == IotDataType.Device) {
  330. entity.setDeviceId(iotTemplate.getDeviceId());
  331. entity.setProjectCode(iotTemplate.getProjectCode());
  332. initRealTopic(entity);
  333. }
  334. iotMainDao.save(entity);
  335. return ResultContent.buildSuccess();
  336. }
  337. private void initRealTopic(IotMain iotMain) {
  338. if (iotMain != null && iotMain.getIotDataType() == IotDataType.Device) {
  339. // 把 topic有占位符的换为具体的值
  340. String deviceId = iotMain.getDeviceId();
  341. String iotTopic = iotMain.getIotTopic();
  342. String realIotTopic = "";
  343. if (StringUtils.isNotEmpty(iotTopic)) {
  344. realIotTopic = iotTopic.replaceAll(Pattern.quote("${deviceId}"), deviceId);
  345. }
  346. iotMain.setRealIotTopic(realIotTopic);
  347. }
  348. }
  349. public ResultContent deleteIotMain(String id) {
  350. IotMain entity = iotMainDao.findTopById(id);
  351. if (ObjectUtils.isEmpty(entity)) {
  352. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  353. }
  354. iotMainDao.delete(entity);
  355. return ResultContent.buildSuccess();
  356. }
  357. public ResultContent<IotMainModel> getIotMain(String id) {
  358. IotMain entity = iotMainDao.findTopById(id);
  359. if (ObjectUtils.isEmpty(entity)) {
  360. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  361. }
  362. return ResultContent.buildSuccess(toModel(entity));
  363. }
  364. public ResultContent<Page<IotMainModel>> pageIotMain(Pageable pageable, IotMainSearch param) {
  365. initSearchParam(param);
  366. if (StringUtils.isEmpty(param.getIotTemplateId())) {
  367. return ResultContent.buildFail("iotTemplateId不能为空");
  368. }
  369. Page<IotMain> page = iotMainDao.page(pageable, param);
  370. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  371. }
  372. /**
  373. * 得到模版物模型的顶用数据
  374. *
  375. * @param templateId
  376. * @return
  377. */
  378. public ResultContent<List<IotMainModel>> getTemplateIotMainList(String templateId, FunctionType functionType) {
  379. List<IotMain> list = iotMainDao.findByIotTemplateAndFunctionTypeOrderByCreateTimeAsc(IotTemplate.build(templateId), functionType);
  380. List<IotMainModel> models = new ArrayList<>();
  381. if (ObjectUtils.isNotEmpty(list)) {
  382. models = list.stream().map(this::toModel).collect(Collectors.toList());
  383. }
  384. return ResultContent.buildSuccess(models);
  385. }
  386. //----------------------------- 物模型 end------------------------------
  387. public IotTemplateModel toModel(IotTemplate entity) {
  388. IotTemplateModel model = null;
  389. if (ObjectUtils.isNotEmpty(entity)) {
  390. model = new IotTemplateModel();
  391. BeanUtils.copyProperties(entity, model);
  392. }
  393. return model;
  394. }
  395. public IotTopicModel toModel(IotTopic entity) {
  396. IotTopicModel model = null;
  397. if (ObjectUtils.isNotEmpty(entity)) {
  398. model = new IotTopicModel();
  399. BeanUtils.copyProperties(entity, model);
  400. }
  401. return model;
  402. }
  403. public IotMainModel toModel(IotMain entity) {
  404. IotMainModel model = null;
  405. if (ObjectUtils.isNotEmpty(entity)) {
  406. model = new IotMainModel();
  407. BeanUtils.copyProperties(entity, model);
  408. }
  409. return model;
  410. }
  411. }