rts-sim-module/fi/relay.go

97 lines
2.5 KiB
Go
Raw Normal View History

2023-10-08 09:59:59 +08:00
package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
// 继电器功能接口
// 控制有极继电器励磁
// id - 继电器uid
// td - 是否通电励磁
// xq - 是否到吸起位置(有极继电器)
func driveYjRelay(w ecs.World, entry *ecs.Entry, td bool, xq bool) {
rd := component.RelayDriveType.Get(entry)
rd.Td = td
rd.Xq = xq
}
// 控制无极继电器励磁
// id - 继电器uid
// td - 是否通电励磁
// xq - 是否到吸起位置(有极继电器)
func driveWjRelay(w ecs.World, entry *ecs.Entry, td bool) {
rd := component.RelayDriveType.Get(entry)
2023-10-08 10:28:50 +08:00
rd.Td = td
2023-10-08 09:59:59 +08:00
}
// 驱动继电器到吸起位置
func DriveRelayUp(w ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
2023-10-08 09:59:59 +08:00
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
if entry.HasComponent(component.YjRelayTag) {
driveYjRelay(w, entry, true, true)
} else {
driveWjRelay(w, entry, true)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id))
2023-10-08 09:59:59 +08:00
}
return ecs.NewOkEmptyResult()
2023-10-08 09:59:59 +08:00
})
return result.Err
2023-10-08 09:59:59 +08:00
}
// 驱动继电器到落下位置
func DriveRelayDown(w ecs.World, id string) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
2023-10-08 10:26:24 +08:00
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
if entry.HasComponent(component.YjRelayTag) {
driveYjRelay(w, entry, true, false)
2023-10-08 09:59:59 +08:00
} else {
2023-10-08 10:26:24 +08:00
driveWjRelay(w, entry, false)
2023-10-08 09:59:59 +08:00
}
2023-10-08 10:26:24 +08:00
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id))
2023-10-08 10:26:24 +08:00
}
return ecs.NewOkEmptyResult()
2023-10-08 09:59:59 +08:00
})
return result.Err
2023-10-08 09:59:59 +08:00
}
// 设置继电器强制故障
func SetRelayFaultForce(w ecs.World, id string, q bool) error {
return updateRelayFault(w, id, func(entry *ecs.Entry) {
component.AddOrUpdateRelayFaultForce(entry, q)
})
}
// 取消继电器强制故障
func CancelRelayFaultForce(w ecs.World, id string) error {
return updateRelayFault(w, id, func(entry *ecs.Entry) {
entry.RemoveComponent(component.RelayFaultForceType)
})
}
func updateRelayFault(w ecs.World, id string, faultHandle func(entry *ecs.Entry)) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
faultHandle(entry)
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}