From 193fa806c2b95cd4a6d07e5c4e43d5641d09badf Mon Sep 17 00:00:00 2001 From: mapleafgo Date: Tue, 10 Dec 2024 16:41:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B8=E6=88=8F=E8=8A=82=E7=82=B9=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=B5=8C=E5=85=A5=E5=9C=A8=E4=B8=80=E4=B8=AA=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 24 ++++++++++++++++++++---- config.yml | 23 ++++++++++++----------- config/config.go | 20 +++++++------------- config/game/game.go | 16 ++++++++++++++++ config/game/push_card.go | 7 +++++++ config/game/wait_card.go | 5 +++++ internal/routes/play/push_card.go | 6 ++++-- internal/routes/play/wait_card.go | 16 +++++++++++++--- internal/server.go | 2 +- 9 files changed, 85 insertions(+), 34 deletions(-) create mode 100644 config/game/game.go create mode 100644 config/game/push_card.go create mode 100644 config/game/wait_card.go diff --git a/cmd/root.go b/cmd/root.go index 4093eec..f1d4864 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,8 +4,11 @@ Copyright © 2024 慕枫Go package cmd import ( + "errors" "game-driver/config" + "game-driver/config/game" "game-driver/internal" + "io/fs" "log" "os" @@ -45,22 +48,35 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.yml", "默认当前目录下的config.yml") + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.yml", "默认当前目录下的 config.yml") } // initConfig reads in config file and ENV variables if set. func initConfig() { viper.SetConfigFile(cfgFile) - viper.AutomaticEnv() // read in environment variables that match - // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { log.Printf("Using config file: %s", viper.ConfigFileUsed()) + } else if errors.Is(err, fs.ErrNotExist) { + log.Printf("无法找到主配置文件: %s", viper.ConfigFileUsed()) + os.Exit(1) + } else { + log.Panicln("read config file error: ", err) } - err := viper.Unmarshal(&config.C) if err != nil { log.Panicln("unmarshal config failed: ", err) } + + // 初始化游戏节点配置 + game.G = game.NewConfig(config.C.Point) + if game.G != nil { // 如果需要游戏配置 + err = viper.UnmarshalKey("game", &game.G) + if err != nil { + log.Panicln("unmarshal game config failed: ", err) + } + } else { + log.Panicln("game config not found") + } } diff --git a/config.yml b/config.yml index 757aa1a..1bccaa0 100755 --- a/config.yml +++ b/config.yml @@ -1,6 +1,7 @@ location: wushan -point: 0 +point: 5 relay: COM7 +maxTimeout: 60 # 单位 s log: level: debug file: @@ -18,13 +19,13 @@ aliyun: timeout: 10 # 单位 s voice: zhifeng_emo game: - maxTimeout: 60 # 单位 s - cardGroups: - - name: gpiochip0 - outOK: 6 - lower: 13 - error: 19 - empty: 26 - push: 11 - reset: 22 - pull: 27 + url: rtu:///dev/ttyUSB0 # 点位 5 的串口地址 +# cardGroups: # 点位 4 的发卡器配置 +# - name: gpiochip0 +# outOK: 6 +# lower: 13 +# error: 19 +# empty: 26 +# push: 11 +# reset: 22 +# pull: 27 diff --git a/config/config.go b/config/config.go index d970ac1..3158ea1 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,6 @@ package config import ( - "game-driver/internal/routes/play/card_pusher" "gopkg.in/natefinch/lumberjack.v2" ) @@ -17,24 +16,19 @@ type AliyunConfig struct { Voice string } -type GameConfig struct { - MaxTimeout int - CardGroups []*card_pusher.LineGroup -} - type Logger struct { File *lumberjack.Logger Level string } type config struct { - Location string - Point int - Relay string - Log Logger - Mqtt MqttConfig - Aliyun AliyunConfig - Game GameConfig + Location string + Point int + Relay string + Log Logger + Mqtt MqttConfig + Aliyun AliyunConfig + MaxTimeout int } var C config diff --git a/config/game/game.go b/config/game/game.go new file mode 100644 index 0000000..e549618 --- /dev/null +++ b/config/game/game.go @@ -0,0 +1,16 @@ +package game + +type Config any + +func NewConfig(point int) Config { + switch point { + case 4: + return ConfigPush{} + case 5: + return ConfigWait{} + default: + return nil + } +} + +var G Config diff --git a/config/game/push_card.go b/config/game/push_card.go new file mode 100644 index 0000000..1ad86f9 --- /dev/null +++ b/config/game/push_card.go @@ -0,0 +1,7 @@ +package game + +import "game-driver/internal/routes/play/card_pusher" + +type ConfigPush struct { + CardGroups []*card_pusher.LineGroup +} diff --git a/config/game/wait_card.go b/config/game/wait_card.go new file mode 100644 index 0000000..086a878 --- /dev/null +++ b/config/game/wait_card.go @@ -0,0 +1,5 @@ +package game + +type ConfigWait struct { + URL string +} diff --git a/internal/routes/play/push_card.go b/internal/routes/play/push_card.go index 6df5d7e..30b5ea3 100644 --- a/internal/routes/play/push_card.go +++ b/internal/routes/play/push_card.go @@ -3,7 +3,7 @@ package play import ( "context" "encoding/json" - "game-driver/config" + "game-driver/config/game" "game-driver/internal/middleware" "game-driver/internal/routes/play/card_pusher" "game-driver/internal/schema" @@ -24,8 +24,10 @@ type ResponseBody struct { } func PushCard(ctx context.Context) leaf.HandlerFunc { + g := (game.G).(game.ConfigPush) + devices := make([]*card_pusher.Device, 0) - for _, group := range config.C.Game.CardGroups { + for _, group := range g.CardGroups { gv, _ := json.Marshal(group) zap.S().Info("发卡器指针:", string(gv)) diff --git a/internal/routes/play/wait_card.go b/internal/routes/play/wait_card.go index a0d6e68..31aedee 100644 --- a/internal/routes/play/wait_card.go +++ b/internal/routes/play/wait_card.go @@ -2,6 +2,8 @@ package play import ( "context" + "errors" + "game-driver/config/game" "game-driver/internal/middleware" "game-driver/internal/schema" "game-driver/leaf" @@ -9,14 +11,22 @@ import ( "game-driver/pkg/channel" "game-driver/pkg/tts" "go.uber.org/zap" + "io/fs" + "os" "sync" "time" ) func WaitCard(ctx context.Context) leaf.HandlerFunc { - reader, err := card_reader.NewReader("rtu:///dev/ttyUSB0") + g := (game.G).(game.ConfigWait) + + reader, err := card_reader.NewReader(g.URL) if err != nil { - zap.S().Panicln("读卡器串口连接失败", err) + if errors.Is(err, fs.ErrNotExist) { + zap.S().Errorf("读卡器串口文件不存在: %s", g.URL) + os.Exit(1) + } + zap.S().Panicf("读卡器串口连接失败 [%T]: %q", err, err) } go func() { <-ctx.Done() @@ -99,7 +109,7 @@ func WaitCard(ctx context.Context) leaf.HandlerFunc { } // 播报恭喜语音 tts.DefaultTTS.Sound(cardOk) - //TODO: 打开炫酷光效 + //TODO: 打开炫酷光效,屏幕跳转恭喜页面 zap.S().Infof("读取到卡片数据%q,开始打开炫酷光效", id) Default(c) } diff --git a/internal/server.go b/internal/server.go index a0f056d..01e42b8 100644 --- a/internal/server.go +++ b/internal/server.go @@ -129,7 +129,7 @@ func Run() { middleware.EmergencyStop(common.GlobalStopper), middleware.SoundStart(), middleware.RelayMaster(nil), - middleware.TimeoutOver(config.C.Game.MaxTimeout), + middleware.TimeoutOver(config.C.MaxTimeout), middleware.TickerAction(), middleware.PlayBgm(), routes.PlayRouter(ctx, config.C.Location, config.C.Point),