Sig Mesh 之解析 scheduler数据

后期计划把私有MeshSig Mesh都写一个系列的文章出来,毕竟在这一块也从事了一段时间了。

最近在鼓捣Sig Mesh的东西,这个是好处就是协议统一(终于不用每家每家都不一样了),但是这个相较于Mesh复杂了很多,尤其是数据以及概念这块,对于定时器这块的解析也确实头疼不已,这里做一个记录。

官方协议对照

Sig Mesh 之解析 scheduler数据_第1张图片
官方协议

解析方法

extension UInt8 {
    /// 0xF0 -> [1,1,1,1,0,0,0,0]
    var bits: [Int] {
        var string = String(self, radix: 2)
        if string.count < 8 {
            let inserCount = 8 - string.count
            for _ in 0.. UInt16 in
            let (offset, element) = arg
            let value = Int(powf(Float(2), Float(offset)))
//            print("offset: ",offset, "value: ", value, "element: ", element)
            return result + UInt16(value * element)
        }
    }
    
}

struct ParseScheduler {
    let index: UInt16
    let year: UInt16
    let month: UInt16
    let day: UInt16
    let hour: UInt16
    let minute: UInt16
    let second: UInt16
    let dayOfWeek: UInt16
    let action: UInt16
    let transitionTime: UInt16
    let sceneNumber: UInt16
    
    init?(data: Data) {
        guard data.count == 10 else { return nil }
        let list = data.flatMap { $0.bits }
        self.index = list[4...7].toUInt16
        self.year = (list[13...15] + list[0...3]).toUInt16
        self.month = (list[17...19] + list[20...23] + list[8...12]).toUInt16
        self.day = (list[28...31] + list[16...16]).toUInt16
        self.hour = (list[39...39] + list[24...27]).toUInt16
        self.minute = (list[33...38]).toUInt16
        self.second = (list[43...47] + list[32...32]).toUInt16
        self.dayOfWeek = (list[52...55] + list[40...42]).toUInt16
        self.action = (list[48...51]).toUInt16
        self.transitionTime = (list[56...63]).toUInt16
        self.sceneNumber = (list[72...79] + list[64...71]).toUInt16
    }
}

func parse() {
    let data = Data([0x30, 0x01, 0xa0, 0xf7, 0x3c, 0x4f, 0x22, 0x00, 0x05, 0x00])
    let list = data.flatMap { $0.bits }
    print("哈哈哈哈哈 未转换", list)


    print("scheduler: ", ParseScheduler(data: data)!)
}

你可能感兴趣的:(Sig Mesh 之解析 scheduler数据)