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