-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
224 lines (189 loc) · 6.95 KB
/
index.ts
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
import * as messaging from 'messaging';
import { settingsStorage } from 'settings';
import { localStorage } from 'local-storage';
import * as fs from 'fs';
import { me } from 'companion';
import { locale } from 'user-settings';
import { log, addDays, getLastStartOfWeek, sameDay } from './../common/util.js';
import { WEB_APP, WAKE_INTERVAL, DEFAULT_START_DAY_OF_WEEK, ACTIVITY_TYPES } from './constants';
import { msToMin, queryParams, formatDate, normalize, stripQuotes } from './util';
import { getActivityLogList, getProfile, refreshToken, getTokens, setTokens } from './fitbitWebApi.ts';
import { enqueue } from './fileTransfer';
interface Activity {
readonly activityName: string;
readonly activeDuration: number;
readonly distance: number;
readonly originalStartTime: string;
readonly pace: number;
readonly steps: number;
readonly averageHeartRate: number;
};
interface SettingsEvent {
key: string;
oldValue: string;
newValue: string;
};
interface Token {
readonly timestamp: number;
readonly value: string;
};
interface User {
readonly startDayOfWeek: string;
}
interface WebState {
readonly barChart: number[];
readonly distRanNum: number;
readonly lastRunHeartRate: number;
readonly lastRunCadence: number;
readonly lastRunTime: string;
}
declare const Promise: any;
function reduceActivities(activities: Activity[]) {
return new Promise((resolve: any, reject: any) => {
log('process runs method');
log('activities length ' + activities.length);
let runs = activities.filter(activity => (ACTIVITY_TYPES.indexOf(activity.activityName) > -1));
log(runs.length);
if (!runs.length) {
const run: Activity = {
activityName: 'Run',
activeDuration: 0,
originalStartTime: '',
distance: 0,
averageHeartRate: 0,
steps: 0,
pace: 0
};
runs = [run];
}
const sRuns = runs.map(run => {
return {
activeDuration: run.activeDuration,
date: new Date(run.originalStartTime),
distance: run.distance,
pace: run.pace,
steps: run.steps,
averageHeartRate: run.averageHeartRate || 0
}
});
log('START DAY OF WEEK ' + startDayOfWeek);
const afterDate = getLastStartOfWeek(new Date(), startDayOfWeek);
//TODO don't bother w future dates
const day0 = sRuns.filter(run => sameDay(run.date, afterDate));
const day1 = sRuns.filter(run => sameDay(run.date, addDays(afterDate, 1)));
const day2 = sRuns.filter(run => sameDay(run.date, addDays(afterDate, 2)));
const day3 = sRuns.filter(run => sameDay(run.date, addDays(afterDate, 3)));
const day4 = sRuns.filter(run => sameDay(run.date, addDays(afterDate, 4)));
const day5 = sRuns.filter(run => sameDay(run.date, addDays(afterDate, 5)));
const day6 = sRuns.filter(run => sameDay(run.date, addDays(afterDate, 6)));
const days = [day0, day1, day2, day3, day4, day5, day6];
const graph: number[] = [];
days.forEach(day => {
const distRanDay = (day.reduce((memo, sRun) => memo + sRun.distance, 0));
graph.push(distRanDay);
});
const min = Math.min.apply(null, graph);
const max = Math.max.apply(null, graph);
//TODO
const barChart = graph.map(normalize(min, max));
const distRanNum = Number(sRuns.reduce((memo, sRun) => memo + sRun.distance, 0).toFixed(1));
const latestRunMinutes = msToMin(sRuns[0].activeDuration);
//const lastRunPace = sRuns[0].pace; //Pace is seconds to run 1km.
const lastRunCadence = Math.floor(sRuns[0].steps / latestRunMinutes);
const lastRunHeartRate = sRuns[0].averageHeartRate;
const noRunsLastRunTime = 'GO RUNNING';
const hasRunsLastRunTime = `LAST RUN ${sRuns[0].date.toLocaleString(locale.language.replace('_', '-'), {weekday: 'short', hour: 'numeric'}).toUpperCase()}`;
const lastRunTime = (sRuns[0].distance) ? hasRunsLastRunTime : noRunsLastRunTime;
const webState: WebState = {barChart, distRanNum, lastRunHeartRate, lastRunCadence, lastRunTime};
resolve(webState);
});
}
function tryToFetchData() {
log('trying to fetch activity data');
const tokens = getTokens();
log('stamped tokens');
log(JSON.stringify(tokens));
const now = new Date().valueOf();
if (tokens.stampedAccessToken || tokens.stampedRefreshToken) {
if (
(tokens.stampedAccessToken && tokens.stampedAccessToken.timestamp + WEB_APP.lifespan < now) ||
(tokens.stampedRefreshToken && !tokens.stampedAccessToken)
) {
//access token is too old, use the refresh token
log('too old, trying to refresh');
refreshToken().then(() => tryToFetchData() );
} else {
useAccessToken();
}
} else {
enqueue({lastRunTime: 'CONNECT SETTINGS'});
}
}
function useAccessToken() {
getProfile()
.then( (user: User) => {
startDayOfWeek = (user.startDayOfWeek === 'SUNDAY') ? 0 : 1;
log('start day ' + startDayOfWeek);
const params = {
afterDate: formatDate(getLastStartOfWeek(new Date(), startDayOfWeek)),
sort: 'desc',
offset: 0,
limit: 100
};
return getActivityLogList(params);
})
.then( (activities: Activity[]) => reduceActivities(activities))
.then( (state: WebState) => enqueue(state))
.catch((err: any) => {
log(`DEBUG: CATCH ON FETCHING`);
log(JSON.stringify(err));
if (err === 'expired_token') {
const tokens = getTokens();
localStorage.removeItem('stampedAccessToken');
log('err is expired token');
if (tokens.stampedRefreshToken.value) {
refreshToken()
.then((response: any) => {
log('then on refresh token');
tryToFetchData();
})
.catch((err: any) => {
log(`CAUGHT REFRESH ERR ${err}`);
//failed to refresh... now what?
enqueue({lastRunTime: 'RECONNECT'});
});
}
}
if (err) {
}
});
}
// Import the Companion module
let startDayOfWeek = DEFAULT_START_DAY_OF_WEEK;
me.wakeInterval = WAKE_INTERVAL;
log(JSON.stringify(me.launchReasons));
tryToFetchData();
settingsStorage.onchange = (evt: SettingsEvent) => {
log('companion: settings onchange');
//log('FROM ONCHANGE');
//Might be a bad idea
//if (evt.key && evt.key === '__oauth') evt.key = 'oauth';
if (evt.key === 'color') {
const settingsState = {
color: stripQuotes(evt.newValue)
};
enqueue(settingsState);
} else if (evt.key === 'tokens') {
const oauthResponse = (evt.newValue) ? JSON.parse(evt.newValue) : (evt.oldValue) ? JSON.parse(evt.oldValue) : null;
//const oauthResponse = (evt.newValue) ? JSON.parse(evt.newValue) : null;
log('OAUTH RESPONSE');
log(JSON.stringify(oauthResponse));
if (oauthResponse && oauthResponse.access_token) {
const tokens = setTokens(oauthResponse);
useAccessToken();
}
} else {
log('unexpected key: ');
log(JSON.stringify(evt))
}
};