80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package system
|
||
|
||
import (
|
||
"github.com/yohamta/donburi/filter"
|
||
"joylink.club/ecs"
|
||
sysEvent "joylink.club/rtsssimulation/system/event"
|
||
"log"
|
||
)
|
||
|
||
// RelayState 继电器有两个线圈1-2,3-4
|
||
// 电流方向1->2,3->4或4->3,2->1时产生同向磁场
|
||
type RelayState struct {
|
||
//继电器是否吸合
|
||
//true-吸合
|
||
Xh bool
|
||
//true-进行吸合动作,false-进行落下动作
|
||
needXh bool
|
||
}
|
||
|
||
func NewRelayState() *RelayState {
|
||
return &RelayState{Xh: false, needXh: false}
|
||
}
|
||
|
||
// ResetNeedXh 使needXh同Xh
|
||
func (me *RelayState) ResetNeedXh() {
|
||
me.needXh = me.Xh
|
||
}
|
||
|
||
// RelayStateComponent 继电器状态组件
|
||
var RelayStateComponent = ecs.NewComponentType[RelayState]()
|
||
|
||
// RelaySystem 继电器系统
|
||
type RelaySystem struct {
|
||
relayQuery *ecs.Query
|
||
}
|
||
|
||
// 继电器常量定义
|
||
const (
|
||
//JWXC-1700无极缓放继电器,缓放时间ms
|
||
JWXC_1700_RELEASE_TIME int64 = 300
|
||
//JWJXC-H125/80无极加强缓放继电器,缓放时间ms
|
||
JWJXC_H125_80_RELEASE_TIME int64 = 300
|
||
//整流式缓放继电器,缓放时间ms
|
||
JZXC_H18_RELEASE_TIME int64 = 300
|
||
//JPXC-1000偏极继电器,缓放时间ms
|
||
JPXC_1000_RELEASE_TIME int64 = 500
|
||
)
|
||
|
||
func NewRelaySystem() *RelaySystem {
|
||
return &RelaySystem{
|
||
relayQuery: ecs.NewQuery(filter.Contains(EntityIdentityComponent, RelayStateComponent)),
|
||
}
|
||
}
|
||
|
||
func RelayAddListeners(w ecs.World) {
|
||
sysEvent.RelayNeedChangeEventBus.Subscribe(w, RelayNeedChangeEventProcessor)
|
||
}
|
||
|
||
// RelayNeedChangeEventProcessor 继电器须要改变事件处理
|
||
var RelayNeedChangeEventProcessor = func(w ecs.World, event sysEvent.RelayNeedChangeEvent) {
|
||
log.Println("==>>收到继电器须改变事件:id = ", event.Id, " xh = ", event.Xh)
|
||
relayEntry := FindEntityById(w, event.Id)
|
||
relayState := RelayStateComponent.Get(relayEntry)
|
||
relayState.needXh = event.Xh
|
||
}
|
||
|
||
// Update world 执行
|
||
func (me *RelaySystem) Update(w ecs.World) {
|
||
me.relayQuery.Each(w, func(e *ecs.Entry) {
|
||
relayState := RelayStateComponent.Get(e)
|
||
//实际与目标不符,须动作
|
||
if relayState.needXh != relayState.Xh {
|
||
relayState.Xh = relayState.needXh
|
||
//发送动作事件
|
||
relayId := EntityIdentityComponent.Get(e).Id
|
||
sysEvent.RelayActionEventBus.Publish(w, &sysEvent.RelayActionEvent{Id: relayId, Xh: relayState.Xh})
|
||
}
|
||
})
|
||
}
|