- Implemented ProcessSession, ProcessLog, and TemperatureReading models for tracking tempering processes. - Created Recipe and RecipePhase models for managing chocolate tempering recipes. - Developed SystemConfiguration, ErrorLog, and Backup models for system settings and error tracking. - Introduced User and UserRole models for user management and authentication. - Added basic structure for schemas and tests.
6.2 KiB
mode
| mode |
|---|
| agent |
The current workspace is a chocolate tempering system. The current state of the codebase is very bad. The one wrote the code is a very stupid person. I want you to rewrite the whole system based on the documentation provided.
The code should be clean, well-structured, and follow best practices and utilze design patterns and modular architecture. The system is built using c# and communicates with hardware via Modbus RTU over serial communication.
Implement the system in Python using FastAPI for the web service, asyncio for asynchronous operations, and SQLAlchemy for database interactions. The system should be modular, with clear separation of concerns between the user interface, control logic, communication, and hardware layers. see docs for specification
the current code can not be seen as correct reference, but it can be used as a reference for the current state of the system. (which is very bad)
┌─────────────────────────────────────────────────────────────┐
│ PYTHON ARCHITECTURE │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ FastAPI │ │ Hardware │ │ Recipe │ │
│ │ Web Service │ │ Service │ │ Service │ │
│ │ (REST API) │ │ (AsyncIO) │ │ (State Mgmt) │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
│ │ │ │ │
│ └───────────────────┼───────────────────┘ │
│ ▼ │
│ ┌─────────────────────────────┐ │
│ │ Message Broker │ │
│ │ (Redis/Celery/NATS) │ │
│ └─────────────────────────────┘ │
│ │ │
│ ┌───────────────────┼───────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Safety & │ │ Data Logger │ │ Configuration │ │
│ │ Monitoring │ │ Service │ │ Service │ │
│ │ (AsyncIO) │ │ (SQLAlchemy) │ │ (Pydantic) │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────────────────────┘
2. Recommended Python Stack
Core Framework & Libraries
- Web Framework: FastAPI (modern, fast, async, great for APIs)
- Hardware Communication:
pymodbus(excellent Modbus RTU/TCP support) - Async Framework:
asyncio+aiofiles+aiohttp - Message Queue: Redis + Celery or NATS.py
- Database: SQLAlchemy + Alembic (migrations) + SQLite/PostgreSQL
- Configuration: Pydantic Settings (type-safe config with validation)
- Logging:
structlog(structured logging) - State Management:
python-statemachineor custom state machine - Data Validation: Pydantic (runtime type checking and validation)
- Testing: pytest + pytest-asyncio + factory-boy
Additional Industrial Libraries
- Serial Communication:
pyserial(underneath pymodbus) - GPIO/Hardware Interface:
RPi.GPIOorgpiozero(if on Raspberry Pi) - Time-series Data:
pandasfor data analysis,influxdb-clientfor metrics - Process Management:
supervisororsystemdfor service management - Monitoring:
prometheus-clientfor metrics,grafanafor dashboards
3. Project Structure
tempering_machine/
├── services/
│ ├── hardware/ # Hardware communication service
│ │ ├── __init__.py
│ │ ├── modbus_client.py
│ │ ├── hardware_manager.py
│ │ └── device_mappings.py
│ ├── recipe/ # Recipe management service
│ │ ├── __init__.py
│ │ ├── state_machine.py
│ │ ├── recipe_controller.py
│ │ └── phase_manager.py
│ ├── safety/ # Safety monitoring service
│ │ ├── __init__.py
│ │ ├── safety_monitor.py
│ │ ├── error_handler.py
│ │ └── emergency_stop.py
│ ├── web/ # Web API service
│ │ ├── __init__.py
│ │ ├── main.py
│ │ ├── routers/
│ │ └── dependencies.py
│ └── data/ # Data logging service
│ ├── __init__.py
│ ├── logger.py
│ └── models.py
├── shared/
│ ├── __init__.py
│ ├── config.py # Pydantic settings
│ ├── database.py # SQLAlchemy setup
│ ├── messaging.py # Message queue client
│ ├── models/ # Database models
│ └── schemas/ # Pydantic schemas
├── frontend/ # React/Vue.js frontend
├── tests/
├── docker/
├── scripts/
├── requirements.txt
├── pyproject.toml
└── docker-compose.yml