Commit 4d2968c388995057cde772e9350f2e0487281c84

Authored by daifengyi
1 parent e756c904

feat:parse sleep

HDFwear/Home/Model/HealthModel.swift
... ... @@ -1158,9 +1158,11 @@ class BloodOxygenModel: Object {
1158 1158  
1159 1159 //MARK: - 睡眠
1160 1160 enum SleepType: Int {
1161   - case awake = 0
1162   - case deep = 1
1163   - case light = 2
  1161 + case awake = 7
  1162 + case eyeMove = 8
  1163 + case light = 9
  1164 + case deep = 10
  1165 +
1164 1166 }
1165 1167  
1166 1168 class SleepModel: Object {
... ... @@ -1191,66 +1193,21 @@ class SleepModel: Object {
1191 1193  
1192 1194 class func toSleepArray(_ bytes: [UInt8]) -> [SleepModel] {
1193 1195 var sleepArray: [SleepModel] = []
1194   - let date = DateInRegion(year: Int(bytes[0])+2000, month: Int(bytes[1]), day: Int(bytes[2])-1, hour: 22, minute: 0, second: 0).date
1195   - let sleepBytes = Array(bytes[3..<bytes.count])
1196   - let num = sleepBytes.count/3
1197   - if num < 2 {
1198   - return []
1199   - }
1200   - let platform = CurDevice.platform
1201   -
1202   - // if platform == ._818 {
1203   - // date = date + 1.days
1204   - //// print(date)
1205   - // }
1206   -
1207   - for i in 0..<num-1 {
1208   - print(Array(sleepBytes[3*i..<3*i+3]))
1209   - var startDate = date
1210   - var endDate = date
1211   - // var type: SleepType = .awake
1212   - var rawInt = Int(sleepBytes[i*3])
1213   -
1214   - if Int(sleepBytes[i*3+1]) > 20 {
1215   - startDate = DateInRegion(year: Int(bytes[0])+2000, month: Int(bytes[1]), day: Int(bytes[2])-1, hour: Int(sleepBytes[i*3+1]), minute: Int(sleepBytes[i*3+2])).date
1216   - } else {
1217   - startDate = DateInRegion(year: Int(bytes[0])+2000, month: Int(bytes[1]), day: Int(bytes[2]), hour: Int(sleepBytes[i*3+1]), minute: Int(sleepBytes[i*3+2])).date
1218   - }
1219   - // if i < num {
1220   - if Int(sleepBytes[(i+1)*3+1]) > 20 {
1221   - endDate = DateInRegion(year: Int(bytes[0])+2000, month: Int(bytes[1]), day: Int(bytes[2])-1, hour: Int(sleepBytes[(i+1)*3+1]), minute: Int(sleepBytes[(i+1)*3+2])).date
1222   - } else {
1223   - endDate = DateInRegion(year: Int(bytes[0])+2000, month: Int(bytes[1]), day: Int(bytes[2]), hour: Int(sleepBytes[(i+1)*3+1]), minute: Int(sleepBytes[(i+1)*3+2])).date
1224   - }
1225   - if platform == ._828 {
1226   - if rawInt == 1 {
1227   - rawInt = 2
1228   - } else if rawInt == 2 {
1229   - rawInt = 1
1230   - }
1231   - endDate = endDate + 1.days
1232   - startDate = startDate + 1.days
1233   - }
1234   -
1235   -
1236   - sleepArray.append(SleepModel(type: SleepType(rawValue: rawInt) ?? .awake, startDate: startDate, endDate: endDate))
1237   - // }
1238   -
1239   -
1240   -
1241   - /*
1242   - if i == 0 {
1243   - sleepArray.append(SleepModel(type: .awake, startDate: date, endDate: date+Int(sleepBytes[i*3+1]).hours+Int(sleepBytes[i*3+2]).minutes))
1244   - } else {
1245   - var rawInt = Int(sleepBytes[(i-1)*3])
1246   - if rawInt == 1 {
1247   - rawInt = 2
1248   - } else if rawInt == 2 {
1249   - rawInt = 1
1250   - }
1251   - sleepArray.append(SleepModel(type: SleepType(rawValue: rawInt) ?? .awake, startDate: date+Int(sleepBytes[(i-1)*3+1]).hours+Int(sleepBytes[(i-1)*3+2]).minutes, endDate: date+Int(sleepBytes[i*3+1]).hours+Int(sleepBytes[i*3+2]).minutes))
1252   - }
1253   - */
  1196 + // 将数组分成13个一组
  1197 + let groupedArray = stride(from: 0, to: bytes.count, by: 13).map { Array(bytes[$0..<min($0 + 13, bytes.count)]) }
  1198 + // 处理每组的前四个数
  1199 + for group in groupedArray {
  1200 + let starthHexValue = group.prefix(4).reduce(0) { ($0 << 8) + UInt32($1) }
  1201 + let start = TimeInterval(starthHexValue)
  1202 + let startDate = Date(timeIntervalSince1970: start)
  1203 + let endHexValue = group[4..<8].reduce(0) { ($0 << 8) + UInt32($1) }
  1204 + let end = TimeInterval(endHexValue)
  1205 + let endDate = Date(timeIntervalSince1970: end)
  1206 + let durationHexValue = group[8..<12].reduce(0) { ($0 << 8) + UInt32($1) }
  1207 + let duration = TimeInterval(endHexValue)
  1208 + let type = group[12]
  1209 + let sleep = SleepModel(type: SleepType(rawValue: Int(type)) ?? .awake, startDate: startDate, endDate: endDate)
  1210 + sleepArray.append(sleep)
1254 1211 }
1255 1212 return sleepArray
1256 1213 }
... ... @@ -1327,6 +1284,8 @@ class SleepModel: Object {
1327 1284 deep += sleep.length
1328 1285 case .none:
1329 1286 break
  1287 + default:
  1288 + break
1330 1289 }
1331 1290 }
1332 1291 sleepTime(awake, light, deep)
... ... @@ -1346,6 +1305,8 @@ class SleepModel: Object {
1346 1305 deep += sleep.length
1347 1306 case .none:
1348 1307 break
  1308 + default:
  1309 + break
1349 1310 }
1350 1311 }
1351 1312  
... ...
HDFwear/Tools/BluetoothManager+Function.swift
... ... @@ -350,6 +350,8 @@ extension BluetoothManager {
350 350 parseTemperaturData(content)
351 351 case 0x8019:// 睡眠数据上报
352 352 print("睡眠数据上报")
  353 + let content = parseContentFromBytes(bytes)
  354 + parseSleepData(content)
353 355 case 0x8020:// 历史压力数据上报
354 356 print("历史压力数据上报")
355 357 let content = parseContentFromBytes(bytes)
... ... @@ -504,4 +506,14 @@ extension BluetoothManager {
504 506 stepClosure?(array, nil)
505 507 }
506 508  
  509 + func parseSleepData (_ content: [UInt8]) {
  510 + guard content.count > 0 else {
  511 + print("无有效的信息")
  512 + sleepClosure?([], nil)
  513 + return
  514 + }
  515 + let array = SleepModel.toSleepArray(content)
  516 + SleepModel.addArray(array)// 加入数据库
  517 + sleepClosure?(array, nil)
  518 + }
507 519 }
... ...