项目正常启动
This commit is contained in:
132
pages/sense/senseDataService.uts
Normal file
132
pages/sense/senseDataService.uts
Normal file
@@ -0,0 +1,132 @@
|
||||
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 }
|
||||
16
pages/sense/types.uts
Normal file
16
pages/sense/types.uts
Normal file
@@ -0,0 +1,16 @@
|
||||
// 设备信息类型
|
||||
export type DeviceInfo = {
|
||||
id: string
|
||||
device_name?: string
|
||||
status?: string // 'online' | 'offline' | 其他状态
|
||||
user_id?: string
|
||||
// 可根据实际需求添加更多字段
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
// 设备查询参数类型
|
||||
export type DeviceParams = {
|
||||
user_id: string
|
||||
// 可根据实际需求添加更多查询参数
|
||||
[key: string]: any
|
||||
}
|
||||
Reference in New Issue
Block a user