|
@@ -1,12 +1,12 @@
|
|
|
package vip.xiaonuo.coldchain.core.service;
|
|
|
|
|
|
-import cn.hutool.core.lang.Assert;
|
|
|
import com.github.jfcloud.influxdb.config.JfcloudInfluxDB2Properties;
|
|
|
import com.github.jfcloud.influxdb.flux.JfcloudFluxDataService;
|
|
|
import com.github.jfcloud.influxdb.service.JfcloudInfluxDBService;
|
|
|
import com.influxdb.client.QueryApi;
|
|
|
import com.influxdb.query.FluxTable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.Assert;
|
|
|
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
|
|
|
import vip.xiaonuo.coldchain.core.util.DateFormatter;
|
|
|
|
|
@@ -67,35 +67,72 @@ public class JfcloudSensorDataService extends JfcloudFluxDataService<SensorData>
|
|
|
* @return 查询到的传感器数据列表
|
|
|
*/
|
|
|
|
|
|
- public List<SensorData> queryDataByDeviceIdAndRoads(String deviceId, Integer roads, String startTimeStr, String endTimeStr,String field) {
|
|
|
+// public List<SensorData> queryDataByDeviceIdAndRoads(String deviceId, Integer roads, String startTimeStr, String endTimeStr,String field) {
|
|
|
+// Assert.notNull(deviceId, "deviceId cannot be null");
|
|
|
+// Assert.notNull(roads, "roads cannot be null");
|
|
|
+// Assert.notNull(startTimeStr, "startTime cannot be null");
|
|
|
+// Assert.notNull(endTimeStr, "endTime cannot be null");
|
|
|
+// Assert.notNull(field, "field cannot be null");
|
|
|
+// //替换起始时间和结束时间2024/09/09 这种格式为2024-09-09
|
|
|
+// startTimeStr = DateFormatter.replaceDateFormat(startTimeStr);
|
|
|
+// endTimeStr = DateFormatter.replaceDateFormat(endTimeStr);
|
|
|
+// // 如果只有日期部分,则手动补充时间部分(00:00:00)
|
|
|
+// if (startTimeStr.length() == 10) {
|
|
|
+// startTimeStr += " 00:00:00";
|
|
|
+// }
|
|
|
+// if (endTimeStr.length() == 10) {
|
|
|
+// endTimeStr += " 23:59:59";
|
|
|
+// }
|
|
|
+// String startTimeFormatted = convertToISO8601(startTimeStr);
|
|
|
+// String endTimeFormatted = convertToISO8601(endTimeStr);
|
|
|
+// String aggregationWindow = FluxAggregationUtils.determineAggregationWindow(startTimeFormatted, endTimeFormatted);
|
|
|
+// String measurement = "sensor_data";
|
|
|
+// Map<String, String> filters = Map.of(
|
|
|
+// "device_id", deviceId,
|
|
|
+// "roads", roads.toString()
|
|
|
+// );
|
|
|
+// String query = FluxQueryBuilder.buildRangeQuery(getBucketName(), startTimeFormatted, endTimeFormatted, measurement, field, filters, aggregationWindow);
|
|
|
+// QueryApi queryApi = jfcloudInfluxDBService.getInfluxDBClient().getQueryApi();
|
|
|
+// List<FluxTable> results = queryApi.query(query);
|
|
|
+// return results.stream().flatMap(table -> table.getRecords().stream()).map(fluxRecord -> mapRecordToEntity(fluxRecord, getEntityClass())) // 转换为 SensorData 实体
|
|
|
+// .collect(Collectors.toList());
|
|
|
+// }
|
|
|
+
|
|
|
+ public List<SensorData> queryDataByDeviceIdAndRoads(String deviceId, Integer roads, String startTimeStr, String endTimeStr, String field) {
|
|
|
Assert.notNull(deviceId, "deviceId cannot be null");
|
|
|
Assert.notNull(roads, "roads cannot be null");
|
|
|
Assert.notNull(startTimeStr, "startTime cannot be null");
|
|
|
Assert.notNull(endTimeStr, "endTime cannot be null");
|
|
|
Assert.notNull(field, "field cannot be null");
|
|
|
- //替换起始时间和结束时间2024/09/09 这种格式为2024-09-09
|
|
|
+
|
|
|
+ // 替换日期格式,确保格式一致
|
|
|
startTimeStr = DateFormatter.replaceDateFormat(startTimeStr);
|
|
|
endTimeStr = DateFormatter.replaceDateFormat(endTimeStr);
|
|
|
- // 如果只有日期部分,则手动补充时间部分(00:00:00)
|
|
|
+
|
|
|
+ // 如果只有日期部分,补充时间部分为00:00:00和23:59:59
|
|
|
if (startTimeStr.length() == 10) {
|
|
|
startTimeStr += " 00:00:00";
|
|
|
}
|
|
|
if (endTimeStr.length() == 10) {
|
|
|
endTimeStr += " 23:59:59";
|
|
|
}
|
|
|
+ // 转换为ISO8601格式
|
|
|
String startTimeFormatted = convertToISO8601(startTimeStr);
|
|
|
String endTimeFormatted = convertToISO8601(endTimeStr);
|
|
|
String aggregationWindow = FluxAggregationUtils.determineAggregationWindow(startTimeFormatted, endTimeFormatted);
|
|
|
- String measurement = "sensor_data";
|
|
|
+ String measurement = "sensor_data"; // 数据表名称
|
|
|
Map<String, String> filters = Map.of(
|
|
|
"device_id", deviceId,
|
|
|
"roads", roads.toString()
|
|
|
);
|
|
|
+ // 构建查询语句
|
|
|
String query = FluxQueryBuilder.buildRangeQuery(getBucketName(), startTimeFormatted, endTimeFormatted, measurement, field, filters, aggregationWindow);
|
|
|
QueryApi queryApi = jfcloudInfluxDBService.getInfluxDBClient().getQueryApi();
|
|
|
List<FluxTable> results = queryApi.query(query);
|
|
|
- return results.stream().flatMap(table -> table.getRecords().stream()).map(fluxRecord -> mapRecordToEntity(fluxRecord, getEntityClass())) // 转换为 SensorData 实体
|
|
|
- .collect(Collectors.toList()); // 返回多条数据
|
|
|
+ return results.stream()
|
|
|
+ .flatMap(table -> table.getRecords().stream())
|
|
|
+ .map(fluxRecord -> mapRecordToEntity(fluxRecord, getEntityClass()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
|