VendorModel的使用

2020-06-11  本文已影响0人  zaq1125

基于siliconLabs的ADK开发,iOS中关于VendorModel的使用。

1、在初始化BluetoothMesh的时候,配置MeshConfiguration,并初始化。
Your_Company_ID和Your_AssignedModel_ID要和固件的一致

//
let localVendorModel = SBMLocalVendorModel.init(vendorCompanyIdentifier: Your_Company_ID, vendorAssignedModelIdentifier: Your_AssignedModel_ID)
let conf = SBMBluetoothMeshConfiguration(localVendorModels: [localVendorModel], limits: limits, andLogger: SBMLogger.sharedInstance())
bluetoothMesh.initialize(conf)

2、绑定设备的时候,绑定VendorModel

func bindWithAdditionalVendorModels(node: SBMNode, to group: SBMGroup, completion: NodeFunctionalityCallback? = nil) {
        var errors = [Error?]()
        let dispatchGroup = DispatchGroup()
        
        guard let element = node.elements.first else { return }
        let vendorModels = element.vendorModels
        vendorModels.forEach {
            dispatchGroup.enter()
            bind(vendorModel: $0, to: group) {
                errors += $0
                dispatchGroup.leave()
            }
        }
        
        dispatchGroup.notify(queue: .main) {
            completion?(errors)
        }
    }

//绑定VendorModel
    func bind(vendorModel: SBMVendorModel, to group: SBMGroup, completion: NodeFunctionalityCallback? = nil) {
        let functionalityBinder = SBMFunctionalityBinder(group: group)
        let subscriptionControl = SBMSubscriptionControl(vendorModel: vendorModel)
        let subscriptionSettings = SBMSubscriptionSettings(group: group)
        let publicationSettings = SBMPublicationSettings(group: group)
        publicationSettings.ttl = 5
        let dispatchGroup = DispatchGroup()
        
        var errors = [Error?]()
        
        dispatchGroup.enter()
        functionalityBinder.bindModel(vendorModel, successCallback: { _, _ in dispatchGroup.leave() }, errorCallback: { _, _, error in
            errors.append(error)
            dispatchGroup.leave()
        })
        
        //绑定订阅
        dispatchGroup.enter()
        Logger.log(message: "Add group: \(group.name) subscription settings to model: \(vendorModel.vendorCompanyIdentifier())")
        subscriptionControl.add(subscriptionSettings, successCallback: { _, _ in dispatchGroup.leave() }, errorCallback: { _, _, error in
            errors.append(error)
            dispatchGroup.leave()
        })
        
        //绑定发布
        dispatchGroup.enter()
        Logger.log(message: "Set group: \(group.name) publication settings to model: \(vendorModel.vendorCompanyIdentifier())")
        subscriptionControl.setPublicationSettings(publicationSettings, successCallback: { _, _ in dispatchGroup.leave() }, errorCallback: { _, _, error in
            errors.append(error)
            dispatchGroup.leave()
        })
        
        dispatchGroup.notify(queue: .main) {
            Logger.log(message: "Bind model: \(vendorModel.vendorCompanyIdentifier()) to group: \(group.name) finished with errors: \(errors.compactMap { $0?.localizedDescription })")
            completion?(errors)
        }
    }

3、绑定注册LocalVendorModel

let localVendorModel = SBMLocalVendorModel.init(vendorCompanyIdentifier: Your_Company_ID, vendorAssignedModelIdentifier: Your_AssignedModel_ID)
let cryptoBinder = SBMLocalVendorCryptoBinder(applicationKey: group.appKey)
cryptoBinder.bindApplicationKey(to: localVendorModel)

//固件端opcode是1,app这边则是0xC1,参考(Mesh Profile 3.7.3.1 Operation codes)
let opcodes:[UInt8] = [UInt8(0) | 0xC1]
localVendorModel.registrationSettings()
let registrator = SBMLocalVendorRegistrator(model: localVendorModel)
let resultErr = registrator.register(SBMLocalVendorSettings(opCodes: Data(bytes: opcodes), messageHandler: { (localModel, appKeyIndex, sourceAddress, destinationAddr, virtualAddr, msg, msgFlag) in
                print(msg)
            }))
let reuslt = registrator.isRegistered()

4、app通过VendorModel给设备发送消息

let companyID = vendorModel.vendorCompanyIdentifier()
let str = "Hello world"
let msgToSend = str.data(using: String.Encoding.utf8, allowLossyConversion: false)
            
var data = Data(bytes: [UInt8(0) | 0xC1])
data.append(Data(bytes: [UInt8(companyID & 0x00ff), UInt8(companyID >> 8 & 0x00ff)]))
data.append(msgToSend!)
let setVendorModel = YKVendorModelStatus(localClient: localVendorModel, vModel: vendorModel, msgData: data, vflags: .reply)
            
setVendorModel.sendMsgToNode(element: element, in: group!) { (element, setVendor, error) -> (Void) in
  if error != nil {
    print(error)
    }
}

附YKVendorModelStatus代码(遵守SBMControlValueSetVendorModel协议)

import UIKit
import BluetoothMesh

class YKVendorModelStatus: NSObject,SBMControlValueSetVendorModel {
    
    typealias SetVendorModelCallBack = (_ element:SBMControlElement,_ setVendorModel:SBMControlValueSetVendorModel,_ error: Error?) -> (Void)
    
    var localVendorModelClient: SBMLocalVendorModel?
    
    var vendorModel: SBMVendorModel
    
    var data: Data
    
    var flags: SBMControlValueSetVendorModelFlag
    
    init(localClient: SBMLocalVendorModel,
         vModel:SBMVendorModel,
         msgData:Data,
         vflags:SBMControlValueSetVendorModelFlag) {
        localVendorModelClient = localClient
        vendorModel = vModel
        data = msgData
        flags = vflags
    }
    
    func sendMsgToNode(element:SBMElement,in group:SBMGroup,completion: SetVendorModelCallBack? = nil) {
        let controlElement = SBMControlElement(element: element, in: group)
        controlElement.setStatus(self, successCallback: { (controlElement, setVendorModel) in
            print("setVendorModel successs")
            completion?(controlElement, setVendorModel, nil)
        }) { (controlElement, setVendorModel, error) in
            print("setVendorModel error \(error)")
            completion?(controlElement, setVendorModel, error)
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读