Blame view

HDFwear/Home/Model/NewGpsModel.swift 3.78 KB
006c74c6   jason   feat:UInt8 combin...
1
2
3
4
5
6
7
8
  //
  //  NewGpsModel.swift
  //  HDFwear
  //
  //  Created by admin on 2024/1/9.
  //
  
  import Foundation
1e5d4268   jason   feat:gps
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
  import UIKit
  import HandyJSON
  import SwiftDate
  
  class NewGpsModel: NSObject {
      required override init() { }
      struct LocationPoint {
          var longitudeValue: UInt32
          var longitudeScale: UInt32
          var latitudeValue: UInt32
          var latitudeScale: UInt32
          
          var longitude: Double {
              return convert(value: longitudeValue, scale: longitudeScale)
          }
          
          var latitude: Double {
              return convert(value: latitudeValue, scale: latitudeScale)
          }
          
          private func convert(value: UInt32, scale: UInt32) -> Double {
              if scale == 0 {
                  return 0
              }
              let degrees = Double(value) / (Double(scale) * 100)
              let minutes = Double(value) / Double(scale) - degrees * 100
              return degrees + (minutes * 1000000 / (60 * Double(scale))) / 1000000.0
          }
      }
      
      var exerciseType: Int = 0
cdaa3f52   jason   feat:log alert
40
      var startTimeInterval: TimeInterval = 0
1e5d4268   jason   feat:gps
41
42
43
      var startTime: Date?
      var locationCount: Int = 0
      var locations: [LocationPoint] = []
cdaa3f52   jason   feat:log alert
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      
      override var description: String {
          let dateFormatter = DateFormatter()
          dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
          dateFormatter.timeZone = TimeZone.current
          
              var description = "NewGpsModel\n"
              description += "Exercise Type: \(exerciseType)\n"
              description += "Start TimeInterval: \(startTimeInterval)\n"
              description += "Start Time: \(dateFormatter.string(from: startTime ?? Date()))\n"
              description += "Location Count: \(locationCount)\n"
  
              if !locations.isEmpty {
                  description += "Locations:\n"
                  for (index, location) in locations.enumerated() {
                      description += "  Location \(index + 1):\n"
                      description += "    Longitude: \(location.longitude)\n"
                      description += "    Latitude: \(location.latitude)\n"
                  }
              }
  
              return description
          }
1e5d4268   jason   feat:gps
67
   
cdaa3f52   jason   feat:log alert
68
      init(exerciseType: Int,startTimeInterval: TimeInterval, startTime: Date? = nil, locationCount: Int, locations: [LocationPoint]) {
1e5d4268   jason   feat:gps
69
          self.exerciseType = exerciseType
cdaa3f52   jason   feat:log alert
70
          self.startTimeInterval = startTimeInterval
1e5d4268   jason   feat:gps
71
72
73
74
75
76
77
78
79
80
81
82
          self.startTime = startTime
          self.locationCount = locationCount
          self.locations = locations
      }
      
      class func toGpsModel(_ data: [UInt8]) -> NewGpsModel {
          let gpsModel = NewGpsModel()
          guard data.count >= 9 else {
              return gpsModel // 数据长度不够
          }
          gpsModel.exerciseType = Int(data[0])
          let combinedUInt32 = data[1..<5].reduce(0) { ($0 << 8) + UInt32($1) }
cdaa3f52   jason   feat:log alert
83
          gpsModel.startTimeInterval = TimeInterval(combinedUInt32)
1e5d4268   jason   feat:gps
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
          gpsModel.startTime = Date(timeIntervalSince1970: TimeInterval(combinedUInt32))
          gpsModel.locationCount = Int(data[5..<9].reduce(0) { ($0 << 8) + UInt32($1) })
          var arr = [LocationPoint]()
          // 将数组分成16个一组
          for index in stride(from: 9, to: data.count, by: 16) {
              let longitudeValue = data[index + 0..<index + 4].reduce(0) { ($0 << 8) + UInt32($1) }
              let longitudeScale = data[index + 4..<index + 8].reduce(0) { ($0 << 8) + UInt32($1) }
              let latitudeValue = data[index + 8..<index + 12].reduce(0) { ($0 << 8) + UInt32($1) }
              let latitudeScale = data[index + 12..<index + 16].reduce(0) { ($0 << 8) + UInt32($1) }
              
              let point = LocationPoint(longitudeValue: longitudeValue, longitudeScale: longitudeScale, latitudeValue: latitudeValue, latitudeScale: latitudeScale)
              arr.append(point)
          }
          gpsModel.locations = arr
          return gpsModel
      }
  }