Blame view

Twear/Tools/BleMessage.swift 8.34 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  //
  //  BleMessage.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/8.
  //
  
  import UIKit
  import SwiftDate
  import SwiftUI
  
  class BleMessage: NSObject {
      static let shared = BleMessage()
      
      func getSettingCmd() -> Data {
          return getSendData(cmd: .set, key: .setting, bytes: [])
      }
  
      func getUserCmd(_ user: UserInfoModel) -> Data {
          let userBytes = [UInt8(user.gender), UInt8(user.age), UInt8(user.height), UInt8(user.weight), UInt8(user.stepsGoal >> 16 & 0xFF), UInt8(user.stepsGoal >> 8 & 0xFF), UInt8(user.stepsGoal & 0xFF), 0x00]
          return getSendData(cmd: .set, key: .user, bytes: userBytes)
      }
      
      func getUnitCmd(_ distance: DistanceUnit, _ temperature: TemperatureUnit) -> Data {
          return getSendData(cmd: .set, key: .unit, bytes: [distance.rawValue, temperature.rawValue])
      }
      
  
      
      func getTimeCmd(format: TimeFormat) -> Data {
          let date = DateInRegion().date
          let timeBytes: [UInt8] = [UInt8(date.year%100), UInt8(date.month), UInt8(date.day), UInt8(date.hour), UInt8(date.minute), UInt8(date.second), format.rawValue]
          return getSendData(cmd: .set, key: .time, bytes: timeBytes)
      }
      
      func getCameraCmd(_ open: Bool) -> Data {
          return getSendData(cmd: .device, key: open ? .openCamera : .exitCamera, bytes: [0x01])
      }
      
      func getFindCmd(_ status: Bool) -> Data {
          return getSendData(cmd: .find, key: .find_device, bytes: [])
      }
      
      func getBatteryCmd() -> Data {
          return getSendData(cmd: .device, key: .battery, bytes: [])
      }
      
      func getLanguageCmd(language: AppSettings.Language, timeFormat: TimeFormat, screenOnTime: Int, pair: UInt8) -> Data {
          if language == .system {
              return getSendData(cmd: .set, key: .language, bytes: [AppSettings.localeLanguage().cmd, timeFormat.rawValue, UInt8(screenOnTime), 0x01])
          } else {
              return getSendData(cmd: .set, key: .language, bytes: [language.cmd, timeFormat.rawValue, UInt8(screenOnTime), pair])
          }
      }
      
      
      func getWeatherCmd(_ array: [WeatherModel]) -> Data {
          var weatherBytes: [UInt8] = []
          for weather in array {
              weatherBytes.append(contentsOf: weather.toBytes())
          }
          return getSendData(cmd: .weather, key: .weather, bytes: (weatherBytes))
      }
      
      func getMessagePush(_ push: MessagePushModel) -> Data {
          return getSendData(cmd: .set, key: .push, bytes: [UInt8(push.value & 0xFF), UInt8(push.value >> 8)])
      }
      
      func getAlarmClockCmd(_ array: [AlarmClockModel]) -> Data {
          var alarmClockBytes: [UInt8] = [UInt8](repeating: 0, count: 30)
          for (i, alarm) in array.enumerated() {
              alarmClockBytes[i*5] = UInt8(alarm.date.hour)
              alarmClockBytes[i*5+1] = UInt8(alarm.date.minute)
              alarmClockBytes[i*5+2] = UInt8(alarm.cycle)
              alarmClockBytes[i*5+3] = 0x01
              alarmClockBytes[i*5+4] = alarm.isOn ? 0x01 : 0x00
          }
          return getSendData(cmd: .set, key: .alarmClock, bytes: alarmClockBytes)
      }
      
      func getDrinkWaterCmd(_ remind: RemindModel) -> Data {
          let bytes: [UInt8] = [remind.isOn ? 0x01 : 0x00, UInt8(remind.startDate.hour),  UInt8(remind.startDate.minute), UInt8(remind.endDate.hour), UInt8(remind.endDate.minute), 127, UInt8(remind.interval & 0xFF), UInt8(remind.interval >> 8)]
          return getSendData(cmd: .set, key: .drink, bytes: bytes)
      }
      
      func getSedentaryCmd(_ remind: RemindModel) -> Data {
          let target = remind.interval/60*100
          let bytes: [UInt8] = [remind.isOn ? 0x01 : 0x00, UInt8(remind.startDate.hour), UInt8(remind.endDate.hour), 127, UInt8(remind.interval & 0xFF), UInt8(remind.interval >> 8),  UInt8(target & 0xFF), UInt8(target >> 8)]
          return getSendData(cmd: .set, key: .sedentary, bytes: bytes)
      }
      
      func getNotDisturbCmd(_ remind: RemindModel) -> Data {
          let bytes: [UInt8] = [remind.isOn ? 0x01 : 0x00, UInt8(remind.startDate.hour), UInt8(remind.startDate.minute), UInt8(remind.endDate.hour), UInt8(remind.endDate.minute)]
          return getSendData(cmd: .remind, key: .disturb, bytes: bytes)
      }
      
      
      func getWristCmd(_ bool: Bool) -> Data {
          let bytes: [UInt8] = bool ? [0x01, 0x01, 0x01] : [0x00, 0x00, 0x00]
          return getSendData(cmd: .device, key: .wrist, bytes: bytes)
      }
      
      func getMenstrualCmd(_ menstrual: MenstrualCalendarModel) -> Data {
          var status: UInt8 = 0x01
          var type: MenstrualDateModel
          if menstrual.newType == nil {
              type = menstrual.type!
          } else {
              type = menstrual.newType!
          }
          switch type {
          case .menstrualPeriod:
              status = 0x04
          case .fertilePeriod:
              status = 0x02
          case .safetyPeriod:
              status = 0x01
          case .ovulationDay:
              status = 0x03
          default:
              status = 0x01
          }
          let bytes: [UInt8] = [status, UInt8(menstrual.days), UInt8(menstrual.whichDay)]
          return getSendData(cmd: .set, key: .menstrual, bytes: bytes)
      }
582f536d   yangbin   common:2022.1.28
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
      func getCardCmd(_ string: String, type: Int) -> Data {
          var cardBytes: [UInt8] = [UInt8(type)]
          for (i, scalar) in string.unicodeScalars.enumerated() {
  //            if i < 20 {
                  cardBytes.append(UInt8(scalar.value & 0xFF))
                  cardBytes.append(UInt8(scalar.value >> 8))
  //            }
          }
          return getSendData(cmd: .set, key: .card, bytes: cardBytes)
      }
      
      func getSyncContactsCmd(_ contacts: [ContactModel], num: Int) -> Data {
          var contactBytes: [UInt8] = []
          var startIndex: Int = 0
          var key: SetCmd = .contact1
          if num == 2 {
              startIndex = 2
              key = .contact2
          } else if num == 3 {
              startIndex = 5
              key = .contact3
          }
          for (i, contact) in contacts.enumerated() {
              contactBytes.append(contentsOf: contact.toBytes(startIndex+i+1))
              print(contact.toBytes(startIndex+i+1))
          }
  //        print(contactBytes)
          return getSendData(cmd: .set, key: key, bytes: contactBytes)
      }
75d24c15   yangbin   123
155
      
582f536d   yangbin   common:2022.1.28
156
  
75d24c15   yangbin   123
157
158
159
160
161
162
163
164
165
166
167
168
      
      
      func getSyncCmd(_ type: SyncType, day: SyncDay = .today) -> Data {
          let date = DateInRegion().date + day.rawValue.days
          let syncBytes: [UInt8] = [type.rawValue, UInt8(date.year%100), UInt8(date.month), UInt8(date.day), 0x00, 0x00, 0x00]
          return getSendData(cmd: .sync, key: .sync, bytes: syncBytes)
      }
      
      func getSyncDialCmd(bytes: [UInt8]) -> Data {
          return getSendData(cmd: .set, key: .dial, bytes: bytes)
      }
      
582f536d   yangbin   common:2022.1.28
169
170
171
172
173
174
175
      func getCustomDialCmd(bytes: [UInt8]) -> Data {
          let data = getSendData(cmd: .set, key: .custom, bytes: bytes)
          var mdata = Data(repeating: 0x00, count: 120)
          mdata[0..<13] = data[0..<13]
          return mdata
      }
      
75d24c15   yangbin   123
176
177
178
179
180
181
182
183
184
185
      func getFirmwareCmd() -> Data {
          return getSendData(cmd: .firmware, key: .firmware, bytes: [])
      }
      
      
      func getSendData(cmd: BleCmd, key: SetCmd, bytes: [UInt8]) -> Data {
          let ack: UInt8 = 0
          let err: UInt8 = 0
          let version: UInt8 = 0
          let serial: UInt16 = 1
582f536d   yangbin   common:2022.1.28
186
  
75d24c15   yangbin   123
187
          
582f536d   yangbin   common:2022.1.28
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
          if key == .custom {
              let count = bytes.count/5
              var mBytes: [UInt8] = [cmd.rawValue, 0x00, key.rawValue, UInt8(count >> 8), UInt8(count & 0xFF)]
              mBytes.append(contentsOf: bytes)
              
              let crc8 = crc8(bytes: mBytes)
              let dataLength = count + 1
              var header: [UInt8] = [0xBA, ack << 5 | err << 4 | version, UInt8(serial >> 8), UInt8(serial & 0xFF), UInt8(crc8 >> 8), UInt8(crc8 & 0xFF), UInt8(dataLength >> 8), UInt8(dataLength & 0xFF)]
              header.append(contentsOf: mBytes)
              let data = Data(bytes: header, count: header.count)
              return data
          } else {
              var mBytes: [UInt8] = [cmd.rawValue, 0x00, key.rawValue, UInt8(bytes.count >> 8), UInt8(bytes.count & 0xFF)]
              mBytes.append(contentsOf: bytes)
              
              let crc8 = crc8(bytes: mBytes)
              
              var header: [UInt8] = [0xBA, ack << 5 | err << 4 | version, UInt8(serial >> 8), UInt8(serial & 0xFF), UInt8(crc8 >> 8), UInt8(crc8 & 0xFF), UInt8(mBytes.count >> 8), UInt8(mBytes.count & 0xFF)]
              header.append(contentsOf: mBytes)
              let data = Data(bytes: header, count: header.count)
              return data
          }
   
75d24c15   yangbin   123
211
212
213
214
215
216
217
218
219
          
      }
      
      
      
      
      
      
  }