2023-10-19 09:33:40 +08:00
|
|
|
package semi_physical_train
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-26 15:06:26 +08:00
|
|
|
"sync"
|
2023-10-19 09:33:40 +08:00
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
2024-01-22 13:10:10 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/tpapi"
|
2023-10-19 09:33:40 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/udp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 半实物仿真列车通信接口
|
|
|
|
type SemiPhysicalTrain interface {
|
2024-01-22 13:10:10 +08:00
|
|
|
tpapi.ThirdPartyApiService
|
2023-10-19 09:33:40 +08:00
|
|
|
// 启动半实物仿真消息处理
|
|
|
|
Start(manager SemiPhysicalMessageManager)
|
|
|
|
// 停止半实物仿真消息处理
|
|
|
|
Stop()
|
2023-10-19 14:44:10 +08:00
|
|
|
// 发送列车控制消息
|
|
|
|
SendTrainControlMessage(info *message.DynamicsTrainInfo)
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SemiPhysicalMessageManager interface {
|
|
|
|
// 处理半实物仿真列车控制消息
|
2023-10-19 14:44:10 +08:00
|
|
|
HandleSemiPhysicalTrainControlMsg(b []byte)
|
2023-10-26 15:06:26 +08:00
|
|
|
// 获取半实物启动参数
|
|
|
|
GetSemiPhysicalRunConfig() *config.VobcConfig
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type semiPhysicalTrainImpl struct {
|
2024-01-22 13:10:10 +08:00
|
|
|
tpapi.ThirdPartyApiService
|
2023-10-19 09:33:40 +08:00
|
|
|
trainControlUdpServer udp.UdpServer
|
|
|
|
trainSpeedInfoUdpClient udp.UdpClient
|
|
|
|
|
2024-01-23 09:37:31 +08:00
|
|
|
state tpapi.ThirdPartyApiServiceState
|
2024-01-22 13:10:10 +08:00
|
|
|
udpDelayRecorder *tpapi.UdpDelayRecorder
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
manager SemiPhysicalMessageManager
|
|
|
|
runConfig *config.VobcConfig
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
2024-01-23 09:37:31 +08:00
|
|
|
func (s *semiPhysicalTrainImpl) updateState(state tpapi.ThirdPartyApiServiceState) {
|
|
|
|
s.state = state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semiPhysicalTrainImpl) State() tpapi.ThirdPartyApiServiceState {
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semiPhysicalTrainImpl) Name() string {
|
|
|
|
return Name
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
func (s *semiPhysicalTrainImpl) handleTrainControlMsg(b []byte) {
|
2024-01-22 13:10:10 +08:00
|
|
|
s.udpDelayRecorder.RecordInterval()
|
2024-01-24 13:25:00 +08:00
|
|
|
// slog.Debug(fmt.Sprintf("半实物列车控制消息近期消息间隔: %v", s.udpDelayRecorder.GetIntervals()))
|
2023-10-19 14:44:10 +08:00
|
|
|
handler := s.manager
|
|
|
|
if handler != nil {
|
|
|
|
handler.HandleSemiPhysicalTrainControlMsg(b)
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
func (s *semiPhysicalTrainImpl) Start(manager SemiPhysicalMessageManager) {
|
2023-10-26 15:06:26 +08:00
|
|
|
if manager == nil {
|
|
|
|
panic("启动半实物消息服务错误: SemiPhysicalMessageManager不能为nil")
|
|
|
|
}
|
|
|
|
if s.manager != nil {
|
|
|
|
panic("启动半实物消息服务错误: 存在正在运行的任务")
|
|
|
|
}
|
|
|
|
s.runConfig = manager.GetSemiPhysicalRunConfig()
|
|
|
|
if s.runConfig == nil || s.runConfig.Ip == "" || !s.runConfig.Open {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 初始化客户端、服务端
|
|
|
|
s.initSemiPhysical()
|
2023-10-19 14:44:10 +08:00
|
|
|
s.manager = manager
|
2024-01-23 09:37:31 +08:00
|
|
|
s.updateState(tpapi.ThirdPartyState_Normal)
|
2024-01-22 13:10:10 +08:00
|
|
|
s.udpDelayRecorder.Start()
|
2023-10-19 14:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semiPhysicalTrainImpl) Stop() {
|
2023-11-01 16:55:16 +08:00
|
|
|
initMutex.Lock()
|
|
|
|
defer initMutex.Unlock()
|
2024-01-22 13:10:10 +08:00
|
|
|
s.udpDelayRecorder.Stop()
|
2023-10-26 15:06:26 +08:00
|
|
|
if s.trainControlUdpServer != nil {
|
|
|
|
s.trainControlUdpServer.Close()
|
|
|
|
}
|
|
|
|
if s.trainSpeedInfoUdpClient != nil {
|
|
|
|
s.trainSpeedInfoUdpClient.Close()
|
|
|
|
}
|
2023-10-19 14:44:10 +08:00
|
|
|
s.manager = nil
|
2024-01-23 09:37:31 +08:00
|
|
|
s.updateState(tpapi.ThirdPartyState_Closed)
|
2023-10-19 14:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semiPhysicalTrainImpl) SendTrainControlMessage(info *message.DynamicsTrainInfo) {
|
|
|
|
sendMsg := &message.TrainSpeedMsg{}
|
|
|
|
sendMsg.DynamicsDecode(info)
|
|
|
|
s.trainSpeedInfoUdpClient.Send(sendMsg.Encode())
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (s *semiPhysicalTrainImpl) initSemiPhysical() {
|
|
|
|
s.trainSpeedInfoUdpClient = udp.NewClient(fmt.Sprintf("%v:%v", s.runConfig.Ip, s.runConfig.RemotePort))
|
|
|
|
s.trainControlUdpServer = udp.NewServer(fmt.Sprintf(":%d", s.runConfig.LocalPort), s.handleTrainControlMsg)
|
2023-10-19 14:44:10 +08:00
|
|
|
s.trainControlUdpServer.Listen()
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
2024-01-23 09:37:31 +08:00
|
|
|
var _default *semiPhysicalTrainImpl
|
2023-10-26 15:06:26 +08:00
|
|
|
var initMutex sync.Mutex
|
2023-10-19 09:33:40 +08:00
|
|
|
|
2024-01-23 09:37:31 +08:00
|
|
|
const Name = "半实物仿真列车"
|
2024-01-22 13:10:10 +08:00
|
|
|
const Interval int = 20
|
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
func Default() SemiPhysicalTrain {
|
2023-10-26 15:06:26 +08:00
|
|
|
initMutex.Lock()
|
|
|
|
defer initMutex.Unlock()
|
|
|
|
if _default == nil {
|
2024-01-22 13:10:10 +08:00
|
|
|
_default = &semiPhysicalTrainImpl{
|
|
|
|
udpDelayRecorder: tpapi.NewUdpDelayRecorder(Interval, func(err error) {
|
|
|
|
if err != nil {
|
2024-01-23 09:37:31 +08:00
|
|
|
_default.updateState(tpapi.ThirdPartyState_Broken)
|
2024-01-22 13:10:10 +08:00
|
|
|
} else {
|
2024-01-23 09:37:31 +08:00
|
|
|
_default.updateState(tpapi.ThirdPartyState_Normal)
|
2024-01-22 13:10:10 +08:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2023-10-19 14:44:10 +08:00
|
|
|
}
|
|
|
|
return _default
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|