更新时间:2024-11-22 02:19:31下载pdf
本文介绍如何管理门锁。
接口说明
- (void)startListening:(BOOL)clearCache;
参数说明
参数 | 说明 |
---|---|
clearCache | 是否清空缓存 |
示例代码
此方法会扫描周围已经被配上网的设备,并且进行连接。
- (void)scanAndConnectAllDevices {
NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(ThingSmartDeviceModel * _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return evaluatedObject.deviceType == ThingSmartDeviceModelTypeBle;
}];
NSMutableArray *deviceList = [NSMutableArray array];
for (NSString *devId in self.dataArray) {
ThingSmartDeviceModel *deviceModel = [ThingSmartDevice deviceWithDeviceId:devId].deviceModel;
[deviceList addObject:deviceModel];
}
BOOL needScan = [deviceList filteredArrayUsingPredicate:pre].count;
if (needScan) {
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
}
订阅设备状态更新通知。
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(updateDeviceState:)
name:@"kNotificationDeviceOnlineUpdate"
object:nil];
在回调中,更新设备在线状态。
- (void)updateDeviceState:(NSNotification *)not {
ThingSmartDeviceModel *device = [ThingSmartDevice deviceWithDeviceId:self.devId].deviceModel;
self.isOnline.text = device.isOnline ? @"Online" : @"Offline";
}
或是单独连接某个蓝牙门锁。
ThingDeviceConnectParams *params = [[ThingDeviceConnectParams alloc] init];
params.sourceType = ThingDeviceConnectSourceTypeNormal;
params.connectType = ThingDeviceConnectTypeNormal;
params.connectTimeoutMills = 10000;
[device connectDeviceWithParams:params success:^{
} failure:^(NSError *error) {
NSLog(@"%@", error);
}];
同样地,如果设备的状态发生变化,会通过 kNotificationDeviceOnlineUpdate
进行通知。
/// Whether the lock has been unlocked
@property (nonatomic, assign, readonly) BOOL isUnlocked;
/// Whether the lock is automatic lock
@property (nonatomic, assign, readonly) BOOL isAutomaticLock;
/// Whether automatic locking is supported
@property (nonatomic, assign, readonly) BOOL isSupportAutomaticLock;
/// Whether manual locking is supported
@property (nonatomic, assign, readonly) BOOL isSupportManualLock;
/// Double locking state
@property (nonatomic, assign, readonly) ThingLockReverseLockState reverseLockState;
/// Door state
@property (nonatomic, assign, readonly) ThingLockDoorState doorState;
isUnlocked
:门锁是否处于打开状态isAutomaticLock
:门锁是否打开了自动关锁isSupportAutomaticLock
:门锁是否支持设置自动关锁isSupportManualLock
:门锁是否可以手动关锁reverseLockState
:门锁的反锁状态doorState
:门的状态,例如当锁打开后但是没有推门,那么门还是处于关闭状态接口说明
+ (void)getLockDeviceListWithSiteId:(long long)siteId
pageSize:(NSInteger)pageSize
startId:(NSInteger)startId
success:(ThingLockDeviceListSuccess)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
siteId | 站点 ID |
pageSize | 每页返回的数量 |
startId | 门锁列表分页游标。第一次传 0 ,后续分页查询上一次回调中的 lastId 值 |
success | 成功回调 |
failure | 失败回调 |
示例代码
保存上一页返回的 lastIndex
数据,作为下一页的入参。
[ThingResidenceSiteManager getLockDeviceListWithSiteId:siteId
pageSize:20
startId:self.lastIndex
success:^(NSArray<NSString *> * _Nullable deviceIdList, NSInteger lastIndex) {
self.lastIndex = lastIndex;
} failure:^(NSError *error) {
}];
接口说明
此接口返回的是门锁设备的附加数据字段,基本设备数据字段通过 [ThingSmartDevice deviceWithDeviceId:self.devId].deviceModel
获取。
+ (void)getLockDetailWithSiteId:(long long)siteId
deviceId:(NSString *)deviceId
success:(ThingLockDetailSuccess)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
siteId | 站点 ID |
deviceId | 设备 ID |
success | 成功回调 |
failure | 失败回调 |
示例代码
[ThingLockDevice getLockDetailWithSiteId:siteId
deviceId:self.devId
success:^(ThingLockDeviceModel * _Nullable lockModel) {
} failure:^(NSError *error) {
}];
ThingLockDeviceModel 字段说明
字段 | 说明 |
---|---|
deviceId | 当前门锁设备 ID |
deviceName | 当前门锁设备名称 |
gatewayId | 当前门锁的网关设备 ID,没有网关时为空 |
gatewayName | 当前门锁的网关设备名称,没有网关时为空 |
deviceType | 门锁设备类型
|
electricQuantity | 门锁电量值 |
deviceRole | 门锁在当前账号下的设备角色
|
supportAbilities | 当前设备支持的能力集合,有以下枚举能力:
|
livecycleType | 设备生命周期
|
timeScheduleInfo | 设备时间设置对象 |
eKeyId | 设备作为电子钥匙时的 ID |
isSupportOta | 设备是否支持 OTA 升级 |
timeZoneId | 设备所在时区 |
account | 设备所属账号 |
接口说明
设备需要在线,否则无法开锁。
- (void)unLockWithSiteId:(long long)siteId
deviceId:(NSString *)deviceId
success:(ThingSuccessHandler)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
siteId | 站点 ID |
deviceId | 设备 ID |
success | 成功回调 |
failure | 失败回调 |
示例代码
关于如何连接设备,查看上述 连接门锁。
[ThingLockManager.shared unLockWithSiteId:SiteManager.shared.siteId
deviceId:self.devId
success:^{
} failure:^(NSError *error) {
}];
接口说明
设备需要在线,否则无法关锁。
- (void)lockWithSiteId:(long long)siteId
deviceId:(NSString *)deviceId
success:(ThingSuccessHandler)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
siteId | 站点 ID |
deviceId | 设备 ID |
success | 成功回调 |
failure | 失败回调 |
示例代码
关于如何连接设备,查看上述 连接门锁。
[ThingLockManager.shared lockWithSiteId:SiteManager.shared.siteId
deviceId:self.devId
success:^{
} failure:^(NSError *error) {
}];
lock
方法前,建议使用 isSupportManualLock
进行判断,YES
表示可以进行手动关锁。接口说明
- (void)updateName:(NSString *)name
success:(nullable ThingSuccessHandler)success
failure:(nullable ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
name | 设备名称 |
success | 成功回调 |
failure | 失败回调 |
示例代码
ThingSmartDevice *device = [ThingSmartDevice deviceWithDeviceId:self.devId];
[device updateName:@""
success:^{
} failure:^(NSError *error) {
}];
接口说明
+ (void)deleteWithDeviceId:(NSString *)devId
success:(ThingSuccessHandler)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
devId | 设备 ID |
success | 成功回调 |
failure | 失败回调 |
示例代码
[ThingLockDevice deleteWithDeviceId:self.devId success:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.navigationController popViewControllerAnimated:YES];
[NSNotificationCenter.defaultCenter postNotificationName:@"UpateDeviceList" object:nil];
} failure:^(NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[Alert showBasicAlertOnVC:self withTitle:@(error.code).errorText message:@""];
}];
与删除门锁的区别在于,强制删除门锁不会判断门锁是否在线。
接口说明
+ (void)forceDeleteWithDeviceId:(NSString *)devId
success:(ThingSuccessHandler)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
devId | 设备 ID |
success | 成功回调 |
failure | 失败回调 |
示例代码
[ThingLockDevice forceDeleteWithDeviceId:self.devId success:^{
} failure:^(NSError *error) {
}];
接口说明
- (void)setAutoLockSwitchWithDeviceId:(NSString *)deviceId
success:(ThingSuccessHandler)success
failure:(ThingFailureError)failure;
参数说明
参数 | 说明 |
---|---|
devId | 设备 ID |
success | 成功回调 |
failure | 失败回调 |
示例代码
[ThingLockManager.shared setAutoLockSwitchWithDeviceId:self.devId success:^{
} failure:^(NSError *error) {
}];
接口说明
@property (nonatomic, assign, readonly) BOOL isSupportAutomaticLock;
参数说明
参数 | 说明 |
---|---|
返回值 | 是否支持自动落锁
|
示例代码
self.automaticLockCell.userInteractionEnabled = deviceModel.isSupportAutomaticLock
设置代理,来监听设备 DPs 变化。
self.lockDevice = [ThingSmartDevice deviceWithDeviceId:self.devId];
self.lockDevice.delegate = self;
在回调中处理数据,例如门锁的开关,反锁状态的变更等。
- (void)device:(ThingSmartDevice *)device dpsUpdate:(NSDictionary *)dps {
NSLog(@"%s dps ==> %@", __func__, dps);
[self updateDeviceStatus];
}
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈