// // BleMessage+Function.swift // HDFwear // // Created by daifengyi on 2023/6/27. // import SwiftDate extension BleMessage { func getResponseCmd(frameNumber: [UInt8], messageId: [UInt8], success: Bool) -> Data { let bytes: [UInt8] = frameNumber + messageId + (success ? [0x00] : [0x01]) return createDataPacket(key: .response, bytes: bytes) } func getQueryDeviceInfoCmd(queryItems: [UInt8]) -> Data { let bytes: [UInt8] = [UInt8(queryItems.count)] + queryItems return createDataPacket(key: .setTimeFormat, bytes: bytes) } func getTimeCmd() -> Data { let date = DateInRegion().date let calendar = Calendar.current let weekday = calendar.component(.weekday, from: date) // 调整星期表示 let adjustedWeekday = (weekday == 1) ? 7 : (weekday - 1) let timeBytes: [UInt8] = [UInt8(date.year%100), UInt8(date.month), UInt8(date.day), UInt8(date.hour), UInt8(date.minute), UInt8(date.second), UInt8(adjustedWeekday)] // return getSendData(cmd: .set, key: .time, bytes: timeBytes) // let a = getPackData(key: .setTime, contentBytes: timeBytes) // let b = createDataPacket(key: .setTime, bytes: timeBytes) return createDataPacket(key: .setTime, bytes: timeBytes) } func getTimeFormatCmd(format: TimeFormat) -> Data { let bytes: [UInt8] = [format.rawValue] return createDataPacket(key: .setTimeFormat, bytes: bytes) } func getTemperatureUnitCmd(unit: TemperatureUnit) -> Data { let bytes: [UInt8] = [unit.rawValue] return createDataPacket(key: .setTemperatureUnit, bytes: bytes) } func getDistanceUnitCmd(unit: DistanceUnit) -> Data { let bytes: [UInt8] = [unit.rawValue] return createDataPacket(key: .setDistanceUnit, bytes: bytes) } func getWristSenseCmd(_ bool: Bool) -> Data { let bytes: [UInt8] = bool ? [0x01] : [0x00] return createDataPacket(key: .setWristSense, bytes: bytes) } func getTouchSenseCmd(_ bool: Bool) -> Data { let bytes: [UInt8] = bool ? [0x01] : [0x00] return createDataPacket(key: .setTouchSense, bytes: bytes) } func getLowPowerRemind(_ bool: Bool) -> Data { let bytes: [UInt8] = bool ? [0x01] : [0x00] return createDataPacket(key: .setLowPowerRemind, bytes: bytes) } func getLanguageCmd(_ lan: UInt8) -> Data { let bytes: [UInt8] = [lan] return createDataPacket(key: .setLanguage, bytes: bytes) } func getRestoreCmd() -> Data { let bytes: [UInt8] = [] return createDataPacket(key: .setRestore, bytes: bytes) } func getHeartRateHighRemindCmd(_ maxHr: UInt8) -> Data { let bytes: [UInt8] = [maxHr] return createDataPacket(key: .setHeartRateHighRemind, bytes: bytes) } func getHeartRateLowRemindCmd(_ minHr: UInt8) -> Data { let bytes: [UInt8] = [minHr] return createDataPacket(key: .setHeartRateLowRemind, bytes: bytes) } func getFindWatchCmd(_ bool: Bool) -> Data { let bytes: [UInt8] = bool ? [0x01] : [0x00] return createDataPacket(key: .setFindWatch, bytes: bytes) } func getPressureAutoDetectCmd(_ bool: Bool) -> Data { let bytes: [UInt8] = bool ? [0x01] : [0x00] return createDataPacket(key: .setPressureAutoDetect, bytes: bytes) } func getBloodOxygenAutoDetectCmd(_ bool: Bool) -> Data { let bytes: [UInt8] = bool ? [0x01] : [0x00] return createDataPacket(key: .setbloodOxygenAutoDetect, bytes: bytes) } func getUserInfoCmd(_ user: UserInfoModel) -> Data { let bytes = [0x00, UInt8(user.weight), 0x00, UInt8(user.height), UInt8(user.gender)] return createDataPacket(key: .setUserInfo, bytes: bytes) } func getNotDisturbCmd(_ remind: RemindModel) -> Data { // guard !weekflag.isEmpty else { // return createDataPacket(key: .setDistanceUnit, bytes: []) // } // var flag: UInt8 = 0 // for item in weekflag { // flag |= 1 << item.rawValue // } // let bytes: [UInt8] = [remind.isOn ? 0x01 : 0x00, isRepeat ? 0x01: 0x00, flag, UInt8(remind.startDate.hour), UInt8(remind.startDate.minute), UInt8(remind.endDate.hour), UInt8(remind.endDate.minute)] let bytes: [UInt8] = [remind.isOn ? 0x01 : 0x00, UInt8(remind.startDate.hour), UInt8(remind.startDate.minute), UInt8(remind.endDate.hour), UInt8(remind.endDate.minute)] return createDataPacket(key: .setNoDisturb, bytes: bytes) } func getSyncCmd(_ type: SyncType) -> Data { let bytes: [UInt8] = [type.rawValue] return createDataPacket(key: .setSynWatch, bytes: bytes) } func getNewWeatherCmd(_ weather: NewWeatherModel) -> Data { let timeInterval = UInt32(weather.date?.timeIntervalSince1970 ?? 0) let byte1: UInt8 = UInt8((timeInterval >> 24) & 0xFF) let byte2: UInt8 = UInt8((timeInterval >> 16) & 0xFF) let byte3: UInt8 = UInt8((timeInterval >> 8) & 0xFF) let byte4: UInt8 = UInt8(timeInterval & 0xFF) // var uint32Data : [UInt8] = [] // withUnsafeBytes(of: timeInterval) { uint32Data.append(contentsOf: $0) } var bytes:[UInt8] = [byte1, byte2, byte3, byte4, UInt8(weather.currentTemperature), UInt8(weather.lowestTemperature), UInt8(weather.highestTemperature), UInt8(weather.type.rawValue), UInt8(weather.airQuality.rawValue), UInt8(weather.humidity), UInt8(weather.wind.rawValue), UInt8(weather.windForce)] for day in weather.next5Days { bytes += [UInt8(day.type.rawValue), UInt8(day.lowestTemperature), UInt8(day.highestTemperature)] } if let utf8Data = weather.position.data(using: .utf8) { let utf8Array = Array(utf8Data) bytes += [UInt8(utf8Array.count)] + utf8Array } else { bytes += [UInt8(1), UInt8(0)] print("转换失败") } return createDataPacket(key: .setWeather, bytes: bytes) } func getNewBeiDouContactCmd(_ contacts: [NewBeiDouContactModel]) -> Data { // 如果超过10个元素,截断数组 var truncatedContacts = contacts if truncatedContacts.count > 10 { truncatedContacts.removeSubrange(10.. Data { let answersCount = answers.count var bytes: [UInt8] = [UInt8(answersCount)] for ans in answers { // utf8 if let utf8Data = ans.data(using: .utf8) { let utf8Array = Array(utf8Data) bytes += [UInt8(utf8Array.count)] + utf8Array }else { bytes += [UInt8(1), UInt8(0)] print("转换失败") } // gbk let cfEncoding = CFStringEncodings.GB_18030_2000 let encoding = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEncoding.rawValue)) let fullStr = ans.toFullWidth()// 半角->全角 if let gbkData = fullStr.data(using: String.Encoding(rawValue: encoding)) { let gbkArray = Array(gbkData) bytes += [UInt8(gbkArray.count)] + gbkArray }else { bytes += [UInt8(1), UInt8(0)] print("转换失败") } } return createDataPacket(key: .setBeiDouQuickAnswer, bytes: bytes) } }