Skip to content

Commit 36614fb

Browse files
committed
feat: enable switching actors with high priority
1 parent fea92ac commit 36614fb

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

server/src/actors/mod.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,29 @@ impl ActorState {
332332
enum SystemState {
333333
AllOff,
334334
Producing,
335-
Consuming,
335+
Consuming(usize),
336336
}
337337

338338
fn compute_system_state(producers: &[ActorState], consumers: &[ActorState]) -> SystemState {
339339
if producers.iter().any(|p| p.is_active()) {
340340
SystemState::Producing
341341
} else if consumers.iter().any(|p| p.is_active()) {
342-
SystemState::Consuming
342+
let total_power = consumers
343+
.iter()
344+
.map(|c| match &c.special_state {
345+
ActorStateType::Switching(switching_actor_state) => {
346+
if switching_actor_state.on {
347+
switching_actor_state.power
348+
} else {
349+
0
350+
}
351+
}
352+
ActorStateType::Regulating(regulating_actor_state) => {
353+
regulating_actor_state.regulator.power()
354+
}
355+
})
356+
.sum();
357+
SystemState::Consuming(total_power)
343358
} else {
344359
SystemState::AllOff
345360
}
@@ -435,7 +450,7 @@ pub(crate) async fn control_actors(
435450
}
436451
}
437452
}
438-
SystemState::Consuming => {
453+
SystemState::Consuming(power_consumption) => {
439454
if received > config.upper_limit {
440455
let mut difference_left = received - margin;
441456
for consumer in consumers.iter_mut().rev() {
@@ -452,13 +467,25 @@ pub(crate) async fn control_actors(
452467
if received < config.lower_limit {
453468
let mut difference_left = -(received - margin) as isize;
454469
for consumer in consumers.iter_mut() {
455-
if difference_left <= 0 {
456-
break;
470+
if let ActorStateType::Switching(SwitchingActorState {
471+
switch,
472+
on,
473+
power,
474+
}) = &mut consumer.special_state
475+
{
476+
if *power <= power_consumption && !*on {
477+
switch.on().await;
478+
break;
479+
}
480+
} else {
481+
if difference_left <= 0 {
482+
break;
483+
}
484+
485+
let actual_effect =
486+
consumer.increase_effect_by(difference_left).await as isize;
487+
difference_left -= actual_effect;
457488
}
458-
459-
let actual_effect =
460-
consumer.increase_effect_by(difference_left).await as isize;
461-
difference_left -= actual_effect;
462489
}
463490
}
464491
}

0 commit comments

Comments
 (0)