门锁管理

更新时间:2024-03-14 07:01:23下载pdf

本文介绍如何管理门锁。

添加门锁设备

接口说明

- (void)activeBLELock:(ThingBLEAdvModel *)deviceInfo
               siteId:(long long)siteId
              success:(nullable void(^)(ThingSmartDeviceModel *deviceModel))success
              failure:(nullable ThingFailureHandler)failure;

参数说明

参数 说明
deviceInfo 搜索到的设备数据模型
siteId 站点 ID
success 成功回调
failure 失败回调

示例代码

设置代理,并且开始搜索设备。

    [ThingSmartBLEManager sharedInstance].delegate = self;
    [ThingSmartBLEManager.sharedInstance startListening:YES];

在回调中获取设备信息,并展示。

- (void)didDiscoveryDeviceWithDeviceInfo:(ThingBLEAdvModel *)deviceInfo {
    NSUInteger index = [self.dataArray indexOfObjectPassingTest:^BOOL(ThingBLEAdvModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        return [obj.uuid isEqualToString:deviceInfo.uuid];
    }];
    if (index != NSNotFound) {
        [self.dataArray replaceObjectAtIndex:index withObject:deviceInfo];
    } else {
        [self.dataArray addObject:deviceInfo];
    }
    [self.tableView reloadData];
}

对搜索到的设备进行配网。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ThingBLEAdvModel *deviceInfo = self.dataArray[indexPath.row];
    long long siteId = SiteManager.shared.siteId;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [ThingBLELockActivator.shared activeBLELock:deviceInfo
                                         siteId:siteId
                                        success:^(ThingSmartDeviceModel * _Nonnull deviceModel) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    } failure:^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

连接门锁

接口说明

- (void)startListening:(BOOL)clearCache;

参数说明

参数 说明
clearCache 是否清空缓存

示例代码

判断设备是否在线。如果不在线,则尝试连接门锁。


    ThingSmartDeviceModel *deviceModel = [ThingSmartDevice deviceWithDeviceId:self.devId].deviceModel;
    if (!deviceModel.isOnline) {
        [ThingSmartBLEManager.sharedInstance startListening:YES];
    } else {
        self.isOnline.text = @"Online";
    }

订阅设备状态更新通知。

    [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";
}

获取已添加的门锁列表

接口说明

+ (void)getLockDeviceListWithSiteId:(long long)siteId
                           category:(NSString *)category
                           pageSize:(NSInteger)pageSize
                            startId:(NSInteger)startId
                            success:(ThingLockDeviceListSuccess)success
                            failure:(ThingFailureError)failure;

参数说明

参数 说明
siteId 站点 ID
pageSize 每页返回的数量
startId 每页数据起始的 ID
success 成功回调
failure 失败回调

示例代码

保存上一页返回的 lastIndex 数据,作为下一页的入参。

    [ThingResidenceSiteManager getLockDeviceListWithSiteId:siteId
                                                  category:@"lock"
                                                  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) {

    }];

打开门锁

接口说明

设备需要在线,否则开锁会失败。

- (void)unLockWithSiteId:(long long)siteId
                deviceId:(NSString *)deviceId
                 success:(ThingSuccessHandler)success
                 failure:(ThingFailureError)failure;

参数说明

参数 说明
siteId 站点 ID
deviceId 设备 ID
success 成功回调
failure 失败回调

示例代码

关于如何连接设备,查看上述 连接门锁

    [ThingLockDevice.shared unLockWithSiteId:SiteManager.shared.siteId
                                    deviceId:self.devId
                                     success:^{
        
    } failure:^(NSError *error) {
        
    }];

修改门锁名称

接口说明

- (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)resetFactory:(nullable ThingSuccessHandler)success failure:(nullable ThingFailureError)failure;

参数说明

参数 说明
success 成功回调
failure 失败回调

示例代码

        ThingSmartDevice *device = [ThingSmartDevice deviceWithDeviceId:self.devId];
        [device resetFactory:^{

        } failure:^(NSError *error) {

        }];