parent
78a9e1d07e
commit
93d71f6125
|
@ -1 +1 @@
|
|||
Subproject commit bdd66d8625e76afb23f501de306224d35a8958d1
|
||||
Subproject commit bc70ae80a0ed73ec417884710e02996103841bf9
|
File diff suppressed because it is too large
Load Diff
2
main.go
2
main.go
|
@ -10,6 +10,7 @@ import (
|
|||
"joylink.club/bj-rtsts-server/docs"
|
||||
"joylink.club/bj-rtsts-server/middleware"
|
||||
"joylink.club/bj-rtsts-server/mqtt"
|
||||
"joylink.club/bj-rtsts-server/third_party"
|
||||
)
|
||||
|
||||
// @title CBTC测试系统API
|
||||
|
@ -26,6 +27,7 @@ import (
|
|||
func main() {
|
||||
engine := InitServer()
|
||||
mqtt.Startup(mqtt.NewMqttOptions(config.Config.Messaging.Mqtt.Address, config.Config.Messaging.Mqtt.Username, config.Config.Messaging.Mqtt.Password))
|
||||
third_party.Init()
|
||||
authMiddleware := middleware.InitGinJwtMiddleware()
|
||||
router := engine.Group("/api")
|
||||
api.InitUserRouter(router, authMiddleware)
|
||||
|
|
|
@ -2,11 +2,12 @@ package message_server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"joylink.club/bj-rtsts-server/dto/common_proto"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"joylink.club/bj-rtsts-server/dto/common_proto"
|
||||
|
||||
"joylink.club/bj-rtsts-server/dto/request_proto"
|
||||
"joylink.club/bj-rtsts-server/dto/state_proto"
|
||||
"joylink.club/rtsssimulation/repository/model/proto"
|
||||
|
@ -161,6 +162,18 @@ func handlerSectionState(w ecs.World, uid string) *state_proto.SectionState {
|
|||
axleState := component.PhysicalSectionStateType.Get(entry)
|
||||
sectionState.Occupied = axleState.Occ
|
||||
sectionState.AxleFault = entry.HasComponent(component.AxleSectionFaultTag)
|
||||
|
||||
wd := entity.GetWorldData(w)
|
||||
sectionModel := wd.Repo.FindPhysicalSection(uid)
|
||||
faDcAxleDeviceEntry := entity.FindAxleManageDevice(wd, sectionModel.CentralizedStation())
|
||||
if faDcAxleDeviceEntry != nil {
|
||||
faDcAxleDevice := component.AxleManageDeviceType.Get(faDcAxleDeviceEntry)
|
||||
axleRuntime := faDcAxleDevice.FindAdr(uid)
|
||||
if axleRuntime != nil {
|
||||
sectionState.AxleDrst = axleRuntime.Drst
|
||||
sectionState.AxlePdrst = axleRuntime.Pdrst
|
||||
}
|
||||
}
|
||||
return sectionState
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -1,26 +1,56 @@
|
|||
package message_server
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"joylink.club/bj-rtsts-server/dto/data_proto"
|
||||
"joylink.club/bj-rtsts-server/mqtt"
|
||||
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
||||
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
||||
)
|
||||
|
||||
var _default = &publishTaskManage{tasks: make(map[string][]ms_api.MsgTask)}
|
||||
|
||||
// 仿真消息任务管理
|
||||
type publishTaskManage struct {
|
||||
mu sync.Mutex
|
||||
tasks map[string][]ms_api.MsgTask
|
||||
}
|
||||
|
||||
func (m *publishTaskManage) AddTasks(simulationId string, tasks ...ms_api.MsgTask) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.tasks[simulationId] = append(m.tasks[simulationId], tasks...)
|
||||
}
|
||||
|
||||
func (m *publishTaskManage) RemoveTasks(simulationId string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
slog.Info("停止并移除仿真消息服务", "simulationId", simulationId, "tasks", m.tasks[simulationId])
|
||||
for _, task := range m.tasks[simulationId] {
|
||||
task.Stop()
|
||||
}
|
||||
delete(m.tasks, simulationId)
|
||||
}
|
||||
|
||||
// 启动仿真所需的消息服务
|
||||
func Start(vs *memory.VerifySimulation) {
|
||||
// 添加仿真状态消息服务
|
||||
_default.AddTasks(vs.SimulationId, NewStateMs(vs))
|
||||
_default.AddTasks(vs.SimulationId, NewTpapiServiceMs(vs))
|
||||
for _, mapId := range vs.MapIds {
|
||||
t := memory.QueryGiType(mapId)
|
||||
switch t {
|
||||
case data_proto.PictureType_StationLayout: // 平面布置图
|
||||
// 添加车站关联的平面布置图、IBP、PSL信息
|
||||
mqtt.GetMsgClient().PublishTask(vs.SimulationId, NewSfpMs(vs, mapId), NewIBPMs(vs, mapId), NewPSLMs(vs, mapId))
|
||||
_default.AddTasks(vs.SimulationId, NewSfpMs(vs, mapId), NewIBPMs(vs, mapId), NewPSLMs(vs, mapId))
|
||||
case data_proto.PictureType_RelayCabinetLayout: // 继电器柜
|
||||
mqtt.GetMsgClient().PublishTask(vs.SimulationId, NewRccMs(vs, mapId))
|
||||
_default.AddTasks(vs.SimulationId, NewRccMs(vs, mapId))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭仿真消息服务
|
||||
func Close(vs *memory.VerifySimulation) {
|
||||
mqtt.GetMsgClient().CloseTask(vs.SimulationId)
|
||||
_default.RemoveTasks(vs.SimulationId)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package message_server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"joylink.club/bj-rtsts-server/dto/state_proto"
|
||||
|
||||
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
||||
|
@ -11,20 +13,54 @@ import (
|
|||
)
|
||||
|
||||
func NewStateMs(vs *memory.VerifySimulation) ms_api.MsgTask {
|
||||
return ms_api.NewMonitorTask(fmt.Sprintf("仿真[%s]状态", vs.SimulationId), func() {
|
||||
ecs.WorldStateChangeEvent.Subscribe(vs.World, func(_ ecs.World, e ecs.WorldStateChange) {
|
||||
s := &state_proto.SimulationStatus{SimulationId: vs.SimulationId}
|
||||
switch e.NewState {
|
||||
case ecs.WorldClosed:
|
||||
s.State = state_proto.SimulationStatus_DESTROY
|
||||
case ecs.WorldError:
|
||||
s.State = state_proto.SimulationStatus_ERROR
|
||||
case ecs.WorldPause:
|
||||
s.State = state_proto.SimulationStatus_PAUSE
|
||||
default:
|
||||
return
|
||||
}
|
||||
mqtt.GetMsgClient().PubSimulationState(vs.SimulationId, s)
|
||||
})
|
||||
})
|
||||
return ms_api.NewScheduleTask(fmt.Sprintf("仿真[%s]状态", vs.SimulationId), func() error {
|
||||
s := &state_proto.SimulationStatus{
|
||||
SimulationId: vs.SimulationId,
|
||||
State: convertWorldState(vs.World.State()),
|
||||
}
|
||||
mqtt.GetMsgClient().PubSimulationState(vs.SimulationId, s)
|
||||
return nil
|
||||
}, 1000*time.Millisecond)
|
||||
// return ms_api.NewMonitorTask(fmt.Sprintf("仿真[%s]状态", vs.SimulationId), func() {
|
||||
// ecs.WorldStateChangeEvent.Subscribe(vs.World, func(_ ecs.World, e ecs.WorldStateChange) {
|
||||
// s := &state_proto.SimulationStatus{SimulationId: vs.SimulationId}
|
||||
// switch e.NewState {
|
||||
// case ecs.WorldClosed:
|
||||
// s.State = state_proto.SimulationStatus_DESTROY
|
||||
// case ecs.WorldError:
|
||||
// s.State = state_proto.SimulationStatus_ERROR
|
||||
// case ecs.WorldPause:
|
||||
// s.State = state_proto.SimulationStatus_PAUSE
|
||||
// default:
|
||||
// return
|
||||
// }
|
||||
// mqtt.GetMsgClient().PubSimulationState(vs.SimulationId, s)
|
||||
// })
|
||||
// })
|
||||
}
|
||||
|
||||
func convertWorldState(state ecs.WorldState) state_proto.SimulationStatus_SimulationState {
|
||||
switch state {
|
||||
case ecs.WorldInit:
|
||||
return state_proto.SimulationStatus_Init
|
||||
case ecs.WorldRunning:
|
||||
return state_proto.SimulationStatus_Running
|
||||
case ecs.WorldPause:
|
||||
return state_proto.SimulationStatus_Pause
|
||||
case ecs.WorldError:
|
||||
return state_proto.SimulationStatus_Error
|
||||
case ecs.WorldClosed:
|
||||
return state_proto.SimulationStatus_Destroy
|
||||
default:
|
||||
panic("unknown world state")
|
||||
}
|
||||
}
|
||||
|
||||
// 发布仿真销毁消息
|
||||
func PubSimulationDestroyMsg(simulationId string) {
|
||||
s := &state_proto.SimulationStatus{
|
||||
SimulationId: simulationId,
|
||||
State: state_proto.SimulationStatus_Destroy,
|
||||
}
|
||||
mqtt.GetMsgClient().PubSimulationState(simulationId, s)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package message_server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
||||
"joylink.club/bj-rtsts-server/mqtt"
|
||||
"joylink.club/bj-rtsts-server/third_party"
|
||||
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
||||
)
|
||||
|
||||
func NewTpapiServiceMs(vs *memory.VerifySimulation) ms_api.MsgTask {
|
||||
return ms_api.NewScheduleTask(fmt.Sprintf("仿真第三方API服务[%s]状态", vs.SimulationId), func() error {
|
||||
rss := third_party.GetRunningServiceStates()
|
||||
mqtt.GetMsgClient().PubTpapiServiceState(vs.SimulationId, rss)
|
||||
return nil
|
||||
}, 1000*time.Millisecond)
|
||||
}
|
|
@ -10,16 +10,14 @@ import (
|
|||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"joylink.club/bj-rtsts-server/dto/state_proto"
|
||||
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
||||
)
|
||||
|
||||
var mqttClient *MqttClient
|
||||
|
||||
// 客户端
|
||||
type MqttClient struct {
|
||||
cc *autopaho.ClientConfig
|
||||
cm *autopaho.ConnectionManager
|
||||
tasks map[string][]ms_api.MsgTask
|
||||
cc *autopaho.ClientConfig
|
||||
cm *autopaho.ConnectionManager
|
||||
}
|
||||
|
||||
// 初始化并启动MQTT客户端服务
|
||||
|
@ -35,7 +33,7 @@ func Startup(cmc *MqttOptions) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mqttClient = &MqttClient{cc: cc, cm: cm, tasks: make(map[string][]ms_api.MsgTask)}
|
||||
mqttClient = &MqttClient{cc: cc, cm: cm}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -81,6 +79,9 @@ func (client *MqttClient) pub(topic string, data protoreflect.ProtoMessage) erro
|
|||
slog.Error("未知发布主题", "topic", topic, "data", data)
|
||||
return fmt.Errorf("未知发布主题: topic=%s", topic)
|
||||
}
|
||||
// else {
|
||||
// slog.Debug("发布主题", "topic", topic, "data", data)
|
||||
// }
|
||||
_, err = client.cm.Publish(context.Background(), &paho.Publish{
|
||||
Topic: topic,
|
||||
QoS: 0,
|
||||
|
@ -89,28 +90,15 @@ func (client *MqttClient) pub(topic string, data protoreflect.ProtoMessage) erro
|
|||
return err
|
||||
}
|
||||
|
||||
// 发布任务
|
||||
func (client *MqttClient) PublishTask(simulationId string, tasks ...ms_api.MsgTask) {
|
||||
client.tasks[simulationId] = append(client.tasks[simulationId], tasks...)
|
||||
}
|
||||
|
||||
// 停止任务
|
||||
func (client *MqttClient) CloseTask(simulationId string) {
|
||||
tasks, ok := client.tasks[simulationId]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
for _, task := range tasks {
|
||||
task.Stop()
|
||||
}
|
||||
client.tasks[simulationId] = nil
|
||||
}
|
||||
|
||||
// 发送仿真状态数据
|
||||
func (client *MqttClient) PubSimulationState(simulationId string, msg *state_proto.SimulationStatus) error {
|
||||
return client.pub(GetStateTopic(simulationId), msg)
|
||||
}
|
||||
|
||||
func (c *MqttClient) PubTpapiServiceState(simulationId string, msg *state_proto.SimulationThirdPartyApiService) error {
|
||||
return c.pub(GetTpapiServiceTopic(simulationId), msg)
|
||||
}
|
||||
|
||||
// 发送IBP状态数据
|
||||
func (client *MqttClient) PubIBPState(simulationId string, mapId int32, stationId uint32, msg *state_proto.PushedDevicesStatus) error {
|
||||
return client.pub(GetIbpTopic(simulationId, mapId, stationId), msg)
|
||||
|
|
|
@ -8,16 +8,18 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
topicPrefix = "/" + config.SystemName + "/simulation/%s/" // 公共部分 仿真ID
|
||||
stateTopic = topicPrefix + "state" // 仿真状态topic
|
||||
sfpTopic = topicPrefix + "sfp/%d" // 平面布置图设备状态topic 地图ID
|
||||
rccTopic = topicPrefix + "rcc/%d" // 继电器柜继电器状态topic 地图ID
|
||||
pslTopic = topicPrefix + "psl/%d/%d" // psl状态topic 地图ID/门控箱ID
|
||||
ibpTopic = topicPrefix + "ibp/%d/%d" // ibp盘状态topic 地图ID/车站ID
|
||||
topicPrefix = "/" + config.SystemName + "/simulation/%s/" // 公共部分 仿真ID
|
||||
stateTopic = topicPrefix + "state" // 仿真状态topic
|
||||
tpapiServiceTopic = topicPrefix + "tpis" // 第三方API服务状态topic
|
||||
sfpTopic = topicPrefix + "sfp/%d" // 平面布置图设备状态topic 地图ID
|
||||
rccTopic = topicPrefix + "rcc/%d" // 继电器柜继电器状态topic 地图ID
|
||||
pslTopic = topicPrefix + "psl/%d/%d" // psl状态topic 地图ID/门控箱ID
|
||||
ibpTopic = topicPrefix + "ibp/%d/%d" // ibp盘状态topic 地图ID/车站ID
|
||||
)
|
||||
|
||||
var topicMap = map[string]string{
|
||||
"state": stateTopic,
|
||||
"tpis": tpapiServiceTopic,
|
||||
"sfp": sfpTopic,
|
||||
"rcc": rccTopic,
|
||||
"psl": pslTopic,
|
||||
|
@ -54,6 +56,10 @@ func GetStateTopic(simulationId string) string {
|
|||
return fmt.Sprintf(stateTopic, simulationId)
|
||||
}
|
||||
|
||||
func GetTpapiServiceTopic(simulation string) string {
|
||||
return fmt.Sprintf(tpapiServiceTopic, simulation)
|
||||
}
|
||||
|
||||
// 信号布置图设备状态消息topic
|
||||
func GetSfpTopic(simulationId string, mapId int32) string {
|
||||
return fmt.Sprintf(sfpTopic, simulationId, mapId)
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit d94a8335dcffbb44d7d865d8b7200da335e2241f
|
||||
Subproject commit fa7211d7bdf68be1df571f222a047ddd6cd764b0
|
|
@ -43,9 +43,10 @@ type Dynamics interface {
|
|||
SendTrainControlMessage(b []byte)
|
||||
}
|
||||
|
||||
var _default Dynamics
|
||||
var _default *dynamics
|
||||
var initMutex sync.Mutex
|
||||
|
||||
const Name = "动力学"
|
||||
const Interval = 15
|
||||
|
||||
func Default() Dynamics {
|
||||
|
@ -53,12 +54,11 @@ func Default() Dynamics {
|
|||
defer initMutex.Unlock()
|
||||
if _default == nil {
|
||||
_default = &dynamics{
|
||||
ThirdPartyApiService: tpapi.NewThirdPartyApiService(),
|
||||
udpDelayRecorder: tpapi.NewUdpDelayRecorder(Interval, func(err error) {
|
||||
if err != nil {
|
||||
_default.UpdateState(tpapi.ThirdPartyState_Broken)
|
||||
_default.updateState(tpapi.ThirdPartyState_Broken)
|
||||
} else {
|
||||
_default.UpdateState(tpapi.ThirdPartyState_Normal)
|
||||
_default.updateState(tpapi.ThirdPartyState_Normal)
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
@ -72,6 +72,7 @@ type dynamics struct {
|
|||
turnoutStateUdpClient udp.UdpClient
|
||||
trainControlUdpClient udp.UdpClient
|
||||
|
||||
state tpapi.ThirdPartyApiServiceState
|
||||
udpDelayRecorder *tpapi.UdpDelayRecorder
|
||||
|
||||
baseUrl string
|
||||
|
@ -81,6 +82,18 @@ type dynamics struct {
|
|||
runConfig *config.DynamicsConfig
|
||||
}
|
||||
|
||||
func (d *dynamics) updateState(state tpapi.ThirdPartyApiServiceState) {
|
||||
d.state = state
|
||||
}
|
||||
|
||||
func (d *dynamics) State() tpapi.ThirdPartyApiServiceState {
|
||||
return d.state
|
||||
}
|
||||
|
||||
func (d *dynamics) Name() string {
|
||||
return Name
|
||||
}
|
||||
|
||||
// 解码列车信息并处理
|
||||
func (d *dynamics) handleDynamicsTrainInfo(b []byte) {
|
||||
d.udpDelayRecorder.RecordInterval()
|
||||
|
@ -227,7 +240,7 @@ func (d *dynamics) Start(manager DynamicsMessageManager) error {
|
|||
ctx, cancle := context.WithCancel(context.Background())
|
||||
go d.sendTurnoutStateTask(ctx)
|
||||
d.turnoutTaskCancel = cancle
|
||||
d.UpdateState(tpapi.ThirdPartyState_Normal)
|
||||
d.updateState(tpapi.ThirdPartyState_Normal)
|
||||
d.udpDelayRecorder.Start()
|
||||
return nil
|
||||
}
|
||||
|
@ -274,7 +287,7 @@ func (d *dynamics) Stop() {
|
|||
d.turnoutTaskCancel()
|
||||
}
|
||||
d.manager = nil
|
||||
d.UpdateState(tpapi.ThirdPartyState_Closed)
|
||||
d.updateState(tpapi.ThirdPartyState_Closed)
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
@ -34,12 +34,25 @@ type semiPhysicalTrainImpl struct {
|
|||
trainControlUdpServer udp.UdpServer
|
||||
trainSpeedInfoUdpClient udp.UdpClient
|
||||
|
||||
state tpapi.ThirdPartyApiServiceState
|
||||
udpDelayRecorder *tpapi.UdpDelayRecorder
|
||||
|
||||
manager SemiPhysicalMessageManager
|
||||
runConfig *config.VobcConfig
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *semiPhysicalTrainImpl) handleTrainControlMsg(b []byte) {
|
||||
s.udpDelayRecorder.RecordInterval()
|
||||
slog.Debug(fmt.Sprintf("半实物列车控制消息近期消息间隔: %v", s.udpDelayRecorder.GetIntervals()))
|
||||
|
@ -63,7 +76,7 @@ func (s *semiPhysicalTrainImpl) Start(manager SemiPhysicalMessageManager) {
|
|||
// 初始化客户端、服务端
|
||||
s.initSemiPhysical()
|
||||
s.manager = manager
|
||||
s.UpdateState(tpapi.ThirdPartyState_Normal)
|
||||
s.updateState(tpapi.ThirdPartyState_Normal)
|
||||
s.udpDelayRecorder.Start()
|
||||
}
|
||||
|
||||
|
@ -78,7 +91,7 @@ func (s *semiPhysicalTrainImpl) Stop() {
|
|||
s.trainSpeedInfoUdpClient.Close()
|
||||
}
|
||||
s.manager = nil
|
||||
s.UpdateState(tpapi.ThirdPartyState_Closed)
|
||||
s.updateState(tpapi.ThirdPartyState_Closed)
|
||||
}
|
||||
|
||||
func (s *semiPhysicalTrainImpl) SendTrainControlMessage(info *message.DynamicsTrainInfo) {
|
||||
|
@ -93,9 +106,10 @@ func (s *semiPhysicalTrainImpl) initSemiPhysical() {
|
|||
s.trainControlUdpServer.Listen()
|
||||
}
|
||||
|
||||
var _default SemiPhysicalTrain
|
||||
var _default *semiPhysicalTrainImpl
|
||||
var initMutex sync.Mutex
|
||||
|
||||
const Name = "半实物仿真列车"
|
||||
const Interval int = 20
|
||||
|
||||
func Default() SemiPhysicalTrain {
|
||||
|
@ -103,12 +117,11 @@ func Default() SemiPhysicalTrain {
|
|||
defer initMutex.Unlock()
|
||||
if _default == nil {
|
||||
_default = &semiPhysicalTrainImpl{
|
||||
ThirdPartyApiService: tpapi.NewThirdPartyApiService(),
|
||||
udpDelayRecorder: tpapi.NewUdpDelayRecorder(Interval, func(err error) {
|
||||
if err != nil {
|
||||
_default.UpdateState(tpapi.ThirdPartyState_Broken)
|
||||
_default.updateState(tpapi.ThirdPartyState_Broken)
|
||||
} else {
|
||||
_default.UpdateState(tpapi.ThirdPartyState_Normal)
|
||||
_default.updateState(tpapi.ThirdPartyState_Normal)
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
|
|
@ -1,24 +1,56 @@
|
|||
package third_party
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"joylink.club/bj-rtsts-server/dto/state_proto"
|
||||
"joylink.club/bj-rtsts-server/third_party/dynamics"
|
||||
"joylink.club/bj-rtsts-server/third_party/semi_physical_train"
|
||||
"joylink.club/bj-rtsts-server/third_party/tpapi"
|
||||
)
|
||||
|
||||
func GetRunningServiceStates(t state_proto.SimulationThirdPartyApiService_Type) *state_proto.SimulationThirdPartyApiService {
|
||||
var tpapiService []tpapi.ThirdPartyApiService
|
||||
|
||||
func Init() {
|
||||
tpapiService = append(
|
||||
tpapiService, dynamics.Default(),
|
||||
semi_physical_train.Default(),
|
||||
)
|
||||
}
|
||||
|
||||
func GetAllServices() []tpapi.ThirdPartyApiService {
|
||||
return tpapiService
|
||||
}
|
||||
|
||||
func convertServiceName(name string) state_proto.SimulationThirdPartyApiService_Type {
|
||||
switch name {
|
||||
case dynamics.Name:
|
||||
return state_proto.SimulationThirdPartyApiService_Dynamics
|
||||
case semi_physical_train.Name:
|
||||
return state_proto.SimulationThirdPartyApiService_SemiPhysicalTrain
|
||||
}
|
||||
return state_proto.SimulationThirdPartyApiService_Undefined
|
||||
}
|
||||
|
||||
func GetRunningServiceStates() *state_proto.SimulationThirdPartyApiService {
|
||||
ss := &state_proto.SimulationThirdPartyApiService{}
|
||||
switch dynamics.Default().State() {
|
||||
case tpapi.ThirdPartyState_Normal:
|
||||
ss.States = append(ss.States, &state_proto.SimulationThirdPartyApiServiceState{
|
||||
Type: state_proto.SimulationThirdPartyApiService_Dynamics,
|
||||
State: state_proto.SimulationThirdPartyApiService_Normal,
|
||||
})
|
||||
case tpapi.ThirdPartyState_Broken:
|
||||
ss.States = append(ss.States, &state_proto.SimulationThirdPartyApiServiceState{
|
||||
Type: state_proto.SimulationThirdPartyApiService_Dynamics,
|
||||
State: state_proto.SimulationThirdPartyApiService_Error,
|
||||
})
|
||||
for _, tpas := range tpapiService {
|
||||
t := convertServiceName(tpas.Name())
|
||||
if t == state_proto.SimulationThirdPartyApiService_Undefined {
|
||||
slog.Error("未知的第三方接口服务类型", "name", tpas.Name())
|
||||
}
|
||||
switch tpas.State() {
|
||||
case tpapi.ThirdPartyState_Normal:
|
||||
ss.States = append(ss.States, &state_proto.SimulationThirdPartyApiServiceState{
|
||||
Type: t,
|
||||
State: state_proto.SimulationThirdPartyApiService_Normal,
|
||||
})
|
||||
case tpapi.ThirdPartyState_Broken:
|
||||
ss.States = append(ss.States, &state_proto.SimulationThirdPartyApiServiceState{
|
||||
Type: t,
|
||||
State: state_proto.SimulationThirdPartyApiService_Error,
|
||||
})
|
||||
}
|
||||
}
|
||||
return ss
|
||||
}
|
||||
|
|
|
@ -20,29 +20,28 @@ const (
|
|||
|
||||
// 第三方对接接口服务
|
||||
type ThirdPartyApiService interface {
|
||||
Name() string
|
||||
// 服务状态
|
||||
State() ThirdPartyApiServiceState
|
||||
// 更新状态
|
||||
UpdateState(state ThirdPartyApiServiceState)
|
||||
}
|
||||
|
||||
func NewThirdPartyApiService() ThirdPartyApiService {
|
||||
return &thirdPartyApiService{
|
||||
state: ThirdPartyState_Closed,
|
||||
}
|
||||
}
|
||||
// func NewThirdPartyApiService() ThirdPartyApiService {
|
||||
// return &thirdPartyApiService{
|
||||
// state: ThirdPartyState_Closed,
|
||||
// }
|
||||
// }
|
||||
|
||||
type thirdPartyApiService struct {
|
||||
state ThirdPartyApiServiceState
|
||||
}
|
||||
// type thirdPartyApiService struct {
|
||||
// state ThirdPartyApiServiceState
|
||||
// }
|
||||
|
||||
func (s *thirdPartyApiService) UpdateState(state ThirdPartyApiServiceState) {
|
||||
s.state = state
|
||||
}
|
||||
// func (s *thirdPartyApiService) UpdateState(state ThirdPartyApiServiceState) {
|
||||
// s.state = state
|
||||
// }
|
||||
|
||||
func (s *thirdPartyApiService) State() ThirdPartyApiServiceState {
|
||||
return s.state
|
||||
}
|
||||
// func (s *thirdPartyApiService) State() ThirdPartyApiServiceState {
|
||||
// return s.state
|
||||
// }
|
||||
|
||||
// UDP通信延时记录
|
||||
type UdpDelayRecorder struct {
|
||||
|
@ -59,7 +58,7 @@ type UdpDelayRecorder struct {
|
|||
// 长时间未收到消息或恢复处理回调
|
||||
handler func(err error)
|
||||
|
||||
cancelFunc func()
|
||||
cancelFunc context.CancelFunc
|
||||
done chan struct{} // 服务协程退出信号
|
||||
}
|
||||
|
||||
|
@ -110,6 +109,9 @@ func (r *UdpDelayRecorder) checkTimeout(ctx context.Context) {
|
|||
func (r *UdpDelayRecorder) Stop() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.cancelFunc == nil {
|
||||
return
|
||||
}
|
||||
r.cancelFunc()
|
||||
<-r.done
|
||||
r.lastReceiedTime = 0
|
||||
|
|
|
@ -46,8 +46,6 @@ type VerifySimulation struct {
|
|||
uidMap map[string]*elementIdStructure
|
||||
// 运行环境配置
|
||||
runConfig *config.ThridPartyConfig
|
||||
// 运行状态
|
||||
runState state_proto.SimulationStatus_SimulationState
|
||||
}
|
||||
|
||||
// 轨旁仿真内存模型
|
||||
|
@ -102,7 +100,6 @@ func CreateSimulation(projectId int32, mapIds []int32, runConfig *dto.ProjectRun
|
|||
verifySimulation := &VerifySimulation{
|
||||
ProjectId: projectId,
|
||||
MapIds: mapIds,
|
||||
runState: state_proto.SimulationStatus_PAUSE,
|
||||
}
|
||||
// 设置运行环境
|
||||
err := verifySimulation.initRunConfig(runConfig)
|
||||
|
@ -117,25 +114,25 @@ func CreateSimulation(projectId int32, mapIds []int32, runConfig *dto.ProjectRun
|
|||
return verifySimulation, nil
|
||||
}
|
||||
|
||||
// 启动
|
||||
func (s *VerifySimulation) Start() {
|
||||
s.runState = state_proto.SimulationStatus_START
|
||||
}
|
||||
// // 启动
|
||||
// func (s *VerifySimulation) Start() {
|
||||
// s.runState = state_proto.SimulationStatus_START
|
||||
// }
|
||||
|
||||
// 销毁
|
||||
func (s *VerifySimulation) Destroy() {
|
||||
s.runState = state_proto.SimulationStatus_DESTROY
|
||||
}
|
||||
// // 销毁
|
||||
// func (s *VerifySimulation) Destroy() {
|
||||
// s.runState = state_proto.SimulationStatus_DESTROY
|
||||
// }
|
||||
|
||||
// 暂停
|
||||
func (s *VerifySimulation) Pause() {
|
||||
s.runState = state_proto.SimulationStatus_PAUSE
|
||||
}
|
||||
// // 暂停
|
||||
// func (s *VerifySimulation) Pause() {
|
||||
// s.runState = state_proto.SimulationStatus_PAUSE
|
||||
// }
|
||||
|
||||
// 获取状态
|
||||
func (s *VerifySimulation) GetRunState() state_proto.SimulationStatus_SimulationState {
|
||||
return s.runState
|
||||
}
|
||||
// // 获取状态
|
||||
// func (s *VerifySimulation) GetRunState() state_proto.SimulationStatus_SimulationState {
|
||||
// return s.runState
|
||||
// }
|
||||
|
||||
// 获取仿真世界信息
|
||||
func (s *VerifySimulation) GetComIdByUid(uid string) uint32 {
|
||||
|
|
|
@ -63,7 +63,7 @@ func CreateSimulation(projectId int32, mapIds []int32, runConfig *dto.ProjectRun
|
|||
return "", err
|
||||
}
|
||||
simulationMap.Store(simulationId, verifySimulation)
|
||||
verifySimulation.Start()
|
||||
// verifySimulation.Start()
|
||||
// 全部成功,启动仿真
|
||||
verifySimulation.World.StartUp()
|
||||
// 启动仿真消息服务
|
||||
|
@ -80,12 +80,14 @@ func DestroySimulation(simulationId string) {
|
|||
}
|
||||
simulationMap.Delete(simulationId)
|
||||
simulationInfo := s.(*memory.VerifySimulation)
|
||||
simulationInfo.Destroy()
|
||||
// simulationInfo.Destroy()
|
||||
// 停止ecs world
|
||||
simulationInfo.World.Close()
|
||||
defer message_server.Close(simulationInfo)
|
||||
// 发布销毁消息
|
||||
defer message_server.PubSimulationDestroyMsg(simulationId)
|
||||
// 停止第三方
|
||||
stopThirdParty(simulationInfo)
|
||||
message_server.Close(simulationInfo)
|
||||
}
|
||||
|
||||
// 创建world
|
||||
|
|
Loading…
Reference in New Issue