Ver Fonte

更新! 取消GateWay信息

TRX há 1 ano atrás
pai
commit
fe6af375e5
14 ficheiros alterados com 447 adições e 0 exclusões
  1. 17 0
      OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/baseParam/SeriesModel.java
  2. 31 0
      OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/ping/DeviceOnLineRecordModel.java
  3. 32 0
      OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/ping/DeviceOnLineRecordSearch.java
  4. 23 0
      OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/statistics/LineModel.java
  5. 43 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/devices/DeviceStatisticsController.java
  6. 17 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/DeviceOnLineRecordDao.java
  7. 17 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/extend/DeviceOnLineRecordDaoExtend.java
  8. 78 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/impl/DeviceOnLineRecordDaoImpl.java
  9. 3 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dataConfig/CommonTTLTimeConfig.java
  10. 79 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/device/DeviceOnLineRecord.java
  11. 5 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceInfoService.java
  12. 88 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceOnLineRecordService.java
  13. 4 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DevicePingInfoService.java
  14. 10 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceStatisticsService.java

+ 17 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/baseParam/SeriesModel.java

@@ -0,0 +1,17 @@
+package com.zhongshu.iot.client.model.baseParam;
+
+import lombok.Data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2025/2/27
+ */
+@Data
+public class SeriesModel {
+
+    private List<Object> series = new ArrayList<Object>();
+
+}

+ 31 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/ping/DeviceOnLineRecordModel.java

@@ -0,0 +1,31 @@
+package com.zhongshu.iot.client.model.iot.ping;
+
+import com.github.microservice.types.deviceUse.DeviceCategory;
+import com.zhongshu.iot.client.model.baseParam.SuperModel;
+import com.zhongshu.iot.client.model.mqtt.DeviceInfoModel;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2025/2/27
+ */
+@Data
+public class DeviceOnLineRecordModel extends SuperModel {
+
+    @Schema(description = "设备、网关ID")
+    private String deviceId;
+
+    @Schema(description = "设备名称、网关名称")
+    private String deviceName;
+
+    @Schema(description = "设备信息")
+    private DeviceInfoModel deviceInfo;
+
+    @Schema(description = "设备品类")
+    private DeviceCategory deviceCategory;
+
+    @Schema(description = "时间")
+    private String timeStr;
+
+}

+ 32 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/ping/DeviceOnLineRecordSearch.java

@@ -0,0 +1,32 @@
+package com.zhongshu.iot.client.model.iot.ping;
+
+import com.github.microservice.types.deviceUse.DeviceCategory;
+import com.zhongshu.iot.client.model.baseParam.SuperSearchParam;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2025/2/27
+ */
+@Data
+public class DeviceOnLineRecordSearch extends SuperSearchParam {
+
+    @Schema(description = "设备、网关ID")
+    private List<String> deviceIds;
+
+    @Schema(description = "设备品类")
+    private List<DeviceCategory> deviceCategories;
+
+    @Schema(description = "设备名称、网关名称")
+    private String deviceName;
+
+    @Schema(description = "分组code")
+    private String projectCode;
+
+    @Schema(description = "设备code,产品code")
+    private String productCode;
+
+}

+ 23 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/statistics/LineModel.java

@@ -0,0 +1,23 @@
+package com.zhongshu.iot.client.model.iot.statistics;
+
+import com.zhongshu.iot.client.model.baseParam.SeriesModel;
+import io.swagger.v3.oas.annotations.media.Schema;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 折现图数据
+ *
+ * @author TRX
+ * @date 2025/2/27
+ */
+public class LineModel {
+
+    @Schema(description = "X轴数据,横向方向")
+    private List<String> xAxis = new ArrayList<String>();
+
+    @Schema(description = "Y轴的数据")
+    private List<SeriesModel> series = new ArrayList<>();
+
+}

+ 43 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/devices/DeviceStatisticsController.java

@@ -0,0 +1,43 @@
+package com.zhongshu.iot.server.core.controller.devices;
+
+import com.github.microservice.auth.security.annotations.ResourceAuth;
+import com.github.microservice.auth.security.type.AuthType;
+import com.github.microservice.net.ResultContent;
+import com.zhongshu.iot.client.model.iot.ping.DeviceOnLineRecordSearch;
+import com.zhongshu.iot.server.core.service.device.DeviceOnLineRecordService;
+import com.zhongshu.iot.server.core.service.device.DeviceStatisticsService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.beans.factory.annotation.Autowired;
+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;
+
+/**
+ * 统计 服务
+ *
+ * @author TRX
+ * @date 2024/3/21
+ */
+@RequestMapping("/deviceStatistics")
+@RestController
+@Validated
+@Tag(name = "统计")
+public class DeviceStatisticsController {
+
+    @Autowired
+    private DeviceStatisticsService deviceStatisticsService;
+
+    @Autowired
+    private DeviceOnLineRecordService deviceOnLineRecordService;
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "设备在线状态")
+    @RequestMapping(value = "statisticsDeviceOnLine", method = {RequestMethod.POST})
+    public ResultContent statisticsDeviceOnLine(@RequestBody DeviceOnLineRecordSearch param) {
+        return deviceOnLineRecordService.statisticsDeviceOnLine(param);
+    }
+
+}

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

@@ -0,0 +1,17 @@
+package com.zhongshu.iot.server.core.dao.iot;
+
+import com.zhongshu.iot.server.core.dao.iot.extend.DeviceOnLineRecordDaoExtend;
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceOnLineRecord;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+/**
+ * @author TRX
+ * @date 2024/3/21
+ */
+public interface DeviceOnLineRecordDao extends DeviceOnLineRecordDaoExtend, MongoRepository<DeviceOnLineRecord, String> {
+
+    DeviceOnLineRecord findTopById(String id);
+
+    DeviceOnLineRecord findTopByDeviceIdAndStatisticsKey(String deviceId, String statisticsKey);
+
+}

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

@@ -0,0 +1,17 @@
+package com.zhongshu.iot.server.core.dao.iot.extend;
+
+import com.zhongshu.iot.client.model.iot.ping.DeviceOnLineRecordSearch;
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceOnLineRecord;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+/**
+ * @Author TRX
+ * @CreateDate: 2023/7/7
+ * @Version: 1.0
+ */
+public interface DeviceOnLineRecordDaoExtend {
+
+    Page<DeviceOnLineRecord> page(Pageable pageable, DeviceOnLineRecordSearch param);
+
+}

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

@@ -0,0 +1,78 @@
+package com.zhongshu.iot.server.core.dao.iot.impl;
+
+import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
+import com.zhongshu.iot.client.model.iot.ping.DeviceOnLineRecordSearch;
+import com.zhongshu.iot.server.core.dao.base.BaseImpl;
+import com.zhongshu.iot.server.core.dao.iot.extend.DeviceOnLineRecordDaoExtend;
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceOnLineRecord;
+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;
+import org.springframework.util.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * @Author TRX
+ * @CreateDate: 2023/4/12
+ * @Version: 1.0
+ */
+public class DeviceOnLineRecordDaoImpl extends BaseImpl implements DeviceOnLineRecordDaoExtend {
+
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
+    @Autowired
+    private DBHelper dbHelper;
+
+    @Override
+    public Page<DeviceOnLineRecord> page(Pageable pageable, DeviceOnLineRecordSearch param) {
+        Criteria criteria = buildFilterCriteria(param);
+        Sort sort = buildSort(param);
+        Query query = Query.query(criteria);
+        query.with(sort);
+        return dbHelper.pages(query, pageable, DeviceOnLineRecord.class);
+    }
+
+    private Criteria buildFilterCriteria(DeviceOnLineRecordSearch param) {
+        Criteria criteria = buildCriteriaAboutTime(param);
+
+        if (ObjectUtils.isNotEmpty(param.getDeviceIds())) {
+            criteria.and("deviceId").in(param.getDeviceIds());
+        }
+
+        if (ObjectUtils.isNotEmpty(param.getDeviceCategories())) {
+            criteria.and("deviceCategory").in(param.getDeviceCategories());
+        }
+
+        // 分组
+        if (StringUtils.isNotEmpty(param.getProjectCode())) {
+            criteria.and("projectCode").is(param.getProjectCode());
+        }
+
+        // 产品
+        if (StringUtils.isNotEmpty(param.getProductCode())) {
+            criteria.and("productCode").is(param.getProductCode());
+        }
+
+        // 模糊搜索
+        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;
+    }
+
+}

+ 3 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dataConfig/CommonTTLTimeConfig.java

@@ -8,6 +8,9 @@ package com.zhongshu.iot.server.core.dataConfig;
  */
 public class CommonTTLTimeConfig {
 
+    // 在线历史数据保存时间
+    public static final Long onLineRecordTTl = 366 * 24L * 60 * 60 * 1000L;
+
     // ping记录保存时间 1天
     public static final Long pingTTl = 1 * 24L * 60 * 60 * 1000L;
 

+ 79 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/device/DeviceOnLineRecord.java

@@ -0,0 +1,79 @@
+package com.zhongshu.iot.server.core.domain.iot.device;
+
+import com.github.microservice.types.deviceUse.DeviceCategory;
+import com.zhongshu.card.client.utils.DateUtils;
+import com.zhongshu.iot.server.core.domain.base.SuperEntity;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.data.mongodb.core.index.Indexed;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.util.Date;
+
+/**
+ * 设备在线状态历史数据
+ *
+ * @author TRX
+ * @date 2025/2/27
+ */
+@Data
+@Document
+@NoArgsConstructor
+@AllArgsConstructor
+public class DeviceOnLineRecord extends SuperEntity {
+
+    @Schema(description = "设备、网关ID")
+    private String deviceId;
+
+    @Schema(description = "设备名称、网关名称")
+    private String deviceName;
+
+    @Schema(description = "设备品类")
+    private DeviceCategory deviceCategory;
+
+    @Schema(description = "分组code")
+    private String projectCode;
+
+    @Schema(description = "设备code,产品code")
+    private String productCode;
+
+    @Schema(description = "时间")
+    private String timeStr;
+
+    @Indexed(expireAfterSeconds = 0)
+    private Date TTL;
+
+    //------------------------------时间信息 start--------------------
+    @Schema(description = "年份,如:2024")
+    private Integer year;
+
+    @Schema(description = "月份,如:6")
+    private Integer month;
+
+    @Schema(description = "当前月的第几天")
+    private Integer dayOfMonth;
+
+    @Schema(description = "今年第几周,如:32")
+    private Integer week;
+
+    @Schema(description = "当前年的第几天")
+    private Integer dayOfYear;
+
+    @Schema(description = "当前的第几小时")
+    private Integer hourOfDay;
+
+    @Schema(description = "")
+    private String statisticsKey;
+
+    public void setTimes() {
+        this.year = DateUtils.getCurrentYear();
+        this.month = DateUtils.getCurrentMonthInYear();
+        this.dayOfMonth = DateUtils.getCurrentDayInMonth();
+        this.hourOfDay = DateUtils.getCurrentHourOfDay();
+
+        this.dayOfYear = DateUtils.getCurrentDayInYear();
+        this.week = DateUtils.getCurrentWeekInYear();
+    }
+}

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

@@ -363,6 +363,11 @@ public class DeviceInfoService {
         return ResultContent.buildSuccess(model);
     }
 
+    public DeviceInfoModel toModel(String deviceId) {
+        DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
+        return toModel(deviceInfo);
+    }
+
     public DeviceInfoModel toModel(DeviceInfo deviceInfo) {
         DeviceInfoModel deviceInfoModel = new DeviceInfoModel();
         if (ObjectUtils.isNotEmpty(deviceInfo)) {

+ 88 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceOnLineRecordService.java

@@ -0,0 +1,88 @@
+package com.zhongshu.iot.server.core.service.device;
+
+import com.github.microservice.net.ResultContent;
+import com.github.microservice.types.deviceUse.OnLineState;
+import com.zhongshu.card.client.utils.DateUtils;
+import com.zhongshu.iot.client.model.iot.ping.DeviceOnLineRecordModel;
+import com.zhongshu.iot.client.model.iot.ping.DeviceOnLineRecordSearch;
+import com.zhongshu.iot.client.model.iot.statistics.LineModel;
+import com.zhongshu.iot.server.core.dao.iot.DeviceOnLineRecordDao;
+import com.zhongshu.iot.server.core.dataConfig.CommonTTLTimeConfig;
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceInfo;
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceOnLineRecord;
+import com.zhongshu.iot.server.core.service.base.SuperService;
+import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.BeanUtils;
+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.Date;
+
+/**
+ * @author TRX
+ * @date 2025/2/27
+ */
+@Slf4j
+@Service
+public class DeviceOnLineRecordService extends SuperService {
+
+    @Autowired
+    private DeviceOnLineRecordDao deviceOnLineRecordDao;
+
+    @Autowired
+    private DeviceInfoService deviceInfoService;
+
+    public void addOnLineRecord(DeviceInfo deviceInfo, OnLineState onLineState) {
+        if (ObjectUtils.isEmpty(deviceInfo) || onLineState != OnLineState.OnLine) {
+            return;
+        }
+        String statisticsKey = DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyyMMddHHmm);
+        DeviceOnLineRecord entity = deviceOnLineRecordDao.findTopByDeviceIdAndStatisticsKey(deviceInfo.getDeviceId(), statisticsKey);
+        if (ObjectUtils.isEmpty(entity)) {
+            entity = new DeviceOnLineRecord();
+            entity.setTimes();
+        }
+        entity.setDeviceId(deviceInfo.getDeviceId());
+        entity.setStatisticsKey(statisticsKey);
+        entity.setDeviceName(deviceInfo.getDeviceName());
+        entity.setDeviceCategory(deviceInfo.getDeviceCategory());
+        entity.setProductCode(deviceInfo.getProductCode());
+        entity.setProjectCode(deviceInfo.getProjectInfoCode());
+        entity.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+        entity.setTTL(new Date(System.currentTimeMillis() + CommonTTLTimeConfig.onLineRecordTTl));
+        deviceOnLineRecordDao.save(entity);
+    }
+
+    public ResultContent<Page<DeviceOnLineRecordModel>> page(Pageable pageable, DeviceOnLineRecordSearch param) {
+        Page<DeviceOnLineRecord> page = deviceOnLineRecordDao.page(pageable, param);
+        return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
+    }
+
+    /**
+     * 统计
+     *
+     * @param search
+     * @return
+     */
+    public ResultContent<LineModel> statisticsDeviceOnLine(DeviceOnLineRecordSearch search) {
+        LineModel lineModel = new LineModel();
+
+
+        return ResultContent.buildSuccess(lineModel);
+    }
+
+    public DeviceOnLineRecordModel toModel(DeviceOnLineRecord entity) {
+        DeviceOnLineRecordModel model = null;
+        if (ObjectUtils.isNotEmpty(entity)) {
+            model = new DeviceOnLineRecordModel();
+            BeanUtils.copyProperties(entity, model);
+            model.setDeviceInfo(deviceInfoService.toModel(entity.getDeviceId()));
+        }
+        return model;
+    }
+
+}

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

@@ -57,6 +57,9 @@ public class DevicePingInfoService extends SuperService {
     @Autowired
     private RedisService redisService;
 
+    @Autowired
+    private DeviceOnLineRecordService deviceOnLineRecordService;
+
     public ResultContent<Object> ping(String dataId, String dataStr) {
         PingResult pingResult = new PingResult();
         GateWayPingInfoParam param = JSONUtil.toBean(dataStr, GateWayPingInfoParam.class);
@@ -265,6 +268,7 @@ public class DevicePingInfoService extends SuperService {
 
         // 通知业务平台
         deviceSyncFullCardService.noticeSyncDeviceOnlineStateChange(deviceId);
+        deviceOnLineRecordService.addOnLineRecord(deviceInfo, onLineState);
     }
 
 }

+ 10 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceStatisticsService.java

@@ -1,10 +1,20 @@
 package com.zhongshu.iot.server.core.service.device;
 
+import com.github.microservice.net.ResultContent;
+import com.zhongshu.iot.client.model.iot.statistics.LineModel;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
 /**
  * 设备统计服务
  *
  * @author TRX
  * @date 2025/2/27
  */
+@Slf4j
+@Service
 public class DeviceStatisticsService {
+
+
+
 }