import { SmartDeviceAbility } from '../../types'; import Compose from './compose'; import { TReturnResData, TReturnRes, TStorageType, Options } from './types'; export type ReturnedDatas = TReturnRes; export type ReturnedData = TReturnResData; /** * 智能存储能力,提供基于设备维度的键值对云端持久化存储 * @public * @since @ray-js/panel-sdk 1.0.0 * @remarks * 通过 SmartDeviceModel 的 abilities 配置挂载后使用。 * 同时支持单设备和群组设备。 * @example * ```typescript * import { SmartDeviceModel, SmartStorageAbility } from '@ray-js/panel-sdk'; * * const sdm = new SmartDeviceModel({ abilities: [new SmartStorageAbility()] }); * ``` */ export declare class SmartStorageAbility implements SmartDeviceAbility { private _deviceId; private _groupId; /** * 全局单例实例 - 用于在应用范围内共享已初始化的实例 */ private static globalInstance; /** * 全局初始化状态 */ private static globalInitialized; /** * @param options - 存储能力初始化配置 */ constructor(options?: Options); /** * 获取全局已初始化的实例 * @internal * @returns 已初始化的全局实例 * @throws Error 如果实例尚未初始化 */ static getInstance(): SmartStorageAbility; /** * 检查全局实例是否已初始化 * @internal * @returns true 表示已初始化 */ static isReady(): boolean; /** * 重置全局状态(主要用于测试) * @internal */ static reset(): void; /** @internal */ readonly abilityOptions: { name: string; waitForInit: boolean; isSupportGroup: boolean; }; /** @internal */ storage: Compose; /** @internal */ storageType: TStorageType; /** * 当前设备 ID */ private get deviceId(); private set deviceId(value); /** * 当前群组 ID */ private get groupId(); private set groupId(value); /** * 初始化存储 ID 并返回 */ private initId; /** * 初始化 Promise - 用于防重入,多次调用 init 会返回同一个 Promise */ private initPromise; /** * 初始化存储能力,建立与云端和本地的存储通道 * @public * @since @ray-js/panel-sdk 1.0.0 * @param options - 初始化配置,可以是设备 ID 字符串或包含 deviceId/groupId/storageType 的对象 * @returns 初始化完成的 SmartStorageAbility 实例 * @remarks * 支持防重入:多次调用 init 会返回同一个 Promise。 * 首次初始化成功后会注册为全局实例,后续可通过 SmartStorageAbility.getInstance() 获取。 * 同时支持单设备和群组设备。 * @example * ```typescript * // 作为 SDM 能力注册,由 SmartDeviceModel 自动初始化 * const sdm = new SmartDeviceModel({ * abilities: [new SmartStorageAbility()], * }); * * // 手动初始化(指定设备 ID) * const storage = new SmartStorageAbility(); * await storage.init({ deviceId: 'device_001' }); * ``` */ init: (options?: Options) => Promise; private doInit; /** * 读取指定 key 的存储数据 * @public * @since @ray-js/panel-sdk 1.0.0 * @param key - 存储键名 * @param callback - 可选回调,在数据读取完成后调用 * @returns 包含时间戳和值的存储结果 * @example * ```typescript * const result = await sdm.storage.get('brightness'); * console.log('值:', result.value, '时间:', result.time); * ``` */ get(key: string, callback?: (cacheData: TReturnRes) => void): Promise>; /** * 读取当前设备/群组下的所有存储数据 * @public * @since @ray-js/panel-sdk 1.0.0 * @param callback - 可选回调,在数据读取完成后调用 * @returns 包含所有存储键值对的结果 * @example * ```typescript * const allData = await sdm.storage.getAll(); * console.log('所有存储数据:', allData); * ``` */ getAll(callback?: (cacheData: TReturnResData) => void): Promise>; /** * 存储指定 key 的数据 * @public * @since @ray-js/panel-sdk 1.0.0 * @param key - 存储键名 * @param value - 要存储的值,序列化后长度不能超过 900 字符(建议 256 以内) * @returns 存储操作结果 * @remarks * 当 storageType 不为 'local' 时,value 序列化后最大长度为 900 字符。 * 本地缓存支持的 value 最大长度超过 1024。 * @example * ```typescript * await sdm.storage.set('brightness', 80); * await sdm.storage.set('config', { mode: 'auto', level: 3 }); * ``` */ set(key: string, value: T): Promise; /** * 批量存储多个键值对 * @public * @since @ray-js/panel-sdk 1.0.0 * @param values - 要存储的键值对集合,最多 30 个 key * @param timestamp - 时间戳,默认为当前时间 * @returns 存储操作结果 * @remarks * 每个 value 序列化后最大长度为 1024 字符,整个 values 对象的 key 数量不能超过 30 个。 * @example * ```typescript * await sdm.storage.setAll({ * brightness: 80, * colorTemp: 4000, * mode: 'white', * }); * ``` */ setAll(values: Record, timestamp?: number): Promise; /** * 删除指定 key 的存储数据 * @public * @since @ray-js/panel-sdk 1.0.0 * @param key - 要删除的存储键名 * @returns 删除操作结果 * @example * ```typescript * await sdm.storage.remove('brightness'); * ``` */ remove(key: string): Promise; }