闪电搜索配网

更新时间:2024-08-22 02:21:52下载pdf

赋予闪电配网能力的路由器或 Wi-Fi 类设备,可以添加其他 Wi-Fi 类设备。闪电配网有搜索和激活两个流程,搜索是通过已配网设备发现未配网设备,激活是利用已配网设备将网络信息直接发送给未配网设备,然后未配网设备进行连云激活。

ThingSmartBusinessExtensionKit 组件提供了比 ThingSmartPegasusActivator 更多的功能。如果您仍直接在使用 ThingSmartPegasusActivator,请参考 此链接

## 配网流程
App云端关联设备设备在家庭中发现支持闪电配网的设备发送命令并开启闪电配网发送命令并开启闪电配网开启闪电配网搜索设备返回设备信息返回设备信息返回设备信息并展现在 App 上alt[发现设备]选择目标设备发送激活命令发送激活命令发送闪电配网数据包连接到路由器并注册到云端返回激活设备信息alt[激活设备]App云端关联设备设备设备闪电配网流程

查询当前家庭中支持闪电配网的设备

当前家庭下闪电设备的获取,可调用 ThingSmartPegasusActivator 中的以下方法:

/// Returns the devices that support Pegasus.
/// @param homeID The home ID.
/// @return A list of devices that support Pegasus.
+ (NSArray <ThingSmartDeviceModel *> *)pegasusDeviceListWithHomeID:(long long)homeID;
参数 说明
homeId 家庭 ID

注册配网类型

基座初始化时,需要注册一下配网的类型, 闪电模式对应为 ThingSmartActivatorTypePegasusModel

接口说明

/// Initialize network configuration types
/// @param typeList Network configuration types
- (void)registerWithActivatorList:(NSArray<ThingSmartActivatorTypeModel *>*)typeList;

参数说明

参数 说明
typeList 配网类型列表

开始搜索

开始搜索时,需要传入已注册的 typeList

接口说明

/// Start searching
/// @param typeList Network configuration types
- (void)startSearch:(NSArray <ThingSmartActivatorTypeModel *>*)typeList;

参数说明

参数 说明
typeList 配网类型列表

停止搜索

接口说明

/// Stop searching
/// @param typeList Network configuration types
/// @param clearCache Whether to clear the cache
- (void)stopSearch:(NSArray <ThingSmartActivatorTypeModel *>*)typeList clearCache:(BOOL)clearCache;

参数说明

参数 说明
typeList 配网类型列表
clearCache 是否清空当前搜索设备缓存

搜索设备回调

搜到设备之后,会通过搜索回调返回设备信息。如果失败,则会返回对应的失败信息。

接口说明

/// Device search callback
/// @param service Search instance
/// @param type Network configuration type
/// @param device Discovered device
/// @param errorModel Error callback
- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service
            activatorType:(ThingSmartActivatorTypeModel *)type
             didFindDevice:(nullable ThingSmartActivatorDeviceModel *)device
                     error:(nullable ThingSmartActivatorErrorModel *)errorModel;

参数说明

参数 说明
service 配网服务
type 配网类型
device 发现设备,返回此次配网的设备模型,失败时返回 nil
errorModel 如果配网失败或者超时,返回此模型,成功时返回 nil

设备激活

接口说明

/// Activate devices with a single network configuration type
/// @param type Network configuration type
/// @param deviceList Devices to be activated
- (void)startActive:(ThingSmartActivatorTypeModel *)type deviceList:(NSArray<ThingSmartActivatorDeviceModel *>*)deviceList;

参数说明

参数 说明
type 配网类型
deviceList 待激活设备列表,目前仅支持单个设备

停止激活

接口说明

/// Stop activating devices
/// @param typeList Array of network configuration types
/// @param clearCache Whether to clear the cache
- (void)stopActive:(NSArray <ThingSmartActivatorTypeModel *>*)typeList clearCache:(BOOL)clearCache;

参数说明

参数 说明
typeList 配网类型
clearCache 清空缓存设备信息

设备激活回调

接口说明

// Device network configuration result callback
/// @param service Device network configuration implementation object
/// @param type Network configuration type
/// @param devices Devices being configured
/// @param errorModel Error encountered during network configuration
- (void)activatorService:(id<ThingSmartActivatorActiveProtocol>)service
           activatorType:(ThingSmartActivatorTypeModel *)type
       didReceiveDevices:(nullable NSArray<ThingSmartActivatorDeviceModel *> *)devices
                   error:(nullable ThingSmartActivatorErrorModel *)errorModel;

参数说明

参数 说明
service 配网服务
type 配网类型
devices 激活成功设备
errorModel 如果配网失败或者超时,返回此模型,成功时返回 nil

错误码说明

配网失败或者配网超时的情况下,会返回 ThingSmartActivatorErrorModel

@interface ThingSmartActivatorErrorModel : NSObject
@property (nonatomic, strong) ThingSmartActivatorDeviceModel *deviceModel;
@property (nonatomic) NSError *error;
@end

其中 error 对应的错误码,定义在 ThingSmartActivatorDiscoveryError 中,以下是常见错误码:

错误码 配网错误
ThingSmartActivatorDiscoveryErrorTimeout 配网超时
ThingSmartActivatorDiscoveryErrorDeviceAlreadyBound 设备强绑定错误。该设备已经被用户绑定,无法被第二个用户绑定,需要第一个用户解绑才能完成配网操作。
ThingSmartActivatorDiscoveryErrorAPPUnsupportProduct 配网账号的 App 和产品没有绑定关系。
ThingSmartActivatorDiscoveryErrorTokenExpired Token 失效。
ThingSmartActivatorDiscoveryErrorGuestNotSupportStrongBind 游客模式无法对强绑定设备进行配网。
ThingSmartActivatorDiscoveryErrorRemoteApiParamIllegal 接口参数不合法。

示例代码

Swift

class pegasusModeConfigurationVC: UITableViewController {

    private var token: String = ""
    var deviceList:[ThingSmartActivatorDeviceModel] = []

    private var typeModel: ThingSmartActivatorTypePegasusModel = {
        let type = ThingSmartActivatorTypePegasusModel()
        type.type = ThingSmartActivatorType.pegasus
        type.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorType.pegasus)
        type.timeout = 120
        return type
    }()

    lazy var discovery: ThingSmartActivatorDiscovery = {
        let discovery = ThingSmartActivatorDiscovery()
        discovery.register(withActivatorList: [self.typeModel])
        discovery.setupDelegate(self)
        discovery.loadConfig()
        return discovery
    }()

    private func startSearch() {
        guard let homeID = Home.current?.homeId else { return }
        let deviceArray = ThingSmartPegasusActivator.pegasusDeviceList(withHomeID: homeID)
        var devIds: [String] = []

        deviceArray.forEach { (obj) in
            devIds.append(obj.devId)
        }

        typeModel.pegasusServerDevIDs = devIds
        discovery.startSearch([self.typeModel])
    }

}

extension pegasusModeConfigurationVC: ThingSmartActivatorSearchDelegate {
    func activatorService(_ service: ThingSmartActivatorSearchProtocol, activatorType type: ThingSmartActivatorTypeModel, didFindDevice device: ThingSmartActivatorDeviceModel?, error errorModel: ThingSmartActivatorErrorModel?) {

        if let device = device {
            SVProgressHUD.dismiss()
            guard let homeID = Home.current?.homeId else { return }
            ThingSmartActivator.sharedInstance()?.getTokenWithHomeId(homeID, success: { [weak self] (token) in
                guard let self = self else { return }
                typeModel.token = token ?? ""
                typeModel.spaceId = homeID
                discovery.startActive(typeModel, deviceList: [device])
                SVProgressHUD.show(withStatus: NSLocalizedString("Activating", comment: "Active pegasus ."))
            }, failure: { (error) in
                let errorMessage = error?.localizedDescription ?? ""
                SVProgressHUD.showError(withStatus: errorMessage)
            })
        }
    }

    func activatorService(_ service: ThingSmartActivatorSearchProtocol, activatorType type: ThingSmartActivatorTypeModel, didUpdateDevice device: ThingSmartActivatorDeviceModel) {

    }

}

extension pegasusModeConfigurationVC: ThingSmartActivatorActiveDelegate {
    func activatorService(_ service: ThingSmartActivatorActiveProtocol, activatorType type: ThingSmartActivatorTypeModel, didReceiveDevices devices: [ThingSmartActivatorDeviceModel]?, error errorModel: ThingSmartActivatorErrorModel?) {
        if (errorModel != nil) {
            SVProgressHUD.showError(withStatus: NSLocalizedString("Failed to Activate pegasus Device", comment: ""))
            return
        }

        if (devices!.count > 0) {
            let deviceModel = devices?.first
            let name = deviceModel?.name ?? NSLocalizedString("Unknown Name", comment: "Unknown name device.")
            SVProgressHUD.showSuccess(withStatus: NSLocalizedString("Successfully Added \(name)", comment: "Successfully added one device."))
        }
    }

}

Objective-C


- (void)starSearch:(NSString *)token {
  ThingSmartActivatorTypePegasusModel *pegasusModel  = [[ThingSmartActivatorTypePegasusModel alloc] init];
  pegasusModel.type = ThingSmartActivatorTypePegasus;
  pegasusModel.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorTypePegasus);
  pegasusModel.timeout = 120;
  pegasusModel.spaceId = homeId;

  NSArray *array = [ThingSmartPegasusActivator pegasusDeviceListWithHomeID:homeId];
  NSMutableArray *devIds = [NSMutableArray array];
  [array enumerateObjectsUsingBlock:^(ThingSmartDeviceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
      [devIds addObject:obj.devId];
  }];
  pegasusModel.pegasusServerDevIDs = devIds;

  [self.discovery registerWithActivatorList:@[pegasusModel]];
  [self.discovery setupDelegate:self];
  typeModel.pegasusServerDevIDs = [ThingSmartActivatorLinkTool pegasusDeviceIdList];
  [self.discovery startSearch:@[pegasusModel]];
}

- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service activatorType:(ThingSmartActivatorTypeModel *)type didFindDevice:(ThingSmartActivatorDeviceModel *)device error:(ThingSmartActivatorErrorModel *)errorModel {

    if (errorModel) {
        [self _connectWifiError:errorModel];
        return;
    }

    if (device) {
        [self _handleDevice:device];
    }
}

- (void)_handleDevice:(ThingSmartActivatorDeviceModel *)deviceModel {
     ThingSmartActivatorTypePegasusModel *pegasusModel = (ThingSmartActivatorTypePegasusModel *)[activatorGateway activatorTypeModelWith:ThingSmartActivatorTypePegasus];

     ThingSmartActivator *activator = [[ThingSmartActivator alloc] init];
    [activator getTokenWithHomeId:homeId success:^(NSString *token) {
        NSLog(@"getToken success: %@", token);
        pegasusModel.token = token;
        [self.discovery startActive:pegasusModel deviceList:@[deviceModel]];
    } failure:^(NSError *error) {
        NSLog(@"getToken failure: %@", error.localizedDescription);
    }];


}

- (void)activatorService:(id<ThingSmartActivatorActiveProtocol>)service
           activatorType:(ThingSmartActivatorTypeModel *)type
       didReceiveDevices:(nullable NSArray<ThingSmartActivatorDeviceModel *> *)devices
                   error:(nullable ThingSmartActivatorErrorModel *)errorModel {
     if (devices && devices.count > 0) {

     }
}

- (ThingSmartActivatorDiscovery *)discovery {
    if (!_discovery) {
        _discovery = [[ThingSmartActivatorDiscovery alloc] init];
    }
    return _discovery;
}