rts-sim-module/system/relay_system.go

80 lines
2.1 KiB
Go
Raw Normal View History

2023-09-12 16:39:30 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
sysEvent "joylink.club/rtsssimulation/system/event"
2023-09-15 13:07:31 +08:00
"log"
2023-09-12 16:39:30 +08:00
)
// 继电器有两个线圈1-2,3-4
// 电流方向1->2,3->4或4->3,2->1时产生同向磁场
type RelayState struct {
//继电器是否吸合
//true-吸合
Xh bool
//true-进行吸合动作false-进行落下动作
needXh bool
}
2023-09-15 10:13:25 +08:00
func NewRelayState() *RelayState {
return &RelayState{Xh: false, needXh: false}
}
// 使needXh同Xh
func (me *RelayState) ResetNeedXh() {
me.needXh = me.Xh
}
2023-09-12 16:39:30 +08:00
// 继电器状态组件
var RelayStateComponent = ecs.NewComponentType[RelayState]()
// 继电器系统
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{
2023-09-12 18:01:51 +08:00
relayQuery: ecs.NewQuery(filter.Contains(EntityIdentityComponent, RelayStateComponent)),
2023-09-12 16:39:30 +08:00
}
}
2023-09-15 13:07:31 +08:00
func RelayAddListeners(w ecs.World) {
sysEvent.RelayNeedChangeEventBus.Subscribe(w, RelayNeedChangeEventProcessor)
}
2023-09-12 16:39:30 +08:00
// 继电器须要改变事件处理
var RelayNeedChangeEventProcessor = func(w ecs.World, event sysEvent.RelayNeedChangeEvent) {
2023-09-15 13:07:31 +08:00
log.Println("==>>收到继电器须改变事件id = ", event.Id, " xh = ", event.Xh)
2023-09-12 18:01:51 +08:00
relayEntry := FindEntityById(w, event.Id)
relayState := RelayStateComponent.Get(relayEntry)
relayState.needXh = event.Xh
2023-09-12 16:39:30 +08:00
}
// 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
//发送动作事件
2023-09-12 18:01:51 +08:00
relayId := EntityIdentityComponent.Get(e).Id
sysEvent.RelayActionEventBus.Publish(w, &sysEvent.RelayActionEvent{Id: relayId, Xh: relayState.Xh})
2023-09-12 16:39:30 +08:00
}
})
}