NewGpsModel.swift 3.78 KB
//
//  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: 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
    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: longitudeValue, longitudeScale: longitudeScale, latitudeValue: latitudeValue, latitudeScale: latitudeScale)
            arr.append(point)
        }
        gpsModel.locations = arr
        return gpsModel
    }
}