Skip to content

Data Handling

Latisha. edited this page Dec 27, 2024 · 1 revision

Data Handling

Data Structures

Core Data Models

DiveData

public struct DiveData: Identifiable {
    public let id: UUID
    public let number: Int
    public let datetime: Date
    
    // Basic dive data
    public var maxDepth: Double
    public var divetime: TimeInterval
    public var temperature: Double
    
    // Profile data
    public var profile: [DiveProfilePoint]
    
    // Gas and tank data
    public var tankPressure: [Double]
    public var gasMix: Int?
    public var tanks: [Tank]?
    
    // Environmental data
    public var salinity: Double?
    public var atmospheric: Double?
    
    // Decompression data
    public var decoModel: DecoModel?
    public var decoStop: DecoStop?
}

Profile Point

public struct DiveProfilePoint {
    public let time: TimeInterval
    public let depth: Double
    public let temperature: Double?
    public let pressure: Double?
    public let po2: Double?
    public let pn2: Double?
    public let phe: Double?
}

Supporting Structures

Tank Information

public struct Tank {
    public var volume: Double
    public var workingPressure: Double
    public var beginPressure: Double
    public var endPressure: Double
    public var gasMix: Int
    public var usage: Usage
    
    public enum Usage {
        case none
        case oxygen
        case diluent
        case sidemount
    }
}

Decompression Model

public struct DecoModel {
    public var type: DecoType
    public var conservatism: Int
    public var gfLow: UInt32?
    public var gfHigh: UInt32?
    
    public enum DecoType {
        case none
        case buhlmann
        case vpm
        case rgbm
        case dciem
    }
}

Parsing System

Parser Components

public class GenericParser {
    // Type-safe sample callback
    private static func sampleCallback(
        type: dc_sample_type_t,
        value: dc_sample_value_t,
        userdata: UnsafeMutableRawPointer?
    ) {
        // Sample processing logic
    }
    
    // Parse dive data
    public static func parseDiveData(
        family: DeviceFamily,
        model: UInt32,
        diveNumber: Int,
        diveData: UnsafePointer<UInt8>,
        dataSize: Int
    ) throws -> DiveData {
        // Parsing implementation
    }
}

Parsing Process

  1. Raw Data Reception
// Handle incoming data chunks
func processDataChunk(_ data: Data) {
    buffer.append(data)
    processCompleteFrames()
}
  1. Frame Processing
func processCompleteFrames() {
    while let frame = findNextFrame() {
        parseFrame(frame)
    }
}
  1. Data Conversion
func convertRawValues(
    depth: Double,
    temperature: Double,
    pressure: Double
) -> DiveProfilePoint {
    // Unit conversion and validation
}

Error Handling

enum ParserError: Error {
    case invalidParameters
    case parserCreationFailed(dc_status_t)
    case datetimeRetrievalFailed(dc_status_t)
    case sampleProcessingFailed(dc_status_t)
}

Fingerprint Mechanism

Fingerprint Structure

struct DiveFingerprint {
    let data: Data
    let timestamp: Date
    let diveNumber: Int
}

Fingerprint Management

  1. Storage
class FingerprintManager {
    private let defaults = UserDefaults.standard
    private let fingerprintKey = "lastDiveFingerprint"
    
    func saveFingerprint(_ fingerprint: Data) {
        defaults.set(fingerprint, forKey: fingerprintKey)
    }
    
    func loadFingerprint() -> Data? {
        return defaults.data(forKey: fingerprintKey)
    }
}
  1. Usage
func setDeviceFingerprint(_ device: UnsafeMutablePointer<device_data_t>) {
    if let fingerprint = fingerprintManager.loadFingerprint() {
        dc_device_set_fingerprint(
            device,
            Array(fingerprint),
            UInt32(fingerprint.count)
        )
    }
}
  1. Update Process
func updateFingerprint(after download: DiveData) {
    if let newFingerprint = download.fingerprint {
        fingerprintManager.saveFingerprint(newFingerprint)
    }
}

Storage Options

Local Storage

  1. UserDefaults
class LocalStorage {
    static let shared = LocalStorage()
    private let defaults = UserDefaults.standard
    
    func saveDiveCount(_ count: Int) {
        defaults.set(count, forKey: "diveCount")
    }
}
  1. File System
class FileManager {
    func saveDiveLog(_ dive: DiveData) throws {
        let encoder = JSONEncoder()
        let data = try encoder.encode(dive)
        try data.write(to: getLogURL(for: dive.id))
    }
}

Database Options

  1. Core Data Model
class CoreDataManager {
    static let shared = CoreDataManager()
    
    func saveDive(_ dive: DiveData) {
        let context = persistentContainer.viewContext
        let diveMO = DiveMO(context: context)
        // Populate managed object
        try? context.save()
    }
}
  1. SQLite Integration
class SQLiteManager {
    func createTables() {
        // Create necessary tables
    }
    
    func insertDive(_ dive: DiveData) {
        // Insert dive data
    }
}

Export Options

  1. JSON Export
func exportToJSON(_ dives: [DiveData]) throws -> Data {
    let encoder = JSONEncoder()
    encoder.dateEncodingStrategy = .iso8601
    return try encoder.encode(dives)
}
  1. CSV Export
func exportToCSV(_ dives: [DiveData]) -> String {
    // Generate CSV format
}

Data Migration

  1. Version Management
struct DataVersion {
    static let current = 1
    
    static func migrate(from oldVersion: Int) {
        // Perform migration steps
    }
}
  1. Backup System
class BackupManager {
    func createBackup() throws {
        // Create data backup
    }
    
    func restore(from backup: URL) throws {
        // Restore from backup
    }
}

Performance Optimization

  1. Caching
class CacheManager {
    private var cache = NSCache<NSString, DiveData>()
    
    func cacheDive(_ dive: DiveData) {
        cache.setObject(dive, forKey: dive.id.uuidString as NSString)
    }
}
  1. Batch Operations
func saveBatch(_ dives: [DiveData]) {
    // Perform batch save
}
  1. Compression
func compressData(_ data: Data) throws -> Data {
    // Implement compression
}