2023-09-27 18:39:18 +08:00
|
|
|
package component
|
|
|
|
|
|
|
|
import (
|
2023-11-03 17:26:32 +08:00
|
|
|
"fmt"
|
|
|
|
|
2023-09-27 18:39:18 +08:00
|
|
|
"joylink.club/ecs"
|
2023-11-03 17:26:32 +08:00
|
|
|
"joylink.club/rtsssimulation/consts"
|
2023-09-27 18:39:18 +08:00
|
|
|
"joylink.club/rtsssimulation/repository"
|
|
|
|
)
|
|
|
|
|
2023-11-03 17:26:32 +08:00
|
|
|
// 世界公共数据
|
|
|
|
var WorldDataType = ecs.NewComponentType[WorldData]()
|
|
|
|
|
2023-09-27 18:39:18 +08:00
|
|
|
// 世界数据单例组件
|
|
|
|
type WorldData struct {
|
|
|
|
Repo *repository.Repository
|
|
|
|
// 世界时间,时间戳,单位ms
|
|
|
|
Time int64
|
|
|
|
|
2023-11-03 17:26:32 +08:00
|
|
|
// uid为key的实体对象map
|
2023-09-27 18:39:18 +08:00
|
|
|
EntityMap map[string]*ecs.Entry
|
2023-11-03 17:26:32 +08:00
|
|
|
// 联锁驱采卡实体
|
|
|
|
CiQcEntities []*ecs.Entry
|
2023-11-06 14:35:32 +08:00
|
|
|
// 集中站计轴管理设备实体
|
|
|
|
AxleManageDeviceEntities []*ecs.Entry
|
2023-09-27 18:39:18 +08:00
|
|
|
}
|
|
|
|
|
2023-11-03 17:26:32 +08:00
|
|
|
// 是否在驱动码表中
|
|
|
|
func (wd *WorldData) IsDefinedInQdTable(uid string) bool {
|
|
|
|
for _, entry := range wd.CiQcEntities {
|
|
|
|
qc := CiQcTableType.Get(entry)
|
|
|
|
_, ok := qc.QueryQdIndex(uid)
|
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询驱动位值
|
|
|
|
func (wd *WorldData) QueryQdBit(uid string) (bool, error) {
|
|
|
|
for _, entry := range wd.CiQcEntities {
|
|
|
|
qc := CiQcTableType.Get(entry)
|
|
|
|
idx, ok := qc.QueryQdIndex(uid)
|
|
|
|
if ok {
|
|
|
|
states := CiQcStateType.Get(entry)
|
|
|
|
return consts.GetBitOfBytes(states.Qbs, idx), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("没有定义id=%s的继电器的驱动码表", uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 根据uid获取对应设备的驱动位值
|
|
|
|
// 注意: 若找不到uid驱动码位会panic
|
|
|
|
func (wd *WorldData) GetQdBit(uid string) bool {
|
|
|
|
bit, err := wd.QueryQdBit(uid)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bit
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取继电器驱动位值
|
|
|
|
// 注意: 若没有uid组件或者找不到uid驱动码位会panic
|
|
|
|
func (wd *WorldData) GetRelayEntityQdBit(entry *ecs.Entry) bool {
|
|
|
|
if !entry.HasComponent(UidType) {
|
|
|
|
panic(fmt.Errorf("获取继电器实体驱动位值异常,实体没有uid组件"))
|
|
|
|
}
|
|
|
|
return wd.GetQdBit(UidType.Get(entry).Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 根据uid获取对应设备的驱动位值
|
|
|
|
func (wd *WorldData) SetQdBit(uid string, v bool) error {
|
|
|
|
for _, entry := range wd.CiQcEntities {
|
|
|
|
qc := CiQcTableType.Get(entry)
|
|
|
|
idx, ok := qc.QueryQdIndex(uid)
|
|
|
|
if ok {
|
|
|
|
states := CiQcStateType.Get(entry)
|
|
|
|
consts.SetBitOfBytes(states.Qbs, idx, v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("没有定义id=%s的继电器的驱动码表", uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
type QdBitParam struct {
|
|
|
|
Uid string
|
|
|
|
Val bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewQdBitParam(uid string, v bool) *QdBitParam {
|
|
|
|
return &QdBitParam{
|
|
|
|
Uid: uid,
|
|
|
|
Val: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wd *WorldData) SetQdBits(params []*QdBitParam) error {
|
|
|
|
for _, param := range params {
|
|
|
|
err := wd.SetQdBit(param.Uid, param.Val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|