2023-10-19 09:33:40 +08:00
|
|
|
package semi_physical_train
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/udp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 半实物仿真列车通信接口
|
|
|
|
type SemiPhysicalTrain interface {
|
|
|
|
// 启动半实物仿真消息处理
|
|
|
|
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-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type semiPhysicalTrainImpl struct {
|
|
|
|
trainControlUdpServer udp.UdpServer
|
|
|
|
trainSpeedInfoUdpClient udp.UdpClient
|
|
|
|
|
|
|
|
manager SemiPhysicalMessageManager
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
func (s *semiPhysicalTrainImpl) handleTrainControlMsg(b []byte) {
|
|
|
|
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) {
|
|
|
|
s.manager = manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semiPhysicalTrainImpl) Stop() {
|
|
|
|
s.manager = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func newSemiPhysicalTrain() SemiPhysicalTrain {
|
|
|
|
s := &semiPhysicalTrainImpl{
|
2023-10-19 14:44:10 +08:00
|
|
|
trainSpeedInfoUdpClient: udp.NewClient(fmt.Sprintf("%v:%v", config.Config.Vobc.Ip, config.Config.Vobc.RemotePort)),
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
2023-10-19 14:44:10 +08:00
|
|
|
s.trainControlUdpServer = udp.NewServer(fmt.Sprintf(":%d", config.Config.Vobc.LocalPort), s.handleTrainControlMsg)
|
|
|
|
s.trainControlUdpServer.Listen()
|
2023-10-19 09:33:40 +08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
var _default SemiPhysicalTrain
|
2023-10-19 09:33:40 +08:00
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
func Default() SemiPhysicalTrain {
|
|
|
|
if !config.Config.Vobc.Open {
|
|
|
|
panic("半实物仿真接口模块未开启")
|
|
|
|
}
|
|
|
|
return _default
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
2023-10-19 14:44:10 +08:00
|
|
|
func Init() {
|
|
|
|
if !config.Config.Vobc.Open {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_default = newSemiPhysicalTrain()
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|