Giter VIP home page Giter VIP logo

k-angama / motiontracking Goto Github PK

View Code? Open in Web Editor NEW
9.0 9.0 1.0 2.04 MB

The Application uses CoreMotion and LocationManager to collect the data and saves it to a file that is transferred to the iPhone using WatchConnectivity

License: MIT License

Swift 99.23% Ruby 0.77%
apple-watch combine-framework core-ml core-motion coremotion ios ios-app ios-swift location-manager mvvm-ios mvvm-pattern mvvm-swift swift watch-connectivity watchos

motiontracking's People

Contributors

k-angama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

vkurpad

motiontracking's Issues

CSVFileFields - Missing userAccelerationX,Y,Z fields and custom ordering

Hi,

Thank you for creating the project.

I noticed userAccelerationX, userAccelerationY and userAccelerationZ are missing in CSVFileManager.swift and then when they are added, timestamp is no longer the first column in the exported csv.

I have made the below changes if you would like to incorporate them, the code for the ordering is by ChatGPT.

//
//  CSVFileManager.swift
//  MotionTracking
//
//  Created by Karim Angama on 23/02/2022.
//

import Foundation
import SwiftCSVExport
import SSZipArchive

/**
 Entity CSV file fields
 
 */
struct CSVFileFields: Codable {
    
    let timestamp: Bool
    let gravityX: Bool
    let gravityY: Bool
    let gravityZ: Bool
    let userAccelerationX: Bool
    let userAccelerationY: Bool
    let userAccelerationZ: Bool
    let rotationRateX: Bool
    let rotationRateY: Bool
    let rotationRateZ: Bool
    let attitudeRoll: Bool
    let attitudePitch: Bool
    let attitudeYaw: Bool
    let locationLatitude: Bool
    let locationLongitude: Bool
    let locationAltitude: Bool
        
    var orderedFields: [(String, Bool)] {
        return [
            ("timestamp", timestamp),
            ("gravityX", gravityX),
            ("gravityY", gravityY),
            ("gravityZ", gravityZ),
            ("userAccelerationX", userAccelerationX),
            ("userAccelerationY", userAccelerationY),
            ("userAccelerationZ", userAccelerationZ),
            ("rotationRateX", rotationRateX),
            ("rotationRateY", rotationRateY),
            ("rotationRateZ", rotationRateZ),
            ("attitudeRoll", attitudeRoll),
            ("attitudePitch", attitudePitch),
            ("attitudeYaw", attitudeYaw),
            ("locationLatitude", locationLatitude),
            ("locationLongitude", locationLongitude),
            ("locationAltitude", locationAltitude)
        ]
    }

    init(timestamp: Bool = true,
         gravityX: Bool = true,
         gravityY: Bool = true,
         gravityZ: Bool = true,
         userAccelerationX: Bool = true,
         userAccelerationY: Bool = true,
         userAccelerationZ: Bool = true,
         rotationRateX: Bool = true,
         rotationRateY: Bool = true,
         rotationRateZ: Bool = true,
         attitudeRoll: Bool = true,
         attitudePitch: Bool = true,
         attitudeYaw: Bool = true,
         latitude: Bool = true,
         longitude: Bool = true,
         altitude: Bool = true
    ) {
        self.timestamp = timestamp
        self.gravityX = gravityX
        self.gravityY = gravityY
        self.gravityZ = gravityZ
        self.userAccelerationX = userAccelerationX
        self.userAccelerationY = userAccelerationY
        self.userAccelerationZ = userAccelerationZ
        self.rotationRateX = rotationRateX
        self.rotationRateY = rotationRateY
        self.rotationRateZ = rotationRateZ
        self.attitudeRoll = attitudeRoll
        self.attitudePitch = attitudePitch
        self.attitudeYaw = attitudeYaw
        self.locationLatitude = latitude
        self.locationLongitude = longitude
        self.locationAltitude = altitude
    }
}

/**
 CSV file manager
 
 */
class CSVFileManager {
    
    var fields = CSVFileFields()
    
    // MARK: Public method

    /**
        Set the fields to add in the csv file
     
        @param fields - entity
     */
    func setFiels(fields: CSVFileFields) {
        self.fields = fields
    }
    
    /**
        Create and save the several file csv
     
        @param block - The file url stored  in the application support directory
     */
    func save(fileUrl: URL, block: @escaping (_ zipPath: String?, _ error: Error?) -> Void) {
        let backgroundQ = DispatchQueue.global(qos: .default)
        let group = DispatchGroup()
        
        var errorSave: Error?
        var filePath: String?
        backgroundQ.async(group: group, execute: { [weak self] in
            do {
                filePath = try self?.save(fileUrl: fileUrl)
            } catch {
                errorSave = error
            }
        })
        
        group.notify(queue: DispatchQueue.main, execute: {
            block(filePath, errorSave)
        })
    }
    
    /**
        Create and save the several files csv
     
        @param block - The file url stored  in the application support directory
     */
    func save(filesUrl: [URL], block: @escaping (_ zipPath: String, _ error: Error?) -> Void) {
        var errorSave: Error?
        let backgroundQ = DispatchQueue.global(qos: .default)
        let group = DispatchGroup()
        
        let zipPath = tempZipPath()
        var filePath: [String] = []
        backgroundQ.async(group: group, execute: { [weak self] in
            for file in filesUrl {
                do {
                    guard let path = try self?.save(fileUrl: file) else { return }
                    filePath.append(path)
                } catch {
                    errorSave = error
                }
            }
        })
        group.notify(queue: DispatchQueue.main, execute: {
            SSZipArchive.createZipFile(atPath: zipPath, withFilesAtPaths: filePath)
            block(zipPath, errorSave)
        })
        
    }
    
    // MARK: Private method
    
    private func tempZipPath() -> String {
        var path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
        path += "/\(nameOfZipFile()).zip"
        return path
    }
    
    /**
        Create and save the file csv
     
        @param fileUrl - The file url stored  in the application support directory
     */
    private func save(fileUrl: URL) throws -> String {
        let json = try String(contentsOf: fileUrl)
        let fileName = fileUrl.deletingPathExtension().lastPathComponent
        let fields = getFields()
        return CSVExport.exportWithString(
            fileName,
            fields: fields,
            values: json
        ).filePath
        
    }
    
    /**
        Create and return the name file
     */
    private func nameOfZipFile() -> String {
        let date = Date().nowDateIso8601
        return "motionTracking_\(date)"
    }
    
    /**
     Return the array fields to add in the csv file
     
     @return   Array fields
     */
    private func getFields() -> [String] {
        return fields.orderedFields.compactMap { $0.1 ? $0.0 : nil }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.