k-angama / motiontracking Goto Github PK
View Code? Open in Web Editor NEWThe 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
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
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 }
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.