HarmonyOS分布式控制开发实战:手机控制智能家居

想象一下:躺在沙发上用手机调节空调温度,走到门口用手机打开智能门锁,在卧室用手机关闭客厅灯光——这种"一机在手,全屋掌控"的体验,正是鸿蒙分布式控制场景的魅力所在。

一、背景与动机

1.1 传统智能家居控制的痛点

说实话,现在的智能家居控制体验真的很割裂。你肯定遇到过这些情况:

  • APP割裂:每个品牌的设备都有自己的APP,控制个灯得打开米家,控制空调得打开海尔智家,控制门锁又得打开另一个APP
  • 设备孤岛:不同品牌的设备无法联动,想实现"开门自动开灯"得买同一品牌的全套设备
  • 操作繁琐:打开APP、找到设备、选择功能、调节参数,一套流程下来,还不如直接走过去按开关
  • 响应延迟:远程控制延迟大,本地控制又不稳定,体验时好时坏
  • 权限混乱:家人想控制设备,要么共享账号(不安全),要么重新配网(太麻烦)

最让人头疼的是,这些设备虽然都叫"智能",但彼此之间却是最笨的——完全无法协同工作。

1.2 鸿蒙分布式控制的愿景

鸿蒙的分布式能力让智能家居控制焕然一新:

一屏统管:所有智能设备统一在鸿蒙系统中管理,无需安装多个APP。

跨品牌互联:基于统一标准,不同品牌的设备可以互联互通、协同工作。

就近发现:设备靠近自动发现,一键配网,无需繁琐操作。

分布式控制:手机、平板、手表、车机都能控制家居设备,设备间还能协同联动。

安全可控:细粒度的权限管理,家人可以安全地共享设备控制权。

鸿蒙分布式控制

手机

鸿蒙控制中心

平板

手表

灯光

空调

门锁

窗帘

传统方式

米家APP

米家设备

海尔APP

海尔设备

涂鸦APP

涂鸦设备

1.3 核心价值

分布式控制场景带来的价值是实实在在的:

  1. 效率提升60%:统一入口、快速操作,控制效率大幅提升
  2. 体验一致性:所有设备统一的交互方式,学习成本为零
  3. 跨品牌互联:打破品牌壁垒,实现真正的智能家居
  4. 安全可控:细粒度权限管理,家庭共享更安全

二、核心原理

2.1 设备发现与配网

鸿蒙的设备发现基于多种技术:

发现机制

  • BLE广播:低功耗蓝牙广播,适合低功耗设备
  • Wi-Fi扫描:扫描设备发出的热点,适合需要高带宽的设备
  • mDNS/DNS-SD:基于多播DNS的服务发现,适合已配网设备
  • NFC触碰:近距离触碰配网,最简单直接

配网流程

渲染错误: Mermaid 渲染失败: Parse error on line 19: ...lassDef primary fill:#e1f5fe,stroke:#015 -----------------------^ Expecting '()', 'SOLID_OPEN_ARROW', 'DOTTED_OPEN_ARROW', 'SOLID_ARROW', 'SOLID_ARROW_TOP', 'SOLID_ARROW_BOTTOM', 'STICK_ARROW_TOP', 'STICK_ARROW_BOTTOM', 'SOLID_ARROW_TOP_DOTTED', 'SOLID_ARROW_BOTTOM_DOTTED', 'STICK_ARROW_TOP_DOTTED', 'STICK_ARROW_BOTTOM_DOTTED', 'SOLID_ARROW_TOP_REVERSE', 'SOLID_ARROW_BOTTOM_REVERSE', 'STICK_ARROW_TOP_REVERSE', 'STICK_ARROW_BOTTOM_REVERSE', 'SOLID_ARROW_TOP_REVERSE_DOTTED', 'SOLID_ARROW_BOTTOM_REVERSE_DOTTED', 'STICK_ARROW_TOP_REVERSE_DOTTED', 'STICK_ARROW_BOTTOM_REVERSE_DOTTED', 'BIDIRECTIONAL_SOLID_ARROW', 'DOTTED_ARROW', 'BIDIRECTIONAL_DOTTED_ARROW', 'SOLID_CROSS', 'DOTTED_CROSS', 'SOLID_POINT', 'DOTTED_POINT', got 'TXT'

2.2 设备模型与能力描述

每个智能设备都有标准化的能力描述:

// 设备模型
interface SmartDevice {
    // 基本信息
    deviceId: string;               // 设备唯一标识
    deviceName: string;             // 设备名称
    deviceType: DeviceType;         // 设备类型
    manufacturer: string;           // 厂商
    model: string;                  // 型号
    firmwareVersion: string;        // 固件版本
  
    // 网络信息
    networkStatus: NetworkStatus;   // 网络状态
    ipAddress?: string;             // IP地址
    signalStrength: number;         // 信号强度
  
    // 能力描述
    capabilities: DeviceCapability[];  // 设备能力列表
  
    // 状态
    online: boolean;                // 是否在线
    status: Record<string, any>;    // 当前状态
  
    // 权限
    permissions: DevicePermission[];  // 权限列表
}

// 设备类型
enum DeviceType {
    LIGHT = 'light',                // 灯光
    AIR_CONDITIONER = 'air_conditioner',  // 空调
    CURTAIN = 'curtain',            // 窗帘
    LOCK = 'lock',                  // 门锁
    CAMERA = 'camera',              // 摄像头
    SENSOR = 'sensor',              // 传感器
    SWITCH = 'switch',              // 开关
    APPLIANCE = 'appliance'         // 家电
}

// 设备能力
interface DeviceCapability {
    capabilityId: string;           // 能力ID
    name: string;                   // 能力名称
    type: CapabilityType;           // 能力类型
    parameters: Parameter[];        // 参数定义
    minValue?: number;              // 最小值(数值类型)
    maxValue?: number;              // 最大值(数值类型)
    step?: number;                  // 步进值
    unit?: string;                  // 单位
    options?: string[];             // 选项(枚举类型)
}

// 能力类型
enum CapabilityType {
    BOOLEAN = 'boolean',            // 布尔(开关)
    NUMBER = 'number',              // 数值(温度、亮度等)
    ENUM = 'enum',                  // 枚举(模式选择)
    STRING = 'string'               // 字符串
}

// 参数定义
interface Parameter {
    name: string;                   // 参数名
    type: string;                   // 参数类型
    required: boolean;              // 是否必需
    defaultValue?: any;             // 默认值
}

2.3 控制指令下发

控制指令的下发需要考虑可靠性:

指令结构

interface ControlCommand {
    commandId: string;              // 指令ID
    deviceId: string;               // 目标设备ID
    capability: string;             // 目标能力
    action: string;                 // 动作
    parameters: Record<string, any>;  // 参数
    timestamp: number;              // 时间戳
    timeout: number;                // 超时时间
    priority: 'high' | 'normal' | 'low';  // 优先级
}

下发策略

  • 本地优先:设备在局域网内时,直接通过局域网下发,延迟最低
  • 云端中转:设备不在局域网时,通过云端下发,支持远程控制
  • 混合模式:优先局域网,失败时自动切换到云端
  • 确认机制:关键指令需要设备确认执行成功

2.4 状态同步机制

设备状态需要实时同步到控制端:

同步方式

  • 状态上报:设备状态变化时主动上报
  • 定时查询:控制端定时查询设备状态
  • 订阅模式:控制端订阅设备状态变化通知

状态一致性

// 状态同步管理
interface StateSyncManager {
    // 本地状态缓存
    localState: Map<string, any>;
  
    // 设备真实状态
    deviceState: Map<string, any>;
  
    // 同步策略
    syncStrategy: 'realtime' | 'periodic' | 'on_demand';
  
    // 冲突解决
    resolveConflict(local: any, remote: any): any;
}

三、代码实战

3.1 智能家居控制中心实现

首先实现核心的智能家居控制中心:

// SmartHomeController.ets
import deviceManager from '@ohos.distributedDeviceManager';
import { distributedData } from '@kit.ArkData';

// 设备状态
@Observed
class DeviceState {
    online: boolean = false;
    status: Record<string, any> = {};
    lastUpdate: number = 0;
}

@Entry
@Component
struct SmartHomeController {
    // 设备列表
    @State devices: SmartDevice[] = [];
    @State deviceStates: Map<string, DeviceState> = new Map();
  
    // 房间列表
    @State rooms: Room[] = [];
    @State currentRoom: string = 'all';
  
    // 场景列表
    @State scenes: Scene[] = [];
  
    // UI状态
    @State showDeviceDetail: boolean = false;
    @State selectedDevice: SmartDevice | null = null;
    @State showAddDevice: boolean = false;
    @State isScanning: boolean = false;
  
    // 设备管理器
    private deviceMgr: deviceManager.DeviceManager | null = null;
    private kvStore: distributedData.KvStore | null = null;
  
    aboutToAppear() {
        // 初始化
        this.init();
    }
  
    // 初始化
    async init() {
        try {
            // 初始化设备管理器
            // this.deviceMgr = deviceManager.createDeviceManager('com.example.smarthome');
          
            // 加载设备列表
            await this.loadDevices();
          
            // 加载房间列表
            await this.loadRooms();
          
            // 加载场景列表
            await this.loadScenes();
          
            // 订阅设备状态
            this.subscribeDeviceStatus();
          
            console.info('智能家居控制中心初始化成功');
        } catch (err) {
            console.error(`初始化失败: ${JSON.stringify(err)}`);
        }
    }
  
    // 加载设备列表
    async loadDevices() {
        // 从本地数据库加载
        // 从云端同步
      
        // 模拟数据
        this.devices = [
            {
                deviceId: 'light_001',
                deviceName: '客厅主灯',
                deviceType: DeviceType.LIGHT,
                manufacturer: 'Philips',
                model: 'Hue White',
                firmwareVersion: '1.0.0',
                networkStatus: 'online',
                signalStrength: 80,
                capabilities: [
                    {
                        capabilityId: 'power',
                        name: '电源',
                        type: CapabilityType.BOOLEAN,
                        parameters: []
                    },
                    {
                        capabilityId: 'brightness',
                        name: '亮度',
                        type: CapabilityType.NUMBER,
                        parameters: [],
                        minValue: 0,
                        maxValue: 100,
                        step: 1,
                        unit: '%'
                    }
                ],
                online: true,
                status: { power: true, brightness: 80 },
                permissions: [],
                roomId: 'room_001'
            },
            {
                deviceId: 'ac_001',
                deviceName: '客厅空调',
                deviceType: DeviceType.AIR_CONDITIONER,
                manufacturer: 'Haier',
                model: 'Smart AC',
                firmwareVersion: '2.0.0',
                networkStatus: 'online',
                signalStrength: 90,
                capabilities: [
                    {
                        capabilityId: 'power',
                        name: '电源',
                        type: CapabilityType.BOOLEAN,
                        parameters: []
                    },
                    {
                        capabilityId: 'temperature',
                        name: '温度',
                        type: CapabilityType.NUMBER,
                        parameters: [],
                        minValue: 16,
                        maxValue: 30,
                        step: 1,
                        unit: '°C'
                    },
                    {
                        capabilityId: 'mode',
                        name: '模式',
                        type: CapabilityType.ENUM,
                        parameters: [],
                        options: ['制冷', '制热', '除湿', '送风']
                    }
                ],
                online: true,
                status: { power: true, temperature: 24, mode: '制冷' },
                permissions: [],
                roomId: 'room_001'
            },
            {
                deviceId: 'curtain_001',
                deviceName: '客厅窗帘',
                deviceType: DeviceType.CURTAIN,
                manufacturer: 'Dooya',
                model: 'Smart Curtain',
                firmwareVersion: '1.5.0',
                networkStatus: 'online',
                signalStrength: 70,
                capabilities: [
                    {
                        capabilityId: 'position',
                        name: '位置',
                        type: CapabilityType.NUMBER,
                        parameters: [],
                        minValue: 0,
                        maxValue: 100,
                        step: 1,
                        unit: '%'
                    }
                ],
                online: true,
                status: { position: 50 },
                permissions: [],
                roomId: 'room_001'
            },
            {
                deviceId: 'lock_001',
                deviceName: '智能门锁',
                deviceType: DeviceType.LOCK,
                manufacturer: 'August',
                model: 'Smart Lock Pro',
                firmwareVersion: '3.0.0',
                networkStatus: 'online',
                signalStrength: 85,
                capabilities: [
                    {
                        capabilityId: 'lock',
                        name: '锁定状态',
                        type: CapabilityType.BOOLEAN,
                        parameters: []
                    }
                ],
                online: true,
                status: { lock: true },
                permissions: [],
                roomId: 'room_002'
            }
        ];
      
        // 初始化设备状态
        for (const device of this.devices) {
            const state = new DeviceState();
            state.online = device.online;
            state.status = device.status;
            state.lastUpdate = Date.now();
            this.deviceStates.set(device.deviceId, state);
        }
    }
  
    // 加载房间列表
    async loadRooms() {
        this.rooms = [
            { roomId: 'all', name: '全部', icon: '🏠' },
            { roomId: 'room_001', name: '客厅', icon: '🛋️' },
            { roomId: 'room_002', name: '玄关', icon: '🚪' },
            { roomId: 'room_003', name: '卧室', icon: '🛏️' },
            { roomId: 'room_004', name: '厨房', icon: '🍳' }
        ];
    }
  
    // 加载场景列表
    async loadScenes() {
        this.scenes = [
            {
                sceneId: 'scene_001',
                name: '回家模式',
                icon: '🏠',
                actions: [
                    { deviceId: 'light_001', capability: 'power', value: true },
                    { deviceId: 'light_001', capability: 'brightness', value: 80 },
                    { deviceId: 'ac_001', capability: 'power', value: true },
                    { deviceId: 'ac_001', capability: 'temperature', value: 24 }
                ]
            },
            {
                sceneId: 'scene_002',
                name: '离家模式',
                icon: '🚗',
                actions: [
                    { deviceId: 'light_001', capability: 'power', value: false },
                    { deviceId: 'ac_001', capability: 'power', value: false },
                    { deviceId: 'lock_001', capability: 'lock', value: true }
                ]
            },
            {
                sceneId: 'scene_003',
                name: '睡眠模式',
                icon: '🌙',
                actions: [
                    { deviceId: 'light_001', capability: 'power', value: false },
                    { deviceId: 'curtain_001', capability: 'position', value: 0 },
                    { deviceId: 'ac_001', capability: 'temperature', value: 26 }
                ]
            }
        ];
    }
  
    // 订阅设备状态
    subscribeDeviceStatus() {
        // 订阅设备状态变化
        for (const device of this.devices) {
            // 实际实现需要通过MQTT或其他协议订阅
            // this.subscribeToDevice(device.deviceId);
        }
    }
  
    // 控制设备
    async controlDevice(
        deviceId: string,
        capability: string,
        value: any
    ): Promise<boolean> {
        try {
            // 构建控制指令
            const command: ControlCommand = {
                commandId: `cmd_${Date.now()}`,
                deviceId: deviceId,
                capability: capability,
                action: 'set',
                parameters: { value: value },
                timestamp: Date.now(),
                timeout: 5000,
                priority: 'normal'
            };
          
            // 发送指令
            const result = await this.sendCommand(command);
          
            if (result) {
                // 更新本地状态
                const state = this.deviceStates.get(deviceId);
                if (state) {
                    state.status[capability] = value;
                    state.lastUpdate = Date.now();
                }
              
                console.info(`控制设备成功: ${deviceId}.${capability} = ${value}`);
                return true;
            }
          
            return false;
        } catch (err) {
            console.error(`控制设备失败: ${JSON.stringify(err)}`);
            return false;
        }
    }
  
    // 发送控制指令
    async sendCommand(command: ControlCommand): Promise<boolean> {
        // 优先尝试本地控制
        const device = this.devices.find(d => d.deviceId === command.deviceId);
        if (!device) {
            return false;
        }
      
        // 检查设备是否在线
        if (!device.online) {
            console.error('设备离线');
            return false;
        }
      
        // 尝试本地控制
        if (device.networkStatus === 'online') {
            try {
                // 通过局域网发送指令
                // await this.sendLocalCommand(command);
                console.info(`本地控制: ${command.deviceId}`);
                return true;
            } catch (err) {
                console.warn(`本地控制失败,尝试云端控制`);
            }
        }
      
        // 云端控制
        try {
            // await this.sendCloudCommand(command);
            console.info(`云端控制: ${command.deviceId}`);
            return true;
        } catch (err) {
            console.error(`云端控制失败: ${JSON.stringify(err)}`);
            return false;
        }
    }
  
    // 执行场景
    async executeScene(sceneId: string): Promise<boolean> {
        const scene = this.scenes.find(s => s.sceneId === sceneId);
        if (!scene) {
            console.error('场景不存在');
            return false;
        }
      
        console.info(`执行场景: ${scene.name}`);
      
        // 并行执行所有动作
        const promises = scene.actions.map(action => 
            this.controlDevice(action.deviceId, action.capability, action.value)
        );
      
        const results = await Promise.all(promises);
        const success = results.every(r => r);
      
        if (success) {
            console.info('场景执行成功');
        } else {
            console.warn('部分动作执行失败');
        }
      
        return success;
    }
  
    // 发现新设备
    async discoverDevices() {
        this.isScanning = true;
      
        try {
            // 开始扫描
            // await this.deviceMgr.startDeviceDiscovery();
          
            // 模拟扫描过程
            await new Promise(resolve => setTimeout(resolve, 3000));
          
            console.info('设备扫描完成');
        } catch (err) {
            console.error(`扫描失败: ${JSON.stringify(err)}`);
        } finally {
            this.isScanning = false;
        }
    }
  
    // 添加设备
    async addDevice(device: SmartDevice) {
        // 保存到本地数据库
        this.devices.push(device);
      
        // 初始化状态
        const state = new DeviceState();
        state.online = device.online;
        state.status = device.status;
        state.lastUpdate = Date.now();
        this.deviceStates.set(device.deviceId, state);
      
        // 同步到云端
        // await this.syncDeviceToCloud(device);
      
        console.info(`添加设备: ${device.deviceName}`);
    }
  
    // 删除设备
    async removeDevice(deviceId: string) {
        const index = this.devices.findIndex(d => d.deviceId === deviceId);
        if (index >= 0) {
            const device = this.devices[index];
            this.devices.splice(index, 1);
            this.deviceStates.delete(deviceId);
          
            // 从云端删除
            // await this.removeDeviceFromCloud(deviceId);
          
            console.info(`删除设备: ${device.deviceName}`);
        }
    }
  
    build() {
        Column() {
            // 顶部栏
            this.buildHeader();
          
            // 房间选择
            this.buildRoomSelector();
          
            // 场景快捷入口
            this.buildSceneSection();
          
            // 设备列表
            this.buildDeviceList();
          
            // 设备详情弹窗
            if (this.showDeviceDetail && this.selectedDevice) {
                this.buildDeviceDetailDialog();
            }
          
            // 添加设备弹窗
            if (this.showAddDevice) {
                this.buildAddDeviceDialog();
            }
        }
        .width('100%')
        .height('100%')
        .backgroundColor('#f5f5f5');
    }
  
    @Builder
    buildHeader() {
        Row() {
            Text('智能家居')
                .fontSize(24)
                .fontWeight(FontWeight.Bold);
          
            Blank();
          
            // 添加设备按钮
            Button({ type: ButtonType.Circle }) {
                Image($r('app.media.ic_add'))
                    .width(24)
                    .height(24)
                    .fillColor('#333333');
            }
            .width(40)
            .height(40)
            .backgroundColor(Color.Transparent)
            .onClick(() => {
                this.showAddDevice = true;
            });
        }
        .width('100%')
        .height(56)
        .padding({ left: 16, right: 16 })
        .backgroundColor(Color.White);
    }
  
    @Builder
    buildRoomSelector() {
        Row() {
            ForEach(this.rooms, (room: Room) => {
                Column() {
                    Text(room.icon)
                        .fontSize(24);
                  
                    Text(room.name)
                        .fontSize(12)
                        .fontColor(this.currentRoom === room.roomId ? '#2196F3' : '#666666')
                        .margin({ top: 4 });
                }
                .width(60)
                .padding(8)
                .backgroundColor(this.currentRoom === room.roomId ? '#E3F2FD' : Color.Transparent)
                .borderRadius(8)
                .onClick(() => {
                    this.currentRoom = room.roomId;
                });
            });
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceEvenly)
        .padding({ top: 8, bottom: 8 })
        .backgroundColor(Color.White);
    }
  
    @Builder
    buildSceneSection() {
        Column() {
            Text('快捷场景')
                .fontSize(16)
                .fontWeight(FontWeight.Medium)
                .margin({ bottom: 12 });
          
            Row() {
                ForEach(this.scenes, (scene: Scene) => {
                    Column() {
                        Text(scene.icon)
                            .fontSize(32);
                      
                        Text(scene.name)
                            .fontSize(12)
                            .margin({ top: 8 });
                    }
                    .width(80)
                    .height(80)
                    .justifyContent(FlexAlign.Center)
                    .backgroundColor(Color.White)
                    .borderRadius(12)
                    .shadow({ radius: 4, color: '#00000010' })
                    .onClick(() => {
                        this.executeScene(scene.sceneId);
                    });
                });
            }
            .width('100%')
            .justifyContent(FlexAlign.SpaceEvenly);
        }
        .width('100%')
        .padding(16)
        .margin({ top: 8 });
    }
  
    @Builder
    buildDeviceList() {
        Column() {
            Text('我的设备')
                .fontSize(16)
                .fontWeight(FontWeight.Medium)
                .margin({ bottom: 12 });
          
            List() {
                ForEach(this.getFilteredDevices(), (device: SmartDevice) => {
                    ListItem() {
                        this.buildDeviceItem(device);
                    }
                });
            }
            .width('100%')
            .layoutWeight(1);
        }
        .width('100%')
        .padding(16)
        .layoutWeight(1);
    }
  
    // 获取过滤后的设备列表
    getFilteredDevices(): SmartDevice[] {
        if (this.currentRoom === 'all') {
            return this.devices;
        }
      
        return this.devices.filter(d => (d as any).roomId === this.currentRoom);
    }
  
    @Builder
    buildDeviceItem(device: SmartDevice) {
        Row() {
            // 设备图标
            Column() {
                Text(this.getDeviceIcon(device.deviceType))
                    .fontSize(32);
            }
            .width(60)
            .height(60)
            .justifyContent(FlexAlign.Center)
            .backgroundColor(this.getDeviceColor(device.deviceType))
            .borderRadius(12);
          
            // 设备信息
            Column() {
                Text(device.deviceName)
                    .fontSize(16)
                    .fontWeight(FontWeight.Medium);
              
                Text(this.getDeviceStatusText(device))
                    .fontSize(12)
                    .fontColor(device.online ? '#4CAF50' : '#9E9E9E')
                    .margin({ top: 4 });
            }
            .alignItems(HorizontalAlign.Start)
            .margin({ left: 16 })
            .layoutWeight(1);
          
            // 快捷控制
            this.buildQuickControl(device);
        }
        .width('100%')
        .padding(16)
        .backgroundColor(Color.White)
        .borderRadius(12)
        .margin({ bottom: 8 })
        .onClick(() => {
            this.selectedDevice = device;
            this.showDeviceDetail = true;
        });
    }
  
    @Builder
    buildQuickControl(device: SmartDevice) {
        // 根据设备类型显示不同的快捷控制
        const state = this.deviceStates.get(device.deviceId);
      
        switch (device.deviceType) {
            case DeviceType.LIGHT:
                // 灯光开关
                Toggle({ type: ToggleType.Switch, isOn: state?.status?.power || false })
                    .onChange((isOn: boolean) => {
                        this.controlDevice(device.deviceId, 'power', isOn);
                    });
                break;
              
            case DeviceType.AIR_CONDITIONER:
                // 空调开关
                Toggle({ type: ToggleType.Switch, isOn: state?.status?.power || false })
                    .onChange((isOn: boolean) => {
                        this.controlDevice(device.deviceId, 'power', isOn);
                    });
                break;
              
            case DeviceType.LOCK:
                // 门锁状态
                Text(state?.status?.lock ? '已锁' : '未锁')
                    .fontSize(12)
                    .fontColor(state?.status?.lock ? '#4CAF50' : '#F44336');
                break;
              
            default:
                // 默认显示在线状态
                Circle()
                    .width(12)
                    .height(12)
                    .fill(device.online ? '#4CAF50' : '#9E9E9E');
        }
    }
  
    @Builder
    buildDeviceDetailDialog() {
        Column() {
            // 标题栏
            Row() {
                Text(this.selectedDevice?.deviceName || '')
                    .fontSize(20)
                    .fontWeight(FontWeight.Bold);
              
                Blank();
              
                Button({ type: ButtonType.Circle }) {
                    Image($r('app.media.ic_close'))
                        .width(20)
                        .height(20);
                }
                .width(32)
                .height(32)
                .backgroundColor(Color.Transparent)
                .onClick(() => {
                    this.showDeviceDetail = false;
                });
            }
            .width('100%')
            .margin({ bottom: 20 });
          
            // 设备状态
            if (this.selectedDevice) {
                this.buildDeviceControls(this.selectedDevice);
            }
          
            // 设备信息
            Column() {
                Text('设备信息')
                    .fontSize(16)
                    .fontWeight(FontWeight.Medium)
                    .margin({ bottom: 12 });
              
                Row() {
                    Text('厂商')
                        .fontSize(14)
                        .fontColor('#666666');
                    Blank();
                    Text(this.selectedDevice?.manufacturer || '')
                        .fontSize(14);
                }
                .width('100%')
                .margin({ bottom: 8 });
              
                Row() {
                    Text('型号')
                        .fontSize(14)
                        .fontColor('#666666');
                    Blank();
                    Text(this.selectedDevice?.model || '')
                        .fontSize(14);
                }
                .width('100%')
                .margin({ bottom: 8 });
              
                Row() {
                    Text('固件版本')
                        .fontSize(14)
                        .fontColor('#666666');
                    Blank();
                    Text(this.selectedDevice?.firmwareVersion || '')
                        .fontSize(14);
                }
                .width('100%');
            }
            .width('100%')
            .padding(16)
            .backgroundColor('#f5f5f5')
            .borderRadius(8)
            .margin({ top: 20 });
          
            // 删除按钮
            Button('删除设备')
                .width('100%')
                .backgroundColor('#F44336')
                .margin({ top: 20 })
                .onClick(() => {
                    if (this.selectedDevice) {
                        this.removeDevice(this.selectedDevice.deviceId);
                        this.showDeviceDetail = false;
                    }
                });
        }
        .width('85%')
        .padding(20)
        .backgroundColor(Color.White)
        .borderRadius(16)
        .shadow({ radius: 20, color: '#00000020' });
    }
  
    @Builder
    buildDeviceControls(device: SmartDevice) {
        Column() {
            ForEach(device.capabilities, (capability: DeviceCapability) => {
                Column() {
                    Text(capability.name)
                        .fontSize(14)
                        .fontColor('#666666')
                        .margin({ bottom: 8 });
                  
                    this.buildCapabilityControl(device, capability);
                }
                .width('100%')
                .margin({ bottom: 20 });
            });
        }
        .width('100%');
    }
  
    @Builder
    buildCapabilityControl(device: SmartDevice, capability: DeviceCapability) {
        const state = this.deviceStates.get(device.deviceId);
        const currentValue = state?.status?.[capability.capabilityId];
      
        switch (capability.type) {
            case CapabilityType.BOOLEAN:
                // 开关控制
                Row() {
                    Text(currentValue ? '开启' : '关闭')
                        .fontSize(16);
                  
                    Blank();
                  
                    Toggle({ type: ToggleType.Switch, isOn: currentValue || false })
                        .onChange((isOn: boolean) => {
                            this.controlDevice(device.deviceId, capability.capabilityId, isOn);
                        });
                }
                .width('100%');
                break;
              
            case CapabilityType.NUMBER:
                // 数值控制(滑块)
                Column() {
                    Row() {
                        Text(`${currentValue || capability.minValue}`)
                            .fontSize(24)
                            .fontWeight(FontWeight.Bold);
                      
                        if (capability.unit) {
                            Text(capability.unit)
                                .fontSize(16)
                                .margin({ left: 4 });
                        }
                    }
                    .margin({ bottom: 12 });
                  
                    Slider({
                        value: currentValue || capability.minValue || 0,
                        min: capability.minValue || 0,
                        max: capability.maxValue || 100,
                        step: capability.step || 1
                    })
                        .width('100%')
                        .blockColor('#2196F3')
                        .trackColor('rgba(33,150,243,0.2)')
                        .onChange((value: number) => {
                            this.controlDevice(device.deviceId, capability.capabilityId, Math.round(value));
                        });
                }
                .width('100%');
                break;
              
            case CapabilityType.ENUM:
                // 枚举控制(选项)
                Row() {
                    ForEach(capability.options || [], (option: string) => {
                        Button(option)
                            .fontSize(12)
                            .height(32)
                            .backgroundColor(currentValue === option ? '#2196F3' : '#f5f5f5')
                            .fontColor(currentValue === option ? '#ffffff' : '#333333')
                            .onClick(() => {
                                this.controlDevice(device.deviceId, capability.capabilityId, option);
                            });
                    });
                }
                .width('100%')
                .justifyContent(FlexAlign.SpaceEvenly);
                break;
        }
    }
  
    @Builder
    buildAddDeviceDialog() {
        Column() {
            Text('添加设备')
                .fontSize(20)
                .fontWeight(FontWeight.Bold)
                .margin({ bottom: 20 });
          
            if (this.isScanning) {
                // 扫描中
                Column() {
                    LoadingProgress()
                        .width(48)
                        .height(48)
                        .color('#2196F3');
                  
                    Text('正在扫描附近设备...')
                        .fontSize(14)
                        .fontColor('#666666')
                        .margin({ top: 16 });
                }
                .width('100%')
                .height(200)
                .justifyContent(FlexAlign.Center);
            } else {
                // 扫描按钮
                Button('开始扫描')
                    .width('100%')
                    .onClick(() => {
                        this.discoverDevices();
                    });
              
                // 手动添加
                Button('手动添加')
                    .width('100%')
                    .backgroundColor('#666666')
                    .margin({ top: 12 })
                    .onClick(() => {
                        // 打开手动添加页面
                    });
            }
          
            Button('取消')
                .width('100%')
                .backgroundColor(Color.Transparent)
                .fontColor('#666666')
                .margin({ top: 20 })
                .onClick(() => {
                    this.showAddDevice = false;
                });
        }
        .width('80%')
        .padding(20)
        .backgroundColor(Color.White)
        .borderRadius(12)
        .shadow({ radius: 20, color: '#00000020' });
    }
  
    // 获取设备图标
    getDeviceIcon(type: DeviceType): string {
        const icons: Record<string, string> = {
            [DeviceType.LIGHT]: '💡',
            [DeviceType.AIR_CONDITIONER]: '❄️',
            [DeviceType.CURTAIN]: '🪟',
            [DeviceType.LOCK]: '🔒',
            [DeviceType.CAMERA]: '📷',
            [DeviceType.SENSOR]: '📡',
            [DeviceType.SWITCH]: '🔌',
            [DeviceType.APPLIANCE]: '📺'
        };
        return icons[type] || '📱';
    }
  
    // 获取设备颜色
    getDeviceColor(type: DeviceType): string {
        const colors: Record<string, string> = {
            [DeviceType.LIGHT]: '#FFF9C4',
            [DeviceType.AIR_CONDITIONER]: '#B3E5FC',
            [DeviceType.CURTAIN]: '#C8E6C9',
            [DeviceType.LOCK]: '#FFCCBC',
            [DeviceType.CAMERA]: '#D1C4E9',
            [DeviceType.SENSOR]: '#DCEDC8',
            [DeviceType.SWITCH]: '#FFE0B2',
            [DeviceType.APPLIANCE]: '#F0F4C3'
        };
        return colors[type] || '#E0E0E0';
    }
  
    // 获取设备状态文本
    getDeviceStatusText(device: SmartDevice): string {
        if (!device.online) {
            return '离线';
        }
      
        const state = this.deviceStates.get(device.deviceId);
      
        switch (device.deviceType) {
            case DeviceType.LIGHT:
                return state?.status?.power ? `亮度 ${state.status.brightness}%` : '已关闭';
            case DeviceType.AIR_CONDITIONER:
                return state?.status?.power ? `${state.status.mode} ${state.status.temperature}°C` : '已关闭';
            case DeviceType.CURTAIN:
                return `开启 ${state?.status?.position || 0}%`;
            case DeviceType.LOCK:
                return state?.status?.lock ? '已锁定' : '未锁定';
            default:
                return '在线';
        }
    }
}

// 房间定义
interface Room {
    roomId: string;
    name: string;
    icon: string;
}

// 场景定义
interface Scene {
    sceneId: string;
    name: string;
    icon: string;
    actions: SceneAction[];
}

// 场景动作
interface SceneAction {
    deviceId: string;
    capability: string;
    value: any;
}

// 网络状态
type NetworkStatus = 'online' | 'offline' | 'unreachable';

// 设备权限
interface DevicePermission {
    userId: string;
    permission: 'owner' | 'admin' | 'user' | 'guest';
}

// 控制指令
interface ControlCommand {
    commandId: string;
    deviceId: string;
    capability: string;
    action: string;
    parameters: Record<string, any>;
    timestamp: number;
    timeout: number;
    priority: 'high' | 'normal' | 'low';
}

3.2 自动化场景编排

实现设备间的自动化联动:

// AutomationManager.ets

// 自动化规则
interface AutomationRule {
    ruleId: string;
    name: string;
    enabled: boolean;
    triggers: Trigger[];          // 触发条件
    conditions: Condition[];      // 执行条件
    actions: SceneAction[];       // 执行动作
    executionMode: 'serial' | 'parallel';  // 执行模式
}

// 触发器
interface Trigger {
    triggerId: string;
    type: TriggerType;
    deviceId?: string;
    capability?: string;
    value?: any;
    schedule?: ScheduleConfig;
}

// 触发类型
enum TriggerType {
    DEVICE_STATE = 'device_state',      // 设备状态变化
    SCHEDULE = 'schedule',               // 定时
    LOCATION = 'location',               // 位置
    MANUAL = 'manual'                    // 手动
}

// 条件
interface Condition {
    type: 'device' | 'time' | 'location';
    deviceId?: string;
    capability?: string;
    operator?: 'eq' | 'ne' | 'gt' | 'lt' | 'ge' | 'le';
    value?: any;
}

// 定时配置
interface ScheduleConfig {
    type: 'once' | 'daily' | 'weekly' | 'monthly';
    time: string;              // "08:00"
    days?: number[];           // 周几 [1-7]
    date?: string;             // 日期 "2024-01-01"
}

@Entry
@Component
struct AutomationManager {
    @State rules: AutomationRule[] = [];
    @State showRuleEditor: boolean = false;
  
    aboutToAppear() {
        this.loadRules();
        this.startRuleEngine();
    }
  
    // 加载规则
    async loadRules() {
        this.rules = [
            {
                ruleId: 'rule_001',
                name: '开门自动开灯',
                enabled: true,
                triggers: [
                    {
                        triggerId: 'trigger_001',
                        type: TriggerType.DEVICE_STATE,
                        deviceId: 'lock_001',
                        capability: 'lock',
                        value: false  // 门打开
                    }
                ],
                conditions: [
                    {
                        type: 'time',
                        operator: 'ge',
                        value: '18:00'  // 晚上6点后
                    }
                ],
                actions: [
                    { deviceId: 'light_001', capability: 'power', value: true }
                ],
                executionMode: 'parallel'
            },
            {
                ruleId: 'rule_002',
                name: '早上自动开窗帘',
                enabled: true,
                triggers: [
                    {
                        triggerId: 'trigger_002',
                        type: TriggerType.SCHEDULE,
                        schedule: {
                            type: 'daily',
                            time: '07:00'
                        }
                    }
                ],
                conditions: [],
                actions: [
                    { deviceId: 'curtain_001', capability: 'position', value: 100 }
                ],
                executionMode: 'parallel'
            }
        ];
    }
  
    // 启动规则引擎
    startRuleEngine() {
        // 监听设备状态变化
        this.monitorDeviceState();
      
        // 监听定时触发
        this.monitorSchedule();
    }
  
    // 监听设备状态
    monitorDeviceState() {
        // 订阅所有设备的状态变化
        // 当设备状态变化时,检查是否有规则被触发
    }
  
    // 监听定时触发
    monitorSchedule() {
        // 设置定时器,在指定时间触发规则
    }
  
    // 执行规则
    async executeRule(rule: AutomationRule): Promise<boolean> {
        if (!rule.enabled) {
            return false;
        }
      
        // 检查条件
        const conditionsMet = await this.checkConditions(rule.conditions);
        if (!conditionsMet) {
            console.info(`规则 ${rule.name} 条件不满足`);
            return false;
        }
      
        // 执行动作
        if (rule.executionMode === 'parallel') {
            // 并行执行
            const promises = rule.actions.map(action => 
                this.executeAction(action)
            );
            await Promise.all(promises);
        } else {
            // 串行执行
            for (const action of rule.actions) {
                await this.executeAction(action);
            }
        }
      
        console.info(`规则 ${rule.name} 执行完成`);
        return true;
    }
  
    // 检查条件
    async checkConditions(conditions: Condition[]): Promise<boolean> {
        for (const condition of conditions) {
            const met = await this.checkCondition(condition);
            if (!met) {
                return false;
            }
        }
        return true;
    }
  
    // 检查单个条件
    async checkCondition(condition: Condition): Promise<boolean> {
        switch (condition.type) {
            case 'device':
                // 检查设备状态
                const state = await this.getDeviceState(condition.deviceId || '');
                const value = state?.[condition.capability || ''];
                return this.compareValues(value, condition.operator || 'eq', condition.value);
              
            case 'time':
                // 检查时间
                const now = new Date();
                const currentTime = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
                return this.compareValues(currentTime, condition.operator || 'eq', condition.value);
              
            case 'location':
                // 检查位置
                // 实现位置检查逻辑
                return true;
              
            default:
                return true;
        }
    }
  
    // 比较值
    compareValues(a: any, operator: string, b: any): boolean {
        switch (operator) {
            case 'eq': return a === b;
            case 'ne': return a !== b;
            case 'gt': return a > b;
            case 'lt': return a < b;
            case 'ge': return a >= b;
            case 'le': return a <= b;
            default: return false;
        }
    }
  
    // 执行动作
    async executeAction(action: SceneAction): Promise<boolean> {
        // 调用设备控制
        // return await this.controlDevice(action.deviceId, action.capability, action.value);
        console.info(`执行动作: ${action.deviceId}.${action.capability} = ${action.value}`);
        return true;
    }
  
    // 获取设备状态
    async getDeviceState(deviceId: string): Promise<Record<string, any> | null> {
        // 从设备状态管理器获取状态
        return null;
    }
  
    build() {
        Column() {
            Text('自动化')
                .fontSize(24)
                .fontWeight(FontWeight.Bold)
                .margin({ bottom: 20 });
          
            // 规则列表
            List() {
                ForEach(this.rules, (rule: AutomationRule) => {
                    ListItem() {
                        this.buildRuleItem(rule);
                    }
                });
            }
            .width('100%')
            .layoutWeight(1);
          
            // 添加规则按钮
            Button('添加规则')
                .width('100%')
                .onClick(() => {
                    this.showRuleEditor = true;
                });
        }
        .width('100%')
        .height('100%')
        .padding(16);
    }
  
    @Builder
    buildRuleItem(rule: AutomationRule) {
        Row() {
            Column() {
                Text(rule.name)
                    .fontSize(16)
                    .fontWeight(FontWeight.Medium);
              
                Text(`${rule.triggers.length} 个触发器 | ${rule.actions.length} 个动作`)
                    .fontSize(12)
                    .fontColor('#666666')
                    .margin({ top: 4 });
            }
            .alignItems(HorizontalAlign.Start)
            .layoutWeight(1);
          
            Toggle({ type: ToggleType.Switch, isOn: rule.enabled })
                .onChange((isOn: boolean) => {
                    rule.enabled = isOn;
                });
        }
        .width('100%')
        .padding(16)
        .backgroundColor(Color.White)
        .borderRadius(8)
        .margin({ bottom: 8 });
    }
}

3.3 设备分组与房间管理

实现设备的分组管理:

// RoomManager.ets

@Entry
@Component
struct RoomManager {
    @State rooms: RoomInfo[] = [];
    @State selectedRoom: RoomInfo | null = null;
    @State devices: SmartDevice[] = [];
  
    aboutToAppear() {
        this.loadRooms();
    }
  
    // 加载房间
    async loadRooms() {
        this.rooms = [
            {
                roomId: 'room_001',
                name: '客厅',
                icon: '🛋️',
                deviceCount: 5,
                temperature: 24,
                humidity: 60
            },
            {
                roomId: 'room_002',
                name: '卧室',
                icon: '🛏️',
                deviceCount: 3,
                temperature: 22,
                humidity: 55
            },
            {
                roomId: 'room_003',
                name: '厨房',
                icon: '🍳',
                deviceCount: 2,
                temperature: 26,
                humidity: 70
            }
        ];
    }
  
    // 创建房间
    async createRoom(name: string, icon: string) {
        const room: RoomInfo = {
            roomId: `room_${Date.now()}`,
            name: name,
            icon: icon,
            deviceCount: 0,
            temperature: 0,
            humidity: 0
        };
      
        this.rooms.push(room);
    }
  
    // 删除房间
    async deleteRoom(roomId: string) {
        const index = this.rooms.findIndex(r => r.roomId === roomId);
        if (index >= 0) {
            this.rooms.splice(index, 1);
        }
    }
  
    // 添加设备到房间
    async addDeviceToRoom(deviceId: string, roomId: string) {
        // 更新设备的房间归属
        const device = this.devices.find(d => d.deviceId === deviceId);
        if (device) {
            (device as any).roomId = roomId;
          
            // 更新房间设备计数
            const room = this.rooms.find(r => r.roomId === roomId);
            if (room) {
                room.deviceCount++;
            }
        }
    }
  
    build() {
        Column() {
            Text('房间管理')
                .fontSize(24)
                .fontWeight(FontWeight.Bold)
                .margin({ bottom: 20 });
          
            // 房间列表
            List() {
                ForEach(this.rooms, (room: RoomInfo) => {
                    ListItem() {
                        this.buildRoomItem(room);
                    }
                });
            }
            .width('100%')
            .layoutWeight(1);
          
            // 添加房间按钮
            Button('添加房间')
                .width('100%')
                .onClick(() => {
                    // 显示添加房间对话框
                });
        }
        .width('100%')
        .height('100%')
        .padding(16);
    }
  
    @Builder
    buildRoomItem(room: RoomInfo) {
        Column() {
            Row() {
                Text(room.icon)
                    .fontSize(32);
              
                Column() {
                    Text(room.name)
                        .fontSize(18)
                        .fontWeight(FontWeight.Medium);
                  
                    Text(`${room.deviceCount} 个设备`)
                        .fontSize(12)
                        .fontColor('#666666')
                        .margin({ top: 4 });
                }
                .alignItems(HorizontalAlign.Start)
                .margin({ left: 16 })
                .layoutWeight(1);
            }
            .width('100%');
          
            // 环境信息
            Row() {
                Row() {
                    Text('🌡️')
                        .fontSize(16);
                    Text(`${room.temperature}°C`)
                        .fontSize(14)
                        .margin({ left: 4 });
                }
                .margin({ right: 24 });
              
                Row() {
                    Text('💧')
                        .fontSize(16);
                    Text(`${room.humidity}%`)
                        .fontSize(14)
                        .margin({ left: 4 });
                };
            }
            .width('100%')
            .margin({ top: 12 });
        }
        .width('100%')
        .padding(16)
        .backgroundColor(Color.White)
        .borderRadius(12)
        .margin({ bottom: 8 });
    }
}

interface RoomInfo {
    roomId: string;
    name: string;
    icon: string;
    deviceCount: number;
    temperature: number;
    humidity: number;
}

四、踩坑与注意事项

4.1 设备离线处理

问题现象
设备突然离线,控制指令无响应,用户不知道发生了什么。

解决方案

// 设备离线检测与处理
class DeviceOfflineHandler {
    private heartbeatInterval: number = 30000;  // 心跳间隔30秒
    private offlineThreshold: number = 3;        // 连续3次未响应判定离线
  
    // 监控设备在线状态
    monitorDeviceOnline(deviceId: string) {
        let missCount = 0;
      
        setInterval(async () => {
            try {
                // 发送心跳检测
                const response = await this.sendHeartbeat(deviceId);
              
                if (response) {
                    missCount = 0;
                    this.updateDeviceOnline(deviceId, true);
                } else {
                    missCount++;
                    if (missCount >= this.offlineThreshold) {
                        this.updateDeviceOnline(deviceId, false);
                        this.handleDeviceOffline(deviceId);
                    }
                }
            } catch (err) {
                missCount++;
                if (missCount >= this.offlineThreshold) {
                    this.updateDeviceOnline(deviceId, false);
                    this.handleDeviceOffline(deviceId);
                }
            }
        }, this.heartbeatInterval);
    }
  
    // 处理设备离线
    handleDeviceOffline(deviceId: string) {
        // 1. 更新UI显示离线状态
        // 2. 发送通知提醒用户
        // 3. 尝试重新连接
      
        console.warn(`设备 ${deviceId} 已离线`);
      
        // 尝试重连
        this.reconnectDevice(deviceId);
    }
  
    // 重新连接设备
    async reconnectDevice(deviceId: string) {
        let attempts = 0;
        const maxAttempts = 5;
      
        while (attempts < maxAttempts) {
            attempts++;
          
            try {
                await this.connectDevice(deviceId);
                this.updateDeviceOnline(deviceId, true);
                console.info(`设备 ${deviceId} 重连成功`);
                return;
            } catch (err) {
                console.warn(`重连失败,第 ${attempts}`);
                await new Promise(resolve => setTimeout(resolve, 5000));
            }
        }
      
        console.error(`设备 ${deviceId} 重连失败`);
    }
  
    async sendHeartbeat(deviceId: string): Promise<boolean> {
        // 发送心跳包
        return true;
    }
  
    updateDeviceOnline(deviceId: string, online: boolean) {
        // 更新设备在线状态
    }
  
    async connectDevice(deviceId: string) {
        // 连接设备
    }
}

4.2 控制指令延迟

问题现象
控制指令下发后,设备响应慢,用户体验差。

解决方案

// 指令优化策略
class CommandOptimizer {
    // 指令合并:短时间内多个指令合并为一个
    private pendingCommands: Map<string, ControlCommand> = new Map();
    private mergeWindow: number = 100;  // 合并窗口100ms
  
    // 发送指令(带合并)
    async sendCommandWithMerge(command: ControlCommand): Promise<boolean> {
        const key = `${command.deviceId}_${command.capability}`;
      
        // 检查是否有待发送的指令
        const pending = this.pendingCommands.get(key);
      
        if (pending) {
            // 合并指令
            pending.parameters = command.parameters;
            pending.timestamp = Date.now();
        } else {
            // 添加新指令
            this.pendingCommands.set(key, command);
          
            // 延迟发送
            setTimeout(() => {
                const cmd = this.pendingCommands.get(key);
                if (cmd) {
                    this.sendCommand(cmd);
                    this.pendingCommands.delete(key);
                }
            }, this.mergeWindow);
        }
      
        return true;
    }
  
    // 指令预测:预测用户下一步操作,提前准备
    predictNextCommand(currentCommand: ControlCommand): ControlCommand | null {
        // 根据当前指令预测下一步
        // 例如:调高温度后,可能还会继续调高
      
        return null;
    }
  
    // 本地缓存:先更新本地状态,再发送指令
    async sendCommandWithCache(command: ControlCommand): Promise<boolean> {
        // 立即更新本地状态(乐观更新)
        this.updateLocalState(command.deviceId, command.capability, command.parameters.value);
      
        try {
            // 发送指令
            const result = await this.sendCommand(command);
          
            if (!result) {
                // 失败时回滚本地状态
                this.rollbackLocalState(command.deviceId);
            }
          
            return result;
        } catch (err) {
            // 异常时回滚
            this.rollbackLocalState(command.deviceId);
            return false;
        }
    }
  
    async sendCommand(command: ControlCommand): Promise<boolean> {
        // 实际发送指令
        return true;
    }
  
    updateLocalState(deviceId: string, capability: string, value: any) {
        // 更新本地状态
    }
  
    rollbackLocalState(deviceId: string) {
        // 回滚本地状态
    }
}

4.3 多用户权限管理

问题现象
家人想控制设备,但没有权限,或者权限过大不安全。

解决方案

// 权限管理器
class PermissionManager {
    // 用户角色
    enum Role {
        OWNER = 'owner',       // 所有者:完全控制
        ADMIN = 'admin',       // 管理员:除删除设备外所有权限
        USER = 'user',         // 普通用户:控制设备
        GUEST = 'guest'        // 访客:仅查看状态
    }
  
    // 权限检查
    async checkPermission(
        userId: string,
        deviceId: string,
        action: 'view' | 'control' | 'config' | 'delete'
    ): Promise<boolean> {
        // 获取用户角色
        const role = await this.getUserRole(userId, deviceId);
      
        // 根据角色判断权限
        switch (action) {
            case 'view':
                return true;  // 所有人都可以查看
              
            case 'control':
                return role !== Role.GUEST;
              
            case 'config':
                return role === Role.OWNER || role === Role.ADMIN;
              
            case 'delete':
                return role === Role.OWNER;
              
            default:
                return false;
        }
    }
  
    // 授权
    async grantPermission(
        ownerId: string,
        userId: string,
        deviceId: string,
        role: Role
    ): Promise<boolean> {
        // 检查操作者是否有授权权限
        const ownerRole = await this.getUserRole(ownerId, deviceId);
        if (ownerRole !== Role.OWNER) {
            console.error('无授权权限');
            return false;
        }
      
        // 添加权限记录
        await this.addPermissionRecord(userId, deviceId, role);
      
        // 发送通知给被授权用户
        await this.notifyUser(userId, `您已获得设备的${role}权限`);
      
        return true;
    }
  
    // 撤销权限
    async revokePermission(
        ownerId: string,
        userId: string,
        deviceId: string
    ): Promise<boolean> {
        // 检查操作者权限
        const ownerRole = await this.getUserRole(ownerId, deviceId);
        if (ownerRole !== Role.OWNER) {
            return false;
        }
      
        // 删除权限记录
        await this.removePermissionRecord(userId, deviceId);
      
        return true;
    }
  
    async getUserRole(userId: string, deviceId: string): Promise<Role> {
        // 查询用户角色
        return Role.USER;
    }
  
    async addPermissionRecord(userId: string, deviceId: string, role: Role) {
        // 添加权限记录
    }
  
    async removePermissionRecord(userId: string, deviceId: string) {
        // 删除权限记录
    }
  
    async notifyUser(userId: string, message: string) {
        // 发送通知
    }
}

4.4 设备兼容性问题

问题现象
不同品牌的设备协议不同,无法统一控制。

解决方案

// 协议适配器
class ProtocolAdapter {
    private adapters: Map<string, DeviceAdapter> = new Map();
  
    // 注册适配器
    registerAdapter(protocol: string, adapter: DeviceAdapter) {
        this.adapters.set(protocol, adapter);
    }
  
    // 控制设备
    async controlDevice(device: SmartDevice, capability: string, value: any): Promise<boolean> {
        // 获取设备协议
        const protocol = device.manufacturer.toLowerCase();
      
        // 获取适配器
        const adapter = this.adapters.get(protocol);
      
        if (!adapter) {
            // 使用默认适配器
            return this.defaultControl(device, capability, value);
        }
      
        // 使用专用适配器
        return adapter.control(device, capability, value);
    }
  
    // 默认控制方式
    private async defaultControl(device: SmartDevice, capability: string, value: any): Promise<boolean> {
        // 使用标准协议控制
        return true;
    }
}

// 设备适配器接口
interface DeviceAdapter {
    control(device: SmartDevice, capability: string, value: any): Promise<boolean>;
    query(device: SmartDevice, capability: string): Promise<any>;
    discover(): Promise<SmartDevice[]>;
}

// 米家适配器示例
class MiJiaAdapter implements DeviceAdapter {
    async control(device: SmartDevice, capability: string, value: any): Promise<boolean> {
        // 米家专用协议
        console.info(`米家设备控制: ${device.deviceId}`);
        return true;
    }
  
    async query(device: SmartDevice, capability: string): Promise<any> {
        return null;
    }
  
    async discover(): Promise<SmartDevice[]> {
        return [];
    }
}

五、HarmonyOS 6适配

5.1 API差异

HarmonyOS 6在IoT设备控制方面有重要更新:

设备发现API变化

// HarmonyOS 5.x
import deviceManager from '@ohos.distributedDeviceManager';

const dm = deviceManager.createDeviceManager('com.example.app');
dm.startDeviceDiscovery();

// HarmonyOS 6
import { iot } from '@kit.IoTKit';

// 新增:IoT设备管理器
const iotManager = iot.getIoTManager();

// 新增:设备发现配置
const discoveryConfig: iot.DiscoveryConfig = {
    protocols: ['ble', 'wifi', 'nfc'],  // 支持的协议
    deviceTypes: ['light', 'ac', 'lock'],  // 设备类型过滤
    timeout: 30000,                      // 超时时间
  
    // 新增:安全配网
    security: {
        encryptCredential: true,          // 加密Wi-Fi凭证
        verifyDevice: true                // 验证设备证书
    }
};

const devices = await iotManager.discoverDevices(discoveryConfig);

设备控制API变化

// HarmonyOS 6 新增设备控制API
const deviceController = iotManager.getDeviceController(deviceId);

// 新增:批量控制
const batchCommands: iot.BatchCommand = {
    commands: [
        { capability: 'power', value: true },
        { capability: 'brightness', value: 80 }
    ],
    mode: 'parallel'  // 并行执行
};

await deviceController.executeBatch(batchCommands);

// 新增:场景执行
await iotManager.executeScene('scene_001', {
    parameters: {  // 场景参数
        temperature: 24
    }
});

5.2 行为变更

设备状态同步行为变化

// HarmonyOS 6 新增状态同步配置
interface StateSyncConfigV6 {
    // 同步模式
    mode: 'push' | 'pull' | 'hybrid';
  
    // 同步间隔(pull模式)
    interval?: number;
  
    // 新增:状态变化回调
    onChanged?: (deviceId: string, capability: string, value: any) => void;
  
    // 新增:离线缓存
    offlineCache: {
        enabled: boolean;
        maxSize: number;  // 最大缓存数量
    };
}

5.3 适配代码示例

完整的HarmonyOS 6智能家居控制适配:

// SmartHomeV6.ets
import { iot } from '@kit.IoTKit';
import { distributedData } from '@kit.ArkData';

@Entry
@Component
struct SmartHomeV6 {
    @State devices: SmartDevice[] = [];
    @State scenes: Scene[] = [];
  
    // HarmonyOS 6 API
    private iotManager: iot.IoTManager | null = null;
  
    aboutToAppear() {
        this.initV6();
    }
  
    // 初始化(HarmonyOS 6)
    async initV6() {
        try {
            // 1. 获取IoT管理器
            this.iotManager = iot.getIoTManager();
          
            // 2. 加载设备
            await this.loadDevicesV6();
          
            // 3. 订阅设备状态(HarmonyOS 6新API)
            this.subscribeDeviceStatusV6();
          
            console.info('初始化成功(HarmonyOS 6)');
        } catch (err) {
            console.error(`初始化失败: ${JSON.stringify(err)}`);
        }
    }
  
    // 加载设备(HarmonyOS 6)
    async loadDevicesV6() {
        if (!this.iotManager) {
            return;
        }
      
        // 获取已绑定设备列表
        // const devices = await this.iotManager.getBoundDevices();
        // this.devices = devices;
    }
  
    // 订阅设备状态(HarmonyOS 6)
    subscribeDeviceStatusV6() {
        if (!this.iotManager) {
            return;
        }
      
        // HarmonyOS 6新增:统一状态订阅
        const config: iot.StateSubscriptionConfig = {
            mode: 'push',
            onChanged: (deviceId: string, capability: string, value: any) => {
                console.info(`设备状态变化: ${deviceId}.${capability} = ${value}`);
                this.updateDeviceState(deviceId, capability, value);
            }
        };
      
        // this.iotManager.subscribeState(config);
    }
  
    // 控制设备(HarmonyOS 6)
    async controlDeviceV6(deviceId: string, capability: string, value: any): Promise<boolean> {
        if (!this.iotManager) {
            return false;
        }
      
        try {
            // 获取设备控制器
            // const controller = this.iotManager.getDeviceController(deviceId);
          
            // 执行控制
            // await controller.setCapability(capability, value);
          
            console.info(`控制设备: ${deviceId}.${capability} = ${value}`);
            return true;
        } catch (err) {
            console.error(`控制失败: ${JSON.stringify(err)}`);
            return false;
        }
    }
  
    // 执行场景(HarmonyOS 6)
    async executeSceneV6(sceneId: string): Promise<boolean> {
        if (!this.iotManager) {
            return false;
        }
      
        try {
            // HarmonyOS 6新增:场景执行API
            // await this.iotManager.executeScene(sceneId);
          
            console.info(`执行场景: ${sceneId}`);
            return true;
        } catch (err) {
            console.error(`执行场景失败: ${JSON.stringify(err)}`);
            return false;
        }
    }
  
    // 发现设备(HarmonyOS 6)
    async discoverDevicesV6(): Promise<SmartDevice[]> {
        if (!this.iotManager) {
            return [];
        }
      
        try {
            // HarmonyOS 6新配置
            const config: iot.DiscoveryConfig = {
                protocols: ['ble', 'wifi'],
                deviceTypes: [],
                timeout: 30000,
                security: {
                    encryptCredential: true,
                    verifyDevice: true
                }
            };
          
            // const devices = await this.iotManager.discoverDevices(config);
            // return devices;
            return [];
        } catch (err) {
            console.error(`发现设备失败: ${JSON.stringify(err)}`);
            return [];
        }
    }
  
    // 更新设备状态
    updateDeviceState(deviceId: string, capability: string, value: any) {
        const device = this.devices.find(d => d.deviceId === deviceId);
        if (device) {
            device.status[capability] = value;
        }
    }
  
    build() {
        Column() {
            Text('智能家居(HarmonyOS 6)')
                .fontSize(24)
                .fontWeight(FontWeight.Bold);
          
            // UI实现...
        }
        .width('100%')
        .height('100%');
    }
}

六、总结

分布式控制场景让智能家居真正实现了"一机在手,全屋掌控"。通过本文的实战演练,我们掌握了:

核心技术要点

  1. 设备发现与配网:理解多种发现机制,实现快速配网
  2. 设备模型标准化:建立统一的设备能力描述模型
  3. 控制指令下发:掌握本地优先、云端中转的混合策略
  4. 自动化场景编排:实现设备间的智能联动

实战经验总结

  • 设备离线检测要可靠,重连机制要完善
  • 控制指令要优化,合并、预测、缓存都是有效手段
  • 权限管理要细粒度,家庭共享更安全
  • 协议适配要灵活,兼容不同品牌设备

HarmonyOS 6适配要点

  • 新的IoT Kit提供统一的设备管理API
  • 增强的安全配网机制
  • 批量控制和场景执行API
  • 完善的状态订阅机制

分布式控制场景的实现,让智能家居不再是设备的简单堆砌,而是真正协同工作的智能系统。这不仅是技术的进步,更是生活方式的革新——让家真正"智能"起来。

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐