-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgettingFitData.gs
More file actions
192 lines (171 loc) · 5.91 KB
/
gettingFitData.gs
File metadata and controls
192 lines (171 loc) · 5.91 KB
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
/*
UrlFetchApp
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
payload = request body
*/
/*
there is a sleep section just i can't seem to get data from it
derived:com.google.activity.segment:com.urbandroid.sleep:session_activity_segment
*/
var syncTopicConst = {
"estimatedSteps":{
"requestInfo":{
"sourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
"duration": 86400000
},
"header": ["start","stop","estimated steps"]
},
"weight":{
"requestInfo":{
"sourceId": "derived:com.google.weight:com.google.android.gms:merge_weight",
"duration": 86400000
},
"header": ["start","stop","average - weight","max - weight","min - weight"]
},
"calories":{
"requestInfo":{
"sourceId": "derived:com.google.calories.expended:com.google.android.gms:merge_calories_expended",
"duration": 86400000
},
"header": ["start","stop","calories - kCal"]
},
"heartRate":{
"requestInfo":{
"sourceId": "derived:com.google.heart_rate.bpm:com.google.android.gms:merge_heart_rate_bpm",
"duration": 300000 // 5 minutes
},
"header": ["start","stop","average - BPM","max - BPM","min - BPM"],
},
"activitys":{
"requestInfo":{
"duration": 120000 // 20 minutes
},
"header": ["start","stop","activitys"]
}
}
var dataTypeValues = {
"com.google.step_count.delta": ["intVal"],
"com.google.calories.expended": ["fpVal"],
"com.google.heart_rate.summary": ["fpVal","fpVal","fpVal"],
"com.google.weight.summary": ["fpVal","fpVal","fpVal"],
"com.google.calories.expended": ["fpVal"]
}
function getHeadersData(syncTopic){
return syncTopicConst[syncTopic].header;
}
function getEarliestNextCall(syncTopic){
return syncTopicConst[syncTopic].requestInfo.duration;
}
function getFitData(startingDate, endingDate, syncTopic) {
var originalData;
if(syncTopic != "activitys"){
originalData = getAggregateData(startingDate, endingDate, syncTopic);
}
else{
originalData = getSessionData(startingDate, endingDate, syncTopic)
}
return originalData;
}
function getAggregateData(startingDate, endingDate, syncTopic){
var fitService = getOAuthService()
//Logger.log(dictonarySyncTopic[syncTopic])
Logger.log(syncTopic);
var response = UrlFetchApp.fetch('https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate', {
'method' : 'post',
'contentType': 'application/json',
'headers': {
Authorization: 'Bearer ' + fitService.getAccessToken()
},
'payload': JSON.stringify({
"aggregateBy": [{
"dataSourceId": syncTopicConst[syncTopic]["requestInfo"].sourceId
}],
"bucketByTime": { "durationMillis": syncTopicConst[syncTopic]["requestInfo"].duration },
"startTimeMillis": startingDate.getTime(),
"endTimeMillis": endingDate.getTime()
})
});
var allData = JSON.parse(response.getContentText())["bucket"]
allData = allData.filter(function(bucket){
return bucket["dataset"][0]["point"].length != 0;
}).map(function(bucket){
return parseBucketSection(bucket["dataset"][0]["point"][0]);
})
return allData;
}
function getSessionData(startingDate, endingDate, syncTopic){
//
var fitService = getOAuthService()
//' +'?startTime='+ startingDate.toISOString() + '&endTime=' + endingDate.toISOString()
//var response = UrlFetchApp.getRequest('https://www.googleapis.com/fitness/v1/users/me/sessions'); // ?startTime='+ startingDate.toISOString() + '&endTime=' + endingDate.toISOString()
var response = UrlFetchApp.fetch('https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=' + startingDate.toISOString() + '&endTime=' + endingDate.toISOString()
, {
'method' : 'get',
'headers': {
Authorization: 'Bearer ' + fitService.getAccessToken()
}
});
var allData = JSON.parse(response.getContentText())["session"].map(function(bucketSection){
return parseBucketSection(bucketSection);
})
//Logger.log(allData)
allData.sort(compare)
return allData;
}
function compare(a,b) {
var tmpA = new Date(a[0]);
var tmpB = new Date(b[0])
if (tmpA < tmpB)
return -1;
if (tmpA > tmpB)
return 1;
return 0;
}
function parseBucketSection(bucketSection){
var returnArray= []
if(bucketSection.startTimeNanos != undefined){
returnArray.push(convertFitnessDataTypes(bucketSection.startTimeNanos,"dateNano"));
returnArray.push(convertFitnessDataTypes(bucketSection.endTimeNanos, "dateNano"))
}
else{
returnArray.push(convertFitnessDataTypes(bucketSection.startTimeMillis,"dateMilli"));
returnArray.push(convertFitnessDataTypes(bucketSection.endTimeMillis, "dateMilli"))
}
if(bucketSection["activityType"] != undefined){
returnArray.push(convertFitnessDataTypes(bucketSection["activityType"], "activitys"));
}
else if(bucketSection["value"] != undefined){
var specificDataTypesValues = dataTypeValues[bucketSection["dataTypeName"]];
if(bucketSection["value"].length == specificDataTypesValues.length ){
for(var i = 0; i < bucketSection["value"].length; i++){
returnArray.push(convertFitnessDataTypes(bucketSection["value"][i][specificDataTypesValues[i]], specificDataTypesValues[i]))
}
}
}
else{
throw "something wen't wrong with the data"
}
return returnArray;
}
//valueData = bucketSection["dataset"][0]["point"][0]["value"][0][syncTopicTypeValue[syncTopic]];
function convertFitnessDataTypes(value,type){
returnValue = undefined;
switch(type){
case "dateNano":
returnValue = convertNanosecondsToDate(parseInt(value)).toString();
break;
case "dateMilli":
returnValue = convertMillisecondsToDate(parseInt(value)).toString();
break;
case "intVal":
case "fpVal":
returnValue = Number(value);
break;
case "activitys":
returnValue = convertActivityToName(value);
break;
default:
throw "Error currently unsupported type"
}
return returnValue;
}