rts-sim-testing-service/third_party/electrical_machinery/electrical_machinery.go

170 lines
5.1 KiB
Go
Raw Normal View History

package electrical_machinery
import (
2024-05-29 11:48:04 +08:00
"encoding/hex"
"fmt"
2024-04-28 11:01:12 +08:00
"joylink.club/bj-rtsts-server/dto/common_proto"
"joylink.club/bj-rtsts-server/dto/state_proto"
"log/slog"
"sync"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
)
2024-04-28 11:01:12 +08:00
const (
PointA = iota + 1
PointB
)
// 电机转速UDP
type ElectricMachinery interface {
2024-04-28 11:01:12 +08:00
Start(manager ElectricMachineryMessageManager) // 启动电机转速UDP消息处理
Stop() // 停止电机转速消息处理
SendElectricMachineryMessage(emMap map[int]*message.ElectricMachinery) // 发送电机转速消息
SendElectricMachineryMessage2(info *message.DynamicsTrainInfo, trainState *state_proto.TrainState) // 发送电机转速消息
ClearOrRemoveTrain(trainState *state_proto.TrainState)
}
type ElectricMachineryMessageManager interface {
2024-04-28 11:01:12 +08:00
GetElectricMachineryRunConfig() []config.ElectricMachineryConfig // 获取电机转速参数
}
type electricalMachineryImpl struct {
2024-04-28 11:01:12 +08:00
//electricalMachineryUdpClient udp.UdpClient
2024-04-28 11:01:12 +08:00
electricalMachineryUdpClientMap map[int]udp.UdpClient
manager ElectricMachineryMessageManager
runConfig []config.ElectricMachineryConfig
}
func (s *electricalMachineryImpl) Start(manager ElectricMachineryMessageManager) {
if manager == nil {
panic("启动电机转速消息服务错误: ElectricMachineryMessageManager不能为nil")
}
if s.manager != nil {
panic("启动电机转速消息服务错误: 存在正在运行的任务")
}
s.runConfig = manager.GetElectricMachineryRunConfig()
2024-04-28 11:01:12 +08:00
allNotOpen := true
for _, c := range s.runConfig {
2024-05-29 11:48:04 +08:00
if &c != nil && c.Open {
2024-04-28 11:01:12 +08:00
allNotOpen = false
}
}
if allNotOpen {
return
}
// 初始化客户端、服务端
s.initElectricalMachinery()
s.manager = manager
}
func (s *electricalMachineryImpl) Stop() {
initMutex.Lock()
defer initMutex.Unlock()
_default = nil
2024-04-28 11:01:12 +08:00
//if s.electricalMachineryUdpClient != nil {
// s.electricalMachineryUdpClient.Close()
//}
for _, c := range s.electricalMachineryUdpClientMap {
if &c != nil {
c.Close()
}
}
s.manager = nil
}
func (s *electricalMachineryImpl) ClearOrRemoveTrain(trainState *state_proto.TrainState) {
collectSpeedMap := initEMMsg(trainState)
if trainState.VobcState.Tc1Active {
collectSpeed(0, 0, trainState.TrainEndsA, collectSpeedMap)
} else if trainState.VobcState.Tc2Active {
collectSpeed(0, 0, trainState.TrainEndsB, collectSpeedMap)
}
// 更新电机转速
s.SendElectricMachineryMessage(collectSpeedMap)
}
2024-04-28 11:01:12 +08:00
func (s *electricalMachineryImpl) SendElectricMachineryMessage(emMap map[int]*message.ElectricMachinery) {
for key, em := range emMap {
client := s.electricalMachineryUdpClientMap[key]
if client != nil {
2024-05-29 11:48:04 +08:00
data := em.Encode()
hexStr := hex.EncodeToString(data)
slog.Info("发送电机转速消息key", hexStr)
err := client.Send(data)
2024-04-28 11:01:12 +08:00
if err != nil {
2024-05-29 11:48:04 +08:00
slog.Info("发送电机转速消息失败key", hexStr)
2024-04-28 11:01:12 +08:00
}
}
}
}
func (s *electricalMachineryImpl) SendElectricMachineryMessage2(info *message.DynamicsTrainInfo, trainState *state_proto.TrainState) {
collectSpeedMap := initEMMsg(trainState)
if trainState.VobcState.Tc1Active {
collectSpeed(info.HeadSpeed1, info.HeadSpeed2, trainState.TrainEndsA, collectSpeedMap)
} else if trainState.VobcState.Tc2Active {
collectSpeed(info.TailSpeed1, info.TailSpeed2, trainState.TrainEndsB, collectSpeedMap)
}
2024-04-28 11:01:12 +08:00
// 更新电机转速
s.SendElectricMachineryMessage(collectSpeedMap)
}
func collectSpeed(speed1, speed2 float32, endP *common_proto.TrainEndsState, resultMap map[int]*message.ElectricMachinery) {
pa := resultMap[PointA]
pb := resultMap[PointB]
if endP.SpeedSensorEnableA || endP.SpeedSensorEnableB {
if endP.AccOutSpeed > 0 {
pa.Speed = float32(endP.AccOutSpeed) / 3.6
pb.Speed = float32(endP.AccOutSpeed) / 3.6
} else {
if endP.SpeedSensorEnableA {
pa.Speed = speed1
}
if endP.SpeedSensorEnableB {
pb.Speed = speed2
}
}
2024-04-19 16:55:51 +08:00
}
}
2024-04-28 11:01:12 +08:00
func initEMMsg(trainState *state_proto.TrainState) map[int]*message.ElectricMachinery {
collectSpeedMap := map[int]*message.ElectricMachinery{
PointA: {
Speed: 0,
WheelDiameter: trainState.WheelDiameter,
IsBack: trainState.VobcState.DirectionBackward},
PointB: {Speed: 0,
WheelDiameter: trainState.WheelDiameter,
IsBack: trainState.VobcState.DirectionBackward},
}
return collectSpeedMap
}
func (s *electricalMachineryImpl) initElectricalMachinery() {
2024-05-29 12:10:01 +08:00
s.electricalMachineryUdpClientMap = map[int]udp.UdpClient{}
2024-04-28 11:01:12 +08:00
for _, c := range s.runConfig {
if &c != nil && c.Ip != "" && c.Open {
ep := PointA
if !c.EndPointA {
ep = PointB
}
s.electricalMachineryUdpClientMap[ep] = udp.NewClient(fmt.Sprintf("%v:%v", c.Ip, c.RemotePort))
}
}
}
var _default ElectricMachinery
var initMutex sync.Mutex
func Default() ElectricMachinery {
initMutex.Lock()
defer initMutex.Unlock()
if _default == nil {
_default = &electricalMachineryImpl{}
}
return _default
}