增加待机报文缓存,无网状态也能执行待机任务;投影仪指令结果以设备状态为准
This commit is contained in:
18
internal/middleware/cache.go
Normal file
18
internal/middleware/cache.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"game-driver/config"
|
||||
"game-driver/leaf"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Cache 缓存中间件
|
||||
func Cache(cache config.Cache) leaf.HandlerFunc {
|
||||
return func(c *leaf.Context) {
|
||||
err := cache.Set(c.Publish)
|
||||
if err != nil {
|
||||
zap.S().Errorln("缓存数据失败: ", err)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// WaitAction 待机任务,支持音乐、TTS、继电器、视频、网页、投影仪、大型激光秀 ctrl
|
||||
func WaitAction(ctrl *common.CtrlWait, device *common.Device) leaf.HandlerFunc {
|
||||
// StandbyAction 待机任务,支持音乐、TTS、继电器、视频、网页、投影仪、大型激光秀 ctrl
|
||||
func StandbyAction(ctrl *common.CtrlWait, device *common.Device) leaf.HandlerFunc {
|
||||
ps := common.NewPauseSub(ctrl)
|
||||
|
||||
return func(c *leaf.Context) {
|
||||
@@ -15,20 +15,20 @@ func PJLink(_ schema.WaitItemModel) func(c context.Context) error {
|
||||
pjc := pjlink.NewClient(cfg.Ip, cfg.Port, cfg.Password, cfg.Id)
|
||||
|
||||
zap.S().Infoln("打开待机投影仪")
|
||||
resp, err := pjc.PowerOn()
|
||||
resp, err := pjc.PowerOnSync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开投影仪异常: %w", err)
|
||||
}
|
||||
zap.S().Infoln("投影仪返回报文:", resp)
|
||||
zap.S().Infoln("打开投影仪结果:", resp)
|
||||
|
||||
<-c.Done()
|
||||
|
||||
zap.S().Infoln("关闭待机投影仪")
|
||||
resp, err = pjc.PowerOff()
|
||||
resp, err = pjc.PowerOffSync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("关闭投影仪异常: %w", err)
|
||||
}
|
||||
zap.S().Infoln("投影仪返回报文:", resp)
|
||||
zap.S().Infoln("关闭投影仪结果:", resp)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Device 设备锁定控制器
|
||||
func Device(d *common.Device, lock bool, play func(c context.Context) error) func(c context.Context) error {
|
||||
return func(c context.Context) error {
|
||||
if lock {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Interval 循环间隔控制器
|
||||
func Interval(interval int64, play func(c context.Context) error) func(c context.Context) error {
|
||||
return func(c context.Context) error {
|
||||
zap.S().Infoln("待机间隔控制器: ", interval)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Pause 暂停控制器
|
||||
func Pause(ps *common.PauseSub, isPause bool, play func(c context.Context) error) func(c context.Context) error {
|
||||
return func(c context.Context) error {
|
||||
var cancel context.CancelFunc
|
||||
|
||||
@@ -13,9 +13,6 @@ import (
|
||||
"game-driver/pkg/relay"
|
||||
"game-driver/pkg/tts"
|
||||
"game-driver/pkg/utils"
|
||||
"github.com/eclipse/paho.golang/autopaho"
|
||||
"github.com/eclipse/paho.golang/paho"
|
||||
"go.uber.org/zap"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
@@ -23,6 +20,10 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/eclipse/paho.golang/autopaho"
|
||||
"github.com/eclipse/paho.golang/paho"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func buildMqtt(c config.MqttConfig, r *leaf.Engine, subTopics ...string) autopaho.ClientConfig {
|
||||
@@ -153,9 +154,10 @@ func Run() {
|
||||
router.RegisterHandler(topicPrefix+"wait",
|
||||
middleware.RunLog(),
|
||||
middleware.PayloadJSON[schema.WaitModel](),
|
||||
middleware.Cache(config.C.StandbyCache),
|
||||
middleware.Unique(common.GlobalBgStopper),
|
||||
middleware.EmergencyStop(common.GlobalBgStopper),
|
||||
routes.WaitAction(common.PassCtrl, device),
|
||||
routes.StandbyAction(common.PassCtrl, device),
|
||||
)
|
||||
// 处理指令
|
||||
router.RegisterHandler(topicPrefix+"command",
|
||||
@@ -163,13 +165,21 @@ func Run() {
|
||||
routes.Command(device),
|
||||
)
|
||||
|
||||
// 从缓存中读取待机报文,如果存在则直接执行
|
||||
publish, err := config.C.StandbyCache.Get()
|
||||
if err != nil {
|
||||
zap.S().Infoln("读取待机缓存失败: ", err)
|
||||
} else {
|
||||
router.HandlerRun(topicPrefix+"wait", publish)
|
||||
}
|
||||
|
||||
// 构建 MQTT 连接
|
||||
mqttBuild := buildMqtt(config.C.Mqtt, router, topicPrefix+"#")
|
||||
|
||||
// 连接 MQTT
|
||||
// 开始连接 MQTT
|
||||
cm, err := autopaho.NewConnection(ctx, mqttBuild)
|
||||
if err != nil {
|
||||
zap.S().Panicln("连接 MQTT 异常: ", err)
|
||||
zap.S().Panicln("创建 MQTT 连接器异常: ", err)
|
||||
}
|
||||
utils.GlobalMqttClient = cm
|
||||
|
||||
|
||||
Reference in New Issue
Block a user