From: liangzhenying <867174124@qq.com> Date: Thu, 27 Jun 2024 09:26:12 +0000 (+0800) Subject: 新增Ai协议 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=c0471ad1da3615accaf7cba807542de381f5e33a;p=mirrors%2FIOTBluetooth-iOS.git 新增Ai协议 --- diff --git a/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift b/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift index 0389481..2322d08 100644 --- a/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift +++ b/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift @@ -131,13 +131,20 @@ extension AbilityViewController{ //目前只支持表盘市场 func sendMarketDial(){ +//系统表盘 +// ID: 101 | Name: sportdial +// ID: 102 | Name: kaleidoscope +// ID: 1052 | Name: classic +// ID: 103 | Name: jellyfish +// ID: 1028 | Name: KUMI + guard let path = Bundle.main.path(forResource: "galaxy", ofType: "lzma") else {return} let file = IBDialFile() file.filePath = path file.fileName = "galaxy.lzma" let info = IBDial() - info.UID = 10001 + info.UID = 10002 info.files = [file] info.theme = "黑暗" diff --git a/IOTBluetooth/IOTBluetooth/Example/Datas.swift b/IOTBluetooth/IOTBluetooth/Example/Datas.swift index 437c280..14b1189 100644 --- a/IOTBluetooth/IOTBluetooth/Example/Datas.swift +++ b/IOTBluetooth/IOTBluetooth/Example/Datas.swift @@ -75,14 +75,14 @@ let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [ (.water, "设置喝水提醒", [.water(IBWater())]), - (.syncHeartRate, "同步心率历史数据", [.value(30)]), - (.syncBloodOxygen, "同步血氧历史数据", [.value(30)]), - (.syncStepCount, "同步历史步数数据", [.value(30)]), - (.syncFatBurning, "同步燃脂时间历史数据", [.value(30)]), - (.syncStand, "同步站立历史数据", [.value(30)]), - (.syncCalorie, "同步卡路里历史数据", [.value(30)]), - (.syncDistance, "同步距离历史数据", [.value(30)]), - (.syncTrain, "同步基础训练历史数据", [.value(30)]), + (.syncHeartRate, "同步心率历史数据", [.value(0)]), + (.syncBloodOxygen, "同步血氧历史数据", [.value(0)]), + (.syncStepCount, "同步历史步数数据", [.value(0)]), + (.syncFatBurning, "同步燃脂时间历史数据", [.value(0)]), + (.syncStand, "同步站立历史数据", [.value(0)]), + (.syncCalorie, "同步卡路里历史数据", [.value(0)]), + (.syncDistance, "同步距离历史数据", [.value(0)]), + (.syncTrain, "同步基础训练历史数据", [.value(0)]), (.syncGPS, "同步路径坐标集历史数据", [.value(1654012800)]), (.sendSyncEnd, "发送同步数据结束消息", []), (.addressBook, "发送通讯录", [.addressBook([IBAddressBook()])]), @@ -94,6 +94,7 @@ let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [ (.dial, "发送市场表盘文件", []), (.revokeFileSend, "撤销当前文件传输", []), + (.deleteFile, "删除指定文件", [.deleteFile(IBDeleteFile())]), (.deleteFiles, "删除指定类型所有文件", [.file(.mp3)]), (.fetchFiles, "获取指定类型所有文件", [.file(.mp3)]), diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.pbxproj b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.pbxproj index 85d1a71..db61b17 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.pbxproj +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 180220CE2C2D5D560090D34A /* IBAiResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 180220CD2C2D5D560090D34A /* IBAiResponse.swift */; }; 180B01982BFC831300524729 /* IBHistoryHeartRate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 180B01972BFC831300524729 /* IBHistoryHeartRate.swift */; }; 180B019E2BFC9CF400524729 /* IBHistoryBaseTraining.swift in Sources */ = {isa = PBXBuildFile; fileRef = 180B019D2BFC9CF400524729 /* IBHistoryBaseTraining.swift */; }; 180CE2652C0D728F00A7006E /* IBAddressBook.swift in Sources */ = {isa = PBXBuildFile; fileRef = 180CE2642C0D728F00A7006E /* IBAddressBook.swift */; }; @@ -114,6 +115,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 180220CD2C2D5D560090D34A /* IBAiResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBAiResponse.swift; sourceTree = ""; }; 180B01972BFC831300524729 /* IBHistoryHeartRate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBHistoryHeartRate.swift; sourceTree = ""; }; 180B019D2BFC9CF400524729 /* IBHistoryBaseTraining.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBHistoryBaseTraining.swift; sourceTree = ""; }; 180CE2642C0D728F00A7006E /* IBAddressBook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBAddressBook.swift; sourceTree = ""; }; @@ -354,6 +356,7 @@ 18884C452C12A4EA00E01458 /* IBEpo.swift */, 18884C432C11DBA300E01458 /* IBDeleteFile.swift */, 1839DBCF2C14442200E33B6E /* IBFetchFile.swift */, + 180220CD2C2D5D560090D34A /* IBAiResponse.swift */, 1849DC902BECD131002C2D2D /* IBCode.swift */, ); path = Models; @@ -521,6 +524,7 @@ 18204C022C199D0E00F94E69 /* IBFileSender+Ota.swift in Sources */, 18EC6E282BFF4C6D00757CA8 /* IBPhoneInfo.swift in Sources */, 18C9589F2C086A9D006A5DE8 /* IBHistorySleep.swift in Sources */, + 180220CE2C2D5D560090D34A /* IBAiResponse.swift in Sources */, 8A65F95F2BABEC4D005C1EA4 /* IBClock.swift in Sources */, 8A0A39D22B980E4C006A3B18 /* JSON.swift in Sources */, 18E7AC962BF5E39900CB9393 /* IBHeartRate.swift in Sources */, diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 55cf124..a29aacd 100644 Binary files a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index a476c2f..b99ea7c 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -14,8 +14,8 @@ filePath = "IOTBluetooth/IBChecker.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "335" - endingLineNumber = "335" + startingLineNumber = "348" + endingLineNumber = "348" landmarkName = "unknown" landmarkType = "0"> diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift index 91d0daf..ad76570 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift @@ -484,9 +484,25 @@ public extension IBAbility { /// - Returns: IBFetchFile \ /// Read from ``IBResponse``.value static let fetchFiles = IBAbility.short.id(0x21).parser(IBFetchFilesParser.self).checker(IBFileChecker.self) + + + /// 发送Ai准备状态回执 + /// - Parameter value:Int + /// 0 = 失败 \ + /// 1 = 成功 \ + internal static let aiReady = IBAbility.short.id(0x05).parser(IBGeneralParser.self).checker(IBValueChecker.self) + + /// 获取Ai录音文件 + internal static let fetchAiAudio = IBAbility.short.id(0x34).parser(IBfetchAiAudioParser.self) + + /// 发送Ai回应内容 + /// - Parameter aiResponse: see ``IBAiResponse`` + internal static let aiResponse = IBAbility.long.id(0x1f).parser(IBGeneralParser.self).checker(IBAiReponseChecker.self) } extension IBAbility { + + // // // // MARK: - 私有 diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBChecker.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBChecker.swift index 7858eda..50ca79f 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBChecker.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBChecker.swift @@ -104,6 +104,19 @@ struct IBOtaChecker: IBChecker { } } +struct IBAiReponseChecker: IBChecker { + static func check(_ parameters: [IBParameter]) -> [UInt8] { + guard case let .aiResponse(value) = parameters[.aiResponse] else { fatalError("缺少参数: aiReponse") } + Logger.log(value.description) + + var data = [value.type.byte] + if let value = value.value.data(using: .utf8),!value.isEmpty { + data += value.count.byte2 + data += value.bytes + } + return data + } +} struct IBTargetStepChecker: IBChecker { static func check(_ parameters: [IBParameter]) -> [UInt8] { guard case let .targetStep(value) = parameters[.targetStep] else { fatalError("缺少参数: targetStep") } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBNotify.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBNotify.swift index 8bad54a..474b91a 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBNotify.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBNotify.swift @@ -187,6 +187,30 @@ extension IBNotify { } return true } + //请求天气 + if data.bytes[1] == 0x05 && data.bytes[2] == 0x03 { + post(.weather) + return true + } + //表盘安装状态 + if(data.bytes[1] == 0x05 && data.bytes[2] == 0x04){ + let result = IBGeneralParser.dataPacket(data) + let code = result.int + let desc = ["安装失败","安装成功"] + Logger.log("表盘安装状态:",desc[code]) + return true + } + //AI录音已准备好 + if(data.bytes[1] == 0x05 && data.bytes[2] == 0x05){ + aiRequest(data) + return true + } + //文件已接收完成 + if data.bytes[1] == 0x05 && data.bytes[2] == 0x20 { + let result = IBGeneralParser.dataPacket(data) + IBBluetooth.shared.fileSender?.sendFileResult(result[0].int == 1) + return true + } //训练状态变化 if data.bytes[1] == 0x05 && data.bytes[2] == 0x50 { let result = IBGeneralParser.dataPacket(data) @@ -202,22 +226,10 @@ extension IBNotify { } return true } - //请求天气 - if data.bytes[1] == 0x05 && data.bytes[2] == 0x03 { - post(.weather) - return true - } - //文件已接收完成 - if data.bytes[1] == 0x05 && data.bytes[2] == 0x20 { - let result = IBGeneralParser.dataPacket(data) - IBBluetooth.shared.fileSender?.sendFileResult(result[0].int == 1) - return true - } //配对 if checkPairing(data){ return true } - return false } @@ -282,4 +294,28 @@ extension IBNotify { IBManager.completeHandler = nil } } + + static func aiRequest(_ data:Data){ + let result = IBGeneralParser.dataPacket(data) + let code = result.int + let desc = ["国内版本","国外版本"] + Logger.log("国内/国外标识:",desc[code]) + IBSender() + .ability(IBAbility.aiReady) + .parameters([.value(1)]) + .response { result in + Logger.log(result) + if result.isSuccess{ + fectchAiAudio() + } + } + } + static func fectchAiAudio(){ + IBSender() + .ability(IBAbility.fetchAiAudio) + .parameters([]) + .response { result in + Logger.log(result) + } + } } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParameter.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParameter.swift index ac342bc..1cc8df9 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParameter.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParameter.swift @@ -37,6 +37,7 @@ enum IBKey: String { case dialFile = "dialFile" case deleteFile = "deleteFile" case fetchFils = "fetchFils" + case aiResponse = "aiResponse" } /// 参数 @@ -99,6 +100,8 @@ public enum IBParameter { case deleteFile(IBDeleteFile) /// 获取指定类型文件目录 case fetchFils(IBFile) + /// 发送Ai回应内容 + case aiResponse(IBAiResponse) var key: IBKey { switch self { @@ -131,6 +134,7 @@ public enum IBParameter { case .dialFile(_): return .dialFile case .deleteFile(_): return .deleteFile case .fetchFils(_): return .fetchFils + case .aiResponse(_): return .aiResponse } } } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift index 2895572..01f2275 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift @@ -336,3 +336,11 @@ struct IBOtherTargetParser: IBParser { return .response.code(.success).value(model) } } + +struct IBfetchAiAudioParser: IBParser { + static func parse(_ data: Data) -> IBResponse { + let dataPacket = dataPacket(data) + Logger.log(dataPacket) + return .response.code(.success).value(dataPacket) + } +} diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift index e0597a8..d498e98 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift @@ -26,11 +26,6 @@ public class IBSender { totalData = ability.checker.check(parameters) } -// totalData = [] -// for i in 0...202{ -// totalData.append(UInt8(i)) -// } - let list = splitData(totalData) if list.count > 1{ var newList = [[UInt8]]() diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAiResponse.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAiResponse.swift new file mode 100644 index 0000000..63afd5a --- /dev/null +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAiResponse.swift @@ -0,0 +1,21 @@ +// +// IBAiResponse.swift +// IOTBluetooth +// +// Created by incar on 2024/6/27. +// + +import Foundation +/// Ai回应模型 +public class IBAiResponse { + /// 类型,0:AI回应错误 1:AI录音解析内容 2:AI回应内容(类型为0时,只发类型即可,后面字节均不发) + var type: Int = 0 + /// 内容 + public var value: String = "" + + public init() { } + + public var description: String { + return "IBAiResponse(type: \(type), value:\(value))" + } +}