1# 介绍<a name="ZH-CN_TOPIC_0000001128264105"></a> 2 3- [简介](#section11660541593) 4- [目录](#section1464106163817) 5- [约束](#section1718733212019) 6- [使用](#section10729231131110) 7- [涉及仓](#section176111311166) 8 9## 简介<a name="section11660541593"></a> 10 11DeviceProfile是设备硬件能力和系统软件特征的管理器,典型的Profile有设备类型、设备名称、设备OS类型、OS版本号等。DeviceProfile提供快速访问本地和远端设备Profile的能力,是发起分布式业务的基础。主要功能如下: 12 13- 本地设备Profile的插入、删除、查询。 14- 远程设备Profile的查询。 15- 订阅远程Profile变化的通知。 16- 跨设备同步Profile。 17 18DeviceProfile模块组成如下图所示: 19 20## 系统架构<a name="section13587185873516"></a> 21 22 23 24**图 1** DeviceProfile组件架构图<a name="fig4460722185514"></a> 25 26## 目录<a name="section1464106163817"></a> 27 28DeviceProfile主要代码目录结构如下: 29 30``` 31├── interfaces 32│ └── innerkits 33│ └── distributeddeviceprofile // innerkits接口 34├── ohos.build 35├── sa_profile // said声明文件 36│ ├── 6001.xml 37│ └── BUILD.gn 38├── services 39│ └── distributeddeviceprofile 40│ ├── BUILD.gn 41│ ├── include 42│ │ ├── contentsensor // CS数据采集头文件 43│ │ ├── dbstorage // 数据库操作头文件 44│ │ ├── devicemanager // 设备管理头文件 45│ │ └── subscribemanager // 订阅管理头文件 46│ ├── src 47│ │ ├── contentsensor // CS数据采集实现 48│ │ ├── dbstorage // 数据库操作实现 49│ │ ├── devicemanager // 设备管理实现 50│ │ ├── subscribemanager // 订阅管理实现 51│ └── test // 测试用例 52└── tools 53 └── dp // 辅助测试工具 54``` 55 56## 约束<a name="section1718733212019"></a> 57 58- 组网设备需在同一局域网中。 59- 组网之前,需先完成设备绑定,绑定流程参见安全子系统中说明。 60 61## 使用<a name="section10729231131110"></a> 62 63### 查询Profile信息 64 65* GetDeviceProfile参数描述 66 67| 名称 | 类型 | 必填 | 描述 | 68| --------- | ---------------------------- | ---- | ----------------------------------- | 69| deviceId | std::string | 是 | 查询指定设备的profile,空值表示查询本地 | 70| serviceId | std::string | 是 | 查询的service id | 71| profile | ServiceCharacteristicProfile | 是 | 返回值 | 72 73* 代码示例 74 75```c++ 76// 声明返回值 77ServiceCharacteristicProfile profile; 78// 执行查询接口GetDeviceProfile 79DistributedDeviceProfileClient::GetInstance().GetDeviceProfile(deviceId, serviceId, profile); 80std::string jsonData = profile.GetCharacteristicProfileJson(); 81result.append("jsonData:" + jsonData + "\n"); 82``` 83 84### 插入Profile信息 85 86* PutDeviceProfile参数描述 87 88| 名称 | 类型 | 必填 | 描述 | 89| --------- | ---------------------------- | ---- | ----------------------------------- | 90| profile | ServiceCharacteristicProfile | 是 | 需要插入的profile信息 | 91 92* 代码示例 93 94```c++ 95// 声明并填充插入数据 96ServiceCharacteristicProfile profile; 97profile.SetServiceId(serviceId); 98profile.SetServiceType(serviceType); 99nlohmann::json j; 100j["testVersion"] = "3.0.0"; 101j["testApiLevel"] = API_LEVEL; 102profile.SetCharacteristicProfileJson(j.dump()); 103// 执行插入接口PutDeviceProfile 104DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile); 105``` 106 107### 删除Profile信息 108 109* DeleteDeviceProfile参数描述 110 111| 名称 | 类型 | 必填 | 描述 | 112| --------- | ---------------------------- | ---- | ----------------------------------- | 113| serviceId | std::string | 是 | 删除特定serviceid的记录 | 114 115* 代码示例 116 117```c++ 118// 声明并填充插入数据 119std::string serviceId = "test"; 120// DeleteDeviceProfile 121DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile(serviceId); 122``` 123 124### 同步Profile信息 125 126* SyncDeviceProfile参数描述 127 128| 名称 | 类型 | 必填 | 描述 | 129| --------- | ---------------------------- | ---- | ----------------------------------- | 130| syncOption| SyncOption | 是 | 指定同步范围和模式 | 131| syncCb | IProfileEventCallback | 是 | 同步结果回调 | 132 133* 代码示例 134 135```c++ 136// 定义同步模式和范围 137SyncOptions syncOption; 138syncOption.SetSyncMode((OHOS::DeviceProfile::SyncMode)atoi(mode.c_str())); 139for (const auto& deviceId : deviceIds) { 140 syncOption.AddDevice(deviceId); 141} 142// 执行同步接口 143DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOption, 144 std::make_shared<ProfileEventCallback>()); 145``` 146 147### 订阅Profile事件(同步、变更事件) 148 149* SubscribeProfileEvents参数描述 150 151| 名称 | 类型 | 必填 | 描述 | 152| -------------- | ---------------------------- | ---- | ----------------------------------- | 153| subscribeInfos | SubscribeInfo | 是 | 指定订阅的事件类型 | 154| eventCb | IProfileEventCallback | 是 | 订阅事件回调 | 155| failedEvents | ProfileEvent | 是 | 失败事件 | 156 157* 代码示例 158 159```c++ 160auto callback = std::make_shared<ProfileEventCallback>(); 161std::list<SubscribeInfo> subscribeInfos; 162 163// 订阅EVENT_PROFILE_CHANGED事件 164ExtraInfo extraInfo; 165extraInfo["deviceId"] = deviceId; 166extraInfo["serviceIds"] = serviceIds; 167SubscribeInfo changeEventInfo; 168changeEventInfo.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED; 169changeEventInfo.extraInfo = std::move(extraInfo); 170subscribeInfos.emplace_back(changeEventInfo); 171 172// 订阅EVENT_SYNC_COMPLETED事件 173SubscribeInfo syncEventInfo; 174syncEventInfo.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED; 175subscribeInfos.emplace_back(syncEventInfo); 176 177// 执行订阅接口 178std::list<ProfileEvent> failedEvents; 179DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos, 180 callback, failedEvents); 181 182// 解除订阅 183std::list<ProfileEvent> profileEvents; 184profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED); 185DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents, 186 callback, failedEvents); 187``` 188 189## 涉及仓<a name="section176111311166"></a> 190 191**[DeviceProfile子系统](zh-cn_topic_0000001115719369.md)** 192 193[device\_profile\_core](https://gitee.com/openharmony-sig/device_profile_core)