rts-sim-testing-service/grpcproto/memory_change_server.go

83 lines
2.2 KiB
Go
Raw Normal View History

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"
2023-08-31 14:06:53 +08:00
"joylink.club/bj-rtsts-server/config"
2023-08-30 18:05:11 +08:00
"joylink.club/bj-rtsts-server/dto"
)
2023-08-31 14:06:53 +08:00
var MemoryChangeServerSuffix = "serve-data"
type MemoryChangeServer struct {
2023-08-31 10:30:44 +08:00
SimulationMap map[string]*state.SimulationStatus
}
// 返回通道格式
func (t *MemoryChangeServer) getChannelName() string {
2023-08-31 14:06:53 +08:00
return config.SimulationId_prefix + MemoryChangeServerSuffix
}
// 消息运行间隔
func (t *MemoryChangeServer) getInterval() time.Duration {
return 1 * time.Second
}
// 返回所有数据
func (t *MemoryChangeServer) allMsgData(params map[string]string) []byte {
2023-08-31 10:30:44 +08:00
arr := make([]*state.SimulationStatus, len(t.SimulationMap))
i := 0
2023-08-31 10:30:44 +08:00
for _, v := range t.SimulationMap {
arr[i] = v
i++
}
msg := &state.MemoryDataStatus{AllSimulations: arr}
data, err := proto.Marshal(msg)
if err != nil {
2023-08-30 18:05:11 +08:00
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
2023-08-31 16:16:18 +08:00
if t.SimulationMap[v.SimulationId] != nil {
continue
}
2023-08-31 16:16:18 +08:00
status := &state.SimulationStatus{SimulationId: v.SimulationId, MapId: v.MapId, ProjectId: v.ProjectId}
t.SimulationMap[v.SimulationId] = status
addArr = append(addArr, status)
}
// 已经移除的仿真
2023-08-31 10:30:44 +08:00
for k, v := range t.SimulationMap {
2023-08-31 16:16:18 +08:00
if allSim[k] {
continue
}
2023-08-31 16:16:18 +08:00
delArr = append(delArr, v)
}
2023-08-31 10:30:44 +08:00
// 删除已替换仿真主键
if len(delArr) > 0 {
for _, v := range delArr {
delete(t.SimulationMap, v.SimulationId)
}
}
2023-08-30 18:05:11 +08:00
if len(addArr) == 0 && len(delArr) == 0 {
2023-08-31 16:16:18 +08:00
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()})
}
2023-08-31 16:16:18 +08:00
return []TopicMsg{{channalName: t.getChannelName(), data: b}}
}