@ -1,11 +1,283 @@
package iscs_sys
import "joylink.club/ecs"
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"joylink.club/rtsssimulation/repository"
"time"
)
// PowerTransmissionSystem 电力传递系统
// PowerTransmissionSystem 电力传递系统 (电压传递)
type PowerTransmissionSystem struct {
queryPowerSource * ecs . Query //电力电源
queryPowerPipe * ecs . Query //电力母线
queryCircuitBreaker * ecs . Query //断路器
queryHandcart * ecs . Query //手车
queryDisconnector * ecs . Query //隔离开关
query3PositionSwitch * ecs . Query //三工位隔离开关
queryRectifier * ecs . Query //整流器
}
func NewPowerTransmissionSystem ( ) * PowerTransmissionSystem {
return & PowerTransmissionSystem {
queryPowerSource : ecs . NewQuery ( filter . Contains ( component . UidType , component . PowerSourceType ) ) ,
queryPowerPipe : ecs . NewQuery ( filter . Contains ( component . PowerPipeType ) ) ,
queryCircuitBreaker : ecs . NewQuery ( filter . Contains ( component . UidType , component . CircuitBreakerType ) ) ,
queryHandcart : ecs . NewQuery ( filter . Contains ( component . UidType , component . HandcartSwitchType ) ) ,
queryDisconnector : ecs . NewQuery ( filter . Contains ( component . UidType , component . DisconnectorType ) ) ,
query3PositionSwitch : ecs . NewQuery ( filter . Contains ( component . UidType , component . ThreePositionSwitchType ) ) ,
queryRectifier : ecs . NewQuery ( filter . Contains ( component . UidType , component . RectifierType ) ) ,
}
}
func ( s * PowerTransmissionSystem ) Update ( w ecs . World ) {
s . powerSourceTransPower ( w )
s . circuitBreakerTransPower ( w )
s . handcartTransPower ( w )
s . disconnectorTransPower ( w )
s . threePositionSwitchTransPower ( w )
s . pipeFittingTransPower ( w )
s . rectifierTransPower ( w )
s . powerPipePower ( w )
}
// 整流器电能传递
// 当线路接通时零线和负极电压规定值为1, 当线路断开时零线和负极电压规定值为0
func ( s * PowerTransmissionSystem ) rectifierTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
s . queryRectifier . Each ( w , func ( entry * ecs . Entry ) {
rectifierId := component . UidType . Get ( entry ) . Id
rectifierModel := wd . Repo . FindById ( rectifierId ) . ( * repository . Rectifier )
//
portL := rectifierModel . PortA //交火
//portN := rectifierModel.PortB //交零
portZ := rectifierModel . PortC //直正
portF := rectifierModel . PortD //直负
//
portLPipeEntry := wd . EntityMap [ portL . Device ( ) . Id ( ) ]
portLPipe := component . PowerPipeType . Get ( portLPipeEntry )
//
portZPipeEntry := wd . EntityMap [ portZ . Device ( ) . Id ( ) ]
portZPipe := component . PowerPipeType . Get ( portZPipeEntry )
portFPipeEntry := wd . EntityMap [ portF . Device ( ) . Id ( ) ]
portFPipe := component . PowerPipeType . Get ( portFPipeEntry )
//L->Z、F
for lpsId , lps := range portLPipe . Sources {
if lps . Ac {
dcVoltage := uint32 ( float64 ( lps . Voltage ) * 0.9 ) //交流电压转直流电压
//L->Z
zps , zpsOk := portZPipe . Sources [ lpsId ]
if zpsOk {
zps . Voltage = dcVoltage
zps . Fresh = lps . Fresh - 1
zps . Ac = false
} else {
portZPipe . Sources [ lpsId ] = & component . ElePower { Ac : false , Voltage : dcVoltage , Fresh : lps . Fresh - 1 }
}
//L->F
fps , fpsOk := portFPipe . Sources [ lpsId ]
dcFVoltage := uint32 ( 0 )
if dcVoltage > 0 {
dcFVoltage = 1
}
if fpsOk {
fps . Voltage = dcFVoltage
fps . Fresh = lps . Fresh - 1
fps . Ac = false
} else {
portFPipe . Sources [ lpsId ] = & component . ElePower { Ac : false , Voltage : dcFVoltage , Fresh : lps . Fresh - 1 }
}
}
}
} )
}
// 三工位隔离开关传递电能
func ( s * PowerTransmissionSystem ) threePositionSwitchTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
s . query3PositionSwitch . Each ( w , func ( entry * ecs . Entry ) {
breakerId := component . UidType . Get ( entry ) . Id
closed := component . ThreePositionSwitchType . Get ( entry ) . Position == component . StpClosedWorking
breakerModel := ( wd . Repo . FindById ( breakerId ) ) . ( * repository . ThreePositionSwitch )
breakerPortA := breakerModel . PortA
breakerPortB := breakerModel . PortB
s . towPipePortsTransPower ( wd , closed , breakerPortA , breakerPortB )
} )
}
// 隔离开关传递电能
func ( s * PowerTransmissionSystem ) disconnectorTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
s . queryHandcart . Each ( w , func ( entry * ecs . Entry ) {
breakerId := component . UidType . Get ( entry ) . Id
closed := component . DisconnectorType . Get ( entry ) . Closed
breakerModel := ( wd . Repo . FindById ( breakerId ) ) . ( * repository . Disconnector )
breakerPortA := breakerModel . PortA
breakerPortB := breakerModel . PortB
s . towPipePortsTransPower ( wd , closed , breakerPortA , breakerPortB )
} )
}
// 手车传递电能
func ( s * PowerTransmissionSystem ) handcartTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
s . queryHandcart . Each ( w , func ( entry * ecs . Entry ) {
breakerId := component . UidType . Get ( entry ) . Id
closed := component . HandcartSwitchType . Get ( entry ) . Position == component . HpClosed
breakerModel := ( wd . Repo . FindById ( breakerId ) ) . ( * repository . HandcartSwitch )
breakerPortA := breakerModel . PortA
breakerPortB := breakerModel . PortB
s . towPipePortsTransPower ( wd , closed , breakerPortA , breakerPortB )
} )
}
// 断路器传递电能
func ( s * PowerTransmissionSystem ) circuitBreakerTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
s . queryCircuitBreaker . Each ( w , func ( entry * ecs . Entry ) {
breakerId := component . UidType . Get ( entry ) . Id
closed := component . CircuitBreakerType . Get ( entry ) . Closed
breakerModel := ( wd . Repo . FindById ( breakerId ) ) . ( * repository . CircuitBreaker )
//断路器A端连接的管线
breakerPortA := breakerModel . PortA
//断路器B端连接的管线
breakerPortB := breakerModel . PortB
//传递电能
s . towPipePortsTransPower ( wd , closed , breakerPortA , breakerPortB )
} )
}
// 电力母线中电力计算
func ( s * PowerTransmissionSystem ) powerPipePower ( w ecs . World ) {
s . queryPowerPipe . Each ( w , func ( entry * ecs . Entry ) {
pipe := component . PowerPipeType . Get ( entry )
voltage := uint32 ( 0 )
ac := false
for _ , power := range pipe . Sources {
if power . Voltage > voltage {
voltage = power . Voltage
ac = power . Ac
}
}
//
pipe . Voltage = voltage
pipe . Ac = ac
} )
}
// 电源传递电能
func ( s * PowerTransmissionSystem ) powerSourceTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
s . queryPowerSource . Each ( w , func ( entry * ecs . Entry ) {
psId := component . UidType . Get ( entry ) . Id
ps := component . PowerSourceType . Get ( entry )
//
psModel := ( wd . Repo . FindById ( psId ) ) . ( * repository . PowerSource )
pipeId := psModel . PortA . Device ( ) . Id ( )
//电源传递电力给母线
pipeEntry := wd . EntityMap [ pipeId ]
powerPipe := component . PowerPipeType . Get ( pipeEntry )
powerPipe . TransPower ( psId , & component . ElePower { Ac : ps . Ac , Voltage : ps . Voltage , Fresh : time . Now ( ) . UnixMilli ( ) } )
} )
}
// 母线管件传递电能
func ( s * PowerTransmissionSystem ) pipeFittingTransPower ( w ecs . World ) {
wd := entity . GetWorldData ( w )
for _ , pf := range wd . Repo . PipeFittingMap {
if pf . IsEle ( ) {
//与管件连接的所有管线
pipes := pf . Ports ( )
//筛选出相对电源
pipePsMap := make ( map [ string ] * component . ElePower )
for _ , pipePort := range pipes {
pipeEntry := wd . EntityMap [ pipePort . Device ( ) . Id ( ) ]
powerPipe := component . PowerPipeType . Get ( pipeEntry )
for epId , ep := range powerPipe . Sources {
pipePs , ok := pipePsMap [ epId ]
if ok {
if ep . Fresh > pipePs . Fresh {
pipePsMap [ epId ] = ep
}
} else {
pipePsMap [ epId ] = ep
}
}
}
//管件连接的管线间电能传递
for _ , pipePort := range pipes {
for pipePsId , pipePs := range pipePsMap { //相对电源
pipeEntry := wd . EntityMap [ pipePort . Device ( ) . Id ( ) ]
powerPipe := component . PowerPipeType . Get ( pipeEntry )
pipePortPs , ok := powerPipe . Sources [ pipePsId ]
if ok {
if pipePs . Fresh > pipePortPs . Fresh {
* powerPipe . Sources [ pipePsId ] = * pipePs
powerPipe . Sources [ pipePsId ] . Fresh -= 1 //保证相对性
}
} else {
powerPipe . Sources [ pipePsId ] = & component . ElePower { }
* powerPipe . Sources [ pipePsId ] = * pipePs
powerPipe . Sources [ pipePsId ] . Fresh -= 1 //保证相对性
}
}
}
}
}
}
// 两位置开关传递电能(断路器、手车、隔离开关)
func ( s * PowerTransmissionSystem ) towPipePortsTransPower (
wd * component . WorldData ,
closed bool ,
breakerPortA * repository . PipePort ,
breakerPortB * repository . PipePort ) {
//断路器A端连接的管线
breakerPortAPipeEntry := wd . EntityMap [ breakerPortA . Device ( ) . Id ( ) ]
breakerPortAPipe := component . PowerPipeType . Get ( breakerPortAPipeEntry )
//断路器B端连接的管线
breakerPortBPipeEntry := wd . EntityMap [ breakerPortB . Device ( ) . Id ( ) ]
breakerPortBPipe := component . PowerPipeType . Get ( breakerPortBPipeEntry )
//A->B
for portAPipePsId , portAPipePs := range breakerPortAPipe . Sources { //A
portBPipePs , ok := breakerPortBPipe . Sources [ portAPipePsId ] //B
if ok {
if portAPipePs . Fresh > portBPipePs . Fresh {
* breakerPortBPipe . Sources [ portAPipePsId ] = * portAPipePs
breakerPortBPipe . Sources [ portAPipePsId ] . Fresh -= 1
if ! closed {
breakerPortBPipe . Sources [ portAPipePsId ] . Voltage = 0
}
}
} else {
breakerPortBPipe . Sources [ portAPipePsId ] = & component . ElePower { }
* breakerPortBPipe . Sources [ portAPipePsId ] = * portAPipePs
breakerPortBPipe . Sources [ portAPipePsId ] . Fresh -= 1
if ! closed {
breakerPortBPipe . Sources [ portAPipePsId ] . Voltage = 0
}
}
}
//B->A
for portBPipePsId , portBPipePs := range breakerPortBPipe . Sources { //B
portAPipePs , ok := breakerPortAPipe . Sources [ portBPipePsId ] //A
if ok {
if portBPipePs . Fresh > portAPipePs . Fresh {
* breakerPortAPipe . Sources [ portBPipePsId ] = * portBPipePs
breakerPortAPipe . Sources [ portBPipePsId ] . Fresh -= 1
if ! closed {
breakerPortAPipe . Sources [ portBPipePsId ] . Voltage = 0
}
}
} else {
breakerPortAPipe . Sources [ portBPipePsId ] = & component . ElePower { }
* breakerPortAPipe . Sources [ portBPipePsId ] = * portBPipePs
breakerPortAPipe . Sources [ portBPipePsId ] . Fresh -= 1
if ! closed {
breakerPortAPipe . Sources [ portBPipePsId ] . Voltage = 0
}
}
}
}