Merge remote-tracking branch 'origin/main'
# Conflicts: # main.py
This commit is contained in:
commit
773e103364
19
dockerfile
Normal file
19
dockerfile
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
# Dockerfile
|
||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install required packages
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
iproute2 \
|
||||||
|
can-utils \
|
||||||
|
libpython3-dev \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Copy all application files
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Install Python dependencies
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||||
31
main.py
31
main.py
|
|
@ -2,6 +2,9 @@ from fastapi import FastAPI, HTTPException, Query
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
from fastapi import Request, APIRouter
|
||||||
|
import subprocess
|
||||||
import platform
|
import platform
|
||||||
if platform.system() in ['Darwin']: # macOS or Windows
|
if platform.system() in ['Darwin']: # macOS or Windows
|
||||||
from MockCAN import CANBackend
|
from MockCAN import CANBackend
|
||||||
|
|
@ -9,13 +12,36 @@ else :
|
||||||
from classCAN import CANBackend # Your real backend
|
from classCAN import CANBackend # Your real backend
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
can_backend = CANBackend()
|
can_backend = CANBackend()
|
||||||
|
|
||||||
|
|
||||||
# Serve static files (HTML, JS, CSS)
|
# Serve static files (HTML, JS, CSS)
|
||||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||||
|
|
||||||
|
@router.post("/webhook")
|
||||||
|
async def github_webhook(request: Request):
|
||||||
|
payload = await request.json()
|
||||||
|
print("[WEBHOOK] Received webhook:", payload.get("head_commit", {}).get("message"))
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Call the update script on the HOST using host bash
|
||||||
|
subprocess.run(
|
||||||
|
["/usr/bin/bash", "-c", "bash /home/hmi/Desktop/HMI/update_hmi.sh"],
|
||||||
|
check=True,
|
||||||
|
capture_output=True,
|
||||||
|
text=True
|
||||||
|
)
|
||||||
|
return {"status": "Update triggered"}
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(f"[WEBHOOK] Update failed:\n{e.stderr}")
|
||||||
|
return {"status": "Update failed", "error": str(e)}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/connect_toggle")
|
@app.post("/connect_toggle")
|
||||||
def connect_toggle():
|
def connect_toggle():
|
||||||
"""Toggle CAN connection."""
|
"""Toggle CAN connection."""
|
||||||
|
|
@ -26,7 +52,7 @@ def connect_toggle():
|
||||||
else:
|
else:
|
||||||
success = can_backend.connect(
|
success = can_backend.connect(
|
||||||
node_id=0x02,
|
node_id=0x02,
|
||||||
eds_path=r"/eds_file/processBoard_0.eds"
|
eds_path = os.path.join(os.path.dirname(__file__), "eds_file", "processBoard_0.eds")
|
||||||
)
|
)
|
||||||
if not success:
|
if not success:
|
||||||
raise HTTPException(status_code=500, detail="Connection failed.")
|
raise HTTPException(status_code=500, detail="Connection failed.")
|
||||||
|
|
@ -41,7 +67,6 @@ def send_command(state: str, pu_number: int, ploop_setpoint: float = Query(...))
|
||||||
return {"status": "success", "command": state.upper(), "pu": pu_number, "ploop_setpoint": ploop_setpoint}
|
return {"status": "success", "command": state.upper(), "pu": pu_number, "ploop_setpoint": ploop_setpoint}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
@app.get("/monitor")
|
@app.get("/monitor")
|
||||||
def get_monitor_data():
|
def get_monitor_data():
|
||||||
# TODO : agree on the structure
|
# TODO : agree on the structure
|
||||||
|
|
@ -136,6 +161,8 @@ async def read_monitor_page():
|
||||||
return HTMLResponse(content=file.read(), status_code=200)
|
return HTMLResponse(content=file.read(), status_code=200)
|
||||||
|
|
||||||
|
|
||||||
|
app.include_router(router)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
|
|
|
||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
fastapi
|
||||||
|
uvicorn[standard]
|
||||||
|
python-can
|
||||||
|
canopen
|
||||||
12
update_hmi.sh
Normal file
12
update_hmi.sh
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "[UPDATE] Pulling latest code..."
|
||||||
|
cd /home/hmi/Desktop/HMI || exit 1
|
||||||
|
git reset --hard HEAD
|
||||||
|
git pull origin main
|
||||||
|
|
||||||
|
echo "[RESTART] Restarting HMI service..."
|
||||||
|
sudo /bin/systemctl restart hmi.service
|
||||||
|
|
||||||
|
echo "[DONE] HMI updated."
|
||||||
Loading…
Reference in New Issue
Block a user