修改infra.repository包

This commit is contained in:
2025-11-05 23:00:07 +08:00
parent 97aea66f7c
commit 10b123ab93
25 changed files with 877 additions and 608 deletions

View File

@@ -40,7 +40,7 @@ func initInfrastructure(ctx context.Context, cfg *config.Config) (*Infrastructur
return nil, err
}
repos := initRepositories(ctx, storage.GetDB())
repos := initRepositories(ctx, storage.GetDB(ctx))
lora, err := initLora(ctx, cfg, repos)
if err != nil {
@@ -279,12 +279,12 @@ func initLora(
if cfg.Lora.Mode == config.LoraMode_LoRaWAN {
logger.Info("当前运行模式: lora_wan。初始化 ChirpStack 监听器和传输层。")
listenHandler = webhook.NewChirpStackListener(logs.AddCompName(baseCtx, "ChirpStackListener"), repos.sensorDataRepo, repos.deviceRepo, repos.areaControllerRepo, repos.deviceCommandLogRepo, repos.pendingCollectionRepo)
comm = lora.NewChirpStackTransport(cfg.ChirpStack, logs.AddCompName(baseCtx, "ChirpStackTransport"))
comm = lora.NewChirpStackTransport(logs.AddCompName(baseCtx, "ChirpStackTransport"), cfg.ChirpStack)
loraListener = lora.NewPlaceholderTransport(logs.AddCompName(baseCtx, "PlaceholderTransport"))
} else {
logger.Info("当前运行模式: lora_mesh。初始化 LoRa Mesh 传输层和占位符监听器。")
listenHandler = webhook.NewPlaceholderListener(logs.AddCompName(baseCtx, "PlaceholderListener"))
tp, err := lora.NewLoRaMeshUartPassthroughTransport(cfg.LoraMesh, logs.AddCompName(baseCtx, "LoRaMeshTransport"), repos.areaControllerRepo, repos.pendingCollectionRepo, repos.deviceRepo, repos.sensorDataRepo)
tp, err := lora.NewLoRaMeshUartPassthroughTransport(logs.AddCompName(baseCtx, "LoRaMeshTransport"), cfg.LoraMesh, repos.areaControllerRepo, repos.pendingCollectionRepo, repos.deviceRepo, repos.sensorDataRepo)
if err != nil {
return nil, fmt.Errorf("无法初始化 LoRa Mesh 模块: %w", err)
}
@@ -319,6 +319,7 @@ func initNotifyService(
// 2. 根据配置,按需创建并收集所有启用的其他 Notifier 实例
if cfg.SMTP.Enabled {
smtpNotifier := notify.NewSMTPNotifier(
logs.AddCompName(baseCtx, "SMTPNotifier"),
cfg.SMTP.Host,
cfg.SMTP.Port,
cfg.SMTP.Username,
@@ -331,6 +332,7 @@ func initNotifyService(
if cfg.WeChat.Enabled {
wechatNotifier := notify.NewWechatNotifier(
logs.AddCompName(baseCtx, "WechatNotifier"),
cfg.WeChat.CorpID,
cfg.WeChat.AgentID,
cfg.WeChat.Secret,
@@ -341,6 +343,7 @@ func initNotifyService(
if cfg.Lark.Enabled {
larkNotifier := notify.NewLarkNotifier(
logs.AddCompName(baseCtx, "LarkNotifier"),
cfg.Lark.AppID,
cfg.Lark.AppSecret,
)
@@ -387,7 +390,7 @@ func initNotifyService(
func initStorage(ctx context.Context, cfg config.DatabaseConfig) (database.Storage, error) {
// 创建存储实例
storage := database.NewStorage(logs.AddCompName(context.Background(), "Storage"), cfg)
if err := storage.Connect(); err != nil {
if err := storage.Connect(ctx); err != nil {
// 错误已在 Connect 内部被记录,这里只需包装并返回
return nil, fmt.Errorf("数据库连接失败: %w", err)
}