rts-sim-module/system/relay_system.go
2023-09-26 17:54:18 +08:00

79 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
sysEvent "joylink.club/rtsssimulation/system/event"
)
// 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})
}
})
}