rts-sim-testing-service/grpcproto/memory_change_server.go
2023-08-31 16:16:18 +08:00

83 lines
2.2 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"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/dto"
)
var MemoryChangeServerSuffix = "serve-data"
type MemoryChangeServer struct {
SimulationMap map[string]*state.SimulationStatus
}
// 返回通道格式
func (t *MemoryChangeServer) getChannelName() string {
return config.SimulationId_prefix + MemoryChangeServerSuffix
}
// 消息运行间隔
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(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
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 {
continue
}
status := &state.SimulationStatus{SimulationId: v.SimulationId, MapId: v.MapId, ProjectId: v.ProjectId}
t.SimulationMap[v.SimulationId] = status
addArr = append(addArr, status)
}
// 已经移除的仿真
for k, v := range t.SimulationMap {
if allSim[k] {
continue
}
delArr = append(delArr, v)
}
// 删除已替换仿真主键
if len(delArr) > 0 {
for _, v := range delArr {
delete(t.SimulationMap, v.SimulationId)
}
}
if len(addArr) == 0 && len(delArr) == 0 {
return nil
}
msg := &state.MemoryDataStatus{AddSimulations: addArr, RemoveSimulations: delArr}
b, err := proto.Marshal(msg)
if err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
return []TopicMsg{{channalName: t.getChannelName(), data: b}}
}