|
@@ -8,97 +8,42 @@ package vip.xiaonuo.coldchain.modular.app.service;
|
|
|
* @date 2024/11/17 22:30:41
|
|
|
*/
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.github.jfcloud.influxdb.service.JfcloudInfluxDBService;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
|
|
|
+import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
|
|
|
import vip.xiaonuo.coldchain.modular.app.param.*;
|
|
|
-import vip.xiaonuo.coldchain.modular.app.param.mock.AppDeviceTestDataGenerator;
|
|
|
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.entity.MonitorTarget;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.param.MonitorTargetPageParam;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.param.TargetStatus;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.service.MonitorTargetService;
|
|
|
+import vip.xiaonuo.coldchain.modular.monitortargetregion.entity.MonitorTargetRegion;
|
|
|
+import vip.xiaonuo.coldchain.modular.monitortargetregion.service.MonitorTargetRegionService;
|
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
|
public class AppDeviceService {
|
|
|
private final MonitorTargetService monitorTargetService;
|
|
|
+ private final MonitorTargetRegionService monitorTargetRegionService;
|
|
|
+ private final JfcloudInfluxDBService jfcloudInfluxDBService;
|
|
|
private final MonitorDeviceService monitorDeviceService;
|
|
|
|
|
|
- public Page<AppDevice> getAllDevices(AppDevicePageParam appDevicePageParam) {
|
|
|
- // 获取分页数据
|
|
|
- return AppDeviceTestDataGenerator.getPagedDeviceData(appDevicePageParam);
|
|
|
- }
|
|
|
-
|
|
|
// 模拟设备保存操作,实际应用中可能会是数据库操作
|
|
|
public boolean addDevice(AppDeviceAddParam device) {
|
|
|
System.out.println("设备已添加:" + device);
|
|
|
return true; // 假设设备保存成功
|
|
|
}
|
|
|
|
|
|
- // 查询设备数据的方法
|
|
|
- public List<AppDeviceData> getDeviceData(AppDeviceQueryParams appDeviceQueryParams, String deviceCode) {
|
|
|
- // 模拟的设备数据列表,添加了 deviceCode
|
|
|
- List<AppDeviceData> appDeviceDataList = new ArrayList<>();
|
|
|
- appDeviceDataList.add(new AppDeviceData("DEVICE001", LocalDateTime.parse("2023-03-18T14:00:40"), 21.0, 60.0, 400.0, 85.0, "Device A"));
|
|
|
- appDeviceDataList.add(new AppDeviceData("DEVICE002", LocalDateTime.parse("2023-03-20T10:15:30"), 18.5, 65.0, 420.0, 75.0, "Device B"));
|
|
|
- appDeviceDataList.add(new AppDeviceData("DEVICE001", LocalDateTime.parse("2023-03-25T12:30:00"), 19.2, 63.0, 410.0, 80.0, "Device A"));
|
|
|
- appDeviceDataList.add(new AppDeviceData("DEVICE003", LocalDateTime.parse("2023-03-28T13:45:10"), 22.5, 55.0, 450.0, 90.0, "Device C"));
|
|
|
-
|
|
|
- List<AppDeviceData> filteredData = new ArrayList<>();
|
|
|
- for (AppDeviceData data : appDeviceDataList) {
|
|
|
- boolean matches = true;
|
|
|
-
|
|
|
- // 时间范围过滤
|
|
|
- if (appDeviceQueryParams.getStartTime() != null && data.getTimestamp().isBefore(appDeviceQueryParams.getStartTime())) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
- if (appDeviceQueryParams.getEndTime() != null && data.getTimestamp().isAfter(appDeviceQueryParams.getEndTime())) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
-
|
|
|
- // 温度范围过滤
|
|
|
- if (appDeviceQueryParams.getMinTemperature() != null && data.getTemperature() < appDeviceQueryParams.getMinTemperature()) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
- if (appDeviceQueryParams.getMaxTemperature() != null && data.getTemperature() > appDeviceQueryParams.getMaxTemperature()) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
-
|
|
|
- // 湿度范围过滤
|
|
|
- if (appDeviceQueryParams.getMinHumidity() != null && data.getHumidity() < appDeviceQueryParams.getMinHumidity()) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
- if (appDeviceQueryParams.getMaxHumidity() != null && data.getHumidity() > appDeviceQueryParams.getMaxHumidity()) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
-
|
|
|
- // 二氧化碳浓度范围过滤
|
|
|
- if (appDeviceQueryParams.getMinCo2Level() != null && data.getCo2Level() < appDeviceQueryParams.getMinCo2Level()) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
- if (appDeviceQueryParams.getMaxCo2Level() != null && data.getCo2Level() > appDeviceQueryParams.getMaxCo2Level()) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
-
|
|
|
- // 设备编码过滤
|
|
|
- if (deviceCode != null && !deviceCode.equals(data.getDeviceCode())) {
|
|
|
- matches = false;
|
|
|
- }
|
|
|
-
|
|
|
- if (matches) {
|
|
|
- filteredData.add(data);
|
|
|
- }
|
|
|
- }
|
|
|
- return filteredData;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 监控对象列表包含温湿度监控指标--全部
|
|
|
*
|
|
@@ -116,27 +61,96 @@ public class AppDeviceService {
|
|
|
* @return
|
|
|
*/
|
|
|
public Page<AppDevice> pageDevices(AppDevicePageParam appDevicePageParam) {
|
|
|
+ // 用于存储最终返回的 AppDevice 列表
|
|
|
List<AppDevice> rlt = Lists.newArrayList();
|
|
|
+ // 创建监控目标分页查询参数对象
|
|
|
MonitorTargetPageParam monitorTargetPageParam = new MonitorTargetPageParam();
|
|
|
+ // 获取当前登录用户的 ID,并设置到查询参数中
|
|
|
monitorTargetPageParam.setUserId(StpLoginUserUtil.getLoginUser().getId());
|
|
|
+ // 设置分页参数:当前页和每页记录数
|
|
|
monitorTargetPageParam.setCurrent(appDevicePageParam.getCurrent());
|
|
|
monitorTargetPageParam.setSize(appDevicePageParam.getSize());
|
|
|
+
|
|
|
+ // 调用 monitorTargetService 查询当前用户的监控目标数据,分页结果
|
|
|
Page<MonitorTarget> pageByUser = monitorTargetService.getPageByUser(monitorTargetPageParam);
|
|
|
- pageByUser.getRecords().forEach(mt -> {
|
|
|
- //获取通道的集合
|
|
|
-// List<MonitorChannel> monitorChannels = monitorChannelService.listByTargetId(mt.getId());
|
|
|
- //单个通道的温湿度信息
|
|
|
+
|
|
|
+ // 遍历监控目标分页记录
|
|
|
+ pageByUser.getRecords().forEach(monitorTarget -> {
|
|
|
+ // 创建新的 AppDevice 对象来存储转换后的设备信息
|
|
|
+ AppDevice appDevice = new AppDevice();
|
|
|
+ // 将监控目标信息复制到 AppDevice 中
|
|
|
+ BeanUtil.copyProperties(monitorTarget, appDevice);
|
|
|
+
|
|
|
+ // 获取当前监控目标的 ID,用于查询子设备数据
|
|
|
+ String monitorTargetId = monitorTarget.getId();
|
|
|
+
|
|
|
+ // 获取当前监控目标下的所有监控区域(子设备数据)
|
|
|
+ List<MonitorTargetRegion> monitorTargetRegions = monitorTargetRegionService.getRegionListByTargetId(monitorTargetId);
|
|
|
+
|
|
|
+ // 如果监控目标下存在区域(即有子设备)
|
|
|
+ if (!monitorTargetRegions.isEmpty()) {
|
|
|
+ // 遍历每个区域,将其转换为子设备数据,并添加到 AppDevice 的子设备列表中
|
|
|
+ for (MonitorTargetRegion monitorTargetRegion : monitorTargetRegions) {
|
|
|
+ // 获取 AppDevice 的子设备列表
|
|
|
+ List<AppDeviceData> children = appDevice.getChildren();
|
|
|
+ // 如果子设备列表为空,初始化一个空的列表
|
|
|
+ if (children == null) {
|
|
|
+ children = new ArrayList<>();
|
|
|
+ appDevice.setChildren(children);
|
|
|
+ }
|
|
|
+ // 创建新的 AppDeviceData 对象,将监控区域数据复制到该子设备对象中
|
|
|
+ AppDeviceData appDeviceData = new AppDeviceData();
|
|
|
+ children.add(appDeviceData);
|
|
|
+ BeanUtil.copyProperties(monitorTargetRegion, appDeviceData);
|
|
|
+ String deviceId = appDeviceData.getDeviceCode();
|
|
|
+ // 传感器路数
|
|
|
+ Integer sensorRoute = appDeviceData.getSensorRoute();
|
|
|
+ if (!Objects.isNull(sensorRoute) && StrUtil.isNotBlank(deviceId)) {
|
|
|
+ String startTime = null, endTime = null;
|
|
|
+ SensorData sensorData = monitorDeviceService.queryLatestDataByDeviceIdAndRoads(deviceId, sensorRoute);
|
|
|
+ List<SensorData> sensorDataList = monitorDeviceService.queryDataByDeviceIdAndRoads(deviceId, sensorRoute, startTime, endTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 这里可以调用外部服务(如 InfluxDB)获取实时数据(温湿度、CO2 等),
|
|
|
+ // 但目前这个逻辑的实际获取部分尚未实现
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新设备的电池电量和插电状态等信息
|
|
|
+ // 这里假设我们取第一个子设备的数据来填充主体设备的电池电量等信息
|
|
|
+ if (!appDevice.getChildren().isEmpty()) {
|
|
|
+ AppDeviceData firstChild = appDevice.getChildren().get(0);
|
|
|
+ appDevice.setPlugInStatus(firstChild.getPlugInStatus());
|
|
|
+ appDevice.setBatteryPercentage(firstChild.getBatteryPercentage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将转换后的设备对象添加到最终的设备列表中
|
|
|
+ rlt.add(appDevice);
|
|
|
});
|
|
|
- return null;
|
|
|
+
|
|
|
+ // 创建分页返回对象,设置分页信息和记录数据
|
|
|
+ Page<AppDevice> pageResult = new Page<>();
|
|
|
+ pageResult.setCurrent(appDevicePageParam.getCurrent()); // 当前页
|
|
|
+ pageResult.setSize(appDevicePageParam.getSize()); // 每页记录数
|
|
|
+ pageResult.setTotal(pageByUser.getTotal()); // 总记录数
|
|
|
+ pageResult.setPages(pageByUser.getPages()); // 总页数
|
|
|
+ pageResult.setRecords(rlt); // 设置当前页的设备数据
|
|
|
+
|
|
|
+ // 返回分页结果
|
|
|
+ return pageResult;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 获取设备总数
|
|
|
*
|
|
|
* @param keyword
|
|
|
* @return
|
|
|
*/
|
|
|
- public TargetStatus getDeviceStatus(String keyword) {
|
|
|
- return monitorTargetService.getTargetCount();
|
|
|
+ public DeviceStatus getDeviceStatus(String keyword) {
|
|
|
+ TargetStatus targetCount = monitorTargetService.getTargetCount();
|
|
|
+ DeviceStatus deviceStatus = new DeviceStatus();
|
|
|
+ BeanUtil.copyProperties(targetCount, deviceStatus);
|
|
|
+ return deviceStatus;
|
|
|
}
|
|
|
}
|