(.syncTrain, "同步基础训练历史数据", [.value(30)]),
(.syncGPS, "同步路径坐标集历史数据", [.value(1654012800)]),
(.sendSyncEnd, "发送同步数据结束消息", []),
- (.addressBook, "发送通讯录", [.addressBook(IBAddressBook.test())]),
+ (.addressBook, "发送通讯录", [.addressBook([IBAddressBook()])]),
// (.revokeFileSend, "撤销当前文件传输", [.file(.mp3)]),
// (.fetchFiles, "获取指定类型所有文件", [.file(.mp3)]),
/// APP获取某表盘ID是否已安装
/// - Parameter value:Int
/// - Returns: Int
- /// 0 = 未安装 \
- /// 1 = 已安装 \
+ /// 1 = 已安装 \
+ /// 2 = 未安装 \
/// Read from ``IBResponse``.value
static let checkDialID = IBAbility.long.id(0x0D).parser(IBCheckDialParser.self).checker(IBSwitchDialChecker.self)
// DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
// IBBluetooth.shared.reconnected()
// }
+// 发送天气数据 设备重启
+// 获取sd卡剩余空间 失败
+// 同步路径坐标集历史数据 无响应
+// 结束运动模式后立即发送其他指令 设备重启
}
public static var logs: [String] { Logger.logs }
struct IBCheckDialParser: IBParser {
static func parse(_ data: Data) -> IBResponse {
- var bytes = data.bytes
- bytes.removeLast()
- let code = bytes.last!.int
+
+ let code = dataPacket(data).int
let desc = ["指令校验失败", "已安装", "未安装"]
return .response.code(code != 0 ? .success:.failure).message(desc[code]).value(code)
}
public typealias IBCompletionHandler = (_ result: IBResponse) -> Void
-//public typealias IBCmdSuccess = (_ result: IBResponse) -> Void
-//public typealias IBCmdError = (_ error: IBError) -> Void
-
/// 蓝牙消息发送类
public class IBSender {
public init() {
var parameters: [IBParameter] = []
var completion: IBCompletionHandler!
-// var cmdSuccess: IBCmdSuccess?
-// var cmdFailure: IBCmdError?
-
- var data: [UInt8] = []
-
- var dataPacket: [UInt8] {
+ var dataPacket: [[UInt8]] {
+ var totalData: [UInt8] = []
if isValue(ability.checker) {
- data = ability.checker.check(parameters)
+ totalData = ability.checker.check(parameters)
}
+
+// totalData = []
+// for i in 0...202{
+// totalData.append(UInt8(i))
+// }
-// let data: [UInt8] = data?.bytes ?? []
- /// 分包数
- let packageCount = 1
- /// 数据包总长度
- let dataTotalLength = data.count
- /// 数据包长度
- let dataLength = data.count
- /// 数据包索引
- let index = 1
-
- var bytes: [UInt8] = [0x73, ability.type.rawValue, ability.id]
- bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2
-
- if data.count > 0 {
- bytes = bytes + data
- bytes = bytes + [LRC.calculateLRC(data)]
- } else {
- bytes = bytes + [0x00]
+ let list = splitData(totalData)
+ if list.count > 1{
+ var newList = [[UInt8]]()
+ /// 分包数
+ let packageCount = list.count
+ /// 数据包总长度
+ let dataTotalLength = totalData.count
+ /// 数据包长度
+ let dataLength = list[0].count
+ /// 数据包索引
+ let index = 1
+
+ var bytes: [UInt8] = [0x73, ability.type.rawValue, ability.id]
+ bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2
+ bytes = bytes + list[0] + [LRC.calculateLRC(list[0])]
+
+ newList.append(bytes)
+ newList.append(contentsOf: list[1...list.count - 1])
+
+ return newList
+ }
+ else{
+ /// 分包数
+ let packageCount = 1
+ /// 数据包总长度
+ let dataTotalLength = totalData.count
+ /// 数据包长度
+ let dataLength = totalData.count
+ /// 数据包索引
+ let index = 1
+
+ var bytes: [UInt8] = [0x73, ability.type.rawValue, ability.id]
+ bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2
+
+ if totalData.count > 0 {
+ bytes = bytes + totalData
+ bytes = bytes + [LRC.calculateLRC(totalData)]
+ } else {
+ bytes = bytes + [0x00]
+ }
+
+ return [bytes]
}
-
- return bytes
}
+ private func splitData(_ listData:[UInt8])->[[UInt8]]{
+ let data = listData.data
+ var list = [[UInt8]]()
+ var index = 0
+ let seg = 492
+ while index < data.count {
+ if index < data.count - seg {
+ let sub:[UInt8] = data[index...index + seg - 1].bytes
+ list.append(sub)
+ index += sub.count
+ }
+ else{
+ let sub:[UInt8] = data[index...data.count - 1].bytes
+ index += sub.count
+ list.append(sub)
+ }
+ }
+ return list
+ }
+
+
func isAnswer(_ data: Data) -> Bool {
return ability.isAnswer(data)
}
IBBluetooth.shared.send(self)
return self
}
-// /// 执行与响应处理
-// @discardableResult
-// func response(success: @escaping IBCmdSuccess,failure:@escaping IBCmdError) -> Self {
-// cmdSuccess = success
-// cmdFailure = failure
-// IBBluetooth.shared.send(self)
-// return self
-// }
}
extension IBSender {
lastSender = sender
startTimer()
- let dataPacket = sender.dataPacket
- Logger.log("\(sender.ability.characteristicUUID!) Write:", dataPacket.data.joined(" "), "Bytes(\(dataPacket.count))")
-
- var writeCharacteristic: CBCharacteristic = writeCharacteristic
- if sender.ability.characteristicUUID == writeBigCharacteristicUUID {
- writeCharacteristic = writeBigCharacteristic
- }
- if sender.ability.characteristicUUID == writeFileCharacteristicUUID {
- writeCharacteristic = writeFileCharacteristic
+ for dataPacket in sender.dataPacket {
+ Logger.log("\(sender.ability.characteristicUUID!) Write:", dataPacket.data.joined(" "), "Bytes(\(dataPacket.count))")
+
+ var writeCharacteristic: CBCharacteristic = writeCharacteristic
+ if sender.ability.characteristicUUID == writeBigCharacteristicUUID {
+ writeCharacteristic = writeBigCharacteristic
+ }
+ if sender.ability.characteristicUUID == writeFileCharacteristicUUID {
+ writeCharacteristic = writeFileCharacteristic
+ }
+ Thread.sleep(forTimeInterval:0.02)
+ currentConnected.peripheral.writeValue(dataPacket.data, for: writeCharacteristic, type: .withResponse)
}
-
- currentConnected.peripheral.writeValue(dataPacket.data, for: writeCharacteristic, type: .withResponse)
}
func sendDeviceInfo(){
return
}
- Logger.log(characteristic.uuid.uuidString, "write success")
+// Logger.log(characteristic.uuid.uuidString, "write success")
}
}
lastSender?.completion?(.response.code(.timeout))
lastSender?.remove()
})
- Logger.log("创建定时器")
+// Logger.log("创建定时器")
}
func deinitTimer() {
Self.timer?.invalidate()
Self.timer = nil
- Logger.log("销毁定时器")
+// Logger.log("销毁定时器")
}
}
public init() { }
- public static func test() -> [IBAddressBook]{
- var list = [IBAddressBook]();
- for i in 0...10{
- let model = IBAddressBook()
- model.gender = i%2
- model.name = "张\(i)"
-
- for j in 0...1{
- let phone = IBPhoneNumber()
- phone.number = "1300000000\(j)"
- phone.location = "深圳市宝安区\(j)"
- phone.operator = "中国移动"
- model.phoneNumbers.append(phone)
- }
- list.append(model)
- }
-
- return list
- }
+// public static func test() -> [IBAddressBook]{
+// var list = [IBAddressBook]();
+// for i in 0...10{
+// let model = IBAddressBook()
+// model.gender = i%2
+// model.name = "张\(i)"
+//
+// for j in 0...1{
+// let phone = IBPhoneNumber()
+// phone.number = "1300000000\(j)"
+// phone.location = "深圳市宝安区\(j)"
+// phone.operator = "中国移动"
+// model.phoneNumbers.append(phone)
+// }
+// list.append(model)
+// }
+//
+// return list
+// }
public var description: String {
var phoneStr = "[ "