Skip to content

Commit

Permalink
trying different app structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vshie committed Dec 2, 2023
1 parent 347c4a1 commit 6e3564f
Showing 1 changed file with 33 additions and 44 deletions.
77 changes: 33 additions & 44 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import requests
import csv
import time
import threading

from datetime import datetime

app = Flask(__name__, static_url_path="/static", static_folder="static")

# Global variable to control the logging thread
logging_active = False
distance_url = 'http://localhost:6040/mavlink/vehicles/1/components/194/messages/DISTANCE_SENSOR'
gps_url = 'http://localhost:6040/mavlink/vehicles/1/components/1/messages/GLOBAL_POSITION_INT'
distance_url = 'http://192.168.1.52:6040/mavlink/vehicles/1/components/194/messages/DISTANCE_SENSOR'
gps_url = 'http://192.168.1.52:6040/mavlink/vehicles/1/components/1/messages/GLOBAL_POSITION_INT'
log_file = 'sensor_data.csv'
log_rate = 2

Expand All @@ -18,42 +20,9 @@

# Initialize a counter for the number of rows added
row_counter = 0
@app.route('/')
def home():
return app.send_static_file("index.html")

@app.route('/start')
def start_logging():
global logging_active
if not logging_active:
logging_active = True
main() # Start the logging script directly
return 'Started'

@app.route("/get_data")
def get_data():
data = {
'unix_timestamp': unix_timestamp,
'distance': distance,
'latitude': latitude,
'longitude': longitude
}
return jsonify(data)

@app.route('/stop')
def stop_logging():
global logging_active
logging_active = False
return 'Stopped'

@app.route('/download')
def download_file():
return send_file('sensor_data.csv', as_attachment=True)

def main():
# Initialize a counter for the number of rows added
row_counter = 0

data = []
def fetch_data():
global data
# Main loop for logging data
while (logging_active == True):
try:
Expand All @@ -72,18 +41,14 @@ def main():

# Extract the values for each column
timestamp = int(time.time() * 1000) # Convert current time to milliseconds
dt = datetime.fromtimestamp(timestamp / 1000) # Convert timestamp to datetime object
global unix_timestamp
dt = datetime.fromtimestamp(timestamp / 1000) # Convert timestamp to datetime object
unix_timestamp = timestamp
year, month, day, hour, minute, second = dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
global distance
distance = distance_data['current_distance']
global latitude
latitude = gps_data['lat'] / 1e7
global longitude
longitude = gps_data['lon'] / 1e7
column_values = [unix_timestamp, year, month, day, hour, minute, second, distance, latitude, longitude]

data = column_values
# Create or append to the log file and write the data
with open(log_file, 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
Expand Down Expand Up @@ -115,5 +80,29 @@ def main():
print(f"An error occurred: {e}")
break

@app.route('/')
def home():
return app.send_static_file("index.html")
def get_data():
return jsonify(data)

@app.route('/start')
def start_logging():
global logging_active
if not logging_active:
logging_active = True
threading.Thread(target=fetch_data).start()# Start the logging script directly
return 'Started'

@app.route('/stop')
def stop_logging():
global logging_active
logging_active = False
return 'Stopped'

@app.route('/download')
def download_file():
return send_file('sensor_data.csv', as_attachment=True)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)

0 comments on commit 6e3564f

Please sign in to comment.