Lock Management

Last Updated on : 2024-03-21 04:26:49download

This topic describes how to manage a lock.

Add a lock

API description

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

Parameter description

Parameter Description
deviceInfo The data model of the discovered device.
siteId The site ID.
success The success callback.
failure The failure callback.

Example

Set the delegate and start searching for devices.

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

Get device information in the callback and show it on the app.

- (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];
}

Pair the discovered device.

- (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];
    }];
}

Connect to a lock

API description

- (void)startListening:(BOOL)clearCache;

Parameter description

Parameter Description
clearCache Specifies whether to clear the cache.

Example

Determine whether the lock is online. If the lock is offline, attempt to connect to it.


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

Subscribe to notifications of device status updates.

    [NSNotificationCenter.defaultCenter addObserver:self 
                                           selector:@selector(updateDeviceState:)
                                               name:@"kNotificationDeviceOnlineUpdate"
                                             object:nil];

Update the device online status in the callback.

- (void)updateDeviceState:(NSNotification *)not {
    ThingSmartDeviceModel *device = [ThingSmartDevice deviceWithDeviceId:self.devId].deviceModel;
    self.isOnline.text = device.isOnline ? @"Online" : @"Offline";
}

Get the list of added locks

API description

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

Parameter description

Parameter Description
siteId The site ID.
pageSize The number of items returned per page.
startId The ID of the first item on each page.
success The success callback.
failure The failure callback.

Example

Save the lastIndex from the result of the previous page and use it as the request parameter for the next page.

    [ThingResidenceSiteManager getLockDeviceListWithSiteId:siteId
                                                  category:@"lock"
                                                  pageSize:20
                                                   startId:self.lastIndex
                                                   success:^(NSArray<NSString *> * _Nullable deviceIdList, NSInteger lastIndex) {
        self.lastIndex = lastIndex;
    } failure:^(NSError *error) {

    }];

Get lock details

API description

This method returns the additional fields for the lock. You can get the basic information about the lock through [ThingSmartDevice deviceWithDeviceId:self.devId].deviceModel.

+ (void)getLockDetailWithSiteId:(long long)siteId
                        deviceId:(NSString *)deviceId
                         success:(ThingLockDetailSuccess)success
                         failure:(ThingFailureError)failure;

Parameter description

Parameter Description
siteId The site ID.
deviceId The device ID.
success The success callback.
failure The failure callback.

Example

    [ThingLockDevice getLockDetailWithSiteId:siteId
                                    deviceId:self.devId
                                     success:^(ThingLockDeviceModel * _Nullable lockModel) {

    } failure:^(NSError *error) {

    }];

Unlock the door

API description

The device must be online. Otherwise, unlocking will fail.

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

Parameter description

Parameter Description
siteId The site ID.
deviceId The device ID.
success The success callback.
failure The failure callback.

Example

See Connect to a lock for device connection.

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

Rename a lock

API description

- (void)updateName:(NSString *)name
           success:(nullable ThingSuccessHandler)success
           failure:(nullable ThingFailureError)failure;

Parameter description

Parameter Description
name The name of the device.
success The success callback.
failure The failure callback.

Example

    ThingSmartDevice *device = [ThingSmartDevice deviceWithDeviceId:self.devId];
    [device updateName:@""
               success:^{

    } failure:^(NSError *error) {

    }];

Delete a lock

API description

- (void)resetFactory:(nullable ThingSuccessHandler)success failure:(nullable ThingFailureError)failure;

Parameter description

Parameter Description
success The success callback.
failure The failure callback.

Example

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

        } failure:^(NSError *error) {

        }];