133 lines
3.1 KiB
Plaintext
133 lines
3.1 KiB
Plaintext
import supa, { supaReady } from '@/components/supadb/aksupainstance.uts'
|
|
import type { DeviceInfo, DeviceParams } from './types.uts'
|
|
|
|
// 服务响应类型
|
|
export type ServiceResponse<T> = {
|
|
error: Error | null
|
|
data: T | null
|
|
}
|
|
|
|
// 设备数据服务类
|
|
export class SenseDataService {
|
|
// 表名常量(根据实际数据库表名调整)
|
|
private static readonly TABLE_NAME = 'sense_devices'
|
|
|
|
/**
|
|
* 获取设备列表
|
|
*/
|
|
static async getDevices(params: DeviceParams): Promise<ServiceResponse<Array<DeviceInfo>>> {
|
|
try {
|
|
await supaReady
|
|
const res = await supa.from(SenseDataService.TABLE_NAME)
|
|
.select('*', {})
|
|
.eq('user_id', params.user_id)
|
|
.execute()
|
|
|
|
if (res.status >= 200 && res.status < 300 && res.data != null) {
|
|
const data = res.data as any
|
|
const devices = Array.isArray(data) ? data as Array<DeviceInfo> : []
|
|
return { error: null, data: devices }
|
|
} else {
|
|
return {
|
|
error: new Error(`获取设备列表失败: ${res.status}`),
|
|
data: null
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
data: null
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 绑定新设备
|
|
*/
|
|
static async bindDevice(deviceData: UTSJSONObject): Promise<ServiceResponse<DeviceInfo>> {
|
|
try {
|
|
await supaReady
|
|
const res = await supa.from(SenseDataService.TABLE_NAME)
|
|
.insert(deviceData)
|
|
.select('*', {})
|
|
.single()
|
|
.execute()
|
|
|
|
if (res.status >= 200 && res.status < 300 && res.data != null) {
|
|
const device = res.data as DeviceInfo
|
|
return { error: null, data: device }
|
|
} else {
|
|
return {
|
|
error: new Error(`绑定设备失败: ${res.status}`),
|
|
data: null
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
data: null
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 解绑设备
|
|
*/
|
|
static async unbindDevice(deviceId: string): Promise<ServiceResponse<null>> {
|
|
try {
|
|
await supaReady
|
|
const res = await supa.from(SenseDataService.TABLE_NAME)
|
|
.delete()
|
|
.eq('id', deviceId)
|
|
.execute()
|
|
|
|
if (res.status >= 200 && res.status < 300) {
|
|
return { error: null, data: null }
|
|
} else {
|
|
return {
|
|
error: new Error(`解绑设备失败: ${res.status}`),
|
|
data: null
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
data: null
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新设备配置
|
|
*/
|
|
static async updateDevice(deviceId: string, configData: UTSJSONObject): Promise<ServiceResponse<DeviceInfo>> {
|
|
try {
|
|
await supaReady
|
|
const res = await supa.from(SenseDataService.TABLE_NAME)
|
|
.update(configData)
|
|
.eq('id', deviceId)
|
|
.select('*', {})
|
|
.single()
|
|
.execute()
|
|
|
|
if (res.status >= 200 && res.status < 300 && res.data != null) {
|
|
const device = res.data as DeviceInfo
|
|
return { error: null, data: device }
|
|
} else {
|
|
return {
|
|
error: new Error(`更新设备配置失败: ${res.status}`),
|
|
data: null
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
data: null
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 导出类型
|
|
export type { DeviceParams }
|