diff --git a/Dockerfile b/Dockerfile index d2204a1..626923f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,5 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 5000 RUN pip freeze > installed_packages.txt -CMD ["python", "restapi.py"] +RUN sed -i 's/socketio.run(app, host="0.0.0.0", port=5000, debug=True)/socketio.run(app, host="0.0.0.0", port=5000, debug=True, allow_unsafe_werkzeug=True)/' /app/restapi.py +CMD ["python", "/app/restapi.py"] diff --git a/restapi.py b/restapi.py index a883646..8ea9068 100644 --- a/restapi.py +++ b/restapi.py @@ -3,7 +3,7 @@ from mlmodel import guess import pandas as pd import sqlite3 - +import os from flask_cors import CORS @@ -14,6 +14,64 @@ received_data = [] DB_PATH = "sensor_data.db" +def init_db(): + """Automatically create the database and tables if they do not exist.""" + conn = sqlite3.connect(DB_PATH) + c = conn.cursor() + c.execute(""" + CREATE TABLE IF NOT EXISTS sensor_data ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + time TEXT, + rotation_speed REAL, + temperature REAL, + level REAL, + kurtosis_axial REAL, + kurtosis_vertical REAL, + kurtosis_horizontal REAL, + peak_axial REAL, + peak_vertical REAL, + peak_horizontal REAL, + vibration_acc_axial REAL, + vibration_acc_vertical REAL, + vibration_acc_horizontal REAL, + vibration_vel_axial REAL, + vibration_vel_vertical REAL, + vibration_vel_horizontal REAL, + band_0_8k REAL, + band_8_16k REAL, + band_16_24k REAL, + band_24_32k REAL, + band_32_40k REAL, + band_40_48k REAL, + band_48_56k REAL, + band_56_64k REAL, + band_64_72k REAL, + band_72_80k REAL, + band_0_200 REAL, + band_200_400 REAL, + band_400_600 REAL, + band_600_800 REAL, + band_800_1000 REAL, + band_1_1_2k REAL, + band_1_2_1_4k REAL, + band_1_4_1_6k REAL, + band_1_6_1_8k REAL, + band_1_8_2k REAL, + fault0 REAL, + fault1 REAL, + fault2 REAL, + fault3 REAL, + fault4 REAL, + fault5 REAL, + fault6 REAL, + fault7 REAL + ) + """) + conn.commit() + conn.close() + +# Automatically initialize the database on application startup +init_db() @app.route('/') @@ -126,4 +184,4 @@ def generate_csv(): if __name__ == "__main__": - socketio.run(app, host="0.0.0.0", port=5000, debug=True) \ No newline at end of file + socketio.run(app, host="0.0.0.0", port=5000, debug=True)