解决部分已知bug

This commit is contained in:
2025-02-27 11:36:40 +08:00
parent 593d7758bf
commit 8780f8555e
14 changed files with 96 additions and 59 deletions

View File

@@ -2,6 +2,7 @@ package routes
import (
"context"
"fmt"
"game-driver/internal/middleware"
"game-driver/internal/schema"
"game-driver/leaf"
@@ -18,10 +19,12 @@ import (
"time"
)
func runAction(c *leaf.Context, item schema.WaitItemModel, rootRules []cronrange.Rule, play func(c context.Context, item schema.WaitItemModel)) {
func runAction(c *leaf.Context, item schema.WaitItemModel, rootRules []cronrange.Rule, play func(c context.Context, item schema.WaitItemModel) error) {
// 设定默认时间规则
if item.Cron == "" {
item.Cron = "* * * *"
}
rules, err := cronrange.Parse(item.Cron)
if err != nil {
zap.S().Errorln("解析时间规则异常: ", err)
@@ -64,7 +67,17 @@ func runAction(c *leaf.Context, item schema.WaitItemModel, rootRules []cronrange
return
default:
if run {
play(ctx, item)
err := play(ctx, item)
if err != nil {
zap.S().Errorln("执行动作异常: ", err)
<-time.After(time.Minute)
}
} else {
select {
case <-c.Done():
return
case <-time.After(time.Second):
}
}
}
}
@@ -73,6 +86,11 @@ func runAction(c *leaf.Context, item schema.WaitItemModel, rootRules []cronrange
func WaitAction(c *leaf.Context) {
payload := leaf.Value[*schema.WaitModel](c, middleware.PayloadJSONKey)
// 设定默认时间规则
if payload.Cron == "" {
payload.Cron = "* * * *"
}
rules, err := cronrange.Parse(payload.Cron)
if err != nil {
zap.S().Errorln("解析时间规则异常: ", err)
@@ -125,11 +143,13 @@ func WaitAction(c *leaf.Context) {
}
}
func audioAction(c context.Context, item schema.WaitItemModel) {
func audioAction(c context.Context, item schema.WaitItemModel) error {
data, err := utils.LinkAudio(item.Data)
if err != nil {
zap.S().Errorln("音频数据获取异常: ", err)
return
return fmt.Errorf("音频数据获取异常: %w", err)
}
if data == nil {
return fmt.Errorf("音频数据获取为空")
}
zap.S().Infoln("播放待机音乐")
@@ -138,8 +158,7 @@ func audioAction(c context.Context, item schema.WaitItemModel) {
ctrl, closer, e := audio.PlayBgmMP3(data)
defer closer()
if e != nil {
zap.S().Errorln("播放待机音乐异常", e)
return
return fmt.Errorf("播放待机音乐异常: %w", e)
}
<-c.Done()
@@ -147,13 +166,14 @@ func audioAction(c context.Context, item schema.WaitItemModel) {
speaker.Lock()
ctrl.Streamer = nil
speaker.Unlock()
return nil
}
func ttsAction(c context.Context, item schema.WaitItemModel) {
func ttsAction(c context.Context, item schema.WaitItemModel) error {
reader, err := tts.DefaultTTS.Get(item.Data)
if err != nil {
zap.S().Errorln("语音合成异常: ", err)
return
return fmt.Errorf("语音合成异常: %w", err)
}
zap.S().Infoln("循环播放待机 TTS 语音")
@@ -163,17 +183,16 @@ func ttsAction(c context.Context, item schema.WaitItemModel) {
audio.PlayWav(c, reader)
select {
case <-c.Done():
return
return nil
case <-time.After(time.Duration(item.Interval) * time.Second):
}
}
}
func relayAction(c context.Context, item schema.WaitItemModel) {
func relayAction(c context.Context, item schema.WaitItemModel) error {
r, err := relay.New(item.Data)
if err != nil {
zap.S().Errorln("继电器初始化异常: ", err)
return
return fmt.Errorf("继电器初始化异常: %w", err)
}
defer r.Close()
@@ -183,13 +202,14 @@ func relayAction(c context.Context, item schema.WaitItemModel) {
_ = r.On(0)
<-c.Done()
_ = r.Off(0)
return nil
}
func videoAction(c context.Context, item schema.WaitItemModel) {
func videoAction(c context.Context, item schema.WaitItemModel) error {
local, err := utils.LinkVideo(item.Data)
if err != nil {
zap.S().Errorln("视频文件获取异常: ", err)
return
return fmt.Errorf("视频文件获取异常: %w", err)
}
zap.S().Infoln("循环播放待机视频")
@@ -201,18 +221,17 @@ func videoAction(c context.Context, item schema.WaitItemModel) {
for {
err := video.Play(c, local)
if err != nil {
zap.S().Infof("视频播放异常: %s", err)
return
return fmt.Errorf("视频播放异常: %w", err)
}
select {
case <-c.Done():
return
return nil
case <-time.After(time.Duration(item.Interval) * time.Second):
}
}
}
func webAction(c context.Context, item schema.WaitItemModel) {
func webAction(c context.Context, item schema.WaitItemModel) error {
zap.S().Infoln("打开待机网页")
// 控制背光
@@ -220,4 +239,5 @@ func webAction(c context.Context, item schema.WaitItemModel) {
defer utils.BlankClose()
browser.OpenApp(c, item.Data)
return nil
}