# Component Inventory - Main

This document provides an inventory of key software components within the Pig Farm Controller application, categorized by their primary function and layer.

## 1. Application Layer Components (`internal/app`)

### 1.1. API and Controllers

These components handle incoming HTTP requests, route them to appropriate handlers, and manage the request/response cycle.

*   **`api.go`**: Initializes the Echo web framework and sets up global middleware and controllers.
*   **`router.go`**: Defines all API routes, mapping HTTP methods and paths to specific controller functions. Includes public and authenticated routes.
*   **`controller/user`**: Handles user-related API endpoints (e.g., registration, login, notifications).
*   **`controller/device`**: Manages device-related API endpoints (e.g., CRUD for devices, area controllers, device templates, manual control).
*   **`controller/management`**: Manages pig batch related API endpoints (e.g., pig houses, pens, pig batches, transfers, sales, health records).
*   **`controller/monitor`**: Provides API endpoints for monitoring various logs and sensor data.
*   **`controller/plan`**: Handles plan-related API endpoints (e.g., CRUD for plans, start/stop plans).
*   **`controller/response.go`**: Utility for standardized API response formatting.

### 1.2. DTOs (Data Transfer Objects)

Located in `internal/app/dto/`, these define the structures for request and response payloads, ensuring clear data contracts for the API.

*   `device_dto.go`
*   `monitor_dto.go`
*   `notification_dto.go`
*   `pig_batch_dto.go`
*   `pig_farm_dto.go`
*   `plan_dto.go`
*   `user_dto.go`

### 1.3. Middleware

Located in `internal/app/middleware/`, these components process requests before they reach the main handlers.

*   `audit.go`**: Implements audit logging for authenticated requests.
*   **`auth.go`**: Handles JWT-based authentication for protected routes.

### 1.4. Application Services

Located in `internal/app/service/`, these orchestrate business logic and interact with domain services and repositories.

*   `device_service.go`
*   `monitor_service.go`
*   `pig_batch_service.go`
*   `pig_farm_service.go`
*   `pig_service.go`
*   `plan_service.go`
*   `user_service.go`

### 1.5. Webhook Handlers

Located in `internal/app/webhook/`, these handle incoming webhooks from external systems.

*   `chirp_stack.go`**: Processes webhooks from ChirpStack LoRaWAN Network Server.
*   `chirp_stack_types.go`**: Defines data structures for ChirpStack webhooks.
*   `placeholder_listener.go`**: Placeholder for other webhook listeners.
*   `transport.go`**: Generic webhook transport interface.

## 2. Domain Layer Components (`internal/domain`)

These components encapsulate the core business logic and domain rules.

*   **`audit/service.go`**: Defines audit logging operations.
*   **`device/device_service.go`**: Core business logic for device management.
*   **`notify/notify.go`**: Defines notification interfaces.
*   **`pig/`**: Contains business logic related to pig management (e.g., `pen_transfer_manager.go`, `pig_batch_service.go`, `pig_sick_manager.go`, `pig_trade_manager.go`).
*   **`scheduler/`**: Manages task scheduling and execution.
*   **`task/`**: Defines various background tasks (e.g., `delay_task.go`, `full_collection_task.go`).
*   **`token/token_service.go`**: Handles JWT token creation and validation.

## 3. Infrastructure Layer Components (`internal/infra`)

These components deal with external concerns and technical details.

*   **`config/config.go`**: Manages application configuration loading.
*   **`database/`**: Handles database connection, migration, and GORM setup.
*   **`logs/logs.go`**: Provides logging utilities (using Zap).
*   **`models/`**: Defines GORM models that map to database tables.
*   **`notify/`**: Implements various notification channels (e.g., `lark.go`, `smtp.go`, `wechat.go`).
*   **`repository/`**: Provides data access logic for different entities (e.g., `user_repository.go`, `device_repository.go`, `pig_batch_repository.go`).
*   **`transport/`**: Handles communication with external systems, particularly LoRaWAN.
    *   `lora/`**: LoRaWAN specific transport implementation.
    *   `proto/`**: Protocol Buffer definitions for external communication.
*   **`utils/`**: General utility functions.
