NewGpsModel.swift
3.85 KB
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
//
// NewGpsModel.swift
// HDFwear
//
// Created by admin on 2024/1/9.
//
import Foundation
import UIKit
import HandyJSON
import SwiftDate
class NewGpsModel: NSObject {
required override init() { }
struct LocationPoint {
var longitudeValue: Int32
var longitudeScale: Int32
var latitudeValue: Int32
var latitudeScale: Int32
var longitude: Double {
return convert(value: longitudeValue, scale: longitudeScale)
}
var latitude: Double {
return convert(value: latitudeValue, scale: latitudeScale)
}
private func convert(value: Int32, scale: Int32) -> 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
var startTimeInterval: TimeInterval = 0
var startTime: Date?
var locationCount: Int = 0
var locations: [LocationPoint] = []
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
}
init(exerciseType: Int,startTimeInterval: TimeInterval, startTime: Date? = nil, locationCount: Int, locations: [LocationPoint]) {
self.exerciseType = exerciseType
self.startTimeInterval = startTimeInterval
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) }
gpsModel.startTimeInterval = TimeInterval(combinedUInt32)
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: Int32(bitPattern:longitudeValue), longitudeScale: Int32(bitPattern:longitudeScale), latitudeValue: Int32(bitPattern:latitudeValue), latitudeScale: Int32(bitPattern:latitudeScale))
arr.append(point)
}
gpsModel.locations = arr
return gpsModel
}
}