Primeiro commit

This commit is contained in:
root
2025-06-06 08:46:00 +01:00
commit cf2ac9caca
35 changed files with 4954 additions and 0 deletions

178
testserver.py Executable file
View File

@@ -0,0 +1,178 @@
from flask import Flask, request, jsonify
from flask_cors import CORS # Adicionando suporte ao CORS
app = Flask(__name__)
# Habilitando CORS para aceitar requisições de origens diferentes
CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}}) # Ajuste para seu frontend
# Dados de exemplo
MOCK_STATE = {
"connected": True,
"charging": False,
"power": 0,
"session_time": 0,
"energy": 0.0
}
MOCK_INFO = {
"version": "1.0.0-mock",
"board": "esp32-mock",
"uptime": 12345,
"buildTime": "2025-06-05 23:00",
"id": "MOCK-123456"
}
MOCK_LOGS = [
"[1] System start...",
"[2] Connected to WiFi",
"[3] Ready."
]
MOCK_DASHBOARD = {
"status": "Active",
"chargers": [
{"id": 1, "status": "Active", "current": 12, "power": 2200},
{"id": 2, "status": "Inactive", "current": 0, "power": 0},
{"id": 3, "status": "Error", "current": 0, "power": 0}
],
"energy_consumed": 50.3,
"charging_time": 120,
"alerts": ["Warning: Charger 1 in error state."]
}
MOCK_SETTINGS = {
"current_limit": 32,
"power_limit": 0,
"total_energy_limit": 0,
"charging_time_limit": 0,
"network_type": "Model A",
"voltage": 220,
"frequency": 60,
"temperature_limit": 60,
"current_network": {
"status": "Connected",
"ip": "192.168.1.100",
"mac": "00:14:22:01:23:45"
}
}
MOCK_SECURITY = {
"authentication_methods": ["RFID", "App", "Password"],
"security_mode": "Enabled",
"users": [
{"username": "admin", "role": "Administrator"},
{"username": "user1", "role": "User"}
],
"mfa_enabled": True
}
MOCK_CONNECTIVITY = {
"wifi": {
"status": "Connected",
"ssid": "EV_Charging_Network",
"signal_strength": -45
},
"mqtt": {
"status": "Connected",
"broker": "mqtt://broker.example.com",
"port": 1883
}
}
MOCK_OCPP = {
"ocpp_version": "2.0",
"ocpp_url": "http://ocppserver.example.com",
"ocpp_id": "MOCK-123456",
"status": "Active"
}
# Autenticação básica (admin:admin)
def check_auth():
auth = request.headers.get("Authorization", "")
return auth == "Basic YWRtaW46YWRtaW4=" # base64 of admin:admin
# Função para atualizar os logs
def add_log(log_message):
MOCK_LOGS.append(f"[{len(MOCK_LOGS) + 1}] {log_message}")
# Definindo as rotas da API
@app.route('/api/v1/info', methods=['GET'])
def get_info():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_INFO)
@app.route('/api/v1/state', methods=['GET'])
def get_state():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_STATE)
@app.route('/api/v1/dashboard', methods=['GET'])
def get_dashboard():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_DASHBOARD)
@app.route('/api/v1/settings', methods=['GET'])
def get_settings():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_SETTINGS)
@app.route('/api/v1/security', methods=['GET'])
def get_security():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_SECURITY)
@app.route('/api/v1/connectivity', methods=['GET'])
def get_connectivity():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_CONNECTIVITY)
@app.route('/api/v1/ocpp', methods=['GET'])
def get_ocpp():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_OCPP)
@app.route('/api/v1/log', methods=['GET'])
def get_logs():
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
return jsonify(MOCK_LOGS), 200, {"X-Count": str(len(MOCK_LOGS))}
@app.route('/api/v1/state/<cmd>', methods=['POST'])
def post_command(cmd):
if not check_auth():
return jsonify({"message": "Unauthorized"}), 401
if cmd == "start":
if MOCK_STATE["charging"]:
return jsonify({"message": "Already charging."}), 400
MOCK_STATE["charging"] = True
MOCK_STATE["power"] = 1500 # Exemplo de potência de carregamento em watts
add_log("Charging started.")
return jsonify({"message": "Charging started."}), 200
elif cmd == "stop":
if not MOCK_STATE["charging"]:
return jsonify({"message": "Not charging."}), 400
MOCK_STATE["charging"] = False
MOCK_STATE["power"] = 0
add_log("Charging stopped.")
return jsonify({"message": "Charging stopped."}), 200
elif cmd == "reset":
MOCK_STATE["charging"] = False
MOCK_STATE["power"] = 0
MOCK_STATE["session_time"] = 0
MOCK_STATE["energy"] = 0.0
add_log("System reset.")
return jsonify({"message": "System reset."}), 200
else:
return jsonify({"message": "Unknown command."}), 400
if __name__ == '__main__':
app.run(port=8080)