2025-09-11 21:04:17 +08:00
|
|
|
|
package core
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-05 16:00:43 +08:00
|
|
|
|
"context"
|
2025-09-11 21:04:17 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"os/signal"
|
|
|
|
|
|
"syscall"
|
|
|
|
|
|
|
2025-09-12 14:58:07 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/api"
|
2025-12-08 19:10:07 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/listener"
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/listener/chirp_stack"
|
2025-09-11 23:48:06 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
2025-12-08 19:10:07 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/lora"
|
2025-09-11 21:04:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Application 是整个应用的核心,封装了所有组件和生命周期。
|
|
|
|
|
|
type Application struct {
|
2025-11-19 19:31:51 +08:00
|
|
|
|
cfgPath string
|
|
|
|
|
|
Config *config.Config
|
|
|
|
|
|
Ctx context.Context
|
|
|
|
|
|
API *api.API
|
2025-10-26 15:48:38 +08:00
|
|
|
|
|
|
|
|
|
|
Infra *Infrastructure
|
|
|
|
|
|
Domain *DomainServices
|
|
|
|
|
|
App *AppServices
|
2025-09-11 21:04:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewApplication 创建并初始化一个新的 Application 实例。
|
|
|
|
|
|
// 这是应用的“组合根”,所有依赖都在这里被创建和注入。
|
|
|
|
|
|
func NewApplication(configPath string) (*Application, error) {
|
2025-10-26 15:48:38 +08:00
|
|
|
|
// 1. 初始化基本组件: 配置和日志
|
2025-09-11 21:04:17 +08:00
|
|
|
|
cfg := config.NewConfig()
|
|
|
|
|
|
if err := cfg.Load(configPath); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("无法加载配置: %w", err)
|
|
|
|
|
|
}
|
2025-11-05 16:00:43 +08:00
|
|
|
|
logs.InitDefaultLogger(cfg.Log)
|
|
|
|
|
|
selfCtx := logs.AddCompName(context.Background(), "Application")
|
|
|
|
|
|
ctx := logs.AddFuncName(selfCtx, selfCtx, "NewApplication")
|
2025-09-11 21:04:17 +08:00
|
|
|
|
|
2025-12-08 19:10:07 +08:00
|
|
|
|
// 2. 初始化基础设施和领域服务 (此时它们是解耦的)
|
2025-11-05 16:00:43 +08:00
|
|
|
|
infra, err := initInfrastructure(ctx, cfg)
|
2025-10-24 21:24:48 +08:00
|
|
|
|
if err != nil {
|
2025-10-26 15:48:38 +08:00
|
|
|
|
return nil, fmt.Errorf("初始化基础设施失败: %w", err)
|
2025-10-24 21:24:48 +08:00
|
|
|
|
}
|
2025-11-08 18:43:46 +08:00
|
|
|
|
domain, err := initDomainServices(ctx, cfg, infra)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("初始化领域服务失败: %w", err)
|
|
|
|
|
|
}
|
2025-11-05 16:00:43 +08:00
|
|
|
|
appServices := initAppServices(ctx, infra, domain)
|
2025-10-24 21:24:48 +08:00
|
|
|
|
|
2025-12-08 19:10:07 +08:00
|
|
|
|
// 3. 【核心组装逻辑】创建应用层监听器并注入到基础设施层
|
|
|
|
|
|
// 此时,所有依赖项(repos, domain services, comm)都已准备就绪
|
|
|
|
|
|
upstreamHandler := listener.NewLoRaListener(
|
|
|
|
|
|
selfCtx,
|
|
|
|
|
|
infra.repos.areaControllerRepo,
|
|
|
|
|
|
infra.repos.pendingCollectionRepo,
|
|
|
|
|
|
infra.repos.deviceRepo,
|
|
|
|
|
|
infra.repos.sensorDataRepo,
|
|
|
|
|
|
infra.repos.deviceCommandLogRepo,
|
|
|
|
|
|
infra.repos.otaRepo,
|
2025-12-08 19:19:11 +08:00
|
|
|
|
domain.deviceCommunicator,
|
2025-12-08 19:10:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 根据 LoRa 模式完成最终的绑定
|
|
|
|
|
|
if cfg.Lora.Mode == config.LoraMode_LoRaWAN {
|
|
|
|
|
|
// 对于 LoRaWAN,创建真正的 Webhook 处理器并替换掉占位符
|
|
|
|
|
|
infra.lora.listenHandler = chirp_stack.NewChirpStackListener(selfCtx, upstreamHandler)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 对于 LoRa Mesh,将处理器注入到已创建的 transport 实例中
|
|
|
|
|
|
if tp, ok := infra.lora.loraListener.(*lora.LoRaMeshUartPassthroughTransport); ok {
|
|
|
|
|
|
tp.SetHandler(upstreamHandler)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 初始化 API 入口点 (现在可以安全地传入 listenHandler)
|
2025-09-27 00:30:47 +08:00
|
|
|
|
apiServer := api.NewAPI(
|
|
|
|
|
|
cfg.Server,
|
2025-11-05 16:10:46 +08:00
|
|
|
|
logs.AddCompName(context.Background(), "API"),
|
2025-11-02 19:59:13 +08:00
|
|
|
|
infra.repos.userRepo,
|
|
|
|
|
|
appServices.pigFarmService,
|
|
|
|
|
|
appServices.pigBatchService,
|
|
|
|
|
|
appServices.monitorService,
|
|
|
|
|
|
appServices.deviceService,
|
2025-12-05 14:44:36 +08:00
|
|
|
|
appServices.deviceTemplateService,
|
|
|
|
|
|
appServices.areaControllerService,
|
2025-11-02 19:59:13 +08:00
|
|
|
|
appServices.planService,
|
|
|
|
|
|
appServices.userService,
|
|
|
|
|
|
appServices.auditService,
|
2025-11-09 22:34:05 +08:00
|
|
|
|
appServices.thresholdAlarmService,
|
2025-11-23 15:16:45 +08:00
|
|
|
|
appServices.nutrientService,
|
|
|
|
|
|
appServices.rawMaterialService,
|
|
|
|
|
|
appServices.pigBreedService,
|
|
|
|
|
|
appServices.pigAgeStageService,
|
|
|
|
|
|
appServices.pigTypeService,
|
2025-11-24 13:25:15 +08:00
|
|
|
|
appServices.recipeService,
|
2025-11-25 18:10:28 +08:00
|
|
|
|
appServices.inventoryService,
|
2025-11-05 21:40:19 +08:00
|
|
|
|
infra.tokenGenerator,
|
2025-12-08 19:10:07 +08:00
|
|
|
|
infra.lora.listenHandler, // 此处传入的是最终组装好的 handler
|
2025-09-27 00:30:47 +08:00
|
|
|
|
)
|
2025-09-15 22:01:00 +08:00
|
|
|
|
|
2025-12-08 19:10:07 +08:00
|
|
|
|
// 5. 组装 Application 对象
|
2025-09-11 21:04:17 +08:00
|
|
|
|
app := &Application{
|
2025-11-19 19:31:51 +08:00
|
|
|
|
cfgPath: configPath,
|
|
|
|
|
|
Config: cfg,
|
|
|
|
|
|
Ctx: selfCtx,
|
|
|
|
|
|
API: apiServer,
|
|
|
|
|
|
Infra: infra,
|
|
|
|
|
|
Domain: domain,
|
|
|
|
|
|
App: appServices,
|
2025-09-11 21:04:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start 启动应用的所有组件并阻塞,直到接收到关闭信号。
|
|
|
|
|
|
func (app *Application) Start() error {
|
2025-11-05 16:00:43 +08:00
|
|
|
|
startCtx, logger := logs.Trace(app.Ctx, app.Ctx, "Start")
|
|
|
|
|
|
logger.Info("应用启动中...")
|
2025-09-11 21:04:17 +08:00
|
|
|
|
|
2025-10-26 15:48:38 +08:00
|
|
|
|
// 1. 启动底层监听器
|
2025-11-05 23:10:51 +08:00
|
|
|
|
if err := app.Infra.lora.loraListener.Listen(startCtx); err != nil {
|
2025-10-09 23:43:19 +08:00
|
|
|
|
return fmt.Errorf("启动 LoRa Mesh 监听器失败: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-26 15:48:38 +08:00
|
|
|
|
// 2. 初始化应用状态 (清理、刷新任务等)
|
2025-11-19 19:31:51 +08:00
|
|
|
|
if err := app.initializeState(startCtx, app.cfgPath); err != nil {
|
2025-10-26 15:48:38 +08:00
|
|
|
|
return fmt.Errorf("初始化应用状态失败: %w", err)
|
2025-09-20 23:50:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-26 15:48:38 +08:00
|
|
|
|
// 3. 启动后台工作协程
|
2025-11-05 21:40:19 +08:00
|
|
|
|
app.Domain.planService.Start(startCtx)
|
2025-09-11 21:04:17 +08:00
|
|
|
|
|
2025-10-26 15:48:38 +08:00
|
|
|
|
// 4. 启动 API 服务器
|
2025-09-12 14:58:07 +08:00
|
|
|
|
app.API.Start()
|
|
|
|
|
|
|
2025-10-26 15:48:38 +08:00
|
|
|
|
// 5. 等待关闭信号
|
2025-09-11 21:04:17 +08:00
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
<-quit
|
|
|
|
|
|
|
|
|
|
|
|
// 接收到信号后,执行优雅关闭
|
|
|
|
|
|
return app.Stop()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Stop 优雅地关闭应用的所有组件。
|
|
|
|
|
|
func (app *Application) Stop() error {
|
2025-11-05 21:40:19 +08:00
|
|
|
|
stopCtx, logger := logs.Trace(app.Ctx, app.Ctx, "Stop")
|
2025-11-05 16:00:43 +08:00
|
|
|
|
logger.Info("应用关闭中...")
|
2025-09-11 21:04:17 +08:00
|
|
|
|
|
2025-09-12 14:58:07 +08:00
|
|
|
|
// 关闭 API 服务器
|
|
|
|
|
|
app.API.Stop()
|
|
|
|
|
|
|
2025-09-11 21:04:17 +08:00
|
|
|
|
// 关闭任务执行器
|
2025-11-05 21:40:19 +08:00
|
|
|
|
app.Domain.planService.Stop(stopCtx)
|
2025-09-11 21:04:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 断开数据库连接
|
2025-11-05 23:10:51 +08:00
|
|
|
|
if err := app.Infra.storage.Disconnect(stopCtx); err != nil {
|
2025-11-05 16:00:43 +08:00
|
|
|
|
logger.Errorw("数据库连接断开失败", "error", err)
|
2025-09-11 21:04:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 23:43:19 +08:00
|
|
|
|
// 关闭 LoRa Mesh 监听器
|
2025-11-05 23:10:51 +08:00
|
|
|
|
if err := app.Infra.lora.loraListener.Stop(stopCtx); err != nil {
|
2025-11-05 16:00:43 +08:00
|
|
|
|
logger.Errorw("LoRa Mesh 监听器关闭失败", "error", err)
|
2025-10-09 23:43:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-11 21:04:17 +08:00
|
|
|
|
// 刷新日志缓冲区
|
2025-11-05 16:00:43 +08:00
|
|
|
|
_ = logger.Sync()
|
2025-09-11 21:04:17 +08:00
|
|
|
|
|
2025-11-05 16:00:43 +08:00
|
|
|
|
logger.Info("应用已成功关闭")
|
2025-09-11 21:04:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|