|
|
@@ -0,0 +1,122 @@
|
|
|
+package com.zhongshu.iot.server.core.service.iot;
|
|
|
+
|
|
|
+import com.github.microservice.models.iotDeviceData.IotDeviceDataModel;
|
|
|
+import com.github.microservice.models.iotDeviceData.IotDeviceDataSearch;
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
+import com.github.microservice.types.deviceUse.IotDeviceDataType;
|
|
|
+import com.zhongshu.iot.client.type.IotDataType;
|
|
|
+import com.zhongshu.iot.server.core.dao.iot.IotDeviceDataDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.iot.IotMainDao;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.IotDeviceData;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.IotMain;
|
|
|
+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 java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author TRX
|
|
|
+ * @date 2025/3/5
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class IotDeviceDataService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotDeviceDataDao iotDeviceDataDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotMainDao iotMainDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存属性的值
|
|
|
+ *
|
|
|
+ * @param deviceId
|
|
|
+ * @param property
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent saveProperty(String deviceId, String property, Object value) {
|
|
|
+ List<IotMain> iotMains = iotMainDao.findByDeviceIdAndIdentifierAndIotDataType(deviceId, property, IotDataType.Device);
|
|
|
+ if (ObjectUtils.isNotEmpty(iotMains)) {
|
|
|
+ IotDeviceData iotDeviceData = iotDeviceDataDao.findTopByDeviceIdAndPropertyOrderByCreateTimeDesc(deviceId, property);
|
|
|
+ if (ObjectUtils.isNotEmpty(iotDeviceData)) {
|
|
|
+ iotDeviceData.setDataType(IotDeviceDataType.History);
|
|
|
+ iotDeviceDataDao.save(iotDeviceData);
|
|
|
+ }
|
|
|
+
|
|
|
+ IotMain iotMain = iotMains.get(0);
|
|
|
+ IotDeviceData temp = new IotDeviceData();
|
|
|
+ temp.setIotMain(iotMain);
|
|
|
+ temp.setIotTemplateId(iotMain.getIotMainTemplateId());
|
|
|
+ temp.setIotThingId(iotMain.getIotThingId());
|
|
|
+ temp.setDeviceId(deviceId);
|
|
|
+ temp.setProjectCode(iotMain.getProjectCode());
|
|
|
+ temp.setProperty(property);
|
|
|
+ temp.setData(value);
|
|
|
+ temp.setDataType(IotDeviceDataType.Current);
|
|
|
+ temp.setTimes();
|
|
|
+ iotDeviceDataDao.save(temp);
|
|
|
+
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ } else {
|
|
|
+ return ResultContent.buildFail("没找到对应属性");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到属性最新的值
|
|
|
+ *
|
|
|
+ * @param deviceId
|
|
|
+ * @param property
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object getDevicePropertyValue(String deviceId, String property) {
|
|
|
+ IotDeviceData entity = getDevicePropertyObject(deviceId, property);
|
|
|
+ if (entity != null) {
|
|
|
+ return entity.getData();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param deviceId
|
|
|
+ * @param property
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public IotDeviceData getDevicePropertyObject(String deviceId, String property) {
|
|
|
+ if (StringUtils.isNotEmpty(deviceId) && StringUtils.isNotEmpty(property)) {
|
|
|
+ return iotDeviceDataDao.findTopByDeviceIdAndPropertyAndDataTypeOrderByCreateTimeDesc(deviceId, property, IotDeviceDataType.Current);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询值
|
|
|
+ *
|
|
|
+ * @param pageable
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<Page<IotDeviceDataModel>> page(Pageable pageable, IotDeviceDataSearch param) {
|
|
|
+ Page<IotDeviceData> page = iotDeviceDataDao.page(pageable, param);
|
|
|
+ return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
|
|
|
+ }
|
|
|
+
|
|
|
+ public IotDeviceDataModel toModel(IotDeviceData entity) {
|
|
|
+ IotDeviceDataModel model = null;
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ model = new IotDeviceDataModel();
|
|
|
+ BeanUtils.copyProperties(entity, model);
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|