import subprocess import requests from lxml import html from flask import Flask, request, jsonify import json import os app = Flask(__name__) def start_service(): command = "systemctl start timelapse.service" try: # Run the command using subprocess.run() process = subprocess.Popen(command, shell=True) except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2- | cut -b 2-" try: # Run the command using subprocess.run() result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) return {"Message": result.stdout.strip('\n')} except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} def stop_service(): command = "systemctl stop timelapse.service" try: # Run the command using subprocess.run() process = subprocess.Popen(command, shell=True) except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2- | cut -b 2-" try: # Run the command using subprocess.run() result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) return {"Message": result.stdout.strip('\n')} except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} def service_status(): command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2- | cut -b 2-" try: # Run the command using subprocess.run() result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2 | cut -d ' ' -f 2" status = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) return {"Message": result.stdout.strip('\n'), "Status": status.stdout.strip('\n')} except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} def read_gps_data_file(): # Define the path to your file. This is a relative path example. Adjust it as needed. file_path = '{{ gps_service_directory }}/gps_data' if os.path.exists(file_path): with open(file_path, 'r') as file: lines = file.readlines() # Assuming the data is in a specific format, you can parse it accordingly lat = lines[0].split(':')[1] lon = lines[1].split(':')[1] speed = lines[2].split(':')[1] try: lat = float(lat.strip('\n')) lon = float(lon.strip('\n')) speed = float(speed.strip('\n')) * 0.62 except: lat = 0 lon = 0 speed = 0 # Round latitude and longitude to 5 decimal places at most lat_rounded = round(lat, 3) lon_rounded = round(lon, 3) speed_rounded = round(speed, 1) data = { "lat": lat_rounded, "lon": lon_rounded, "speed": speed_rounded } else: data = { "lat": 37.548, "lon": -121.961, "speed": 0 } return jsonify(data) @app.route('/return_gps', methods=['GET']) def return_gps(): return read_gps_data_file() @app.route('/start', methods=['GET']) def start(): try: return jsonify(start_service()) except ValueError as e: print(e) return jsonify({'error': e}), 400 @app.route('/stop', methods=['GET']) def stop(): try: return jsonify(stop_service()) except ValueError as e: print(e) return jsonify({'error': e}), 400 @app.route('/status', methods=['GET']) def status(): try: return jsonify(service_status()) except ValueError as e: print(e) return jsonify({'error': e}), 400 @app.route('/test', methods=['GET']) def test(): return jsonify({'message': 'Hello there'}) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)