forked from dvmarinoff/Auuki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.js
175 lines (156 loc) · 5.04 KB
/
controllers.js
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
import { xf } from './xf.js';
import { FileHandler } from './file.js';
import { workouts } from './workouts/workouts.js';
import { zwo, intervalsToGraph } from './workouts/parser.js';
import { RecordedData, RecordedLaps } from './test/mock.js';
function DeviceController(args) {
const controllable = args.controllable;
const hrb = args.hrb;
const powerMeter = args.powerMeter;
const antFec = args.antFec;
const antHrm = args.antHrm;
let watch = args.watch;
let mode = 'erg';
xf.sub('db:mode', m => { mode = m; });
xf.sub('db:powerTarget', power => {
if(mode === 'erg') {
console.log();
if(controllable.device.connected) {
controllable.setPowerTarget(power);
}
if(antFec.connected) {
antFec.setPowerTarget(power);
}
}
});
xf.sub('db:resistanceTarget', target => {
let resistance = target;
resistance = parseInt(resistance);
if(controllable.device.connected) {
controllable.setResistanceTarget(resistance);
}
if(antFec.connected) {
antFec.setResistanceTarget(resistance);;
}
});
xf.sub('db:slopeTarget', target => {
let slope = target;
slope *= 100;
slope = parseInt(slope);
if(controllable.device.connected) {
controllable.setSlopeTarget({grade: slope});
}
if(antFec.connected) {
antFec.setSlopeTarget({grade: slope});
}
});
xf.sub('ui:workoutStart', e => { watch.startWorkout(); });
xf.sub('ui:watchStart', e => { watch.start(); });
xf.sub('workout:restore', e => { watch.restoreWorkout(); });
xf.sub('ui:watchPause', e => { watch.pause(); });
xf.sub('ui:watchResume', e => { watch.resume(); });
xf.sub('ui:watchLap', e => { watch.lap(); });
xf.sub('ui:watchStop', e => {
const stop = confirm('Confirm Stop?');
if(stop) {
watch.stop();
}
});
xf.sub('ui:controllable:switch', e => {
if(controllable.device.connected) {
controllable.disconnect();
} else {
controllable.connect();
}
});
xf.sub('ui:hrb:switch', e => {
if(hrb.device.connected) {
hrb.disconnect();
} else {
hrb.connect();
}
});
xf.sub('ui:pm:switch', e => {
if(powerMeter.isConnected()) {
powerMeter.disconnect();
} else {
powerMeter.connect();
}
});
xf.sub('ui:antHrm:switch', e => {
if(antHrm.connected) {
antHrm.disconnect();
} else {
antHrm.connect();
}
});
xf.sub('ui:antFec:switch', e => {
if(antFec.connected) {
antFec.disconnect();
} else {
antFec.connect();
}
});
}
function FileController() {
xf.sub('db:workoutFile', workoutFile => {
let fileHandler = new FileHandler();
fileHandler.readFile(workoutFile);
});
}
function WorkoutController() {
let ftp = 80;
let index = 0;
let workout = {};
xf.reg('db:ftp', e => {
ftp = e.ftp;
xf.dispatch('workouts:init', workouts); // ??
xf.dispatch('ui:workout:set', 0); // ??
});
xf.reg('file:upload:workout', e => {
let graph = ``;
let xml = e;
let workout = zwo.parse(xml);
workout.intervals.forEach( interval => {
interval.steps.forEach( step => {
step.power = Math.round(ftp * step.power);
});
});
workout.id = index;
if(workout.name === '' || workout.name === undefined) {
workout.name = `Custom ${index}`;
}
if(workout.type === '' || workout.type === undefined) {
workout.type = 'Custom';
}
if(workout.description === '' || workout.description === undefined) {
workout.description = 'Custom workout';
}
workout.xml = xml;
workout.graph = intervalsToGraph(workout.intervals, ftp);
xf.dispatch('workout:add', workout);
index += 1;
});
xf.reg('workouts:init', e => {
let workoutFiles = e;
workoutFiles.forEach( w => {
let workout = zwo.parse(w.xml);
workout.intervals.forEach( interval => {
interval.steps.forEach( step => {
if(step.power >= 10) {
step.power = step.power; // abs power
} else {
step.power = Math.round(ftp * step.power); // % FTP power
}
});
});
let graph = intervalsToGraph(workout.intervals, ftp);
w.intervals = workout.intervals;
w.id = index;
w.graph = graph;
xf.dispatch('workout:add', w);
index += 1;
});
});
}
export { DeviceController, FileController, WorkoutController };