uploaded base code

This commit is contained in:
2025-03-15 18:28:52 +02:00
parent dd379a6f5c
commit 4066abb255
10 changed files with 302 additions and 0 deletions

136
app.py Normal file
View File

@@ -0,0 +1,136 @@
from flask import Flask, request, jsonify, render_template
import mysql.connector
import json
import os
from flask_socketio import SocketIO
# Load configuration
CONFIG_FILE = "config.json"
def load_config():
with open(CONFIG_FILE, "r", encoding="utf-8") as config_file:
return json.load(config_file)
def save_config(updated_config):
with open(CONFIG_FILE, "w", encoding="utf-8") as config_file:
json.dump(updated_config, config_file, indent=4, ensure_ascii=False)
def translate(key):
return translations.get(config.get("language", "en"), {}).get(key, key)
config = load_config()
db_config = config["database"]
matrix_size = config["matrix_size"]
skip_n = config.get("skip_n", True)
rtl = config.get("rtl", False)
# Load translations
with open("translations.json", "r", encoding="utf-8") as trans_file:
translations = json.load(trans_file)
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False # Ensure UTF-8 support
socketio = SocketIO(app, cors_allowed_origins="*")
def init_db():
try:
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS storage (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
phone VARCHAR(20),
location VARCHAR(10),
bulk BOOLEAN)''')
conn.commit()
except Exception as e:
print(f"Database initialization error: {e}")
finally:
if 'conn' in locals() and conn.is_connected():
conn.close()
@app.route('/')
def index():
return render_template("index.html", rtl=rtl)
@app.route('/store', methods=['POST'])
def store_item():
try:
data = request.json
name = data.get("name")
phone = data.get("phone")
location = data.get("location")
bulk = data.get("bulk", 0)
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute("INSERT INTO storage (name, phone, location, bulk) VALUES (%s, %s, %s, %s)", (name, phone, location, bulk))
conn.commit()
socketio.emit("update", {"message": translate("item_stored")})
return jsonify({"message": translate("item_stored")})
except Exception as e:
print(f"Error storing item: {e}")
return jsonify({"error": "An error occurred"}), 500
finally:
if 'conn' in locals() and conn.is_connected():
conn.close()
@app.route('/retrieve', methods=['GET'])
def retrieve_items():
try:
phone = request.args.get("phone")
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute("SELECT * FROM storage WHERE phone = %s", (phone,))
items = cursor.fetchall()
return jsonify(items)
except Exception as e:
print(f"Error retrieving items: {e}")
return jsonify({"error": "An error occurred"}), 500
finally:
if 'conn' in locals() and conn.is_connected():
conn.close()
@app.route('/remove', methods=['POST'])
def remove_item():
try:
data = request.json
item_id = data.get("id")
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute("DELETE FROM storage WHERE id = %s", (item_id,))
conn.commit()
socketio.emit("update", {"message": translate("item_removed")})
return jsonify({"message": translate("item_removed")})
except Exception as e:
print(f"Error removing item: {e}")
return jsonify({"error": "An error occurred"}), 500
finally:
if 'conn' in locals() and conn.is_connected():
conn.close()
@app.route('/config', methods=['GET', 'POST'])
def configure_app():
global config, db_config, matrix_size, skip_n, rtl # Move this line to the start of the function
if request.method == 'GET':
return jsonify(config)
try:
new_config = request.json
save_config(new_config)
# Reload configuration
config = load_config()
db_config = config["database"]
matrix_size = config["matrix_size"]
skip_n = config.get("skip_n", True)
rtl = config.get("rtl", False)
return jsonify({"message": "Configuration updated successfully"})
except Exception as e:
print(f"Error updating config: {e}")
return jsonify({"error": "Failed to update config"}), 500
if __name__ == '__main__':
init_db()
socketio.run(app, host='0.0.0.0', port=5000, debug=True)