all working i think

This commit is contained in:
2025-07-28 06:21:45 -07:00
parent b740ba9991
commit 211930cde3
30 changed files with 900 additions and 117 deletions

78
archive/app.py Normal file
View File

@ -0,0 +1,78 @@
# 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)