Forráskód Böngészése

更新! 取消GateWay信息

TRX 1 éve
szülő
commit
fc3243f0c3

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

@@ -25,7 +25,7 @@ import java.util.List;
 @NoArgsConstructor
 public class IotTemplateModel extends SuperModel {
 
-    @Schema(description = "模版名称")
+    @Schema(description = "产品名称")
     private String name;
 
     @Schema(description = "数据状态")
@@ -82,7 +82,7 @@ public class IotTemplateModel extends SuperModel {
     @Schema(description = "所属分组code")
     private String projectCode;
 
-    @Schema(description = "物模型所属模版ID,就是管理设备时绑定的那个模版")
+    @Schema(description = "物模型所属产品ID,就是管理设备时绑定的那个产品")
     private String iotTemplateId;
 
     @Schema(description = "设备数量")

+ 76 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotTemplateSimpleModel.java

@@ -0,0 +1,76 @@
+package com.zhongshu.iot.client.model.iot;
+
+import com.github.microservice.types.deviceUse.DeviceCategory;
+import com.github.microservice.types.deviceUse.DeviceSpecType;
+import com.github.microservice.types.deviceUse.DeviceType;
+import com.zhongshu.iot.client.model.baseParam.SuperModel;
+import com.zhongshu.iot.client.type.DataState;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 产品信息
+ *
+ * @author TRX
+ * @date 2024/6/20
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class IotTemplateSimpleModel  {
+
+    private String id;
+
+    @Schema(description = "产品名称")
+    private String name;
+
+    @Schema(description = "数据状态")
+    private DataState state;
+
+    @Schema(description = "所属物模型id")
+    private String iotThingId;
+
+    @Schema(description = "关联code,产品code")
+    private String productCode;
+
+    @Schema(description = "产品品类,直连设备、网关子设备、网关设备")
+    private DeviceCategory deviceCategory;
+
+    private String deviceCategoryStr;
+
+    public String getDeviceCategoryStr() {
+        if (deviceCategory != null) {
+            return deviceCategory.getRemark();
+        }
+        return "";
+    }
+
+    @Schema(description = "适用设备类型")
+    private DeviceType deviceType;
+
+    private String deviceTypeStr;
+
+    public String getDeviceTypeStr() {
+        if (deviceType != null) {
+            return deviceType.getRemark();
+        }
+        return "";
+    }
+
+    @Schema(description = "适用设备型号")
+    private DeviceSpecType specType;
+
+    private String specTypeStr;
+
+    public String getSpecTypeStr() {
+        if (specType != null) {
+            return specType.getRemark();
+        }
+        return "";
+    }
+}

+ 27 - 3
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/DeviceInfoModel.java

@@ -5,9 +5,11 @@ import com.github.microservice.types.deviceUse.DeviceSpecType;
 import com.github.microservice.types.deviceUse.DeviceType;
 import com.github.microservice.types.deviceUse.OnLineState;
 import com.zhongshu.iot.client.model.baseParam.SuperModel;
+import com.zhongshu.iot.client.model.iot.IotTemplateSimpleModel;
 import com.zhongshu.iot.client.type.DataState;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
+import org.apache.commons.lang3.ObjectUtils;
 
 /**
  * @author TRX
@@ -23,7 +25,19 @@ public class DeviceInfoModel extends SuperModel {
     private String deviceName;
 
     @Schema(description = "产品code")
-    private String projectCode;
+    private String productCode;
+
+    private String productName;
+
+    public String getProductName() {
+        if (ObjectUtils.isNotEmpty(iotTemplate)) {
+            return iotTemplate.getName();
+        }
+        return "";
+    }
+
+    @Schema(description = "产品信息")
+    private IotTemplateSimpleModel iotTemplate;
 
     @Schema(description = "在线状态")
     private OnLineState onLineState;
@@ -82,12 +96,22 @@ public class DeviceInfoModel extends SuperModel {
     @Schema(description = "最后上线时间")
     private Long lastOnlineTime;
 
-    @Schema(description = "所属项目")
+    @Schema(description = "所属分组")
     private ProjectInfoModel projectInfo;
 
-    @Schema(description = "项目code")
+    @Schema(description = "分组code")
     private String projectInfoCode;
 
+    @Schema(description = "分组名称")
+    private String projectInfoName;
+
+    public String getProjectInfoName() {
+        if (ObjectUtils.isNotEmpty(projectInfo)) {
+            return projectInfo.getName();
+        }
+        return "";
+    }
+
     @Schema(description = "固件版本")
     private String firmwareVersion;
 

+ 7 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/DeviceInfoSearchParam.java

@@ -41,5 +41,12 @@ public class DeviceInfoSearchParam extends SuperSearchParam {
     @Schema(description = "区分是网关还是设备")
     private List<DeviceCategory> deviceCategorys;
 
+    @Schema(description = "设置状态")
     private DeviceState state;
+
+    @Schema(description = "最后上线时间搜索")
+    private Long startLastOnlineTime;
+
+    private Long endLastOnlineTime;
+
 }

+ 15 - 14
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/devices/DeviceController.java

@@ -79,10 +79,14 @@ public class DeviceController {
     }
 
     @ResourceAuth(value = "user", type = AuthType.User)
-    @Operation(summary = "删除设备")
-    @RequestMapping(value = "deleteDeviceInfo", method = {RequestMethod.POST})
-    public ResultContent deleteDeviceInfo(@RequestBody DeviceIdParam param) {
-        return deviceInfoService.deleteDeviceInfo(param.getDeviceId());
+    @Operation(summary = "启用-禁用 设备")
+    @RequestMapping(value = "updateDeviceState", method = {RequestMethod.POST})
+    public ResultContent updateDeviceState(@RequestBody DeviceInfoUpdateRemark param) {
+        Assert.hasText(param.getId(), "ID不能为空");
+        if (param.getState() != null) {
+            return ResultContent.buildFail("state不能为空");
+        }
+        return deviceInfoService.updateDeviceState(param);
     }
 
     @ResourceAuth(value = "user", type = AuthType.User)
@@ -92,6 +96,13 @@ public class DeviceController {
         return deviceInfoService.cancelDeviceInfo(param.getDeviceId());
     }
 
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "删除设备")
+    @RequestMapping(value = "deleteDeviceInfo", method = {RequestMethod.POST})
+    public ResultContent deleteDeviceInfo(@RequestBody DeviceIdParam param) {
+        return deviceInfoService.deleteDeviceInfo(param.getDeviceId());
+    }
+
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "查询设备详情")
     @RequestMapping(value = "getDeviceById", method = {RequestMethod.GET})
@@ -126,16 +137,6 @@ public class DeviceController {
         return deviceInfoService.updateProject(param);
     }
 
-    @ResourceAuth(value = "user", type = AuthType.User)
-    @Operation(summary = "启用-禁用 设备")
-    @RequestMapping(value = "updateDeviceState", method = {RequestMethod.POST})
-    public ResultContent updateDeviceState(@RequestBody DeviceInfoUpdateRemark param) {
-        Assert.hasText(param.getId(), "ID不能为空");
-        if (param.getState() != null) {
-            return ResultContent.buildFail("state不能为空");
-        }
-        return deviceInfoService.updateDeviceState(param);
-    }
 
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "改变是否上传日志")

+ 10 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/mqtt/impl/DeviceInfoDaoImpl.java

@@ -92,7 +92,7 @@ public class DeviceInfoDaoImpl extends BaseImpl implements DeviceInfoDaoExtend {
     public Criteria buildFilterCriteria(DeviceInfoSearchParam param) {
         Criteria criteria = buildCriteriaAboutTime(param);
 
-        // 设备品类
+        // 设备品类 网关子设备 网关
         if (ObjectUtils.isNotEmpty(param.getDeviceCategorys())) {
             criteria.and("deviceCategory").in(param.getDeviceCategorys());
         }
@@ -136,6 +136,15 @@ public class DeviceInfoDaoImpl extends BaseImpl implements DeviceInfoDaoExtend {
             criteria.and("state").is(param.getState());
         }
 
+        // 最后上线时间搜索
+        if (!CommonUtil.longIsEmpty(param.getStartLastOnlineTime()) && !CommonUtil.longIsEmpty(param.getEndLastOnlineTime())) {
+            criteria.and("lastOnlineTime").gte(param.getStartLastOnlineTime()).lte(param.getEndLastOnlineTime());
+        } else if (!CommonUtil.longIsEmpty(param.getStartLastOnlineTime()) && CommonUtil.longIsEmpty(param.getEndLastOnlineTime())) {
+            criteria.and("lastOnlineTime").gte(param.getStartLastOnlineTime());
+        } else if (CommonUtil.longIsEmpty(param.getStartLastOnlineTime()) && !CommonUtil.longIsEmpty(param.getEndLastOnlineTime())) {
+            criteria.and("lastOnlineTime").lte(param.getEndLastOnlineTime());
+        }
+
         // 模糊搜索
         List<Criteria> criterias = new ArrayList<>();
         if (StringUtils.isNotEmpty(param.getDeviceName())) {

+ 3 - 3
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/device/DeviceInfo.java

@@ -68,6 +68,9 @@ public class DeviceInfo extends SuperEntity {
     @Schema(description = "最后下线时间")
     private Long lastOfflineTime;
 
+    @Schema(description = "最后离线时间")
+    private Long lastOffLineTime;
+
     @Schema(description = "所属项目")
     @DBRef(lazy = true)
     private ProjectInfo projectInfo;
@@ -81,9 +84,6 @@ public class DeviceInfo extends SuperEntity {
     @Schema(description = "连接参数")
     private JSONObject connectParam;
 
-    @Schema(description = "最后离线时间")
-    private Long lastOffLineTime;
-
     @Schema(description = "设备本地日志上报")
     private Boolean isReportLogs = Boolean.TRUE;
 

+ 17 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceInfoService.java

@@ -23,6 +23,7 @@ import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
 import com.zhongshu.iot.server.core.service.iot.IotThingService;
 import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
 import com.zhongshu.iot.server.core.service.user.OperationLogsService;
+import com.zhongshu.iot.server.core.util.CommonUtil;
 import com.zhongshu.iot.server.core.util.DateUtils;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
 import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
@@ -99,6 +100,10 @@ public class DeviceInfoService {
             // 编辑
             deviceInfo = temp;
             param.setId(null);
+            if (deviceInfo.getIsDelete() != null && deviceInfo.getIsDelete()) {
+                deviceInfo.setIsDelete(Boolean.FALSE);
+                deviceInfo.setState(DeviceState.Enable);
+            }
         } else {
             // 添加
             deviceInfo.setActivityTime(System.currentTimeMillis());
@@ -130,8 +135,9 @@ public class DeviceInfoService {
 
         DeviceInfo finalDeviceInfo = deviceInfo;
         CompletableFuture.runAsync(() -> {
-
+            // 设备关联的物模型创建或更新
             iotThingService.initAutoThingByDevice(finalDeviceInfo);
+
             // 日志
             operationLogsService.addLogs(String.format("添加了设备;%s", finalDeviceInfo.getDeviceName()), LogsLevel.Middle, finalDeviceInfo);
 
@@ -156,6 +162,12 @@ public class DeviceInfoService {
      * @return
      */
     public ResultContent<Page<DeviceInfoModel>> pageDevice(Pageable pageable, DeviceInfoSearchParam param) {
+        if (!CommonUtil.longIsEmpty(param.getStartLastOnlineTime())) {
+            param.setStartLastOnlineTime(DateUtils.getDayStartTime(param.getStartLastOnlineTime()));
+        }
+        if (!CommonUtil.longIsEmpty(param.getEndLastOnlineTime())) {
+            param.setEndLastOnlineTime(DateUtils.getDayEndTime(param.getEndLastOnlineTime()));
+        }
         Page<DeviceInfo> page = deviceInfoDao.page(pageable, param);
         return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
     }
@@ -419,7 +431,11 @@ public class DeviceInfoService {
         DeviceInfoModel deviceInfoModel = new DeviceInfoModel();
         if (ObjectUtils.isNotEmpty(deviceInfo)) {
             BeanUtils.copyProperties(deviceInfo, deviceInfoModel);
+            // 分组信息
             deviceInfoModel.setProjectInfo(projectInfoService.toModel(deviceInfo.getProjectInfo()));
+
+            // 产品信息
+            deviceInfoModel.setIotTemplate(iotService.toSimpleModel(deviceInfo.getProductCode()));
         }
         return deviceInfoModel;
     }

+ 7 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DevicePingInfoService.java

@@ -45,6 +45,9 @@ public class DevicePingInfoService extends SuperService {
     @Autowired
     private DeviceInfoDao deviceInfoDao;
 
+    @Autowired
+    private DeviceInfoService deviceInfoService;
+
     @Autowired
     private CommonService commonService;
 
@@ -89,12 +92,15 @@ public class DevicePingInfoService extends SuperService {
     private ResultContent<Object> devicePing(String dataId, String dataStr) {
         DevicePingInfoParam param = JSONUtil.toBean(dataStr, DevicePingInfoParam.class);
         String deviceId = param.getDeviceId();
+        ResultContent resultContent = deviceInfoService.deviceCanUse(param.getDeviceId());
+        if (resultContent.isFailed()) {
+            return ResultContent.buildFail(resultContent.getMsg());
+        }
         log.info("devicePing 设备在线处理:{}", deviceId);
         DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
         if (ObjectUtils.isNotEmpty(deviceInfo)) {
 
             updateDeviceOnLine(deviceInfo, OnLineState.OnLine);
-
             // ping记录
             DevicePingInfo devicePingInfo = new DevicePingInfo();
             devicePingInfo.setPingType(deviceInfo.getDeviceType().name());

+ 6 - 3
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/GateWayManagerService.java

@@ -184,14 +184,16 @@ public class GateWayManagerService extends SuperService {
         DeviceInfo gateWayInfo;
         if (StringUtils.isNotEmpty(param.getGateWayId())) {
             gateWayInfo = deviceInfoDao.findTopByDeviceId(param.getGateWayId());
-            if (ObjectUtils.isEmpty(gateWayInfo)) {
-                return ResultContent.buildFail("gateWayId数据不存在");
+            ResultContent resultContent = deviceInfoService.deviceCanUse(gateWayInfo);
+            if (resultContent.isFailed()) {
+                return ResultContent.buildFail(resultContent.getMsg());
             }
             registType = RegistType.Gateway;
 
             if (StringUtils.isEmpty(gateWayInfo.getMqttUserName()) || !gateWayInfo.getMqttUserName().equals(param.getMqttUserName())) {
                 return ResultContent.buildFail("设备关联的连接账号与所属网关不匹配,注册失败");
             }
+            // 判断网关是否可以使用
         } else {
             gateWayInfo = null;
             registType = RegistType.DirectConnection;
@@ -216,6 +218,7 @@ public class GateWayManagerService extends SuperService {
                 // 保存设备信息
                 ResultContent<DeviceInfo> resultContent = deviceInfoService.addDeviceInfo(device);
                 DeviceInfo deviceInfo = resultContent.getContent();
+                // 绑定设备和网关的关系
                 deviceBindGateWay(deviceInfo, gateWayInfo);
                 deviceInfos.add(deviceInfo);
                 // 更新设备的topic
@@ -243,7 +246,7 @@ public class GateWayManagerService extends SuperService {
         if (ObjectUtils.isEmpty(deviceInfo)) {
             return ResultContent.buildFail("设备信息为空");
         }
-        // 如果设备网关
+        // 如果设备网关
         if (deviceInfo.getDeviceCategory() == DeviceCategory.GW) {
             List<GateWay2Device> _list = gateWay2DeviceDao.findByDeviceInfo(deviceInfo);
             gateWay2DeviceDao.deleteAll(_list);

+ 20 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotServiceImpl.java

@@ -632,6 +632,26 @@ public class IotServiceImpl extends SuperService {
         return model;
     }
 
+    /**
+     * 转换产品模型
+     *
+     * @param productCode
+     * @return
+     */
+    public IotTemplateSimpleModel toSimpleModel(String productCode) {
+        IotTemplate entity = iotTemplateDao.findTopByProductCodeAndIotDataType(productCode, IotDataType.IotTemplate);
+        return toSimpleModel(entity);
+    }
+
+    public IotTemplateSimpleModel toSimpleModel(IotTemplate entity) {
+        IotTemplateSimpleModel model = null;
+        if (ObjectUtils.isNotEmpty(entity)) {
+            model = new IotTemplateSimpleModel();
+            BeanUtils.copyProperties(entity, model);
+        }
+        return model;
+    }
+
     public IotTopicModel toModel(IotTopic entity) {
         IotTopicModel model = null;
         if (ObjectUtils.isNotEmpty(entity)) {

+ 1 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotThingService.java

@@ -89,6 +89,7 @@ public class IotThingService extends SuperService {
             if (StringUtils.isEmpty(productCode)) {
                 return ResultContent.buildFail("关联的产品code为空");
             }
+            // 产品信息
             IotTemplate iotTemplate = iotTemplateDao.findTopByProductCodeAndIotDataType(productCode, IotDataType.IotTemplate);
             if (ObjectUtils.isEmpty(iotTemplate)) {
                 return ResultContent.buildFail(String.format("产品code不存在", productCode));