Blame view

HDFwear/Tools/BleMessage+Function.swift 9.65 KB
6102d0b3   daifengyi   feat:time format
1
2
3
4
5
6
7
8
9
10
  //
  //  BleMessage+Function.swift
  //  HDFwear
  //
  //  Created by daifengyi on 2023/6/27.
  //
  
  import SwiftDate
  
  extension BleMessage {
3cba738e   daifengyi   feat:response
11
12
13
14
15
      func getResponseCmd(frameNumber: [UInt8], messageId: [UInt8], success: Bool) -> Data {
          let bytes: [UInt8] = frameNumber + messageId + (success ? [0x00] : [0x01])
          return createDataPacket(key: .response, bytes: bytes)
      }
      
079b41b8   daifengyi   feat:query device...
16
17
      func getQueryDeviceInfoCmd(queryItems: [UInt8]) -> Data {
          let bytes: [UInt8] = [UInt8(queryItems.count)] + queryItems
a4172a7a   jason   fix:query device ...
18
          return createDataPacket(key: .queryDeviceInfo, bytes: bytes)// 这里的0x02不好使而是0x10有用何解
079b41b8   daifengyi   feat:query device...
19
20
      }
      
6102d0b3   daifengyi   feat:time format
21
22
      func getTimeCmd() -> Data {
          let date = DateInRegion().date
7a9ecfe8   jason   feat:set time par...
23
24
25
26
27
          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)]
6102d0b3   daifengyi   feat:time format
28
29
30
31
32
33
34
35
36
37
          //        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)
      }
50eee3a1   daifengyi   feat:temperature ...
38
39
40
41
42
43
44
45
46
47
48
      
      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)
      }
      
2db3eb12   daifengyi   feat:wrist sense
49
50
51
52
      func getWristSenseCmd(_ bool: Bool) -> Data {
          let bytes: [UInt8] = bool ? [0x01] : [0x00]
          return createDataPacket(key: .setWristSense, bytes: bytes)
      }
50eee3a1   daifengyi   feat:temperature ...
53
      
5f08af0b   daifengyi   feat:touch sense
54
55
56
57
58
      func getTouchSenseCmd(_ bool: Bool) -> Data {
          let bytes: [UInt8] = bool ? [0x01] : [0x00]
          return createDataPacket(key: .setTouchSense, bytes: bytes)
      }
      
e817a7d0   daifengyi   feat:low power re...
59
60
61
62
      func getLowPowerRemind(_ bool: Bool) -> Data {
          let bytes: [UInt8] = bool ? [0x01] : [0x00]
          return createDataPacket(key: .setLowPowerRemind, bytes: bytes)
      }
07a5f009   daifengyi   feat:language
63
64
65
66
67
      
      func getLanguageCmd(_ lan: UInt8) -> Data {
          let bytes: [UInt8] = [lan]
          return createDataPacket(key: .setLanguage, bytes: bytes)
      }
829c07bb   daifengyi   feat:restore, hr ...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
      
      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)
      }
      
c7f49054   jason   feat: rest & exer...
84
85
86
87
88
89
90
91
92
93
      func getRestHeartRateRemindCmd(bool:Bool, minHr: UInt8 = 40, maxHr: UInt8 = 120) -> Data {
          let bytes: [UInt8] = [(bool ? 0x01 : 0x00), minHr, maxHr]
          return createDataPacket(key: .setRestHeartRateRemind, bytes: bytes)
      }
      
      func getExerciseHeartRateRemindCmd(bool:Bool, minHr: UInt8, maxHr: UInt8 = 187) -> Data {
          let bytes: [UInt8] = [(bool ? 0x01 : 0x00), minHr, maxHr]
          return createDataPacket(key: .setExerciseHeartRateRemind, bytes: bytes)
      }
      
829c07bb   daifengyi   feat:restore, hr ...
94
95
96
97
98
      func getFindWatchCmd(_ bool: Bool) -> Data {
          let bytes: [UInt8] = bool ? [0x01] : [0x00]
          return createDataPacket(key: .setFindWatch, bytes: bytes)
      }
      
d8efb388   daifengyi   feat: pressure ox...
99
100
101
102
103
104
105
      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]
a9f41925   jason   feat:blood oxygen...
106
          return createDataPacket(key: .setBloodOxygenAutoDetect, bytes: bytes)
d8efb388   daifengyi   feat: pressure ox...
107
      }
6ef0f40d   daifengyi   feat:user info
108
109
      
      func getUserInfoCmd(_ user: UserInfoModel) -> Data {
c8e3bebb   jason   feat:set user info
110
          let bytes = [0x00, UInt8(user.weight), 0x00, UInt8(user.height), UInt8(user.gender), UInt8(user.age)]
6ef0f40d   daifengyi   feat:user info
111
112
          return createDataPacket(key: .setUserInfo, bytes: bytes)
      }
d6dfcaed   daifengyi   feat:no disturb
113
      
02bd7bf8   jason   feat:no disturb
114
115
      func getNotDisturbCmd(_ noDisturb: NewNoDisturbModel) -> Data {
          let bytes: [UInt8] = [noDisturb.enable ? 0x01 : 0x00, UInt8(noDisturb.startHour), UInt8(noDisturb.startMinute), UInt8(noDisturb.endHour), UInt8(noDisturb.endMinute)]
d6dfcaed   daifengyi   feat:no disturb
116
117
          return createDataPacket(key: .setNoDisturb, bytes: bytes)
      }
92b5af79   daifengyi   feat:synchronize 0
118
      
10c6e004   jason   feat:sleep data
119
120
      func getSyncCmd(_ type: SyncType, _ option:SyncOption = .now) -> Data {
          let bytes: [UInt8] = [type.rawValue, option.rawValue]
92b5af79   daifengyi   feat:synchronize 0
121
122
          return createDataPacket(key: .setSynWatch, bytes: bytes)
      }
b05829d0   jason   feat:set weather
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      
      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 {
9b1c370b   jason   feat:beidou
144
              bytes += [UInt8(1), UInt8(0)]
b05829d0   jason   feat:set weather
145
146
147
148
              print("转换失败")
          }
          return createDataPacket(key: .setWeather, bytes: bytes)
      }
9b1c370b   jason   feat:beidou
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
      
      func getNewBeiDouContactCmd(_ contacts: [NewBeiDouContactModel]) -> Data {
          // 如果超过10个元素截断数组
          var truncatedContacts = contacts
          if truncatedContacts.count > 10 {
              truncatedContacts.removeSubrange(10..<truncatedContacts.count)
          }
          let contactsCount = truncatedContacts.count
          var bytes: [UInt8] = [UInt8(contactsCount)]
          for model in truncatedContacts {
              // name
              if let utf8Data = model.name.data(using: .utf8) {
                  let utf8Array = Array(utf8Data)
                  bytes += [UInt8(utf8Array.count)] + utf8Array
              }else {
                  bytes += [UInt8(1), UInt8(0)]
                  print("转换失败")
              }
              // phone
              if let asciiData = model.phone.data(using: .ascii) {
                  let asciiArray = Array(asciiData)
                  bytes += [UInt8(asciiArray.count)] + asciiArray
              }else {
                  bytes += [UInt8(1), UInt8(0)]
                  print("转换失败")
              }
              // type
              bytes.append(UInt8(model.type.rawValue))
          }
          return createDataPacket(key: .setBeiDouContact, bytes: bytes)
      }
      
      func getNewBeiDouQuickAnswerCmd(_ answers: [String]) -> 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)
      }
03cac185   jason   feat:heart rate d...
207
208
209
210
211
      
      func getHeartRateDetectIntervalCmd(bool:Bool = false, interval: UInt8 = 30) -> Data {
          let bytes: [UInt8] = [(bool ? 0x01 : 0x00), interval]
          return createDataPacket(key: .setHeartRateDetectInterval, bytes: bytes)
      }
a9f41925   jason   feat:blood oxygen...
212
213
214
215
216
      
      func getBloodOxygenLowRemindCmd(_ minHr: UInt8) -> Data {
          let bytes: [UInt8] = [minHr]
          return createDataPacket(key: .setBloodOxygenLowRemind, bytes: bytes)
      }
5caab734   jason   feat:message remind
217
218
219
220
221
222
223
224
225
226
227
228
229
230
      
      func getMessageRemindCmd(enable: Bool, system: Bool, wechat: Bool, qq: Bool) -> Data {
          var byte: UInt8 = 0
              // 设置最低位
              byte |= enable ? 1 : 0
              // 设置第二位
              byte |= system ? (1 << 1) : 0
              // 设置第三位
              byte |= wechat ? (1 << 2) : 0
              // 设置最高位
              byte |= qq ? (1 << 3) : 0
          let bytes: [UInt8] = [byte]
          return createDataPacket(key: .setMessageRemind, bytes: bytes)
      }
0f975482   jason   feat:exercise
231
232
233
234
235
      
      func getExerciseStatusCmd(status: Int, type: Int) -> Data {
          let bytes: [UInt8] = [UInt8(status), UInt8(type)]
          return createDataPacket(key: .setExerciseStatus, bytes: bytes)
      }
6102d0b3   daifengyi   feat:time format
236
  }