Lu Xianghui 4 dní pred
rodič
commit
a2473eb789

+ 11 - 0
.dockerignore

@@ -0,0 +1,11 @@
+.git
+.venv
+__pycache__/
+*.py[cod]
+*.egg-info/
+.pytest_cache/
+.mypy_cache/
+.ruff_cache/
+build/
+dist/
+wheels/

+ 20 - 0
Dockerfile

@@ -0,0 +1,20 @@
+FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
+
+WORKDIR /app
+
+ENV PYTHONDONTWRITEBYTECODE=1 \
+    PYTHONUNBUFFERED=1 \
+    UV_COMPILE_BYTECODE=1 \
+    UV_LINK_MODE=copy \
+    PATH="/app/.venv/bin:$PATH"
+
+COPY pyproject.toml uv.lock ./
+RUN uv sync --frozen --no-dev --no-install-project
+
+COPY app ./app
+COPY main.py README.md ./
+RUN uv sync --frozen --no-dev
+
+EXPOSE 8000
+
+CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

+ 1 - 1
app/schemas/modbus.py

@@ -26,7 +26,7 @@ class ModbusBaseRequest(BaseModel):
     port: int = Field(ge=1, le=65535)
     word_byte_order: Literal["ABCD", "BADC", "CDAB", "DCBA"] = "ABCD"
     address_base: int = Field(default=0, ge=0)
-    slave_id: int = Field(default=1, ge=0, le=247)
+    slave_id: int = Field(ge=0, le=247)
 
     @field_validator("ip")
     @classmethod

+ 12 - 0
intergration/modbus/test_modbus_schema.py

@@ -10,6 +10,7 @@ class ModbusSchemaTest(unittest.TestCase):
         request = ModbusRawReadRequest(
             ip="192.168.75.248",
             port=5512,
+            slave_id=1,
             read={"function_code": 3, "address": 0, "quantity": 10},
         )
 
@@ -24,11 +25,22 @@ class ModbusSchemaTest(unittest.TestCase):
 
         self.assertIn("port", str(context.exception))
 
+    def test_slave_id_is_required(self):
+        with self.assertRaises(ValidationError) as context:
+            ModbusRawReadRequest(
+                ip="192.168.75.248",
+                port=5512,
+                read={"function_code": 3, "address": 0, "quantity": 10},
+            )
+
+        self.assertIn("slave_id", str(context.exception))
+
     def test_ip_must_be_valid_address(self):
         with self.assertRaises(ValidationError) as context:
             ModbusRawReadRequest(
                 ip="not-an-ip",
                 port=5512,
+                slave_id=1,
                 read={"function_code": 3, "address": 0, "quantity": 10},
             )