Created
September 27, 2017 18:13
-
-
Save raypendergraph/e58cb8606d6ec9b9cd61aa9094254ac9 to your computer and use it in GitHub Desktop.
Snippet to test motion activity data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let df = DateFormatter() | |
df.dateFormat = "yyyy-MM-dd HH:mm:ss Z" | |
let startDate = df.date(from: "2017-09-27 14:50:19 +0000")! | |
let endDate = df.date(from: "2017-09-27 15:14:28 +0000")! | |
let cmm = CMMotionActivityManager() | |
let lowestConfidence = CMMotionActivityConfidence.low | |
cmm.queryActivityStarting(from: startDate, to: endDate, to: OperationQueue.main) { | |
(activities, error) in | |
guard let activities = activities else { | |
return | |
} | |
print("------------------------") | |
print("Confidence levels are low:\(CMMotionActivityConfidence.low.rawValue) - high:\(CMMotionActivityConfidence.high.rawValue)") | |
print("Using confidence: \(lowestConfidence.rawValue)") | |
print("\(startDate) - \(endDate)") | |
print("start, time, confidence, stationary, unknown, walking, running, cycling, automotive") | |
var lastEventDate = endDate | |
var totalEventDate = endDate //for coalescing .unknown events for totals | |
var map = [ActivityType: TimeInterval]() | |
activities.reversed().forEach { | |
(a) in | |
guard a.confidence.rawValue >= lowestConfidence.rawValue else { return } | |
let eventDuration = lastEventDate.timeIntervalSince(a.startDate) | |
lastEventDate = a.startDate | |
let values = [String(describing: a.startDate), | |
String(describing: eventDuration), | |
String(describing: a.confidence.rawValue), | |
String(describing: a.stationary), | |
String(describing: a.unknown), | |
String(describing: a.walking), | |
String(describing: a.running), | |
String(describing: a.cycling), | |
String(describing: a.automotive)] | |
print(values.joined(separator: ", ")) | |
let type = ActivityType.from(motionActivity: a) | |
if type != .unknown { | |
let totalDuration = totalEventDate.timeIntervalSince(a.startDate) | |
totalEventDate = a.startDate | |
if let current = map[type] { | |
map[type] = current + totalDuration | |
} | |
else { | |
map[type] = totalDuration | |
} | |
} | |
} | |
print("Totals: \(map.debugDescription)") | |
print("------------------------") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment