-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreadmill.html
More file actions
189 lines (174 loc) · 6.93 KB
/
Copy pathtreadmill.html
File metadata and controls
189 lines (174 loc) · 6.93 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Treadmill Controller</title>
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="font-mono flex justify-center bg-black text-white">
<div x-data="treadmillController()" class="p-10 max-w-4xl w-full">
<h2
class="mt-10 bg-gradient-to-r from-green-500 to-red-500 bg-clip-text text-transparent text-2xl md:text-4xl font-extrabold text-center"
>
Treadmill Controller 🏃
</h2>
<!-- LIVE DATA -->
<section class="text-center">
<h2 class="mt-10 text-xl font-bold">Live Data</h2>
<div class="p-3 grid sm:grid-flow-dense gap-2 text-center">
<p class="bg-gray-500 text-sm rounded-lg p-4">
Speed (km/h)<br /><span class="font-extrabold text-2xl" x-text="data.speed"></span>
</p>
<p class="sm:col-span-2 bg-gray-500 text-sm rounded-lg p-4">
Distance (m)<br /><span class="font-extrabold text-2xl" x-text="data.distance"></span>
</p>
<p class="bg-gray-500 text-sm rounded-lg p-4">
Time<br /><span class="font-extrabold text-2xl" x-text="data.time"></span>
</p>
<button class="bg-gray-500 text-sm rounded-lg p-4" @click="stepsOpen = !stepsOpen">
Steps<br /><span class="font-extrabold text-2xl" x-text="data.steps"></span>
</button>
<p class="bg-gray-500 text-sm rounded-lg p-4">
Calories<br /><span class="font-extrabold text-2xl" x-text="data.calories"></span>
</p>
</div>
</section>
<div
class="text-center"
x-show="stepsOpen"
x-transition.duration.1000ms
x-transition:leave.delay.120ms
x-transition.delay.80ms
>
<input
x-model="height"
type="text"
placeholder="Height cm"
class="bg-gray-50 text-gray-900 text-sm rounded-full p-2.5 text-center"
/>
<button @click="sendHeight" class="py-2.5 mt-2 px-6 rounded-full font-extrabold bg-gray-400">></button>
</div>
<!-- CONTROLS -->
<section class="text-center mt-5">
<h2 class="text-xl font-bold">Control</h2>
<div class="grid md:grid-cols-2 gap-4 p-4">
<button @click="stopTreadmill" class="py-3 px-8 m-1 rounded-lg font-extrabold bg-red-600 hover:bg-red-700">
Stop
</button>
<button
@click="startTreadmill"
class="py-3 px-8 m-1 rounded-lg font-extrabold bg-green-600 hover:bg-green-700"
>
Start
</button>
</div>
<h2 class="text-xl font-bold">Speed</h2>
<div class="text-center mt-5">
<button @click="decreaseSpeed" class="py-3 px-8 m-1 rounded-lg font-extrabold bg-red-600 hover:bg-red-700">
-
</button>
<button @click="increaseSpeed" class="py-3 px-8 m-1 rounded-lg font-extrabold bg-green-600">+</button>
</div>
</section>
<!-- WORKPUTS -->
<section class="text-center mt-10" x-show="Object.keys(selectedWorkoutData).length" x-transition>
<h2 class="text-xl font-bold">Select a Workout</h2>
<select class="bg-gray-600 p-2 rounded" x-model="selectedWorkout" @change="updateWorkoutPlan">
<template x-for="(workout, index) in workouts" :key="index">
<option :value="index" x-text="workout.name"></option>
</template>
</select>
<div class="p-6">
<button @click="startWorkout" class="py-3 px-8 m-1 rounded-lg font-extrabold bg-green-600 hover:bg-green-700">
Start Workout
</button>
</div>
<h2 class="text-xl font-bold mt-5">Workout Plan</h2>
<table class="table-auto w-full border-collapse border border-gray-600 mt-4">
<thead>
<tr class="bg-gray-700">
<th class="border border-gray-600 px-4 py-2">Speed (m/s)</th>
<th class="border border-gray-600 px-4 py-2">Duration (mins)</th>
</tr>
</thead>
<tbody>
<template x-for="(segment, index) in selectedWorkoutData" :key="index">
<tr class="bg-gray-800">
<td class="border border-gray-600 px-4 py-2" x-text="segment[0]"></td>
<td class="border border-gray-600 px-4 py-2" x-text="segment[1] / 60"></td>
</tr>
</template>
</tbody>
</table>
</section>
</div>
<script>
function treadmillController() {
return {
data: { speed: "-", distance: "-", steps: "-", calories: "-", time: "-" },
workouts: [],
selectedWorkout: 0,
selectedWorkoutData: {},
height: null,
stepsOpen: false,
ws: null,
init() {
this.fetchWorkouts();
this.setupWebSocket();
},
setupWebSocket() {
this.ws = new WebSocket("ws://localhost:8000/ws");
this.ws.onmessage = (event) => {
let newData = JSON.parse(event.data);
this.data.speed = newData.speed;
this.data.time = newData.time;
this.data.distance = newData.distance;
this.data.steps = newData.steps;
this.data.calories = newData.calories;
};
},
fetchWorkouts() {
fetch("http://localhost:8000/workouts")
.then((response) => response.json())
.then((data) => {
this.workouts = data;
this.updateWorkoutPlan();
})
.catch((error) => console.error("Error fetching workouts:", error));
},
updateWorkoutPlan() {
this.selectedWorkoutData = this.workouts[this.selectedWorkout]?.plan || {};
},
sendHeight() {
this.stepsOpen = false;
this.sendCommand(`height?value=${this.height}`);
},
sendCommand(endpoint, body = {}) {
fetch(`http://localhost:8000/${endpoint}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
},
startTreadmill() {
this.sendCommand("start");
},
stopTreadmill() {
this.sendCommand("stop");
},
startWorkout() {
this.sendCommand("start-workout", { name: this.workouts[this.selectedWorkout]?.name });
},
increaseSpeed() {
this.sendCommand(`speed?value=${parseFloat(this.data.speed) + 0.1}`);
},
decreaseSpeed() {
this.sendCommand(`speed?value=${parseFloat(this.data.speed) - 0.1}`);
},
};
}
</script>
</body>
</html>