Layer Overview
APM’s architecture is built on three distinct layers:- Core Layer - Domain models and interface contracts
- Infrastructure Layer - Concrete implementations
- Presentation Layer - User interfaces and entry points
Core Layer
Location:source/Core/
The Core layer defines the business domain without any implementation details. It contains only interfaces, models, and enums.
Core Interfaces
IWebSocketService
Location:source/Core/Interfaces/IWebSocketService.cs
Defines the contract for WebSocket server functionality.
- Accept WebSocket connections from web clients
- Receive print job requests and template updates
- Send results and scale data to connected clients
- Manage client lifecycle (connect/disconnect)
IPrintService
Location:source/Core/Interfaces/IPrintService.cs
Orchestrates the complete print workflow.
- Process print jobs from request to completion
- Manage printer configuration
- Coordinate rendering, command generation, and transmission
ITicketRenderer
Location:source/Core/Interfaces/ITicketRenderer.cs
Transforms document data into structured ticket content.
- Parse and validate document data
- Apply templates to create visual layout
- Generate structured content ready for ESC/POS conversion
IEscPosGenerator
Location:source/Core/Interfaces/IEscPosGenerator.cs
Generates ESC/POS commands for thermal printers.
- Convert ticket content to ESC/POS byte commands
- Handle text formatting, alignment, fonts
- Generate barcode and QR code commands
- Process images and logos
ISettingsRepository
Location:source/Core/Interfaces/ISettingsRepository.cs
Manages printer configuration persistence.
ITemplateRepository
Location:source/Core/Interfaces/ITemplateRepository.cs
Manages print templates for different document types.
IScaleService
Location:source/Core/Interfaces/IScaleService.cs
Manages serial scale communication.
ILoggingService
Location:source/Core/Interfaces/ILoggingService.cs
Centralized logging interface.
Platform-Specific Interfaces
IWorkerServiceManager (Windows only):- Manage Windows Service lifecycle
- Start/stop/restart service
- Query service status
- System tray icon management
- Notification handling
- Platform-specific operations
- File system access
- Native API calls
Core Models
Location:source/Core/Models/
PrintJobRequest
Represents an incoming print request.PrintJobResult
Represents the outcome of a print job.PrinterSettings
Stores printer configuration.PrintTemplate
Defines the structure and layout of a document.TicketContent
Represents rendered ticket ready for ESC/POS conversion.Document Data Models
- SaleTicketDocumentData - Sales receipt data
- CommandDocumentData - Kitchen/bar command data
- ElectronicInvoiceDocumentData - Invoice data
- BarcodeStickerDocumentData - Barcode label data
- ComprobanteEgresoDocumentData - Payment voucher data
Scale Models
- Scale - Scale configuration
- ScaleData - Weight reading data
- ScaleStatus - Connection status enum
Infrastructure Layer
Location:source/Infraestructure/
Provides concrete implementations of all core interfaces.
Key Services
WebSocketServerService
Location:source/Infraestructure/Services/WebSocketServerService.cs
Implementation Details:
- Uses
HttpListenerto accept WebSocket connections - Maintains
ConcurrentDictionaryof connected clients - Implements message queue for processing requests
- Handles both print jobs and template updates
- Manages scale data subscriptions per client
PrintService
Location:source/Infraestructure/Services/PrintService.cs:27
Print Job Processing Flow:
- Validate Printer - Retrieve printer settings from repository
- Deserialize Document - Convert JSON to typed document model based on
DocumentType - Check Template Type - Determine thermal vs. dot matrix
- Render Content:
- Thermal:
ITicketRenderer→TicketContent - Dot Matrix:
DotMatrixRendererService→ string grid
- Thermal:
- Generate Commands:
- Thermal:
IEscPosGenerator→ ESC/POS bytes - Dot Matrix:
EscPGeneratorService→ ESC/P bytes
- Thermal:
- Transmit:
- TCP/IP:
TcpIpPrinterClient - USB:
LocalRawPrinterClient - IPP:
IppPrinterClient
- TCP/IP:
- Handle Copies - Send to additional printers if configured
TicketRendererService
Location:source/Infraestructure/Services/TicketRendererService.cs
Responsibilities:
- Load template by document type
- Merge template with document data
- Process dynamic fields and expressions
- Handle images, barcodes, QR codes
- Format text elements (alignment, font, style)
EscPosGeneratorService
Location:source/Infraestructure/Services/EscPosGeneratorService.cs
ESC/POS Command Generation:
- Text formatting (bold, underline, size)
- Alignment (left, center, right)
- Barcode generation (CODE128, EAN13, etc.)
- QR code generation
- Image dithering and printing
- Paper cut commands
SerialScaleService
Location:source/Infraestructure/Services/SerialScaleService.cs
Serial Communication:
- Manages multiple scales via
System.IO.Ports.SerialPort - Continuous background reading
- Protocol-specific data parsing
- Stability detection
- Event-driven weight broadcasting
Printer Clients
TcpIpPrinterClient
Location:source/Infraestructure/Services/TcpIpPrinterClient.cs
- Direct socket connection to network printers
- Sends raw ESC/POS bytes over TCP
LocalRawPrinterClient
Location:source/Infraestructure/Services/LocalRawPrinterClient.cs
- Windows spooler integration
- Sends raw bytes to USB/local printers
- Uses Win32 API for direct printing
IppPrinterClient
Location:source/Infraestructure/Services/IppPrinterClient.cs
- Internet Printing Protocol support
- Network printer compatibility
Repositories
SettingsRepository
Location:source/Infraestructure/Repositories/SettingsRepository.cs
- JSON file-based storage
- Stores
printers.jsonin app data directory - Thread-safe read/write operations
TemplateRepository
Location:source/Infraestructure/Repositories/TemplateRepository.cs
- Individual JSON files per template
- Default template generation
- Supports both thermal and dot matrix templates
JsonScaleRepository
Location:source/Infraestructure/Repositories/JsonScaleRepository.cs
- Stores
scales.jsonconfiguration - Scale CRUD operations
Rendering Services
DotMatrixRendererService
Location:source/Infraestructure/Services/DotMatrixRendererService.cs
- Text-grid based rendering for dot matrix printers
- Fixed-width character positioning
- Supports LX-300 and similar printers
EscPGeneratorService
Location:source/Infraestructure/Services/EscPGeneratorService.cs
- ESC/P command generation (different from ESC/POS)
- Line printer control codes
- Character density and pitch control
Logger
Location:source/Infraestructure/Services/Logger.cs
- File-based logging
- Log levels (Info, Warning, Error)
- Rotating log files
- Console output in debug mode
Dependency Injection
All components use constructor injection following the Dependency Inversion Principle.Service Registration Pattern
Both WorkerService and UI register the same core services:Platform-Specific Registration
Windows:Next Steps
- Worker Service - Windows Service implementation details
- MAUI Application - UI application architecture
- System Overview - High-level architecture