2024-04-02 18:20:10 +08:00
|
|
|
package train_pc_sim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-29 15:19:04 +08:00
|
|
|
"encoding/hex"
|
2024-04-02 18:20:10 +08:00
|
|
|
"fmt"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
"joylink.club/bj-rtsts-server/dto/state_proto"
|
2024-04-09 08:55:33 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
2024-04-02 18:20:10 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/tcp"
|
2024-04-13 09:40:25 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/tpapi"
|
2024-04-02 18:20:10 +08:00
|
|
|
"log/slog"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-04-09 08:55:33 +08:00
|
|
|
type TrainControlEvent struct {
|
|
|
|
Command byte
|
|
|
|
Status byte
|
|
|
|
}
|
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
//var FireTrainControlEventType = ecs.NewEventType[TrainControlEvent]()
|
2024-04-09 08:55:33 +08:00
|
|
|
|
2024-04-02 18:20:10 +08:00
|
|
|
type TrainPcSim interface {
|
2024-04-29 13:58:14 +08:00
|
|
|
tpapi.ThirdPartyApiService
|
|
|
|
Start(pcSimManage TrainPcSimManage)
|
2024-04-02 18:20:10 +08:00
|
|
|
Stop()
|
2024-06-27 10:37:52 +08:00
|
|
|
|
|
|
|
// SendDriverActive Deprecated 发送驾驶端激活
|
2024-05-24 09:00:43 +08:00
|
|
|
SendDriverActive(train *state_proto.TrainState)
|
2024-04-29 13:58:14 +08:00
|
|
|
// SendHandleSwitch 发送牵引制动手柄
|
2024-05-24 09:00:43 +08:00
|
|
|
SendHandleSwitch(oldTraction, oldBrakeForce int64, tractionState bool, train *state_proto.TrainState)
|
2024-04-29 13:58:14 +08:00
|
|
|
// SendTrainDirection 列车运行方向
|
2024-04-16 17:26:37 +08:00
|
|
|
//因文档说明不清楚,在调用的时候目前是注释状态,现场调试可能会用到
|
2024-05-24 09:00:43 +08:00
|
|
|
SendTrainDirection(train *state_proto.TrainState, trainForward, trainBackward bool)
|
2024-04-09 08:55:33 +08:00
|
|
|
//发送应答器信息数据
|
2024-06-13 18:13:21 +08:00
|
|
|
SendBaliseData(train *state_proto.TrainState, msgType byte, data []byte)
|
2024-07-04 09:26:37 +08:00
|
|
|
SendBaliseData2(train *state_proto.TrainState, msgType byte, data []string)
|
2024-04-09 08:55:33 +08:00
|
|
|
//发布列车控制的相关事件
|
2024-05-24 09:00:43 +08:00
|
|
|
PublishTrainControlEvent(train *state_proto.TrainState, events []TrainControlEvent)
|
2024-04-29 13:58:14 +08:00
|
|
|
// CreateOrRemoveSpeedPLace 创建或删除速度位置信息
|
2024-04-09 08:55:33 +08:00
|
|
|
CreateOrRemoveSpeedPLace(train *state_proto.TrainState)
|
2024-04-29 13:58:14 +08:00
|
|
|
// CreateOrRemoveTrain 创建或删除列车
|
2024-06-27 10:37:52 +08:00
|
|
|
CreateOrRemoveTrain(train *state_proto.TrainState, isCreate bool) error
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type TrainPcSimManage interface {
|
2024-05-24 09:00:43 +08:00
|
|
|
GetTrainPcSimConfig() []config.VehiclePCSimConfig
|
|
|
|
//GetConnTrain() *state_proto.TrainState
|
|
|
|
GetConnTrain2() []*state_proto.TrainState
|
2024-06-27 10:37:52 +08:00
|
|
|
//获取列车模拟量数据
|
|
|
|
ObtainTrainDigitalMockData(train *state_proto.TrainState) []message.TrainPcSimBaseMessage
|
2024-04-29 13:58:14 +08:00
|
|
|
// TrainPcSimDigitalOutInfoHandle 4.4.1. 车载输出数字量信息报文内容
|
2024-05-24 09:00:43 +08:00
|
|
|
TrainPcSimDigitalOutInfoHandle(connType state_proto.TrainConnState_TrainConnType, data []byte)
|
2024-04-29 13:58:14 +08:00
|
|
|
// TrainPcSimDigitalReportHandle 4.4.2. 车载输出数字反馈量信息报文内容
|
2024-05-24 09:00:43 +08:00
|
|
|
TrainPcSimDigitalReportHandle(connType state_proto.TrainConnState_TrainConnType, data []byte)
|
2024-07-04 09:26:37 +08:00
|
|
|
FindConnTrain(ct state_proto.TrainConnState_TrainConnType) *state_proto.TrainState
|
2024-04-29 13:58:14 +08:00
|
|
|
// TrainPcSimMockInfo 门模式
|
2024-04-13 09:40:25 +08:00
|
|
|
//TrainDoorModeHandle(state byte)
|
2024-04-09 08:55:33 +08:00
|
|
|
//处理列车pc仿真模拟量数据
|
2024-05-24 09:00:43 +08:00
|
|
|
TrainPcSimMockInfo(connType state_proto.TrainConnState_TrainConnType, data []byte)
|
2024-04-29 13:58:14 +08:00
|
|
|
// TrainBtmQuery 处理列车btm查询
|
2024-05-24 09:00:43 +08:00
|
|
|
TrainBtmQuery(connType state_proto.TrainConnState_TrainConnType, data []byte)
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
|
2024-04-13 09:40:25 +08:00
|
|
|
const Name = "车载pc仿真"
|
2024-06-27 10:37:52 +08:00
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
func FindTrainPcSimClientKey(t *state_proto.TrainState) string {
|
|
|
|
if t.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_A {
|
|
|
|
return "A"
|
|
|
|
} else if t.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_B {
|
|
|
|
return "B"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2024-04-13 09:40:25 +08:00
|
|
|
func (d *trainPcSimService) Name() string {
|
|
|
|
return Name
|
|
|
|
}
|
|
|
|
func (d *trainPcSimService) State() tpapi.ThirdPartyApiServiceState {
|
|
|
|
return d.state
|
|
|
|
}
|
|
|
|
func (d *trainPcSimService) updateState(state tpapi.ThirdPartyApiServiceState) {
|
|
|
|
d.state = state
|
|
|
|
}
|
|
|
|
|
2024-04-02 18:20:10 +08:00
|
|
|
var (
|
|
|
|
initLock = &sync.Mutex{}
|
|
|
|
singleObj *trainPcSimService
|
|
|
|
)
|
|
|
|
|
|
|
|
func Default() TrainPcSim {
|
|
|
|
defer initLock.Unlock()
|
|
|
|
initLock.Lock()
|
|
|
|
if singleObj == nil {
|
|
|
|
singleObj = &trainPcSimService{}
|
|
|
|
}
|
|
|
|
return singleObj
|
|
|
|
}
|
|
|
|
|
|
|
|
type trainPcSimService struct {
|
2024-06-27 10:37:52 +08:00
|
|
|
state tpapi.ThirdPartyApiServiceState
|
2024-06-12 19:50:36 +08:00
|
|
|
newPcSimclientMap map[string]*trainPcReciverData
|
|
|
|
cancleContext context.CancelFunc
|
|
|
|
trainPcSimManage TrainPcSimManage
|
|
|
|
speedPlace *message.TrainSpeedPlaceReportMsg
|
|
|
|
configs []config.VehiclePCSimConfig
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
|
2024-04-13 09:40:25 +08:00
|
|
|
// 接受来自pc仿真的消息
|
|
|
|
func (d *trainPcSimService) readError(err error) {
|
|
|
|
slog.Error("连接车载pc仿真tcp服务断开", err)
|
|
|
|
d.updateState(tpapi.ThirdPartyState_Broken)
|
2024-04-16 17:26:37 +08:00
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
func (d *trainPcSimService) newCloseAllConn() {
|
|
|
|
for _, rd := range d.newPcSimclientMap {
|
|
|
|
if rd != nil {
|
|
|
|
rd.tcpClient.Close()
|
2024-06-27 10:37:52 +08:00
|
|
|
rd.tcpClient = nil
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
|
|
|
|
func (d *trainPcSimService) newCloseConn(clientKey string) {
|
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
if rd != nil {
|
|
|
|
rd.tcpClient.Close()
|
2024-06-27 10:37:52 +08:00
|
|
|
rd.tcpClient = nil
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
func (d *trainPcSimService) findConfig(tcChar string) (*config.VehiclePCSimConfig, error) {
|
|
|
|
configFlag := false
|
|
|
|
if tcChar == "A" {
|
|
|
|
configFlag = true
|
|
|
|
} else if tcChar == "B" {
|
|
|
|
configFlag = false
|
|
|
|
} else {
|
2024-06-27 10:37:52 +08:00
|
|
|
return nil, fmt.Errorf(fmt.Sprintf("未知车载pc连接标识:%v", tcChar))
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
for _, cfg := range d.configs {
|
|
|
|
if cfg.Open && cfg.TrainEnds == configFlag {
|
|
|
|
return &cfg, nil
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
return nil, fmt.Errorf("未找到对应的车载pc连接配置")
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
func (d *trainPcSimService) connTrainPcSim(ctx context.Context) {
|
2024-06-12 19:50:36 +08:00
|
|
|
|
2024-04-18 11:14:05 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-06-12 19:50:36 +08:00
|
|
|
d.newCloseAllConn()
|
2024-04-18 11:14:05 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
trains := d.trainPcSimManage.GetConnTrain2()
|
|
|
|
if len(trains) > 0 {
|
|
|
|
for _, t := range trains {
|
|
|
|
clientKey := FindTrainPcSimClientKey(t)
|
|
|
|
if clientKey == "" {
|
|
|
|
slog.Error("未找到对应的pc仿真连接,trainId:", t.Id, "删除对应客户端")
|
2024-06-12 19:50:36 +08:00
|
|
|
d.newCloseConn(clientKey)
|
2024-05-24 09:00:43 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
if rd == nil {
|
|
|
|
d.newPcSimclientMap[clientKey] = &trainPcReciverData{pcSimManage: d.trainPcSimManage, clientKey: clientKey, tcpClient: &tcp.TcpClient{}}
|
|
|
|
}
|
|
|
|
if !rd.tcpClient.IsConning() {
|
|
|
|
d.newCloseConn(clientKey)
|
|
|
|
d.initConn(clientKey)
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
2024-04-28 11:01:12 +08:00
|
|
|
}
|
2024-04-18 11:14:05 +08:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2024-04-16 17:26:37 +08:00
|
|
|
}
|
2024-04-18 11:14:05 +08:00
|
|
|
}()
|
2024-04-13 09:40:25 +08:00
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
|
|
|
|
func (d *trainPcSimService) initConn(clientKey string) {
|
|
|
|
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
2024-06-27 10:37:52 +08:00
|
|
|
if rd != nil && rd.tcpClient != nil && rd.tcpClient.IsConning() {
|
|
|
|
return
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
rd = &trainPcReciverData{pcSimManage: d.trainPcSimManage, clientKey: clientKey, tcpClient: &tcp.TcpClient{}}
|
|
|
|
d.newPcSimclientMap[clientKey] = rd
|
2024-06-12 19:50:36 +08:00
|
|
|
cfg, _ := d.findConfig(clientKey)
|
|
|
|
addr := fmt.Sprintf("%v:%v", cfg.PcSimIp, cfg.PcSimPort)
|
|
|
|
|
2024-06-13 10:06:54 +08:00
|
|
|
client2, err := tcp.StartTcpClient(addr, rd.receiverDataHandle, d.readError)
|
2024-05-24 09:00:43 +08:00
|
|
|
if err != nil {
|
2024-06-18 17:10:21 +08:00
|
|
|
slog.Error(fmt.Sprintf("车载pc连接失败 clientKey:%v,error:%v", clientKey, err.Error()))
|
2024-05-24 09:00:43 +08:00
|
|
|
d.updateState(tpapi.ThirdPartyState_Broken)
|
|
|
|
} else {
|
2024-06-12 19:50:36 +08:00
|
|
|
rd.tcpClient = client2
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
2024-04-29 13:58:14 +08:00
|
|
|
func (d *trainPcSimService) Start(pcSimManage TrainPcSimManage) {
|
2024-05-24 09:00:43 +08:00
|
|
|
configs := pcSimManage.GetTrainPcSimConfig()
|
2024-06-12 19:50:36 +08:00
|
|
|
d.newPcSimclientMap = make(map[string]*trainPcReciverData)
|
2024-06-27 10:37:52 +08:00
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
if len(configs) <= 0 {
|
|
|
|
slog.Info("车载pc仿真配置未开启")
|
|
|
|
return
|
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
|
|
|
|
closedCount := 0
|
2024-05-24 09:00:43 +08:00
|
|
|
for _, c := range configs {
|
|
|
|
if !c.Open {
|
2024-06-27 10:37:52 +08:00
|
|
|
closedCount++
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
|
|
|
|
if closedCount == len(configs) {
|
|
|
|
slog.Error("车载pc仿真配置未开启")
|
2024-04-02 18:20:10 +08:00
|
|
|
return
|
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
d.configs = configs
|
2024-04-02 18:20:10 +08:00
|
|
|
ctx, ctxFun := context.WithCancel(context.Background())
|
2024-04-13 09:40:25 +08:00
|
|
|
d.cancleContext = ctxFun
|
|
|
|
d.trainPcSimManage = pcSimManage
|
2024-05-24 09:00:43 +08:00
|
|
|
d.connTrainPcSim(ctx)
|
2024-04-13 09:40:25 +08:00
|
|
|
d.updateState(tpapi.ThirdPartyState_Normal)
|
2024-06-18 17:10:21 +08:00
|
|
|
go d.sendTrainLocationAndSpeedTask(ctx)
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
2024-04-13 09:40:25 +08:00
|
|
|
func (d *trainPcSimService) Stop() {
|
|
|
|
d.updateState(tpapi.ThirdPartyState_Closed)
|
|
|
|
if d.cancleContext != nil {
|
|
|
|
d.cancleContext()
|
|
|
|
d.cancleContext = nil
|
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
//d.closeAllConn()
|
|
|
|
d.newCloseAllConn()
|
2024-04-09 08:55:33 +08:00
|
|
|
}
|
2024-04-13 09:40:25 +08:00
|
|
|
func (d *trainPcSimService) CreateOrRemoveSpeedPLace(train *state_proto.TrainState) {
|
2024-05-24 09:00:43 +08:00
|
|
|
if train.ConnState.Conn && (train.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_A || train.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_B) {
|
2024-04-09 08:55:33 +08:00
|
|
|
train.PluseCount = &state_proto.SensorSpeedPulseCount{}
|
2024-04-13 09:40:25 +08:00
|
|
|
d.speedPlace = &message.TrainSpeedPlaceReportMsg{TrainId: train.Id}
|
2024-04-09 08:55:33 +08:00
|
|
|
} else {
|
|
|
|
train.PluseCount = nil
|
2024-04-13 09:40:25 +08:00
|
|
|
d.speedPlace = nil
|
2024-04-09 08:55:33 +08:00
|
|
|
}
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
func (d *trainPcSimService) CreateOrRemoveTrain(train *state_proto.TrainState, isCreate bool) error {
|
2024-05-24 09:00:43 +08:00
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-27 10:37:52 +08:00
|
|
|
d.initConn(clientKey)
|
2024-06-13 14:16:52 +08:00
|
|
|
log := "删除列车"
|
2024-06-27 10:37:52 +08:00
|
|
|
data := []byte{message.FLAG_CAMMAND_REMOVE_TRAIN}
|
|
|
|
if isCreate {
|
2024-06-13 14:16:52 +08:00
|
|
|
log = "创建列车"
|
2024-06-27 10:37:52 +08:00
|
|
|
data[0] = message.FLAG_CAMMAND_CREATE_TRAIN
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
msg := &message.TrainPcSimBaseMessage{Data: data, Type: message.RECIVE_TRAIN_CREATE_REMOVE}
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
if rd != nil {
|
2024-06-13 14:16:52 +08:00
|
|
|
sd := msg.Encode()
|
|
|
|
slog.Info(fmt.Sprintf("%v-列车号:%v,发送数据:%v", log, train.Id, hex.EncodeToString(sd)))
|
|
|
|
err := rd.tcpClient.Send(sd)
|
2024-06-12 19:50:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
initTrainErr := d.initTrain(rd, train, isCreate)
|
|
|
|
if !isCreate {
|
2024-06-12 19:50:36 +08:00
|
|
|
d.newCloseConn(clientKey)
|
|
|
|
}
|
2024-06-27 10:37:52 +08:00
|
|
|
if initTrainErr != nil {
|
|
|
|
return initTrainErr
|
|
|
|
}
|
2024-04-18 11:14:05 +08:00
|
|
|
|
2024-06-27 10:37:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (d *trainPcSimService) initTrain(rd *trainPcReciverData, train *state_proto.TrainState, isCreate bool) error {
|
|
|
|
msgs := make([]message.TrainPcSimBaseMessage, 0)
|
|
|
|
if isCreate {
|
|
|
|
tmpMsgs := d.trainPcSimManage.ObtainTrainDigitalMockData(train)
|
|
|
|
msgs = append(msgs, tmpMsgs...)
|
|
|
|
msgs = append(msgs, message.TrainPcSimBaseMessage{Data: []byte{}, Type: message.SENDER_TRAIN_TC_ACTIVE}) //驾驶室激活
|
|
|
|
msgs = append(msgs, message.TrainPcSimBaseMessage{Data: []byte{message.DRIVER_ACTIVE_REPORT, 1}, Type: message.SENDER_TRAIN_OUTR_INFO}) //驾驶室激活反馈按钮
|
|
|
|
} else {
|
|
|
|
msgs = append(msgs, message.TrainPcSimBaseMessage{Data: []byte{}, Type: message.SENDER_TRAIN_TC_NOT_ACTIVE}) //驾驶室激活
|
|
|
|
}
|
|
|
|
for _, msg := range msgs {
|
|
|
|
data := msg.Encode()
|
|
|
|
hexData := hex.EncodeToString(data)
|
|
|
|
err := rd.tcpClient.Send(data)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(fmt.Sprintf("列车设置激活%v 失败:%v", isCreate, hexData))
|
|
|
|
return fmt.Errorf(fmt.Sprintf("列车设置激活%v 失败:%v", isCreate, hex.EncodeToString(data)), err)
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
return nil
|
2024-04-13 09:40:25 +08:00
|
|
|
}
|
2024-04-02 18:20:10 +08:00
|
|
|
|
|
|
|
// 依据文档80ms发送列车速度位置
|
2024-04-13 09:40:25 +08:00
|
|
|
func (d *trainPcSimService) sendTrainLocationAndSpeedTask(ctx context.Context) {
|
2024-04-02 18:20:10 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
trains := d.trainPcSimManage.GetConnTrain2()
|
|
|
|
for _, train := range trains {
|
|
|
|
if train.ConnState.Conn && train.PluseCount != nil {
|
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
|
|
|
|
s1, s2 := train.PluseCount.PulseCount1, train.PluseCount.PulseCount2
|
|
|
|
d.speedPlace.ParsePulseCount1(s1, s2)
|
|
|
|
data := d.speedPlace.Encode(train.TrainRunUp, s1, s2)
|
2024-06-27 10:37:52 +08:00
|
|
|
bm := &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_LOCATION_INFO, Data: data}
|
2024-06-12 19:50:36 +08:00
|
|
|
train.PluseCount.PulseCount1 = 0
|
|
|
|
train.PluseCount.PulseCount2 = 0
|
2024-06-14 16:48:59 +08:00
|
|
|
dataCode := bm.Encode()
|
2024-07-04 09:26:37 +08:00
|
|
|
slog.Info(fmt.Sprintf("发送列车速度位置,列车:%v,s1: %v,s2: %v,c2: %v,c2: %v,发送数据:%v", train.Id, s1, s2, d.speedPlace.PulseCount1, d.speedPlace.PulseCount2, hex.EncodeToString(dataCode)))
|
2024-06-14 16:48:59 +08:00
|
|
|
err := rd.tcpClient.Send(dataCode)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(fmt.Sprintf("发送列车速度位置失败,列车:%v,发送数据:%v", train.Id, hex.EncodeToString(dataCode)))
|
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
|
2024-04-16 17:26:37 +08:00
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
|
2024-04-02 18:20:10 +08:00
|
|
|
time.Sleep(time.Millisecond * 80)
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 08:55:33 +08:00
|
|
|
|
2024-06-27 10:37:52 +08:00
|
|
|
// SendDriverActive Deprecated 发送驾驶激活
|
2024-05-24 09:00:43 +08:00
|
|
|
func (d *trainPcSimService) SendDriverActive(train *state_proto.TrainState) {
|
|
|
|
vobc := train.VobcState
|
|
|
|
|
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-12 19:50:36 +08:00
|
|
|
//client := d.pcSimClientMap[clientKey]
|
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
2024-05-24 09:00:43 +08:00
|
|
|
defulatBuf := make([]byte, 0)
|
|
|
|
msg := &message.TrainPcSimBaseMessage{Data: defulatBuf}
|
|
|
|
if train.TrainRunUp {
|
|
|
|
if vobc.Tc1Active {
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_TC_ACTIVE
|
2024-05-24 09:00:43 +08:00
|
|
|
} else if vobc.Tc1Active == false {
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_TC_NOT_ACTIVE
|
2024-05-24 09:00:43 +08:00
|
|
|
}
|
|
|
|
} else if !train.TrainRunUp {
|
|
|
|
if vobc.Tc2Active {
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_TC_ACTIVE
|
2024-05-24 09:00:43 +08:00
|
|
|
} else if vobc.Tc2Active == false {
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_TC_NOT_ACTIVE
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
}
|
2024-06-13 18:13:21 +08:00
|
|
|
//:"创建列车-列车号:1,发送数据:eb0050010156e4"}
|
|
|
|
//{,"msg":"发送驾驶激活列车","1":"数据","!BADKEY":"eb0004002437"}
|
|
|
|
|
2024-05-29 15:19:04 +08:00
|
|
|
da := msg.Encode()
|
2024-06-18 17:10:21 +08:00
|
|
|
slog.Info(fmt.Sprintf("发送驾驶激活列车id:%v,数据:%v", train.Id, hex.EncodeToString(da)))
|
2024-06-12 19:50:36 +08:00
|
|
|
err := rd.tcpClient.Send(da)
|
|
|
|
//err := client.Send(da)
|
2024-05-29 15:19:04 +08:00
|
|
|
if err != nil {
|
2024-06-18 17:10:21 +08:00
|
|
|
slog.Error(fmt.Sprintf("发送驾驶激活失败列车id:%v,数据:%v,err:%v", train.Id, hex.EncodeToString(da), err.Error()))
|
2024-05-29 15:19:04 +08:00
|
|
|
}
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
func (d *trainPcSimService) SendHandleSwitch(oldTraction, oldBrakeForce int64, tractionState bool, train *state_proto.TrainState) {
|
|
|
|
tc := train.ConnState
|
|
|
|
if tc.Conn {
|
|
|
|
vobc := train.VobcState
|
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
|
|
|
|
//client := d.pcSimClientMap[clientKey]
|
2024-04-09 08:55:33 +08:00
|
|
|
msg := &message.TrainPcSimBaseMessage{}
|
2024-04-02 18:20:10 +08:00
|
|
|
newTraction := vobc.TractionForce
|
2024-06-27 10:37:52 +08:00
|
|
|
//newBrake := -vobc.BrakeForce
|
|
|
|
//newOldBrakeForce := -oldBrakeForce
|
2024-04-18 11:14:05 +08:00
|
|
|
if tractionState {
|
|
|
|
if newTraction <= oldTraction && newTraction == 0 {
|
2024-04-16 17:26:37 +08:00
|
|
|
//手柄取消前进
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.RECIVE_TRAIN_HAND_KEY_CANCLE_FORWARD
|
2024-04-16 17:26:37 +08:00
|
|
|
} else if newTraction > oldTraction {
|
|
|
|
//手柄前进
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_HAND_KEY_FORWARD
|
2024-04-18 11:14:05 +08:00
|
|
|
} else {
|
|
|
|
//手柄前进
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_HAND_KEY_FORWARD
|
2024-04-18 11:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2024-06-27 10:37:52 +08:00
|
|
|
/*if newBrake >= newOldBrakeForce && newBrake == 0 {
|
2024-04-18 11:14:05 +08:00
|
|
|
//手柄取消后退
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.RECIVE_TRAIN_HAND_KEY_CACLE_BACKWARD
|
|
|
|
} else
|
|
|
|
if newBrake < newOldBrakeForce {
|
2024-04-18 11:14:05 +08:00
|
|
|
//手柄后退
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.RECIVE_TRAIN_HAND_KEY_BACKWARD
|
2024-04-18 11:14:05 +08:00
|
|
|
} else {
|
|
|
|
//手柄后退
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.RECIVE_TRAIN_HAND_KEY_BACKWARD
|
|
|
|
}*/
|
|
|
|
msg.Type = message.RECIVE_TRAIN_HAND_KEY_BACKWARD
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
2024-05-29 15:19:04 +08:00
|
|
|
da := msg.Encode()
|
|
|
|
slog.Info("发送列车手柄消息", "clientKey", clientKey, "msg", hex.EncodeToString(da))
|
2024-06-12 19:50:36 +08:00
|
|
|
err := rd.tcpClient.Send(da)
|
|
|
|
//err := client.Send(da)
|
2024-05-29 15:19:04 +08:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("发送列车手柄消息失败", "clientKey", clientKey, "msg", hex.EncodeToString(da))
|
|
|
|
}
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
func (d *trainPcSimService) SendTrainDirection(train *state_proto.TrainState, trainForward, trainBackward bool) {
|
2024-04-16 17:26:37 +08:00
|
|
|
|
|
|
|
baseMsgs := make([]*message.TrainPcSimBaseMessage, 0)
|
|
|
|
if !trainForward && !trainBackward {
|
2024-06-27 10:37:52 +08:00
|
|
|
baseMsgs = append(baseMsgs, &message.TrainPcSimBaseMessage{Type: message.RECIVE_TRAIN_HAND_KEY_CANCLE_FORWARD})
|
|
|
|
baseMsgs = append(baseMsgs, &message.TrainPcSimBaseMessage{Type: message.RECIVE_TRAIN_HAND_KEY_CACLE_BACKWARD})
|
2024-04-16 17:26:37 +08:00
|
|
|
} else if trainForward {
|
2024-06-27 10:37:52 +08:00
|
|
|
baseMsgs = append(baseMsgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_HAND_KEY_FORWARD})
|
2024-04-16 17:26:37 +08:00
|
|
|
} else if trainBackward {
|
2024-06-27 10:37:52 +08:00
|
|
|
baseMsgs = append(baseMsgs, &message.TrainPcSimBaseMessage{Type: message.RECIVE_TRAIN_HAND_KEY_BACKWARD})
|
2024-04-16 17:26:37 +08:00
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
//client := d.pcSimClientMap[clientKey]
|
2024-04-16 17:26:37 +08:00
|
|
|
for _, msg := range baseMsgs {
|
2024-05-29 15:19:04 +08:00
|
|
|
da := msg.Encode()
|
2024-06-18 17:10:21 +08:00
|
|
|
slog.Info(fmt.Sprintf("发送列车方向列车:%v ,数据:%v", train.Id, hex.EncodeToString(da)))
|
2024-06-12 19:50:36 +08:00
|
|
|
err := rd.tcpClient.Send(da)
|
|
|
|
//err := client.Send(da)
|
2024-05-29 15:19:04 +08:00
|
|
|
if err != nil {
|
2024-06-18 17:10:21 +08:00
|
|
|
slog.Error(fmt.Sprintf("发送列车方向失败列车:%v ,数据:%v,err:%v", train.Id, hex.EncodeToString(da), err.Error()))
|
2024-05-29 15:19:04 +08:00
|
|
|
}
|
2024-04-16 17:26:37 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-04 09:26:37 +08:00
|
|
|
func (d *trainPcSimService) SendBaliseData2(train *state_proto.TrainState, msgType byte, data []string) {
|
|
|
|
for _, hexData := range data {
|
|
|
|
dd, _ := hex.DecodeString(hexData)
|
|
|
|
msg := &message.TrainPcSimBaseMessage{Type: msgType, Data: dd}
|
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
da := msg.Encode()
|
|
|
|
slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息,数据类型:0x%X,数据:%v", msgType, hex.EncodeToString(da)))
|
|
|
|
|
|
|
|
err := rd.tcpClient.Send(da)
|
|
|
|
if err != nil {
|
|
|
|
slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息失败,数据:%v", hex.EncodeToString(da)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 18:20:10 +08:00
|
|
|
|
2024-06-13 18:13:21 +08:00
|
|
|
func (d *trainPcSimService) SendBaliseData(train *state_proto.TrainState, msgType byte, data []byte) {
|
2024-04-09 08:55:33 +08:00
|
|
|
msg := &message.TrainPcSimBaseMessage{}
|
2024-04-02 18:20:10 +08:00
|
|
|
msg.Type = msgType
|
|
|
|
msg.Data = data
|
2024-05-24 09:00:43 +08:00
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
|
2024-05-29 15:19:04 +08:00
|
|
|
da := msg.Encode()
|
2024-07-04 09:26:37 +08:00
|
|
|
slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息,无应答器:%v,数据:%v", msgType == message.RECIVE_TRAIN_BTM_NOT_DATA, hex.EncodeToString(da)))
|
2024-04-18 11:14:05 +08:00
|
|
|
|
2024-06-12 19:50:36 +08:00
|
|
|
err := rd.tcpClient.Send(da)
|
2024-05-29 15:19:04 +08:00
|
|
|
if err != nil {
|
2024-06-18 17:10:21 +08:00
|
|
|
slog.Info(fmt.Sprintf("发送列车PC仿真应答器信息失败,数据:%v", hex.EncodeToString(da)))
|
2024-05-29 15:19:04 +08:00
|
|
|
}
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
func (d *trainPcSimService) PublishTrainControlEvent(train *state_proto.TrainState, events []TrainControlEvent) {
|
2024-04-02 18:20:10 +08:00
|
|
|
if len(events) <= 0 {
|
2024-04-09 08:55:33 +08:00
|
|
|
slog.Warn("发布事件数量为空")
|
2024-04-02 18:20:10 +08:00
|
|
|
return
|
|
|
|
}
|
2024-05-24 09:00:43 +08:00
|
|
|
clientKey := FindTrainPcSimClientKey(train)
|
2024-06-12 19:50:36 +08:00
|
|
|
rd := d.newPcSimclientMap[clientKey]
|
|
|
|
|
|
|
|
//client := d.pcSimClientMap[clientKey]
|
2024-04-02 18:20:10 +08:00
|
|
|
for _, event := range events {
|
2024-04-18 11:14:05 +08:00
|
|
|
msg := &message.TrainPcSimBaseMessage{}
|
2024-06-27 10:37:52 +08:00
|
|
|
msg.Type = message.SENDER_TRAIN_OUTR_INFO
|
2024-04-18 11:14:05 +08:00
|
|
|
data := []byte{event.Command, event.Status}
|
|
|
|
msg.Data = data
|
2024-06-13 18:13:21 +08:00
|
|
|
code := msg.Encode()
|
|
|
|
hexCode := hex.EncodeToString(code)
|
|
|
|
slog.Info(fmt.Sprintf("输出列车控制输出量,命令码位:%v 对应状态:%v,发送数据:%v", event.Command, event.Status, hexCode))
|
|
|
|
err := rd.tcpClient.Send(code)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(fmt.Sprintf("输出列车控制输出量发送失败,命令码位:%v 对应状态:%v,发送数据:%v", event.Command, event.Status, hexCode))
|
|
|
|
}
|
2024-06-12 19:50:36 +08:00
|
|
|
//client.Send(msg.Encode())
|
2024-04-18 11:14:05 +08:00
|
|
|
//FireTrainControlEventType.Publish(world, &event)
|
|
|
|
|
2024-04-02 18:20:10 +08:00
|
|
|
}
|
|
|
|
}
|