import type { onDpDataChange, onDeviceOnlineStatusUpdate, onDeviceInfoUpdated, onNetworkStatusChange, onBluetoothAdapterStateChange, onGroupDpDataChangeEvent, onGroupInfoChange } from '@ray-js/ray'; import SmartDeviceModel from '../SmartDeviceModel'; import { DevInfo, DpSchema, DpState, DpValue, NetworkState, GetTTTEventListenerParams, GroupInfo } from '../../types'; import { PublishDpsOptions } from '../../kit'; export type DpSchemaList = DpSchema[]; export type ReadonlyDpSchemaList = readonly DpSchema[]; export type SmartDeviceModelProp = Record; export type SmartDeviceModelWatch = (dpState: DpState) => void & { [dpCode: string]: (dpValue: DpValue) => void; }; export type SmartDeviceActions = { [dpCode: string]: any; }; export interface SmartDeviceAbility { /** * 初始化能力方法 */ init: (...args: any[]) => Promise; /** * 当前能力配置,用于挂载到 SDM 的 abilities 属性上时使用 */ abilityOptions: { /** * 当前能力名,用于挂载到 SDM 的 abilities 属性上 */ name: string; /** * 是否支持群组环境 */ isSupportGroup: boolean; /** * 是否在当前 ability init 完毕以后才完成 SDM 初始化,默认不等待 * @default false */ waitForInit?: boolean; /** * 初始化能力方法的入参 */ initParams?: any; }; } /** * @internal * 智能设备高级能力列表 */ export type SmartDeviceModelAbilities = Array; /** * SmartDeviceModel 的 abilities 容器类型,仅用于泛型约束与内部组合。 * @internal */ export type SmartDeviceModelAbility = { [abilityName: string]: A; }; export type SmartDeviceModelDpListener = (dpState: DpValue, data?: DpDataChangeParams) => void; /** * 设备 DP 上报事件参数类型,供内部拦截器签名复用。 * @internal */ export type DpDataChangeParams = GetTTTEventListenerParams; export type GroupDpDataChangeParams = GetTTTEventListenerParams; /** * 内部使用的 DpDataChangeParams,携带 dp-kit 注入的来源标记 * 用于区分是面板乐观更新还是设备真实上报 * @internal */ export type InternalDpDataChangeParams = DpDataChangeParams & { /** dp-kit 注入的来源标记 */ __from__?: string; }; /** * 内部使用的 GroupDpDataChangeParams,携带 dp-kit 注入的来源标记 * @internal */ export type InternalGroupDpDataChangeParams = GroupDpDataChangeParams & { /** dp-kit 注入的来源标记 */ __from__?: string; }; /** * 设备在线状态更新事件参数类型,供内部拦截器签名复用。 * @internal */ export type DeviceOnlineStatusUpdateParams = GetTTTEventListenerParams; /** * 设备信息更新事件参数类型,供内部拦截器签名复用。 * @internal */ export type DeviceInfoUpdatedParams = GetTTTEventListenerParams; export type GroupInfoChangeParams = GetTTTEventListenerParams; /** * 网络状态变化事件参数类型,供内部拦截器签名复用。 * @internal */ export type NetworkStatusChangeParams = GetTTTEventListenerParams; /** * 蓝牙适配器状态变化事件参数类型,供内部拦截器签名复用。 * @internal */ export type BluetoothAdapterStatusChangeParams = GetTTTEventListenerParams; export type SmartDeviceModelNetwork = NetworkState; export type SmartDeviceModelBluetooth = BluetoothAdapterStatusChangeParams; export type GetDpValueBySchema = T['type'] extends 'raw' ? string : T['property']['type'] extends 'bool' ? boolean : T['property']['type'] extends 'value' | 'bitmap' ? number : string; export type DpCommonAction = { /** * 下发 DP 点 * @param value - DP 对应的值 */ set: (value: param, options?: PublishDpsOptions) => Promise; }; export type DpBooleanAction = DpCommonAction & { /** * 打开当前布尔类型功能点 */ on: (options?: PublishDpsOptions) => Promise; /** * 关闭当前布尔类型功能点 */ off: (options?: PublishDpsOptions) => Promise; /** * 切换当前布尔类型功能点 */ toggle: (options?: PublishDpsOptions) => Promise; }; export type DpNumberAction = DpCommonAction & { /** * 根据当前功能点步长 step 进行递增 */ inc: (options?: PublishDpsOptions) => Promise; /** * 根据当前功能点步长 step 进行递减 */ dec: (options?: PublishDpsOptions) => Promise; }; export type DpBitmapAction = DpCommonAction & { /** * 开启指定 bit 位 * @param idx - 指定 bit 位 */ on: (idx: number, options?: PublishDpsOptions) => Promise; /** * 关闭指定 bit 位 * @param idx - 指定 bit 位 */ off: (idx: number, options?: PublishDpsOptions) => Promise; /** * 切换指定 bit 位 * @param idx - 指定 bit 位 */ toggle: (idx: number, options?: PublishDpsOptions) => Promise; }; export type DpEnumAction = DpCommonAction & { /** * 切换到前一个枚举值 */ prev: (options?: PublishDpsOptions) => Promise; /** * 切换到下一个枚举值 */ next: (options?: PublishDpsOptions) => Promise; /** * 随机切换到一个枚举值 */ random: (options?: PublishDpsOptions) => Promise; }; export type DpStringAction = DpCommonAction; export type DpRawAction = DpCommonAction; export type GetDpActionBySchema = T['type'] extends 'raw' ? DpRawAction : T['property']['type'] extends 'bool' ? DpBooleanAction : T['property']['type'] extends 'value' ? DpNumberAction : T['property']['type'] extends 'bitmap' ? DpBitmapAction : T['property']['type'] extends 'enum' ? DpEnumAction : T['property']['type'] extends 'string' | 'raw' ? DpStringAction : Record; export type GetDpCodesBySchema = T['type'] extends 'raw' ? string : T['property']['type'] extends 'bool' ? boolean : T['property']['type'] extends 'value' ? number : T['property']['type'] extends 'bitmap' ? number : T['property']['type'] extends 'enum' ? T['property']['range'][number] : T['property']['type'] extends 'string' | 'raw' ? string : any; export type GetSmartDeviceDpCodes = { readonly [Dp in S[number]['code']]: GetDpCodesBySchema>; }; export type GetSmartDeviceModelProps = { readonly [Dp in S[number]['code']]: GetDpValueBySchema>; }; export type GetSmartDeviceModelWatch = { readonly [Dp in S[number]['code']]: (listener: SmartDeviceModelDpListener) => { unwatch: () => void; }; }; export type GetSmartDeviceModelActions = { readonly [Dp in S[number]['code']]: GetDpActionBySchema>; }; export type GetSmartDeviceModelDpSchema = { readonly [Dp in S[number]['code']]: Extract; }; export type GetSmartDeviceModelDevInfo = Omit & { dpCodes: { readonly [Dp in S[number]['code']]: GetDpValueBySchema>; }; dps: { readonly [DpId in S[number]['id']]: GetDpValueBySchema>; }; idCodes: { readonly [DpId in S[number]['id']]: Extract['code']; }; codeIds: { readonly [Dp in S[number]['code']]: Extract['id']; }; schema: S; }; export type GetSmartGroupModelGroupInfo = Omit & { dpCodes: { readonly [Dp in S[number]['code']]: GetDpValueBySchema>; }; dps: { readonly [DpId in S[number]['id']]: GetDpValueBySchema>; }; idCodes: { readonly [DpId in S[number]['id']]: Extract['code']; }; codeIds: { readonly [Dp in S[number]['code']]: Extract['id']; }; schema: S; }; export type SmartDevicesMap = { [key: string]: SmartDeviceModel; }; export type GetSmartDevicesProps = { [K in keyof M]: M[K] extends { model: { props: infer P; }; } ? P : never; }; export type GetSmartDevicesActions = { [K in keyof M]: M[K] extends { model: { actions: infer A; }; } ? A : never; };