Blame view

Twear/Tools/BleMessage.swift 6.42 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  //
  //  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)
      }
      
      
      
      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)
      }
      
      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
          
          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)]//[UInt8](repeating: 0x00, count: 8)
  //        let data: [UInt8]
          header.append(contentsOf: mBytes)
          let data = Data(bytes: header, count: header.count)
          return data
          
      }
      
      
      
      
      
      
  }