-
Notifications
You must be signed in to change notification settings - Fork 1
/
cls_healthkit.py
379 lines (321 loc) · 11.3 KB
/
cls_healthkit.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from dataclasses import dataclass
from typing import Dict
HK_APPLE_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S %z"
HK_APPLE_DATE_FORMAT = "%Y-%m-%d"
HK_APPLE_TIMEZONE = '-0700'
@dataclass
class HKWorkout:
_workout_activity_type: str
_duration: float
_duration_unit: str
_total_distance: float
_total_distance_unit: str
_total_energy_burned: float
_total_energy_burned_unit: str
_source_name: str
_source_version: str
_device: str
_creation_date: str
_start_date: str
_end_date: str
@classmethod
def create(cls, attr: Dict[str, str]) -> 'HKWorkout':
return cls(
attr['workoutActivityType'],
float(attr.get('duration', 0.0)),
attr.get('durationUnit', ''),
float(attr.get('totalDistance', 0.0)),
attr.get('totalDistanceUnit', ''),
float(attr.get('totalEnergyBurned', 0.0)),
attr.get('totalEnergyBurnedUnit', ''),
attr['sourceName'],
attr.get('sourceVersion', ''),
attr.get('device', ''),
attr.get('creationDate', ''),
attr['startDate'],
attr['endDate']
)
@property
def workout_activity_type(self):
return self._workout_activity_type
@property
def duration(self):
return self._duration
@property
def duration_unit(self):
return self._duration_unit
@property
def total_distance(self):
return self._total_distance
@property
def total_distance_unit(self):
return self._total_distance_unit
@property
def total_energy_burned(self):
return self._total_energy_burned
@property
def total_energy_burned_unit(self):
return self._total_energy_burned_unit
@property
def source_name(self):
return self._source_name
@property
def source_version(self):
return self._source_version
@property
def device(self):
return self._device
@property
def creation_date(self):
return self._creation_date
@property
def start_date(self):
return self._start_date
@property
def end_date(self):
return self._end_date
class HKWorkoutWithMetaData(HKWorkout):
def __init__(self,
_workout_activity_type: str,
_duration: float,
_duration_unit: str,
_total_distance: float,
_total_distance_unit: str,
_total_energy_burned: float,
_total_energy_burned_unit: str,
_source_name: str,
_source_version: str,
_device: str,
_creation_date: str,
_start_date: str,
_end_date: str,
_is_indoor: bool,
_average_mets: str,
_weather_temperature: str,
_weather_humidity: str,
_timezone: str,
_elevation_ascended: str
):
super().__init__(_workout_activity_type,
_duration,
_duration_unit,
_total_distance,
_total_distance_unit,
_total_energy_burned,
_total_energy_burned_unit,
_source_name,
_source_version,
_device,
_creation_date,
_start_date,
_end_date
)
self._is_indoor = _is_indoor
self._average_mets = _average_mets
self._weather_temperature = _weather_temperature
self._weather_humidity = _weather_humidity
self._timezone = _timezone
self._elevation_ascended = _elevation_ascended
@classmethod
def create(cls, attr: Dict[str, str]) -> 'HKWorkoutWithMetaData':
return cls(
attr['workoutActivityType'],
float(attr.get('duration', 0.0)),
attr.get('durationUnit', ''),
float(attr.get('totalDistance', 0.0)),
attr.get('totalDistanceUnit', ''),
float(attr.get('totalEnergyBurned', 0.0)),
attr.get('totalEnergyBurnedUnit', ''),
attr['sourceName'],
attr.get('sourceVersion', ''),
attr.get('device', ''),
attr.get('creationDate', ''),
attr['startDate'],
attr['endDate'],
bool(int(attr['HKIndoorWorkout'])) if attr['HKIndoorWorkout'] != '' else False,
attr['HKAverageMETs'],
attr['HKWeatherTemperature'],
attr['HKWeatherHumidity'],
attr['HKTimeZone'],
attr['HKElevationAscended']
)
@property
def is_indoor(self):
return self._is_indoor
@property
def average_mets(self):
return self._average_mets
@property
def weather_temperature(self):
return self._weather_temperature
@property
def weather_humidity(self):
return self._weather_humidity
@property
def timezone(self):
return self._timezone
@property
def elevation_ascended(self):
return self._elevation_ascended
@dataclass
class HKRecord:
_type: str
_unit: str
_value: str
_source_name: str
_source_version: str
_device: str
_creation_date: str
_start_date: str
_end_date: str
@property
def type(self):
return self._type
@property
def unit(self):
return self._unit
@property
def value(self):
return self._value
@property
def source_name(self):
return self._source_name
@property
def source_version(self):
return self._source_version
@property
def device(self):
return self._device
@property
def creation_date(self):
return self._creation_date
@property
def start_date(self):
return self._start_date
@property
def end_date(self):
return self._end_date
class HKRecordQuantityTypeIdentifier(HKRecord):
def __init__(self,
record_type: str,
unit: str,
value: str,
source_name: str,
source_version: str,
device: str,
creation_date: str,
start_date: str,
end_date: str):
super().__init__(
record_type,
unit,
value,
source_name,
source_version,
device,
creation_date,
start_date,
end_date
)
self._value: float = float(self._value)
class HKRecordFactory:
_HKRecordTypes = {
'HKCategoryTypeIdentifierAppleStandHour': HKRecord,
'HKCategoryTypeIdentifierAudioExposureEvent': HKRecord,
'HKCategoryTypeIdentifierSleepAnalysis': HKRecord,
'HKQuantityTypeIdentifierActiveEnergyBurned': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierAppleExerciseTime': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierAppleStandTime': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierBasalEnergyBurned': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierBloodPressureDiastolic': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierBloodPressureSystolic': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierBodyFatPercentage': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierBodyMass': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierBodyMassIndex': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierDietaryCholesterol': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierDistanceWalkingRunning': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierEnvironmentalAudioExposure': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierFlightsClimbed': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierHeadphoneAudioExposure': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierHeartRate': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierHeartRateVariabilitySDNN': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierHeight': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierLeanBodyMass': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierRestingHeartRate': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierStepCount': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierVO2Max': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierWaistCircumference': HKRecordQuantityTypeIdentifier,
'HKQuantityTypeIdentifierWalkingHeartRateAverage': HKRecordQuantityTypeIdentifier
}
@staticmethod
def create(attr: Dict[str, str]) -> HKRecord:
try:
return HKRecordFactory._HKRecordTypes[attr['type']](
attr['type'],
attr.get('unit', ''),
attr.get('value', ''),
attr['sourceName'],
attr.get('sourceVersion', ''),
attr.get('device', ''),
attr.get('creationDate', ''),
attr['startDate'],
attr['endDate']
)
except KeyError:
raise ValueError(f"{type} is not supported.")
@dataclass
class HKActivitySummary:
_date_components: str
_active_energy_burned: float
_active_energy_burned_goal: float
_active_energy_burned_unit: str
_apple_move_minutes: float
_apple_move_minutes_goal: float
_apple_exercise_time: float
_apple_exercise_time_goal: float
_apple_stand_hours: int
_apple_stand_hours_goal: int
@classmethod
def create(cls, attr: Dict[str, str]) -> 'HKActivitySummary':
return cls(
attr.get('dateComponents', ''),
float(attr.get('activeEnergyBurned', 0.0)),
float(attr.get('activeEnergyBurnedGoal', 0.0)),
attr.get('activeEnergyBurnedUnit', ''),
float(attr.get('appleMoveMinutes', 0.0)),
float(attr.get('appleMoveMinutesGoal', 0.0)),
float(attr.get('appleExerciseTime', 0.0)),
float(attr.get('appleExerciseTimeGoal', 0.0)),
int(attr.get('appleStandHours', 0)),
int(attr.get('appleStandHoursGoal', 0))
)
@property
def date_components(self):
return self._date_components
@property
def active_energy_burned(self):
return self._active_energy_burned
@property
def active_energy_burned_goal(self):
return self._active_energy_burned_goal
@property
def active_energy_burned_unit(self):
return self._active_energy_burned_unit
@property
def apple_move_minutes(self):
return self._apple_move_minutes
@property
def apple_move_minutes_goal(self):
return self._apple_move_minutes_goal
@property
def apple_exercise_time(self):
return self._apple_exercise_time
@property
def apple_exercise_time_goal(self):
return self._apple_exercise_time_goal
@property
def apple_stand_hours(self):
return self._apple_stand_hours
@property
def apple_stand_hours_goal(self):
return self._apple_stand_hours_goal