完善小程序设备页与系统横幅提醒

This commit is contained in:
stu2not
2026-05-07 16:05:50 +08:00
parent f825c60176
commit 7518455e2e
16 changed files with 1520 additions and 15 deletions

View File

@@ -5,6 +5,10 @@ export interface DeviceStatus {
device_id: string
child_id?: number | null
child_name?: string | null
sleep_mode?: number | null
disable_time_start?: string | null
disable_time_end?: string | null
timezone?: string | null
power?: number | null
volume?: number | null
signal?: number | null
@@ -24,12 +28,61 @@ export interface DeviceStatus {
location_updated_at?: string | null
}
export interface DeviceAlarmItem {
alarm_id: number
device_id: string
child_id?: number | null
child_name?: string | null
source_msg_id: string
created_at: string
}
export interface DeviceAlarmListResponse {
items: DeviceAlarmItem[]
total: number
}
export interface DeviceVolumeUpdateResponse {
device_id: string
level: number
msg_id: string
}
export interface DeviceSleepScheduleUpdateResponse {
device_id: string
sleep_mode: number
start?: string | null
end?: string | null
timezone: string
msg_id?: string | null
}
export interface DeviceRemoteSleepWakeResponse {
device_id: string
switch: 'on' | 'off'
msg_id: string
}
function normalizeTimeValue(value?: string | null): string | null {
if (value === null || value === undefined) return null
const trimmed = String(value).trim()
if (!trimmed) return null
const matched = trimmed.match(/^(\d{1,2}):(\d{2})(?::\d{2})?:?$/)
if (!matched) return trimmed.replace(/:+$/, '')
const hour = matched[1].padStart(2, '0')
return `${hour}:${matched[2]}`
}
function normalizeDeviceStatus(status: DeviceStatus): DeviceStatus {
return {
...status,
disable_time_start: normalizeTimeValue(status.disable_time_start),
disable_time_end: normalizeTimeValue(status.disable_time_end),
}
}
async function resolveDeviceId(deviceId?: string): Promise<string> {
const normalizedDeviceId = String(deviceId || '').trim()
if (normalizedDeviceId) return normalizedDeviceId
@@ -42,7 +95,8 @@ export async function getDeviceStatus(deviceId?: string): Promise<DeviceStatus |
if (!resolvedDeviceId) return null
try {
return await request<DeviceStatus>(`/banban/devices/${resolvedDeviceId}/status`)
const result = await request<DeviceStatus>(`/banban/devices/${resolvedDeviceId}/status`)
return normalizeDeviceStatus(result)
} catch (error: any) {
if (error?.status === 404) return null
throw error
@@ -60,3 +114,53 @@ export async function setDeviceVolume(level: number, deviceId?: string): Promise
data: { level },
})
}
export async function setDeviceSleepSchedule(
start: string,
end: string,
timezone = 'Asia/Shanghai',
deviceId?: string
): Promise<DeviceSleepScheduleUpdateResponse> {
const resolvedDeviceId = await resolveDeviceId(deviceId)
if (!resolvedDeviceId) {
throw new Error('当前没有可用设备')
}
const result = await request<DeviceSleepScheduleUpdateResponse>(`/banban/devices/${resolvedDeviceId}/sleep-schedule`, {
method: 'POST',
data: { start, end, timezone },
})
return {
...result,
start: normalizeTimeValue(result.start),
end: normalizeTimeValue(result.end),
}
}
export async function setDeviceRemoteSleepWake(
switchValue: 'on' | 'off',
deviceId?: string
): Promise<DeviceRemoteSleepWakeResponse> {
const resolvedDeviceId = await resolveDeviceId(deviceId)
if (!resolvedDeviceId) {
throw new Error('当前没有可用设备')
}
return request<DeviceRemoteSleepWakeResponse>(`/banban/devices/${resolvedDeviceId}/sleep-wake`, {
method: 'POST',
data: { switch: switchValue },
})
}
export async function getDeviceAlarms(
deviceId?: string,
limit = 20
): Promise<DeviceAlarmListResponse> {
const resolvedDeviceId = await resolveDeviceId(deviceId)
if (!resolvedDeviceId) {
return { items: [], total: 0 }
}
return request<DeviceAlarmListResponse>(`/banban/devices/${resolvedDeviceId}/alarms?limit=${limit}`)
}