rts-sim-testing-service/third_party/btm_vobc/btm_vobc.go
2024-05-29 15:19:04 +08:00

115 lines
3.4 KiB
Go

package btm_vobc
import (
"context"
"encoding/hex"
"fmt"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
"log/slog"
"sync"
)
type BtmVobcManage interface {
GetBtmVobcConfig() config.BtmVobcConfig
}
type BtmVobcService interface {
Start(btmVobcManage BtmVobcManage)
Stop()
SendData(data []byte)
}
type BtmVobcClient struct {
calFun context.CancelFunc
client udp.UdpClient
server udp.UdpServer
manage BtmVobcManage
}
var (
btmVobcLocker sync.Mutex
btmVobcClient *BtmVobcClient
)
func Default() BtmVobcService {
defer btmVobcLocker.Unlock()
btmVobcLocker.Lock()
if btmVobcClient == nil {
btmVobcClient = &BtmVobcClient{}
}
return btmVobcClient
}
func (b *BtmVobcClient) Start(btmVobcManage BtmVobcManage) {
cfg := btmVobcManage.GetBtmVobcConfig()
if !cfg.Open {
slog.Info("11号线 btm vobc配置未开启...")
return
}
udpServer := udp.NewServer(fmt.Sprintf("%v:%d", cfg.LocalUdpIp, cfg.LocalUdpPort), b.handleBtmVobcFrames)
err := udpServer.Listen()
if err != nil {
slog.Error("11号线 btm VOBC 服务启动失败...")
return
}
//
udpClient := udp.NewClient(fmt.Sprintf("%s:%d", cfg.RemoteIp, cfg.RemoteUdpPort))
b.manage = btmVobcManage
b.server = udpServer
b.client = udpClient
}
func (b *BtmVobcClient) handleBtmVobcFrames(cfs []byte) {
frameType, dataText, err := message.BtmVobcDecode(cfs)
if err != nil {
return
}
if frameType == message.COMMAND_TYPE {
idCommand := &message.BtmVobcIdCommand{}
idCommand.Decode(dataText)
slog.Info("成功接受btm vobc的id命令帧,原数据", hex.EncodeToString(cfs))
//userMsg, _ := hex.DecodeString(testUserBtmMsg)
//msg := &message.BtmVobcMessage{FontTtl: 5, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 4, BtmMsg: userMsg, ResponseTime: 10,
// VobcLifeNum: idCommand.VobcLifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: idCommand.AutoIdFrame}}
//sendData := msg.Encode()
//fmt.Println("发送btm vobc len:", len(sendData), "报文:", hex.EncodeToString(sendData), "报文序列号:", msg.MsgSerial)
//btmCli.Send(sendData)
//freeMsg, _ := hex.DecodeString(freeBtmMsg)
//msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0),
// FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: idCommand.VobcLifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: idCommand.AutoIdFrame}}
//sendData2 := msg2.Encode()
//fmt.Println("发送btm vobc 空报文:", hex.EncodeToString(sendData2), "len:", len(sendData2), "报文序列号:", msg2.MsgSerial, "atoId=", idCommand.AutoIdFrame)
//btmCli.Send(sendData2)
} else if frameType == message.REQUEST_TYPE {
req := &message.BtmVobcReq{}
req.Decode(dataText)
fmt.Println(req, "========================")
} /*else {
slog.Error("btm vobc 解析未知命令帧类型", strconv.FormatInt(int64(frameType), 16), frameType, "原始数据:", hex.EncodeToString(cfs), "长度:", len(cfs))
return
}*/
}
func (b *BtmVobcClient) SendData(data []byte) {
if b.client != nil {
slog.Info("发送btm vobc 报文:", hex.EncodeToString(data), "长度:", len(data))
err := b.client.Send(data)
if err != nil {
slog.Error("发送btm vobc 报文失败:", err)
return
}
}
}
func (b *BtmVobcClient) Stop() {
if b.server != nil {
b.server.Close()
}
if b.client != nil {
b.client.Close()
}
}