TuyaLink 设备配网

更新时间:2024-08-22 02:20:38下载pdf

TuyaLink 生态设备接入是面向物联网生态领域(自研模组/成品智能设备)全面开放的设备上云解决方案。通过此方案可以快速加入涂鸦生态体系,实现跨领域设备间互连互通,并可使用平台丰富的 PaaS、SaaS 和 App 等应用开发能力,最大程度地降低物联网整体解决方案的落地实施成本,减少开发周期。更多详情,请参考 生态设备接入

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

## 配网流程
App业务拓展 SDK设备云端设备进入配网模式扫描设备二维码1解析二维码信息调用二维码解析接口2调用二维码解析接口3返回解析结果4返回解析结果5alt[扫描设备]发送设备激活请求6发送设备激活请求7向云端注册设备8返回激活设备列表9返回激活设备列表10alt[激活配网]App业务拓展 SDK设备云端TuyaLink 设备配网流程

二维码解析接口

扫描设备二维码,获取 URL,然后通过通用接口获取设备信息。通过接口返回的参数,确定该二维码是 TuyaLink 设备或者其他设备。

@protocol ThingSmartActivatorRequestProtocol <NSObject>

/// Provide QR code parsing capability
/// @param param Request Parameters
/// @param success Network success callback
/// @param failure Network Failure Callbacks
- (void)requestParseQRCodeWithParam:(ThingActivatorParseQRCodeRequestData *)param success:(void(^)(ThingSmartAScanCodeModel *result))success failure:(ThingActivatorCallbackError)failure;
@end

参数说明

参数 说明
param 请求参数
success 解析成功返回信息
failure 解析失败回调

返回结果说明:

@interface ThingSmartAScanCodeModel : NSObject
@property (nonatomic, strong) NSString * _Nullable actionName;
@property (nonatomic, assign) id _Nullable actionData;
@end

TuyaLink 设备对应 actionName"device_net_conn_bind_tuyalink",具体使用可参考示例代码。

注册配网类型

基座初始化时,需要注册一下配网的类型,TuyaLink 设备对应为 ThingSmartActivatorTypeThingLinkModel

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

参数说明

参数 说明
typeList 配网类型列表

设备激活

接口说明

/// 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 中。

示例代码

Swift

class TuyaLinkDeviceBindTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "ThingLink Bind"
    }

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

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

    var request: ThingSmartActivatorDiscoveryRequest = {
        let request = ThingSmartActivatorDiscoveryRequest()
        return request
    }()

    func bindThingLink(qrcode codeStr: String?) -> Void {
        let homeId = (Home.current?.homeId)!
        SVProgressHUD.show()
        let requestDate = ThingActivatorParseQRCodeRequestData()
        requestDate.code = codeStr ?? ""
        request.requestParseQRCode(withParam: requestDate) { codeModel in
            if codeModel.actionName == "device_net_conn_bind_tuyalink" {
                self.typeModel.uuid = codeModel.actionData?.object(forKey: "uuid") as! String
                self.typeModel.spaceId = homeId
                self.discovery.startActive(self.typeModel, deviceList: [])
            }
        } failure: { error in

        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = QRCodeScanerViewController()
        vc.scanCallback = { [weak self] codeStr in
            self?.bindThingLink(qrcode: codeStr)
        }
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension TuyaLinkDeviceBindTableViewController: ThingSmartActivatorActiveDelegate {
    func activatorService(_ service: ThingSmartActivatorActiveProtocol, activatorType type: ThingSmartActivatorTypeModel, didReceiveDevices devices: [ThingSmartActivatorDeviceModel]?, error errorModel: ThingSmartActivatorErrorModel?) {
        if errorModel != nil {
            SVProgressHUD.showError(withStatus: "Bind Failure. (\(errorModel?.error.localizedDescription ?? ""))")
            return
        }

        let device = devices?.first
        SVProgressHUD.show(withStatus: "Bind Success. \n devId: \(device?.uniqueID ?? "") \n name: \(device?.name ?? "")")
    }
}

Objective-C

- (void)qrParse:(NSString *)url {
ThingActivatorParseQRCodeRequestData *param = [[ThingActivatorParseQRCodeRequestData alloc] init];
    param.code = url;

[self.requestService requestParseQRCodeWithParam:param success:^(ThingSmartAScanCodeModel * _Nonnull codeModel) {
       if ([result.actionName isEqualToString:@"device_net_conn_bind_tuyalink"]){
        NSDictionary *dict = result.actionData;
        NSString *uuid = [dict objectForKey:@"uuid"];
        ThingSmartActivatorTypeThingLinkModel *type = [[ThingSmartActivatorTypeThingLinkModel alloc] init];
        type.type = ThingSmartActivatorTypeThingLink;
        type.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorTypeThingLink);
        type.timeout = 120;
        type.spaceId = [ThingSmartActivatorLinkTool getSpaceId];
        type.uuid = uuid;
        [self startConfigQRCode:type];
       }
   } failure:^(NSError *error) {

   }];
}

- (void)startConfigQRCode:(ThingSmartActivatorTypeMQTTDirectlyModel *)type {

  [self.discovery registerWithActivatorList:@[type]];
  [self.discovery setupDelegate:self];
  [self.discovery startActive:type deviceList:@[]];

}

- (void)activatorService:(id<ThingSmartActivatorActiveProtocol>)service 
           activatorType:(ThingSmartActivatorTypeModel *)type 
       didReceiveDevices:(NSArray<ThingSmartActivatorDeviceModel *> *)devices 
                   error:(ThingSmartActivatorErrorModel *)errorModel {    
    if (errorModel) {
        [self _connectWifiError:errorModel];
        return;
    }

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

- (void)_handleDevice:(ThingSmartActivatorDeviceModel *)deviceModel {

}

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

- (ThingSmartActivatorDiscoveryRequest *)requestService {
    if (!_requestService) {
        _requestService = [[ThingSmartActivatorDiscoveryRequest alloc] init];
    }
    return _requestService;
}