TRX 1 anno fa
parent
commit
bb94db176c

+ 0 - 20
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotDeviceDataModel.java

@@ -1,20 +0,0 @@
-package com.zhongshu.iot.client.model.iot;
-
-import com.github.microservice.models.baseParam.SuperModel;
-import io.swagger.v3.oas.annotations.media.Schema;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-/**
- * @author TRX
- * @date 2024/7/16
- */
-@Data
-@AllArgsConstructor
-@NoArgsConstructor
-public class IotDeviceDataModel extends SuperModel {
-    @Schema(description = "对应的值")
-    private Object data;
-
-}

+ 2 - 3
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainAttributeModel.java

@@ -16,7 +16,6 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 public class IotMainAttributeModel extends IotMainModel {
 
-    @Schema(description = "属性的值")
-    private IotDeviceDataModel deviceData;
-
+    @Schema(description = "属性对应的最新值")
+    private Object value;
 }

+ 8 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/IotDeviceDataDao.java

@@ -1,16 +1,23 @@
 package com.zhongshu.iot.server.core.dao.iot;
 
+import com.github.microservice.types.deviceUse.IotDeviceDataType;
+import com.zhongshu.iot.server.core.dao.iot.extend.IotDeviceDataDaoExtend;
 import com.zhongshu.iot.server.core.domain.iot.IotDeviceData;
 import com.zhongshu.iot.server.core.domain.iot.IotMain;
+import org.springframework.data.mongodb.repository.MongoRepository;
 
 /**
  * @author TRX
  * @date 2024/3/21
  */
-public interface IotDeviceDataDao extends org.springframework.data.mongodb.repository.MongoRepository<IotDeviceData, String> {
+public interface IotDeviceDataDao extends MongoRepository<IotDeviceData, String>, IotDeviceDataDaoExtend {
 
     IotDeviceData findTopById(String id);
 
     IotDeviceData findTopByIotMain(IotMain iotMain);
 
+    IotDeviceData findTopByDeviceIdAndPropertyOrderByCreateTimeDesc(String deviceId, String property);
+
+    IotDeviceData findTopByDeviceIdAndPropertyAndDataTypeOrderByCreateTimeDesc(String deviceId, String property, IotDeviceDataType dataType);
+
 }

+ 17 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/extend/IotDeviceDataDaoExtend.java

@@ -0,0 +1,17 @@
+package com.zhongshu.iot.server.core.dao.iot.extend;
+
+import com.github.microservice.models.iotDeviceData.IotDeviceDataSearch;
+import com.zhongshu.iot.server.core.domain.iot.IotDeviceData;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+/**
+ * @Author TRX
+ * @CreateDate: 2023/7/7
+ * @Version: 1.0
+ */
+public interface IotDeviceDataDaoExtend {
+
+    Page<IotDeviceData> page(Pageable pageable, IotDeviceDataSearch param);
+
+}

+ 75 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/impl/IotDeviceDataDaoImpl.java

@@ -0,0 +1,75 @@
+package com.zhongshu.iot.server.core.dao.iot.impl;
+
+import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
+import com.github.microservice.models.iotDeviceData.IotDeviceDataSearch;
+import com.zhongshu.iot.server.core.dao.base.BaseImpl;
+import com.zhongshu.iot.server.core.dao.iot.extend.IotDeviceDataDaoExtend;
+import com.zhongshu.iot.server.core.domain.iot.IotDeviceData;
+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.data.domain.Sort;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+
+/**
+ * @Author TRX
+ * @CreateDate: 2023/4/12
+ * @Version: 1.0
+ */
+public class IotDeviceDataDaoImpl extends BaseImpl implements IotDeviceDataDaoExtend {
+
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
+    @Autowired
+    private DBHelper dbHelper;
+
+    @Override
+    public Page<IotDeviceData> page(Pageable pageable, IotDeviceDataSearch param) {
+        Criteria criteria = buildFilterCriteria(param);
+        Sort sort = buildSort(param);
+        Query query = Query.query(criteria);
+        query.with(sort);
+        return dbHelper.pages(query, pageable, IotDeviceData.class);
+    }
+
+    private Criteria buildFilterCriteria(IotDeviceDataSearch param) {
+        Criteria criteria = buildCriteriaAboutTime(param);
+
+        if (StringUtils.isNotEmpty(param.getDeviceId())) {
+            criteria.and("deviceId").is(param.getDeviceId());
+        }
+
+        if (ObjectUtils.isNotEmpty(param.getProperties())) {
+            criteria.and("property").is(param.getProperties());
+        }
+
+        if (StringUtils.isNotEmpty(param.getProjectCode())) {
+            criteria.and("projectCode").is(param.getProjectCode());
+        }
+
+        if (StringUtils.isNotEmpty(param.getIotThingId())) {
+            criteria.and("iotThingId").is(param.getIotThingId());
+        }
+
+        if (param.getDataType() != null) {
+            criteria.and("dataType").is(param.getDataType());
+        }
+
+        // 模糊搜索
+//        List<Criteria> criterias = new ArrayList<>();
+//        if (StringUtils.isNotEmpty(param.getDeviceName())) {
+//            Pattern pattern = Pattern.compile("^.*" + param.getDeviceName() + ".*$");
+//            criterias.add(Criteria.where("deviceName").is(pattern));
+//        }
+//        if (!CollectionUtils.isEmpty(criterias)) {
+//            criteria.andOperator(criterias.toArray(new Criteria[]{}));
+//        }
+        return criteria;
+    }
+
+}

+ 0 - 3
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/IotDeviceData.java

@@ -37,9 +37,6 @@ public class IotDeviceData extends SuperEntity {
     @Schema(description = "所属分组code")
     private String projectCode;
 
-    @Schema(description = "关联字段")
-    private IotAttribute iotAttribute;
-
     @Schema(description = "属性值")
     private String property;
 

+ 122 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotDeviceDataService.java

@@ -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;
+    }
+
+}

+ 10 - 3
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotPropertyService.java

@@ -38,6 +38,9 @@ public class IotPropertyService extends SuperService {
     @Autowired
     private IotDeviceDataDao iotDeviceDataDao;
 
+    @Autowired
+    private IotDeviceDataService iotDeviceDataService;
+
     /**
      * 属性上传处理
      *
@@ -78,8 +81,12 @@ public class IotPropertyService extends SuperService {
         }
     }
 
-    private void saveProperty(String deviceId, String key, Object value, PropertyResult.DeviceAttrs deviceAttrs) {
-
-        deviceAttrs.addSuccess(key);
+    private void saveProperty(String deviceId, String property, Object value, PropertyResult.DeviceAttrs deviceAttrs) {
+        ResultContent resultContent = iotDeviceDataService.saveProperty(deviceId, property, value);
+        if (resultContent.isSuccess()) {
+            deviceAttrs.setSuccess();
+        } else {
+            deviceAttrs.setFailed(resultContent.getMsg());
+        }
     }
 }

+ 11 - 8
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotServiceImpl.java

@@ -14,7 +14,10 @@ import com.zhongshu.iot.server.core.dao.iot.*;
 import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
 import com.zhongshu.iot.server.core.dao.mqtt.GateWay2DeviceDao;
 import com.zhongshu.iot.server.core.dataConfig.ResultMessage;
-import com.zhongshu.iot.server.core.domain.iot.*;
+import com.zhongshu.iot.server.core.domain.iot.IotMain;
+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.IotTopic;
 import com.zhongshu.iot.server.core.domain.iot.device.DeviceInfo;
 import com.zhongshu.iot.server.core.domain.iot.device.GateWay2Device;
 import com.zhongshu.iot.server.core.service.base.CommonService;
@@ -83,6 +86,9 @@ public class IotServiceImpl extends SuperService {
     @Autowired
     private IotThingSyncService iotThingSyncService;
 
+    @Autowired
+    private IotDeviceDataService iotDeviceDataService;
+
     //----------------------------- 模版 start----------------------------
 
     /**
@@ -676,13 +682,10 @@ public class IotServiceImpl extends SuperService {
         if (ObjectUtils.isNotEmpty(entity)) {
             model = new IotMainAttributeModel();
             BeanUtils.copyProperties(entity, model);
-            if (entity.getFunctionType() != FunctionType.Attribute) {
-                IotDeviceData iotDeviceData = iotDeviceDataDao.findTopByIotMain(entity);
-                if (ObjectUtils.isNotEmpty(iotDeviceData)) {
-                    IotDeviceDataModel deviceData = new IotDeviceDataModel();
-                    BeanUtils.copyProperties(iotDeviceData, deviceData);
-                    model.setDeviceData(deviceData);
-                }
+
+            // 属性值
+            if (entity.getFunctionType() == FunctionType.Attribute && entity.getIotDataType() == IotDataType.Device) {
+                model.setValue(iotDeviceDataService.getDevicePropertyValue(entity.getDeviceId(), entity.getIdentifier()));
             }
         }
         return model;