Add ability to connect to hear rate sensor and post data back to server on workout complete, currently not rendering graphing data

This commit is contained in:
Peter Stockings
2023-05-07 18:15:31 +10:00
parent 5f1a5e6a4a
commit 43e5f66cc1
3 changed files with 138 additions and 6 deletions

25
app.py
View File

@@ -68,6 +68,15 @@ class CadenceReading(db.Model):
power = db.Column(db.Integer, nullable=False)
class HeartRateReading(db.Model):
__tablename__ = 'heartrate_readings'
id = db.Column(db.Integer, primary_key=True)
workout_id = db.Column(db.Integer, db.ForeignKey(
'workouts.id', ondelete='CASCADE'), nullable=False)
created_at = db.Column(db.DateTime, nullable=False)
bpm = db.Column(db.Integer, nullable=False)
@app.route('/', methods=['GET'])
def get_workouts():
return render_users_and_workouts()
@@ -113,21 +122,31 @@ def workouts(user_id):
elif request.method == 'POST':
user = User.query.get(user_id)
app.logger.info(f'Creating workout for user {user.name} ({user.id})')
data = request.json
data = data['workout']
workout = data['workout'] or []
heart_rate = data['heart_rate'] or []
# create a new workout
workout = Workout(user_id=user_id, bike_id=user.bike_id)
db.session.add(workout)
db.session.commit()
app.logger.info(
f'Workout({workout.id}) created for user {user.name} ({user.id}) with {len(workout)} cadence readings and {len(heart_rate)} heart rate readings')
# add cadence readings to the workout
for d in data:
for w in workout:
cadence_reading = CadenceReading(
workout_id=workout.id, created_at=d['timestamp'], rpm=d['rpm'], distance=d['distance'], speed=d['speed'], calories=d['calories'], power=d['power'])
workout_id=workout.id, created_at=w['timestamp'], rpm=w['rpm'], distance=w['distance'], speed=w['speed'], calories=w['calories'], power=w['power'])
db.session.add(cadence_reading)
for h in heart_rate:
heart_rate_reading = HeartRateReading(
workout_id=workout.id, created_at=h['timestamp'], bpm=h['bpm'])
db.session.add(heart_rate_reading)
db.session.commit()
return jsonify({'message': 'Workout created successfully.'}), 201