# app.py from flask import Flask, request, jsonify import subprocess import requests import psycopg2 import os import re from datetime import datetime, timedelta import io import json app = Flask(__name__) # API endpoint for charge @app.route('/test', methods=['GET']) def test(): return jsonify({"message": "Hello there"}), 200 # API endpoint for service status @app.route('/get-info', methods=['GET']) def get_info(): result = service_status() print(result) return jsonify(result) # # Stop Service # @app.route('/stop', methods=['GET']) # def stop_service(): # result = stop_timelapse() # print(result) # return jsonify(result) # # # Start Service # @app.route('/start', methods=['GET']) # def start_service(): # result = start_timelapse() # print(result) # return jsonify(result) # get service status def service_status(): service_name = "timelapse.service" try: # Check the status of the service using systemctl result = subprocess.run(['systemctl', 'is-active', service_name], check=True, capture_output=True, text=True) if result.returncode == 0: active_status = "running" if result.stdout.strip() == b'active\n' else "not running" # Get the last message from the system log for this service logs_result = subprocess.run(['journalctl', '-u', service_name, '-xe'], check=True, capture_output=True, text=True) last_message = logs_result.stdout response = { "service_status": active_status, "service_message": last_message.strip() if last_message else "No message available" } return json.dumps(response) else: raise Exception("Service is not running") except subprocess.CalledProcessError as e: response = { "service_status": "error", "service_message": str(e) } return json.dumps(response) except Exception as e: response = { "service_status": "error", "service_message": str(e) } return json.dumps(response) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)