forked from C3-PRO/c3-pro-ios-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreMotionReporter.swift
334 lines (285 loc) · 12.1 KB
/
CoreMotionReporter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// CoreMotionReporter.swift
// C3PRO
//
// Created by Pascal Pfiffner on 24/05/16.
// Copyright © 2016 University Hospital Zurich. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import CoreMotion
import SMART
import SQLite
/**
Dumps latest activity data from CoreMotion to a SQLite database and returns previously archived activity data.
See [HealthKit/README.md](https://github.com/C3-PRO/c3-pro-ios-framework/tree/master/Sources/HealthKit#core-motion-data-persistence) for detailed instructions.
*/
open class CoreMotionReporter: ActivityReporter {
/// The filesystem path to the database.
open let databaseLocation: String
lazy var motionManager = CMMotionActivityManager()
/**
Designated initializer.
- parameter path: The filesystem path to the SQLite database
*/
public init(path: String) {
databaseLocation = path
}
// MARK: - SQLite
/**
Returns the SQLite connection object to use.
*/
func connection() throws -> Connection {
#if false
let fm = FileManager()
if let attrs = try? fm.attributesOfItem(atPath: databaseLocation) as NSDictionary {
let size = attrs.fileSize()
c3_logIfDebug("REPORTER database is \(size / 1024) KB")
}
#endif
return try Connection(databaseLocation)
}
// MARK: - Archiving
fileprivate var lastArchival: Date?
/**
Archive all available activities (that happened since the last sampling) to our SQLite database.
The data store is lossless, meaning that all aspects of the activities are preserved. The db format has been kept as compact as
possible, using NSDate's `timeIntervalSinceReferenceDate` to store the date which also serves as primary key. In my setup, iPhone 6S
plus Apple Watch, this amounts to 420 KB of data for a week.
- parameter processor: A `CoreMotionActivityInterpreter` instance to handle CMMotionActivity processing (not interpretation!) before the
callback returns; uses an `CoreMotionStandardActivityInterpreter` instance if none is provided
- parameter callback: The callback to call when done, with an error if something happened, nil otherwise. Called on the main queue
*/
open func archiveActivities(processor: CoreMotionActivityInterpreter? = nil, callback: @escaping ((_ numNewActivities: Int, _ error: Error?) -> Void)) {
if let lastArchival = lastArchival, lastArchival.timeIntervalSinceNow > -30 {
callback(0, nil)
return
}
do {
let db = try connection()
let activities = Table("activities")
let start = Expression<Double>("start") // Start in seconds since NSDate reference date (1/1/2001)
let activity = Expression<Int>("activity") // bitmask over MotionActivityType
let confidence = Expression<Int>("confidence") // 0 = low, 1 = medium, 2 = high
// create table if needed
_ = try db.run(activities.create(ifNotExists: true) { t in
t.column(start, unique: true)
t.column(activity)
t.column(confidence)
})
// grab latest startDate
let now = Date()
var latest: Date?
let query = activities.select(start).order(start.desc).limit(1)
for row in try db.prepare(query) {
latest = Date(timeIntervalSinceReferenceDate: row[start])
}
if let latest = latest, (now.timeIntervalSinceReferenceDate - latest.timeIntervalSinceReferenceDate) < 2*60 {
c3_logIfDebug("Latest activity was sampled \(latest), not archiving again")
c3_performOnMainQueue() {
callback(0, nil)
}
return
}
// collect activities and store to database
let processor = processor ?? CoreMotionStandardActivityInterpreter()
collectCoreMotionActivities(startingOn: latest, processor: processor) { samples, collError in
if let error = collError {
callback(0, error)
return
}
if 0 == samples.count {
callback(0, nil)
return
}
// insert into database
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
do {
try db.transaction() {
print("\(Date()) ARCHIVER inserting \(samples.count) samples")
for sample in samples {
_ = try db.run(activities.insert(or: .ignore, // UNIQUE constraint on `start` may fail, which we want to ignore
start <- round(sample.startDate.timeIntervalSinceReferenceDate * 10) / 10,
activity <- sample.type.rawValue,
confidence <- sample.confidence.rawValue))
}
print("\(Date()) ARCHIVER done inserting")
}
self.lastArchival = Date()
c3_performOnMainQueue() {
callback(samples.count, nil)
}
}
catch let error {
c3_performOnMainQueue() {
callback(0, error)
}
}
}
}
}
catch let error {
c3_performOnMainQueue() {
callback(0, error)
}
}
}
/**
Sample CMMotionActivityManager for activities from the given start date up until now. If no start date is given, starts sampling 15 days
back, which is useless since there's at max 7 days of activity data available (as of iOS 9). But who knows, maybe it gets bumped up one
day.
There is a bit of processing happening, rather than raw instances being returned, in the receiver's `preprocess(activities:)`
implementation.
- parameter startingOn: The Date at which to start sampling, up until now
- parameter processor: A `CoreMotionActivityInterpreter` instance to handle CMMotionActivity preprocessing (not interpretation!) before
the callback returns
- parameter callback: The callback to call when sampling completes. Will execute on the main queue
*/
func collectCoreMotionActivities(startingOn start: Date?, processor: CoreMotionActivityInterpreter, callback: @escaping (([CoreMotionActivity], Error?) -> Void)) {
let collectorQueue = OperationQueue()
var begin = start ?? Date()
if nil == start {
begin = begin.addingTimeInterval(-15*24*3600) // there's at most 7 days of activity available. Be conservative and use 15 days.
}
motionManager.queryActivityStarting(from: begin, to: Date(), to: collectorQueue) { activities, error in
if let activities = activities {
let samples = processor.preprocess(activities: activities)
c3_performOnMainQueue() {
callback(samples, nil)
}
}
else if let error = error, CMErrorDomain != error._domain && 104 != error._code { // CMErrorDomain error 104 means "no data available"
c3_logIfDebug("No activity data received with error: \(error)")
c3_performOnMainQueue() {
callback([], error)
}
}
else {
c3_logIfDebug("No activity data received")
c3_performOnMainQueue() {
callback([], nil)
}
}
}
}
// MARK: - Retrieval
open func reportForActivityPeriod(startingAt start: Date, until: Date, callback: @escaping ((_ period: ActivityReportPeriod?, _ error: Error?) -> Void)) {
reportForActivityPeriod(startingAt: start, until: until, interpreter: nil, callback: callback)
}
/**
Retrieves activities performed between two given dates and runs an interpreter over the results which may process the activities in a
certain way.
- parameter startingAt: The start date
- parameter until: The end date; uses "now" if nil
- parameter interpreter: The interpreter to use; uses a fresh instance of `CoreMotionStandardActivityInterpreter` if nil
- parameter callback: The callback to call when all activities are retrieved and the interpreter has run
*/
open func reportForActivityPeriod(startingAt start: Date, until: Date? = nil, interpreter: CoreMotionActivityInterpreter? = nil, callback: @escaping ((_ report: ActivityReportPeriod?, _ error: Error?) -> Void)) {
archiveActivities() { newActivities, error in
if let error = error {
c3_logIfDebug("Ignoring error when archiving most recent activities before retrieving: \(error)")
}
let endDate = until ?? Date()
// dispatch to background queue and call back on the main queue
DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async() {
do {
let interpreter = interpreter ?? CoreMotionStandardActivityInterpreter()
let activities = try self.retrieveActivities(startingAt: start, until: endDate, interpreter: interpreter)
let report = self.report(forActivities: activities)
if nil == report.period.start {
report.period.start = start.fhir_asDateTime()
}
if nil == report.period.end {
report.period.end = until?.fhir_asDateTime()
}
c3_performOnMainQueue() {
callback(report, nil)
}
}
catch let error {
c3_performOnMainQueue() {
callback(nil, error)
}
}
}
}
}
/**
Internal method that connects to the SQLite database, retrieves activities between the two dates, instantiates
`InterpretedCoreMotionActivity` for all of them and lets the interpreter do its work.
- parameter startingAt: The start date
- parameter until: The end date; uses "now" if nil
- parameter interpreter: The interpreter to use; uses a fresh instance of `CoreMotionStandardActivityInterpreter` if nil
*/
func retrieveActivities(startingAt start: Date, until: Date, interpreter: CoreMotionActivityInterpreter) throws -> [InterpretedCoreMotionActivity] {
let db = try connection()
let activitiesTable = Table("activities")
let startCol = Expression<Double>("start")
let activityCol = Expression<Int>("activity")
let confidenceCol = Expression<Int>("confidence")
let startTime = start.timeIntervalSinceReferenceDate
let endTime = until.timeIntervalSinceReferenceDate
// query database
let filtered = activitiesTable.filter(startCol >= startTime).filter(startCol <= endTime) // */
var collected = [InterpretedCoreMotionActivity]()
for row in try db.prepare(filtered) {
let activity = InterpretedCoreMotionActivity(start: row[startCol], activity: row[activityCol], confidence: row[confidenceCol], end: 0.0)
if let prev = collected.last {
prev.endDate = activity.startDate
}
collected.append(activity)
}
if let last = collected.last {
last.endDate = until
}
// run interpreter and return
return interpreter.interpret(activities: collected)
}
// MARK: - Reporting
/**
Takes an array of activities and sums up all activities per type to generate an activity report for the period spanned by the
activities.
- parameter forActivities: The activites to aggregate into a report
- returns: An ActivityReportPeriod instance for the period defined by all individual activities
*/
func report(forActivities activities: [InterpretedCoreMotionActivity]) -> ActivityReportPeriod {
let calendar = NSCalendar.current
var earliest: Date?
var latest: Date?
// aggregate seconds
var periods = [CoreMotionActivityInterpretation: Int]()
for activity in activities {
earliest = (nil == earliest || activity.startDate.compare(earliest!) == .orderedAscending) ? activity.startDate as Date : earliest
latest = (nil == latest || activity.endDate.compare(latest!) == .orderedDescending) ? activity.endDate as Date : latest
let baseSecs = periods[activity.interpretation] ?? 0
let newSecs = calendar.dateComponents([.second], from: activity.startDate, to: activity.endDate).second ?? 0
periods[activity.interpretation] = baseSecs + newSecs
}
// instantiate durations
var durations = [CoreMotionActivitySum]()
for (type, secs) in periods {
let minutes = Duration()
minutes.value = FHIRDecimal("\(secs / 60)")
minutes.unit = FHIRString("minute")
let duration = CoreMotionActivitySum(type: type, duration: minutes)
durations.append(duration)
}
let period = Period()
period.start = earliest?.fhir_asDateTime()
period.end = latest?.fhir_asDateTime()
let data = ActivityReportPeriod(period: period)
data.coreMotionActivities = durations
return data
}
}