浏览代码

机构类型

TRX 1 年之前
父节点
当前提交
e3266bea8f

+ 49 - 6
src/main/java/com/zswl/dataservice/controller/hardware/DeviceController.java

@@ -1,8 +1,10 @@
 package com.zswl.dataservice.controller.hardware;
 
+import com.zswl.dataservice.model.iot.IotMainModel;
 import com.zswl.dataservice.model.mqtt.DeviceInfoAddParam;
 import com.zswl.dataservice.model.mqtt.DeviceInfoModel;
 import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam;
+import com.zswl.dataservice.model.mqtt.DeviceInfoUpdateRemark;
 import com.zswl.dataservice.service.mqtt.DeviceInfoService;
 import com.zswl.dataservice.utils.result.ResultContent;
 import io.swagger.v3.oas.annotations.Operation;
@@ -14,10 +16,9 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
 import org.springframework.util.Assert;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
 
 /**
  * 设备管理 服务
@@ -50,13 +51,55 @@ public class DeviceController {
 
     @Operation(summary = "删除设备")
     @RequestMapping(value = "deleteDeviceInfo", method = {RequestMethod.GET})
-    public ResultContent deleteDeviceInfo(String deviceId) {
+    public ResultContent deleteDeviceInfo(
+            @Parameter(name = "deviceId", description = "数据ID", example = "")
+            @RequestParam("deviceId") String deviceId) {
         return deviceInfoService.deleteDeviceInfo(deviceId);
     }
 
     @Operation(summary = "查询设备")
     @RequestMapping(value = "getDeviceById", method = {RequestMethod.GET})
-    public ResultContent<DeviceInfoModel> getDeviceById(String deviceId) {
+    public ResultContent<DeviceInfoModel> getDeviceById(
+            @Parameter(name = "deviceId", description = "数据ID", example = "")
+            @RequestParam("deviceId") String deviceId) {
         return deviceInfoService.getDeviceById(deviceId);
     }
+
+    @Operation(summary = "编辑备注")
+    @RequestMapping(value = "updateRemark", method = {RequestMethod.POST})
+    public ResultContent updateRemark(@RequestBody DeviceInfoUpdateRemark param) {
+        Assert.hasText(param.getId(), "ID不能为空");
+        return deviceInfoService.updateRemark(param);
+    }
+
+    @Operation(summary = "编辑关联的模版")
+    @RequestMapping(value = "updateIopTemplate", method = {RequestMethod.POST})
+    public ResultContent updateIopTemplate(@RequestBody DeviceInfoUpdateRemark param) {
+        Assert.hasText(param.getId(), "ID不能为空");
+        return deviceInfoService.updateIopTemplate(param);
+    }
+
+    @Operation(summary = "改变是否上传日志")
+    @RequestMapping(value = "updateIsReportLogs", method = {RequestMethod.POST})
+    public ResultContent updateIsReportLogs(@RequestBody DeviceInfoUpdateRemark param) {
+        Assert.hasText(param.getId(), "ID不能为空");
+        return deviceInfoService.updateIsReportLogs(param);
+    }
+
+    @Operation(summary = "ping设备")
+    @RequestMapping(value = "testDevice", method = {RequestMethod.GET})
+    public ResultContent<Long> testDevice(
+            @Parameter(name = "deviceId", description = "数据ID", example = "")
+            @RequestParam("deviceId") String deviceId) {
+        return deviceInfoService.testDevice(deviceId);
+    }
+
+    @Operation(summary = "物模型数据")
+    @RequestMapping(value = "getDeviceIotMainData", method = {RequestMethod.GET})
+    public ResultContent<List<IotMainModel>> getDeviceIotMainData(
+            @Parameter(name = "deviceId", description = "数据ID", example = "")
+            @RequestParam("deviceId") String deviceId) {
+        return deviceInfoService.getDeviceIotMain(deviceId);
+    }
+
 }

+ 4 - 0
src/main/java/com/zswl/dataservice/dao/iot/IotMainDao.java

@@ -6,6 +6,8 @@ import com.zswl.dataservice.dao.mqtt.extend.DeviceInfoDaoExtend;
 import com.zswl.dataservice.domain.iot.IotMain;
 import com.zswl.dataservice.domain.iot.IotTemplate;
 
+import java.util.List;
+
 /**
  * @author TRX
  * @date 2024/3/21
@@ -14,4 +16,6 @@ public interface IotMainDao extends MongoDao<IotMain>, IotMainDaoExtend {
 
     IotMain findTopById(String id);
 
+    List<IotMain> findByIotTemplateOrderByCreateTimeAsc(IotTemplate iotTemplate);
+
 }

+ 35 - 0
src/main/java/com/zswl/dataservice/domain/mqtt/DeviceInfo.java

@@ -1,10 +1,14 @@
 package com.zswl.dataservice.domain.mqtt;
 
+import cn.hutool.json.JSONObject;
 import com.zswl.dataservice.domain.base.SuperEntity;
+import com.zswl.dataservice.domain.iot.IotTemplate;
+import com.zswl.dataservice.utils.mqtt.type.OnLineState;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import org.springframework.data.mongodb.core.mapping.DBRef;
 import org.springframework.data.mongodb.core.mapping.Document;
 
 /**
@@ -25,4 +29,35 @@ public class DeviceInfo extends SuperEntity {
     @Schema(description = "设备名称")
     private String deviceName;
 
+    @Schema(description = "在线状态")
+    private OnLineState onLineState;
+
+    @Schema(description = "ip地址")
+    private String ip;
+
+    @Schema(description = "激活时间")
+    private Long activityTime;
+
+    @Schema(description = "最后上线时间")
+    private Long lastOnlineTime;
+
+    @Schema(description = "所属项目")
+    @DBRef(lazy = true)
+    private ProjectInfo projectInfo;
+
+    @Schema(description = "固件版本")
+    private String firmwareVersion;
+
+    @Schema(description = "连接参数")
+    private JSONObject connectParam;
+
+    @Schema(description = "最后离线时间")
+    private Long lastOffLineTime;
+
+    @Schema(description = "设备本地日志上报")
+    private Boolean isReportLogs = Boolean.TRUE;
+
+    @Schema(description = "物联网模版信息")
+    @DBRef(lazy = true)
+    private IotTemplate iotTemplate;
 }

+ 27 - 0
src/main/java/com/zswl/dataservice/model/mqtt/DeviceInfoAddParam.java

@@ -1,8 +1,13 @@
 package com.zswl.dataservice.model.mqtt;
 
+import cn.hutool.json.JSONObject;
+import com.zswl.dataservice.domain.iot.IotTemplate;
+import com.zswl.dataservice.domain.mqtt.ProjectInfo;
 import com.zswl.dataservice.model.baseParam.SuperParam;
+import com.zswl.dataservice.utils.mqtt.type.OnLineState;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
+import org.springframework.data.mongodb.core.mapping.DBRef;
 
 /**
  * @author TRX
@@ -16,4 +21,26 @@ public class DeviceInfoAddParam extends SuperParam {
 
     @Schema(description = "设备名称")
     private String deviceName;
+
+    @Schema(description = "在线状态")
+    private OnLineState onLineState;
+
+    @Schema(description = "ip地址")
+    private String ip;
+
+    @Schema(description = "所属项目")
+    @DBRef(lazy = true)
+    private String projectInfoOid;
+
+    @Schema(description = "固件版本")
+    private String firmwareVersion;
+
+    @Schema(description = "连接参数")
+    private JSONObject connectParam;
+
+    private Boolean isReportLogs = Boolean.TRUE;
+
+    @Schema(description = "物联网模版信息")
+    @DBRef(lazy = true)
+    private String iotTemplateId;
 }

+ 45 - 0
src/main/java/com/zswl/dataservice/model/mqtt/DeviceInfoModel.java

@@ -1,8 +1,14 @@
 package com.zswl.dataservice.model.mqtt;
 
+import cn.hutool.json.JSONObject;
+import com.zswl.dataservice.domain.iot.IotTemplate;
+import com.zswl.dataservice.domain.mqtt.ProjectInfo;
 import com.zswl.dataservice.model.baseParam.SuperModel;
+import com.zswl.dataservice.model.iot.IotTemplateModel;
+import com.zswl.dataservice.utils.mqtt.type.OnLineState;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
+import org.springframework.data.mongodb.core.mapping.DBRef;
 
 /**
  * @author TRX
@@ -16,4 +22,43 @@ public class DeviceInfoModel extends SuperModel {
 
     @Schema(description = "设备名称")
     private String deviceName;
+
+    @Schema(description = "在线状态")
+    private OnLineState onLineState;
+
+    private String onLineStateStr;
+
+    public String getOnLineStateStr() {
+        if (onLineState != null) {
+            return onLineState.getRemark();
+        }
+        return "";
+    }
+
+    @Schema(description = "ip地址")
+    private String ip;
+
+    @Schema(description = "激活时间")
+    private Long activityTime;
+
+    @Schema(description = "最后上线时间")
+    private Long lastOnlineTime;
+
+    @Schema(description = "所属项目")
+    private ProjectInfoModel projectInfo;
+
+    @Schema(description = "固件版本")
+    private String firmwareVersion;
+
+    @Schema(description = "连接参数")
+    private JSONObject connectParam;
+
+    @Schema(description = "最后离线时间")
+    private Long lastOffLineTime;
+
+    @Schema(description = "设备本地日志上报")
+    private Boolean isReportLogs = Boolean.TRUE;
+
+    @Schema(description = "物联网模版信息")
+    private IotTemplateModel iotTemplate;
 }

+ 24 - 0
src/main/java/com/zswl/dataservice/model/mqtt/DeviceInfoUpdateRemark.java

@@ -0,0 +1,24 @@
+package com.zswl.dataservice.model.mqtt;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/6/23
+ */
+@Data
+public class DeviceInfoUpdateRemark {
+
+    @Schema(description = "数据ID")
+    private String id;
+
+    @Schema(description = "备注")
+    private String remark;
+
+    @Schema(description = "模版ID")
+    private String iotTemplateId;
+
+    @Schema(description = "设备本地日志上报")
+    private Boolean isReportLogs = Boolean.TRUE;
+}

+ 20 - 0
src/main/java/com/zswl/dataservice/service/iot/IotServiceImpl.java

@@ -25,6 +25,10 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
 /**
  * @author TRX
  * @date 2024/6/20
@@ -204,6 +208,22 @@ public class IotServiceImpl extends SuperService {
         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) {
+        List<IotMain> list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(IotTemplate.build(templateId));
+        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) {

+ 117 - 0
src/main/java/com/zswl/dataservice/service/mqtt/DeviceInfoService.java

@@ -1,20 +1,32 @@
 package com.zswl.dataservice.service.mqtt;
 
+import com.zswl.dataservice.dao.iot.IotTemplateDao;
 import com.zswl.dataservice.dao.mqtt.DeviceInfoDao;
+import com.zswl.dataservice.dao.mqtt.ProjectInfoDao;
+import com.zswl.dataservice.dataConfig.ResultMessage;
+import com.zswl.dataservice.domain.iot.IotTemplate;
 import com.zswl.dataservice.domain.mqtt.DeviceInfo;
+import com.zswl.dataservice.domain.mqtt.ProjectInfo;
+import com.zswl.dataservice.model.iot.IotMainModel;
 import com.zswl.dataservice.model.mqtt.DeviceInfoAddParam;
 import com.zswl.dataservice.model.mqtt.DeviceInfoModel;
 import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam;
+import com.zswl.dataservice.model.mqtt.DeviceInfoUpdateRemark;
+import com.zswl.dataservice.service.iot.IotServiceImpl;
 import com.zswl.dataservice.utils.bean.BeanUtils;
 import com.zswl.dataservice.utils.mqtt.type.LogsLevel;
 import com.zswl.dataservice.utils.page.PageEntityUtil;
 import com.zswl.dataservice.utils.result.ResultContent;
 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.ArrayList;
+import java.util.List;
+
 /**
  * @author TRX
  * @date 2024/5/14
@@ -27,6 +39,18 @@ public class DeviceInfoService {
     @Autowired
     OperationLogsService operationLogsService;
 
+    @Autowired
+    IotTemplateDao iotTemplateDao;
+
+    @Autowired
+    IotServiceImpl iotService;
+
+    @Autowired
+    ProjectInfoDao projectInfoDao;
+
+    @Autowired
+    ProjectInfoService projectInfoService;
+
     /**
      * 添加设备
      *
@@ -40,6 +64,16 @@ public class DeviceInfoService {
         }
         deviceInfo = new DeviceInfo();
         BeanUtils.copyProperties(param, deviceInfo);
+        ProjectInfo projectInfo = null;
+        if (StringUtils.isNotEmpty(param.getProjectInfoOid())) {
+            projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoOid());
+        }
+        deviceInfo.setProjectInfo(projectInfo);
+        IotTemplate iotTemplate = null;
+        if (StringUtils.isNotEmpty(param.getIotTemplateId())) {
+            iotTemplate = iotTemplateDao.findTopById(param.getIotTemplateId());
+        }
+        deviceInfo.setIotTemplate(iotTemplate);
         deviceInfoDao.save(deviceInfo);
         operationLogsService.addLogs(String.format("添加了设备;%s", deviceInfo.getDeviceName()), LogsLevel.Middle, deviceInfo);
         return ResultContent.buildSuccess(deviceInfo);
@@ -73,6 +107,87 @@ public class DeviceInfoService {
         return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
     }
 
+    /**
+     * @return
+     */
+    public ResultContent updateRemark(DeviceInfoUpdateRemark param) {
+        DeviceInfo deviceInfo = deviceInfoDao.findTopById(param.getId());
+        if (ObjectUtils.isEmpty(deviceInfo)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
+        }
+        deviceInfo.setRemark(param.getRemark());
+        deviceInfoDao.save(deviceInfo);
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 编辑设备关联的模版
+     *
+     * @param param
+     * @return
+     */
+    public ResultContent updateIopTemplate(DeviceInfoUpdateRemark param) {
+        DeviceInfo deviceInfo = deviceInfoDao.findTopById(param.getId());
+        if (ObjectUtils.isEmpty(deviceInfo)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
+        }
+        IotTemplate iotTemplate = iotTemplateDao.findTopById(param.getIotTemplateId());
+        if (ObjectUtils.isEmpty(iotTemplate)) {
+            return ResultContent.buildFail(String.format("模版ID不存在:", param.getId()));
+        }
+        deviceInfo.setIotTemplate(iotTemplate);
+        deviceInfoDao.save(deviceInfo);
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 编辑设备关联的模版
+     *
+     * @param param
+     * @return
+     */
+    public ResultContent updateIsReportLogs(DeviceInfoUpdateRemark param) {
+        DeviceInfo deviceInfo = deviceInfoDao.findTopById(param.getId());
+        if (ObjectUtils.isEmpty(deviceInfo)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
+        }
+        deviceInfo.setIsReportLogs(param.getIsReportLogs());
+        deviceInfoDao.save(deviceInfo);
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 测试设备的连接时间
+     *
+     * @param id
+     * @return
+     */
+    public ResultContent<Long> testDevice(String id) {
+        Long time = 0L;
+
+        return ResultContent.buildSuccess(time);
+    }
+
+    /**
+     * 得到设备关联的物模型定义
+     *
+     * @param id
+     * @return
+     */
+    public ResultContent<List<IotMainModel>> getDeviceIotMain(String id) {
+        DeviceInfo deviceInfo = deviceInfoDao.findTopById(id);
+        if (ObjectUtils.isEmpty(deviceInfo)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
+        }
+        List<IotMainModel> models = new ArrayList<>();
+        if (deviceInfo.getIotTemplate() != null) {
+            ResultContent<List<IotMainModel>> resultContent = iotService.getTemplateIotMainList(deviceInfo.getIotTemplate().getId());
+            models = resultContent.getContent();
+        }
+
+        return ResultContent.buildSuccess(models);
+    }
+
     /**
      * 删除设备
      *
@@ -102,6 +217,8 @@ public class DeviceInfoService {
         DeviceInfoModel deviceInfoModel = new DeviceInfoModel();
         if (ObjectUtils.isNotEmpty(deviceInfo)) {
             BeanUtils.copyProperties(deviceInfo, deviceInfoModel);
+            deviceInfoModel.setIotTemplate(iotService.toModel(deviceInfo.getIotTemplate()));
+            deviceInfoModel.setProjectInfo(projectInfoService.toModel(deviceInfo.getProjectInfo()));
         }
         return deviceInfoModel;
     }

+ 2 - 1
src/main/java/com/zswl/dataservice/service/mqtt/ProjectInfoService.java

@@ -89,8 +89,9 @@ public class ProjectInfoService {
     }
 
     public ProjectInfoModel toModel(ProjectInfo entity) {
-        ProjectInfoModel projectInfoModel = new ProjectInfoModel();
+        ProjectInfoModel projectInfoModel = null;
         if (ObjectUtils.isNotEmpty(entity)) {
+            projectInfoModel = new ProjectInfoModel();
             BeanUtils.copyProperties(entity, projectInfoModel);
         }
         return projectInfoModel;