更新时间:2022-02-17 05:26:26下载pdf
设备管理主要提供设备相关的操作,设备控制,设备状态状态变化监听,设备重命名,设备固件升级,设备移除,设备恢复出厂设置等操作。
类名 | 说明 |
---|---|
TuyaSmartDevice | 设备管理类 |
TuyaSmartDeviceModel | 设备数据模型类 |
TuyaSmartDeviceModel
数据模型
字段 | 类型 | 描述 |
---|---|---|
devId | NSString | 设备唯一 id |
name | NSString | 设备名称 |
iconUrl | NSString | 设备图标 URL |
isOnline | BOOL | 设备在线状态。此状态包含 Wi-Fi、局域网、蓝牙在线状态,只要其中任意一个在线,即为 YES |
isCloudOnline | BOOL | 设备 Wi-Fi 在线状态 |
isLocalOnline | BOOL | 设备局域网在线状态 |
isShare | BOOL | 是否为分享设备 |
dps | NSDictionary | 设备功能点数据 |
dpCodes | NSDictionary | 设备功能点数据,code-value 形式 |
schemaArray | NSArray | 设备 dp 点规则信息 |
productId | NSString | 设备所对应的产品 id |
capability | NSUInteger | 设备产品能力值 |
deviceType | TuyaSmartDeviceModelType | 设备类型 |
supportGroup | BOOL | 是否支持创建群组 |
gwType | NSString | “v” 代表虚拟体验设备,为空为真实配网设备 |
pv | NSString | 设备协议版本,Wi-Fi 协议版本或蓝牙协议版本 |
lpv | NSString | 设备局域网协议版本。默认为空,该值在设备局域网连接成功后,才会有值 |
latitude | NSString | 维度 |
longitude | NSString | 经度 |
localKey | NSString | 设备通信使用的 key |
uuid | NSString | 设备 uuid |
homeId | long long | 设备所在家庭 id |
roomId | long long | 设备所在房间 id |
upgrading | BOOL | 是否在升级中 |
timezoneId | NSString | 设备时区 |
nodeId | NSString | 设备短地址,非子设备类型值为空。用于区分网关下子设备的唯一地址 |
parentId | NSString | 父设备(上一级)id,非子设备类型值为空。子设备用于寻找对应的网关设备 id。蓝牙 mesh 子设备或为 mesh id 或对应的网关设备 id |
isMeshBleOnline | BOOL | 设备蓝牙 mesh 本地在线状态 |
devKey | NSString | 标准 SIG Mesh 设备蓝牙通信 key |
standard | BOOL | 是否为标准化产品设备。如果为标准设备,可以使用标准设备控制功能 |
standSchemaModel | TuyaSmartStandSchemaModel | 设备标准 dp 点规则信息 |
activeTime | NSTimeInterval | 激活时间 |
homeDisplayOrder | NSInteger | 设备序号,家庭获取设备列表时,可通过该属性进行排序 |
sharedTime | long long | 分享时间 |
注意:错误的设备 id 可能会导致初始化失败,此时设备的实例返回 nil
接口说明
根据设备 id 去初始化设备控制类。
/**
* Get TuyaSmartDevice instance. If current user don't have this device, a nil will be return.
* 获取设备实例。如果当前用户没有该设备,将会返回nil。
*
* @param devId Device ID
* @return instance
*/
+ (nullable instancetype)deviceWithDeviceId:(NSString *)devId;
参数说明
参数 | 说明 |
---|---|
devId | 设备 id |
示例代码
TuyaSmartDevice *device = [TuyaSmartDevice deviceWithDeviceId:devId];
device.delegate = self;
实现 TuyaSmartDeviceDelegate
代理协议后,可以在设备状态更变的回调中进行处理,刷新 App 设备控制面板的 UI
示例代码
Objc:
- (void)initDevice {
self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
self.device.delegate = self;
}
#pragma mark - TuyaSmartDeviceDelegate
- (void)device:(TuyaSmartDevice *)device dpsUpdate:(NSDictionary *)dps {
// 设备的 dps 状态发生变化,刷新界面 UI
}
- (void)deviceInfoUpdate:(TuyaSmartDevice *)device {
//当前设备信息更新 比如 设备名称修改、设备在线离线状态等
}
- (void)deviceRemoved:(TuyaSmartDevice *)device {
//当前设备被移除
}
- (void)device:(TuyaSmartDevice *)device signal:(NSString *)signal {
// Wifi信号强度
}
- (void)device:(TuyaSmartDevice *)device firmwareUpgradeProgress:(NSInteger)type progress:(double)progress {
// 固件升级进度
}
- (void)device:(TuyaSmartDevice *)device firmwareUpgradeStatusModel:(TuyaSmartFirmwareUpgradeStatusModel *)upgradeStatusModel {
// 设备升级状态的回调
}
Swift:
func initDevice() {
device = TuyaSmartDevice(deviceId: "your_device_id")
device?.delegate = self
}
// MARK: - TuyaSmartDeviceDelegate
func device(_ device: TuyaSmartDevice?, dpsUpdate dps: [AnyHashable : Any]?) {
// 设备的 dps 状态发生变化,刷新界面 UI
}
func deviceInfoUpdate(_ device: TuyaSmartDevice?) {
//当前设备信息更新 比如 设备名称修改、设备在线离线状态等
}
func deviceRemoved(_ device: TuyaSmartDevice?) {
//当前设备被移除
}
func device(_ device: TuyaSmartDevice?, signal: String?) {
// Wifi信号强度
}
func device(_ device: TuyaSmartDevice?, firmwareUpgradeProgress type: Int, progress: Double) {
// 固件升级进度
}
func device(_ device: TuyaSmartDevice?, firmwareUpgradeStatusModel upgradeStatusModel: TuyaSmartFirmwareUpgradeStatusModel?) {
// 设备升级状态的回调
}
接口说明
查询单个 dp 数据。
该接口并非同步接口,查询后的数据会通过代理 - (void)device:(TuyaSmartDevice *)device dpsUpdate:(NSDictionary *)dps
回调。
示例代码
Objc:
- (void)queryDP {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
// 查询 dp = "1" 的数据
[self.device publishDps:@{@"1":null} mode:TYDevicePublishModeAuto success:^{
NSLog(@"query dp success");
} failure:^(NSError *error) {
NSLog(@"query dp failure: %@", error);
}];
}
Swift:
func queryDP() {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
// 查询 dp = "1" 的数据
device.publishDps([
"1": null
], mode: TYDevicePublishModeAuto, success: {
print("query dp success")
}, failure: { error in
if let error = error {
print("query dp failure: \(error)")
}
})
}
[warning] 注意事项
该接口主要是针对那些数据不主动去上报的 dp 点,例如倒计时信息查询。 常规查询 dp 数据值可以通过 TuyaSmartDeviceModel.dps 里面获取。
接口说明
- (void)updateName:(NSString *)name success:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
参数说明
参数 | 说明 |
---|---|
name | 设备名称 |
success | 成功回调 |
failure | 失败回调 |
示例代码
Objc:
- (void)modifyDeviceName:(NSString *)mame {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
[self.device updateName:name success:^{
NSLog(@"updateName success");
} failure:^(NSError *error) {
NSLog(@"updateName failure: %@", error);
}];
}
Swift:
func modifyDeviceName(_ name: String) {
device?.updateName(name, success: {
print("updateName success")
}, failure: { (error) in
if let e = error {
print("updateName failure: \(e)")
}
})
}
设备被移除后,会重新进入待配网状态(快连模式)
接口说明
- (void)remove:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
参数说明
参数 | 说明 |
---|---|
success | 成功回调 |
failure | 失败回调 |
示例代码
Objc:
- (void)removeDevice {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
[self.device remove:^{
NSLog(@"remove success");
} failure:^(NSError *error) {
NSLog(@"remove failure: %@", error);
}];
}
Swift:
func removeDevice() {
device?.remove({
print("remove success")
}, failure: { (error) in
if let e = error {
print("remove failure: \(e)")
}
})
}
设备恢复出厂设置后,会重新进入待配网状态(快连模式),设备的相关数据会被清除掉
接口说明
- (void)resetFactory:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
参数说明
参数 | 说明 |
---|---|
success | 成功回调 |
failure | 失败回调 |
示例代码
Objc:
- (void)removeDevice {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
[self.device resetFactory:^{
NSLog(@"reset success");
} failure:^(NSError *error) {
NSLog(@"reset failure: %@", error);
}];
}
Swift:
func removeDevice() {
device?.resetFactory({
print("reset success")
}, failure: { (error) in
if let e = error {
print("reset failure: \(e)")
}
})
}
接口说明
调用获取设备 Wi-Fi 信号之后,会通过 TuyaSmartDeviceDelegate
的 device:signal:
方法进行回调
- (void)getWifiSignalStrengthWithSuccess:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
参数说明
参数 | 说明 |
---|---|
success | 发送获取 Wi-Fi 强度成功回调 |
failure | 失败回调 |
示例代码
Objc:
- (void)getWifiSignalStrength {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
// self.device.delegate = self;
[self.device getWifiSignalStrengthWithSuccess:^{
NSLog(@"get wifi signal strength success");
} failure:^(NSError *error) {
NSLog(@"get wifi signal strength failure: %@", error);
}];
}
#pragma mark - TuyaSmartDeviceDelegate
- (void)device:(TuyaSmartDevice *)device signal:(NSString *)signal {
NSLog(@" signal : %@", signal);
}
Swift:
func getWifiSignalStrength() {
self.device?.getWifiSignalStrength(success: {
print("get wifi signal strength success")
}, failure: { (error) in
if let e = error {
print("get wifi signal strength failure: \(e)")
}
})
}
// MARK: - TuyaSmartDeviceDelegate
func device(_ device: TuyaSmartDevice!, signal: String!) {
}
如果是网关设备,可以获取网关下子设备的列表
接口说明
- (void)getSubDeviceListFromCloudWithSuccess:(nullable void (^)(NSArray <TuyaSmartDeviceModel *> *subDeviceList))success failure:(nullable TYFailureError)failure;
参数说明
参数 | 说明 |
---|---|
success | 成功回调,网关下的子设备信息 |
failure | 失败回调 |
示例代码
Objc:
- (void)getSubDeviceList {
// self.device = [TuyaSmartDevice deviceWithDeviceId:@"your_device_id"];
[self.device getSubDeviceListFromCloudWithSuccess:^(NSArray<TuyaSmartDeviceModel *> *subDeviceList) {
NSLog(@"get sub device list success");
} failure:^(NSError *error) {
NSLog(@"get sub device list failure: %@", error);
}];
}
Swift:
func getSubDeviceList() {
device?.getSubDeviceListFromCloud(success: { (subDeviceList) in
print("get sub device list success")
}, failure: { (error) in
if let e = error {
print("get sub device list failure: \(e)")
}
})
}
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈