rts-sim-testing-service/grpcproto/memory_change_server.go
2023-08-30 13:25:57 +08:00

71 lines
1.7 KiB
Go

package apiproto
import (
"time"
"google.golang.org/protobuf/proto"
"joylink.club/bj-rtsts-server/ats/verify/protos/state"
"joylink.club/bj-rtsts-server/ats/verify/simulation"
)
type MemoryChangeServer struct {
simulationMap map[string]*state.SimulationStatus
}
// 返回通道格式
func (t *MemoryChangeServer) getChannelName() string {
return "memory-data-change"
}
// 消息运行间隔
func (t *MemoryChangeServer) getInterval() time.Duration {
return 1 * time.Second
}
// 返回所有数据
func (t *MemoryChangeServer) allMsgData(params map[string]string) []byte {
arr := make([]*state.SimulationStatus, len(t.simulationMap))
i := 0
for _, v := range t.simulationMap {
arr[i] = v
i++
}
msg := &state.MemoryDataStatus{AllSimulations: arr}
data, err := proto.Marshal(msg)
if err != nil {
panic(err)
}
return data
}
// 定时发送数据
func (t *MemoryChangeServer) onTick() []TopicMsg {
simArr := simulation.GetSimulationArr()
addArr, delArr := []*state.SimulationStatus{}, []*state.SimulationStatus{}
// 增加的仿真
allSim := make(map[string]bool)
for _, v := range simArr {
allSim[v.SimulationId] = true
if t.simulationMap[v.SimulationId] == nil {
addArr = append(addArr, &state.SimulationStatus{
SimulationId: v.SimulationId,
MapId: v.MapId,
ProjectId: v.ProjectId,
})
}
}
// 已经移除的仿真
for k, v := range t.simulationMap {
if !allSim[k] {
delArr = append(delArr, v)
}
}
msg := &state.MemoryDataStatus{AddSimulations: addArr, RemoveSimulations: delArr}
b, err := proto.Marshal(msg)
if err != nil {
panic(err)
}
msgArr := []TopicMsg{{channalName: t.getChannelName(), data: b}}
return msgArr
}