chore : update env defaults and make targets
This commit is contained in:
34
app/main.py
34
app/main.py
@@ -3,8 +3,10 @@ import time
|
||||
import socket
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from .serial_bridge import SerialBridge, SerialConfig
|
||||
from .mock_bridge import MockBridge
|
||||
|
||||
app = FastAPI(title="Pont bascule API", version="1.3")
|
||||
|
||||
@@ -16,20 +18,30 @@ def env(name: str, default: str) -> str:
|
||||
return os.getenv(name, default)
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
def hex2b(s: str) -> bytes:
|
||||
s = s.strip().replace("0x", "").replace(",", " ")
|
||||
parts = [p for p in s.split() if p]
|
||||
return bytes(int(p, 16) for p in parts)
|
||||
|
||||
|
||||
cfg = SerialConfig(
|
||||
port=env("SERIAL_PORT", "/dev/ttyUSB0"),
|
||||
baudrate=int(env("SERIAL_BAUDRATE", "9600")),
|
||||
timeout_s=float(env("SERIAL_TIMEOUT_S", "1.0")),
|
||||
open_delay_s=float(env("SERIAL_OPEN_DELAY_S", "2.0")), # ✅ comme Tkinter
|
||||
post_write_delay_s=float(env("SERIAL_POST_WRITE_DELAY_S", "0.5")), # ✅
|
||||
)
|
||||
bridge = SerialBridge(cfg)
|
||||
APP_MODE = env("APP_MODE", "serial").strip().lower()
|
||||
if APP_MODE not in ("serial", "mock"):
|
||||
APP_MODE = "serial"
|
||||
|
||||
if APP_MODE == "mock":
|
||||
cfg = None
|
||||
bridge = MockBridge()
|
||||
else:
|
||||
cfg = SerialConfig(
|
||||
port=env("SERIAL_PORT", "/dev/ttyUSB0"),
|
||||
baudrate=int(env("SERIAL_BAUDRATE", "9600")),
|
||||
timeout_s=float(env("SERIAL_TIMEOUT_S", "1.0")),
|
||||
open_delay_s=float(env("SERIAL_OPEN_DELAY_S", "2.0")), # ✅ comme Tkinter
|
||||
post_write_delay_s=float(env("SERIAL_POST_WRITE_DELAY_S", "0.5")), # ✅
|
||||
)
|
||||
bridge = SerialBridge(cfg)
|
||||
|
||||
|
||||
class CustomReq(BaseModel):
|
||||
@@ -40,12 +52,12 @@ class CustomReq(BaseModel):
|
||||
def health():
|
||||
return {
|
||||
"ok": True,
|
||||
"mode": "serial",
|
||||
"mode": APP_MODE,
|
||||
"busy": bridge.busy(),
|
||||
"hostname": socket.gethostname(),
|
||||
"timestamp": time.time(),
|
||||
"port": cfg.port,
|
||||
"baudrate": cfg.baudrate,
|
||||
"port": cfg.port if cfg else None,
|
||||
"baudrate": cfg.baudrate if cfg else None,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user