基本逻辑完成

This commit is contained in:
2024-11-01 17:40:34 +08:00
commit f9b9beea4b
40 changed files with 1869 additions and 0 deletions

72
internal/common/device.go Normal file
View File

@@ -0,0 +1,72 @@
package common
import (
"context"
"fmt"
"github.com/eclipse/paho.golang/autopaho"
"github.com/eclipse/paho.golang/paho"
"sync"
"sync/atomic"
)
type DeviceMan interface {
sync.Locker
Status() int
PublishStatus()
}
type Device struct {
mu sync.Mutex
C context.Context
cm *autopaho.ConnectionManager
topic string
status atomic.Int32
OnChange func()
}
func (d *Device) Lock() {
defer d.OnChange()
d.mu.Lock()
d.status.Store(1)
}
func (d *Device) Unlock() {
defer d.OnChange()
d.status.Store(0)
d.mu.Unlock()
}
func (d *Device) Status() int {
return int(d.status.Load())
}
// PublishStatus 推送设备状态
func (d *Device) PublishStatus() {
err := d.cm.AwaitConnection(d.C)
if err != nil {
return
}
_, _ = d.cm.Publish(d.C, &paho.Publish{
Topic: d.topic,
Payload: []byte(fmt.Sprint(d.Status())),
QoS: 1,
})
}
func DefaultDevice(ctx context.Context, cm *autopaho.ConnectionManager, topic string) *Device {
return &Device{
C: ctx,
cm: cm,
topic: topic,
}
}
func NewDevice(ctx context.Context, cm *autopaho.ConnectionManager, topic string, onChange func()) *Device {
return &Device{
C: ctx,
cm: cm,
topic: topic,
OnChange: onChange,
}
}

View File

@@ -0,0 +1,58 @@
package common
import (
"sync"
"sync/atomic"
)
type Stopper interface {
Reset()
Stop()
Done() <-chan struct{}
}
var closedchan = make(chan struct{})
func init() {
close(closedchan)
}
type simpleStopper struct {
mu sync.Mutex
done atomic.Value
}
func (g *simpleStopper) Reset() {
g.mu.Lock()
defer g.mu.Unlock()
g.done = atomic.Value{}
}
func (g *simpleStopper) Stop() {
g.mu.Lock()
defer g.mu.Unlock()
d, _ := g.done.Load().(chan struct{})
if d == nil {
g.done.Store(closedchan)
} else {
close(d)
}
}
func (g *simpleStopper) Done() <-chan struct{} {
d := g.done.Load()
if d != nil {
return d.(chan struct{})
}
g.mu.Lock()
defer g.mu.Unlock()
d = g.done.Load()
if d == nil {
d = make(chan struct{})
g.done.Store(d)
}
return d.(chan struct{})
}
// GlobalStopper 全局停止器
var GlobalStopper Stopper = &simpleStopper{}

View File

@@ -0,0 +1,65 @@
package middleware
import (
"bytes"
"game-driver/internal/schema"
"game-driver/leaf"
"game-driver/pkg/audio"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
)
func open(u string) io.ReadCloser {
p, _ := strings.CutPrefix(u, "file://")
f, e := os.Open(p)
if e != nil {
log.Printf("BGM 文件 [%v] 打开错误: %v\n", u, e)
return nil
}
return f
}
func get(u string) io.ReadCloser {
resp, e := http.Get(u)
if e != nil {
log.Printf("BGM 文件 [%v] 下载失败: %v\n", u, e)
return nil
}
return resp.Body
}
func toBuffer(b io.ReadCloser) io.ReadCloser {
data := &bytes.Buffer{}
_, _ = data.ReadFrom(b)
defer b.Close()
return io.NopCloser(data)
}
func PlayBgm() leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
u, err := url.Parse(pm.BGM)
if err != nil {
log.Println("BGM URL 解析错误: ", err)
} else {
var bgm io.ReadCloser
if u.Scheme == "file" {
bgm = open(u.String())
} else if u.Scheme == "http" || u.Scheme == "https" {
bgm = get(u.String())
} else {
log.Printf("不支持的BGM文件协议: %v\n", u.String())
}
// 获取到了资源进行播放
if bgm != nil {
go audio.PlayMP3(c, toBuffer(bgm))
}
}
c.Next()
}
}

View File

@@ -0,0 +1,16 @@
package middleware
import (
"game-driver/internal/common"
"game-driver/leaf"
)
// DeviceLock 设备锁中间件
func DeviceLock(d *common.Device) leaf.HandlerFunc {
return func(c *leaf.Context) {
d.Lock()
defer d.Unlock()
c.Next()
}
}

View File

@@ -0,0 +1,25 @@
package middleware
import (
"encoding/json"
"game-driver/internal/schema"
"game-driver/leaf"
"log"
)
type JSONKey string
const PayloadJSONKey JSONKey = "payload_json"
// PayloadJSON 解析报文
func PayloadJSON() leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := &schema.PlayModal{}
err := json.Unmarshal(c.Payload, pm)
if err != nil {
log.Panicf("报文解析错误: %v\n", err)
}
leaf.WithValue[*schema.PlayModal](c, PayloadJSONKey, pm)
c.Next()
}
}

View File

@@ -0,0 +1,19 @@
package middleware
import (
"game-driver/internal/schema"
"game-driver/leaf"
"game-driver/pkg/relay"
)
// RelayMaster 继电器中间件
func RelayMaster(r *relay.Device) leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
if r != nil && pm.Power {
r.On(1)
defer r.Off(1)
}
c.Next()
}
}

View File

@@ -0,0 +1,26 @@
package middleware
import (
"game-driver/internal/schema"
"game-driver/leaf"
"game-driver/pkg/tts"
)
// SoundStart 开始词播报
func SoundStart(t *tts.AliTTS) leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
t.Sound(pm.TTS.Start)
defer func() {
switch leaf.Value[leaf.EndType](c, leaf.EndKey) {
case leaf.EndTimer:
t.Sound(pm.TTS.End)
case leaf.EndStop:
t.Sound(pm.TTS.Stop)
}
}()
c.Next()
}
}

View File

@@ -0,0 +1,32 @@
package middleware
import (
"game-driver/internal/common"
"game-driver/leaf"
)
// EmergencyStop 紧急停止中间件
func EmergencyStop() leaf.HandlerFunc {
return func(c *leaf.Context) {
cancel := leaf.WithCancel(c)
defer common.GlobalStopper.Reset()
// 结束信号通道
a := make(chan struct{})
// 发送结束信号
defer close(a)
go func() {
select {
case <-a:
case <-common.GlobalStopper.Done():
{
cancel()
leaf.WithValue[leaf.EndType](c, leaf.EndKey, leaf.EndStop)
}
}
}()
c.Next()
}
}

View File

@@ -0,0 +1,80 @@
package middleware
import (
"game-driver/internal/schema"
"game-driver/leaf"
"game-driver/pkg/tts"
"sync"
"time"
)
// TimerAction 定时器中间件,用于定时触发屏幕打印和语音播报。 t 是语音播报实例
func TimerAction(t *tts.AliTTS, maxTimeout int) leaf.HandlerFunc {
return func(c *leaf.Context) {
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
// 构建打印和语音播报的时间映射
printMap := make(map[int]schema.PrintModal, len(pm.Print))
for _, p := range pm.Print {
printMap[p.Time] = p
}
ttsMap := make(map[int]schema.TTSTimer, len(pm.TTS.Timer))
for _, t := range pm.TTS.Timer {
ttsMap[t.Time] = t
}
// 定时器
var timer *time.Timer
if pm.Timeout != 0 {
timer = time.NewTimer(time.Second * time.Duration(pm.Timeout))
} else {
timer = time.NewTimer(time.Second * time.Duration(maxTimeout))
}
defer timer.Stop()
// 等待组
var wait sync.WaitGroup
defer wait.Wait()
// 结束信号通道
a := make(chan struct{})
// 发送结束信号
defer close(a)
cancel := leaf.WithCancel(c)
go func() {
start := time.Now()
// 等待结束
wait.Add(1)
defer wait.Done()
// 定时器
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
// 结束标志
for over := false; !over; {
select {
case <-a:
over = true
case <-timer.C: // 定时器结束
{
cancel()
over = true
leaf.WithValue[leaf.EndType](c, leaf.EndKey, leaf.EndTimer)
}
case m := <-ticker.C:
{
s := int(m.Sub(start).Seconds())
if _, ok := printMap[s]; ok {
//TODO: 屏幕打印
}
if to, ok := ttsMap[s]; ok {
t.Sound(to.Value)
}
}
}
}
}()
c.Next()
}
}

View File

@@ -0,0 +1,21 @@
package routes
import (
"game-driver/internal/common"
"game-driver/leaf"
"log"
)
func Command(d *common.Device) leaf.HandlerFunc {
return func(c *leaf.Context) {
cmd := string(c.Payload)
switch cmd {
case "stop":
common.GlobalStopper.Stop()
case "status":
d.PublishStatus()
default:
log.Printf("接收到无效指令: %s\n", cmd)
}
}
}

30
internal/schema/play.go Normal file
View File

@@ -0,0 +1,30 @@
package schema
type TTSTimer struct {
Time int `json:"time"`
Value string `json:"value"`
}
type TTSModal struct {
Start string `json:"start"`
End string `json:"end"`
Stop string `json:"stop"`
Timer []TTSTimer `json:"timer"`
}
type PrintModal struct {
Time int `json:"time"`
Text string `json:"text"`
Duration int `json:"duration"`
}
type PlayModal struct {
Timeout int `json:"timeout"`
Power bool `json:"power"`
BGM string `json:"bgm"`
Volume float64 `json:"volume"`
DefaultPrint string `json:"default-print"`
TTS TTSModal `json:"tts"`
Print []PrintModal `json:"print"`
Game map[string]any `json:"game"`
}

152
internal/server.go Normal file
View File

@@ -0,0 +1,152 @@
package internal
import (
"context"
"fmt"
"game-driver/config"
"game-driver/internal/common"
"game-driver/internal/middleware"
"game-driver/internal/routes"
"game-driver/leaf"
"game-driver/pkg/tts"
"github.com/eclipse/paho.golang/autopaho"
"github.com/eclipse/paho.golang/paho"
"log"
"net/url"
"os"
"os/signal"
"syscall"
"time"
)
func buildMqtt(c config.MqttConfig, r *leaf.Engine, subTopics ...string) autopaho.ClientConfig {
u, err := url.Parse(c.Url)
if err != nil {
log.Panicln("mqtt url parse error: ", err)
}
subscriptions := make([]paho.SubscribeOptions, 0)
for _, topic := range subTopics {
subscriptions = append(subscriptions, paho.SubscribeOptions{Topic: topic, QoS: 0})
}
mqttConfig := autopaho.ClientConfig{
ServerUrls: []*url.URL{u},
KeepAlive: 20,
CleanStartOnInitialConnection: false,
SessionExpiryInterval: 60,
OnConnectionUp: func(cm *autopaho.ConnectionManager, connAck *paho.Connack) {
log.Println("mqtt connection up")
if _, err := cm.Subscribe(context.Background(), &paho.Subscribe{
Subscriptions: subscriptions,
}); err != nil {
log.Printf("failed to subscribe (%s). This is likely to mean no messages will be received.", err)
return
}
log.Println("mqtt subscription made")
},
OnConnectError: func(err error) {
log.Printf("error whilst attempting connection: %s\n", err)
},
ClientConfig: paho.ClientConfig{
ClientID: "TestSubscriber",
OnPublishReceived: []func(paho.PublishReceived) (bool, error){
func(pr paho.PublishReceived) (bool, error) {
r.Route(pr.Packet.Packet())
return true, nil
},
},
OnClientError: func(err error) { fmt.Printf("client error: %s\n", err) },
OnServerDisconnect: func(d *paho.Disconnect) {
if d.Properties != nil {
fmt.Printf("server requested disconnect: %s\n", d.Properties.ReasonString)
} else {
fmt.Printf("server requested disconnect; reason code: %d\n", d.ReasonCode)
}
},
},
}
return mqttConfig
}
func Run() {
topicPrefix := fmt.Sprintf("server/%s/%v/", config.C.Location, config.C.Point)
publishTopic := fmt.Sprintf("device/%s/%v/status", config.C.Location, config.C.Point)
log.Println("topicPrefix: ", topicPrefix)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
router := leaf.Default(ctx)
router.DefaultHandler(func(c *leaf.Context) {
log.Println("接收到未处理消息: " + string(c.Payload))
})
// 构建 MQTT 连接
mqttBuild := buildMqtt(config.C.Mqtt, router, topicPrefix+"#")
// 连接 MQTT
cm, err := autopaho.NewConnection(ctx, mqttBuild)
if err != nil {
log.Panicln("连接 MQTT 异常: ", err)
}
// 构建语音合成对象
t := tts.New(ctx, config.C.Aliyun)
// 构建继电器对象
//r, err := relay.New(config.C.Relay, func(msg string) {
// log.Println("串口返回: ", msg)
//})
//if err != nil {
// log.Panicln("串口连接异常: ", err)
//}
//defer r.Close()
// 构建全局设备变量
device := common.DefaultDevice(ctx, cm, publishTopic)
// 设备状态变化
device.OnChange = func() { device.PublishStatus() }
// 处理启动报文
router.RegisterHandler(topicPrefix+"play",
middleware.PayloadJSON(),
middleware.DeviceLock(device),
middleware.EmergencyStop(),
middleware.SoundStart(t),
middleware.RelayMaster(nil),
middleware.TimerAction(t, config.C.Game.MaxTimeout),
middleware.PlayBgm(),
func(c *leaf.Context) {
log.Println("接收到启动消息: ", string(c.Payload))
select {
case <-c.Done():
log.Println("程序已关闭")
case <-time.After(10 * time.Second):
log.Println("10s 结束")
}
},
)
// 处理指令
router.RegisterHandler(topicPrefix+"command", routes.Command(device))
// 启动完成发送一次设备状态
device.PublishStatus()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
fmt.Println("接收到关闭命令 - 正在关闭程序")
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := cm.Disconnect(ctx); err != nil {
fmt.Printf("断开连接异常: %s\n", err)
}
fmt.Println("关闭完成")
}