feat(miniprogram): BLE 设备同步模块 — 扫描+连接+数据上传
- Task 18: BLE 类型定义(NormalizedReading/DeviceAdapter/BLEDevice)+ BLEManager 连接管理器 - Task 19: XiaomiBandAdapter 心率读取适配器(标准 HRS Service 0x180D) - Task 20: device-sync API 层 + 设备同步页面 + app.config 路由注册
This commit is contained in:
67
apps/miniprogram/src/services/device-sync.ts
Normal file
67
apps/miniprogram/src/services/device-sync.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { api } from './request';
|
||||
import type { NormalizedReading } from './ble/types';
|
||||
|
||||
interface BatchReadingRequest {
|
||||
device_id: string;
|
||||
device_model?: string;
|
||||
readings: {
|
||||
device_type: string;
|
||||
values: Record<string, number>;
|
||||
measured_at: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
interface BatchResult {
|
||||
accepted: number;
|
||||
duplicates: number;
|
||||
earliest: string | null;
|
||||
latest: string | null;
|
||||
}
|
||||
|
||||
/** 将标准化读数转换为后端批量请求格式并上传 */
|
||||
export async function uploadReadings(
|
||||
patientId: string,
|
||||
deviceId: string,
|
||||
deviceModel: string | undefined,
|
||||
readings: NormalizedReading[],
|
||||
): Promise<number> {
|
||||
if (readings.length === 0) return 0;
|
||||
|
||||
const body: BatchReadingRequest = {
|
||||
device_id: deviceId,
|
||||
device_model: deviceModel,
|
||||
readings: readings.map((r) => ({
|
||||
device_type: r.device_type,
|
||||
values: r.values,
|
||||
measured_at: r.measured_at,
|
||||
})),
|
||||
};
|
||||
|
||||
const result = await api.post<BatchResult>(
|
||||
`/health/patients/${patientId}/device-readings/batch`,
|
||||
body,
|
||||
);
|
||||
return result.accepted;
|
||||
}
|
||||
|
||||
/** 查询设备原始数据 */
|
||||
export async function queryDeviceReadings(
|
||||
patientId: string,
|
||||
params?: { device_type?: string; hours?: number },
|
||||
) {
|
||||
return api.get<{ data: unknown[]; total: number }>(
|
||||
`/health/patients/${patientId}/device-readings`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
/** 查询小时级降采样数据 */
|
||||
export async function queryHourlyReadings(
|
||||
patientId: string,
|
||||
params: { device_type: string; days?: number },
|
||||
) {
|
||||
return api.get<{ data: unknown[]; total: number }>(
|
||||
`/health/patients/${patientId}/device-readings/hourly`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user