286 lines
9.2 KiB
Go
286 lines
9.2 KiB
Go
package fi
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"joylink.club/ecs"
|
||
"joylink.club/rtsssimulation/component"
|
||
"joylink.club/rtsssimulation/consts"
|
||
"joylink.club/rtsssimulation/entity"
|
||
)
|
||
|
||
// ValveOperate 阀门操作
|
||
//
|
||
// deviceId 阀门id;
|
||
// openRate 阀门打开百分比值,0-全关,100-全开;
|
||
func ValveOperate(w ecs.World, deviceId string, openRate uint8) error {
|
||
if openRate < 0 || openRate > 100 {
|
||
return fmt.Errorf("阀门打开百分比值[%d]不在范围[0,100]内", openRate)
|
||
}
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.ValveType) && deviceEntry.HasComponent(component.ValveControllerType) && deviceEntry.HasComponent(component.FixedPositionTransformType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是阀门", deviceId))
|
||
}
|
||
component.ValveControllerType.Get(deviceEntry).TargetOpenRate = openRate
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// PurificationDeviceOperate 净化装置操作
|
||
//
|
||
// optStart : true-启动净化器;false-关停净化器
|
||
func PurificationDeviceOperate(w ecs.World, deviceId string, optStart bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.PurificationDeviceType) && deviceEntry.HasComponent(component.CounterType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是净化装置", deviceId))
|
||
}
|
||
device := component.PurificationDeviceType.Get(deviceEntry)
|
||
counter := component.CounterType.Get(deviceEntry)
|
||
if optStart {
|
||
if device.Running {
|
||
return ecs.NewErrResult(fmt.Errorf("净化装置[%s]已经启动在运行", deviceId))
|
||
}
|
||
device.Starting = true
|
||
counter.Val = 0
|
||
counter.Step = w.Tick()
|
||
} else { //关停
|
||
device.Running = false
|
||
}
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
/////////////////////////////////////////////////////////////////////////
|
||
|
||
// EscalatorOptEnum 自动扶梯操作定义
|
||
type EscalatorOptEnum = uint8
|
||
|
||
const (
|
||
EscalatorOptUp EscalatorOptEnum = iota //上行
|
||
EscalatorOptDown //下行
|
||
EscalatorOptStop //停止
|
||
EscalatorOptEs //急停
|
||
)
|
||
|
||
// EscalatorDeviceOperate 自动扶梯操作
|
||
func EscalatorDeviceOperate(w ecs.World, deviceId string, opt EscalatorOptEnum) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.EscalatorType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是自动扶梯", deviceId))
|
||
}
|
||
//
|
||
escalator := component.EscalatorType.Get(deviceEntry)
|
||
escalator.EmergencyStop = false
|
||
switch opt {
|
||
case EscalatorOptUp:
|
||
escalator.Running = 1
|
||
case EscalatorOptDown:
|
||
escalator.Running = -1
|
||
case EscalatorOptStop:
|
||
escalator.Running = 0
|
||
case EscalatorOptEs:
|
||
escalator.EmergencyStop = true
|
||
}
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// ElevatorDeviceOperate 垂直电梯操作
|
||
//
|
||
// optRun : true-运行;false-停止
|
||
func ElevatorDeviceOperate(w ecs.World, deviceId string, optRun bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.ElevatorType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是垂直电梯", deviceId))
|
||
}
|
||
elevator := component.ElevatorType.Get(deviceEntry)
|
||
elevator.Running = optRun
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// ChillerUnitOperate 冷水机组
|
||
//
|
||
// optRun : true-正常运行;false-停止
|
||
func ChillerUnitOperate(w ecs.World, deviceId string, optRun bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.ChillerUnitType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是冷水机组", deviceId))
|
||
}
|
||
elevator := component.ChillerUnitType.Get(deviceEntry)
|
||
elevator.Running = optRun
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// WaterPumpOperate 水泵
|
||
//
|
||
// optRun : true-正常运行;false-停止
|
||
func WaterPumpOperate(w ecs.World, deviceId string, optRun bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.WaterPumpType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是水泵", deviceId))
|
||
}
|
||
pump := component.WaterPumpType.Get(deviceEntry)
|
||
pump.Running = optRun
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// WaterClosedContainerOperate 封闭式水容器(如冷却塔、水处理器)
|
||
//
|
||
// optRun : true-正常运行;false-停止
|
||
func WaterClosedContainerOperate(w ecs.World, deviceId string, optRun bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.WaterClosedContainerType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是封闭式水容器(如冷却塔、水处理器)", deviceId))
|
||
}
|
||
container := component.WaterPumpType.Get(deviceEntry)
|
||
container.Running = optRun
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// CivilDefenseDoorOperate 封闭式水容器(如冷却塔、水处理器)
|
||
//
|
||
// optOpen : true-开门;false-关门
|
||
func CivilDefenseDoorOperate(w ecs.World, deviceId string, optOpen bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.CivilDefenseDoorType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是人防门", deviceId))
|
||
}
|
||
door := component.CivilDefenseDoorType.Get(deviceEntry)
|
||
door.Open = optOpen
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// 应急照明操作
|
||
func emergencyLightingOperate(w ecs.World, deviceId string, opt component.EmergencyLightingMode) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.EmergencyLightingType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是应急照明", deviceId))
|
||
}
|
||
de := component.DeviceExceptionType.Get(deviceEntry)
|
||
if de.Exception != consts.DeviceExceptionNon {
|
||
return ecs.NewErrResult(fmt.Errorf("应急照明[%s]有故障,不能设置工作模式", deviceId))
|
||
}
|
||
//
|
||
device := component.EmergencyLightingType.Get(deviceEntry)
|
||
device.Mode = opt
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|
||
|
||
// EmergencyLightingAutoOperate 应急照明自动/正常运行
|
||
func EmergencyLightingAutoOperate(w ecs.World, deviceId string) error {
|
||
return emergencyLightingOperate(w, deviceId, component.ElmAuto)
|
||
}
|
||
|
||
// EmergencyLightingEmergencyOperate 应急照明开启应急模式
|
||
func EmergencyLightingEmergencyOperate(w ecs.World, deviceId string) error {
|
||
return emergencyLightingOperate(w, deviceId, component.ElmEmergency)
|
||
}
|
||
|
||
// EmergencyLightingFirefightingOperate 应急照明开启消防强制启动模式
|
||
func EmergencyLightingFirefightingOperate(w ecs.World, deviceId string) error {
|
||
return emergencyLightingOperate(w, deviceId, component.ElmFirefighting)
|
||
}
|
||
|
||
// AirCurtainOperate 空气幕操作
|
||
func AirCurtainOperate(w ecs.World, deviceId string, optRun bool) error {
|
||
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
if !ok {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
}
|
||
//
|
||
if !(deviceEntry.HasComponent(component.AirCurtainType)) {
|
||
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是空气幕", deviceId))
|
||
}
|
||
device := component.AirCurtainType.Get(deviceEntry)
|
||
de := component.DeviceExceptionType.Get(deviceEntry)
|
||
if optRun && de.Exception != consts.DeviceExceptionNon {
|
||
return ecs.NewErrResult(fmt.Errorf("空气幕[%s]有故障,不能开启", deviceId))
|
||
}
|
||
//
|
||
device.Running = optRun
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
})
|
||
return r.Err
|
||
}
|