Skip to main content

frequenz_microgrid/logical_meter/
formula.rs

1// License: MIT
2// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3
4//! Formula module for the logical meter.
5
6use std::marker::PhantomData;
7
8use async_trait::async_trait;
9pub(crate) mod aggregation_formula;
10mod async_formula;
11pub(crate) mod coalesce_formula;
12pub(crate) mod graph_formula_provider;
13pub use async_formula::Formula;
14
15use crate::{
16    Error,
17    Sample,
18    metric::Metric,
19    quantity::Quantity, //
20};
21use tokio::sync::{
22    broadcast,
23    mpsc, //
24};
25
26use super::logical_meter_actor;
27
28/// Connects logical meter formulas to the component graph formulas.
29pub(crate) trait GraphFormulaConnector: std::fmt::Display {
30    type GraphFormulaType: frequenz_microgrid_component_graph::Formula;
31}
32
33#[async_trait]
34pub trait FormulaSubscriber: std::fmt::Display + Sync + Send {
35    type QuantityType: Quantity;
36    async fn subscribe(&self) -> Result<broadcast::Receiver<Sample<Self::QuantityType>>, Error>;
37}
38
39/// Parameters for creating a logical meter formula.
40pub(super) struct FormulaParams<F: GraphFormulaConnector, M: Metric> {
41    pub(super) formula: F::GraphFormulaType,
42    pub(super) instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>,
43    phantom: PhantomData<M>,
44}
45
46impl<F: GraphFormulaConnector, M: Metric> FormulaParams<F, M> {
47    pub(super) fn new(
48        formula: F::GraphFormulaType,
49        instructions_tx: mpsc::Sender<logical_meter_actor::Instruction>,
50    ) -> Self {
51        Self {
52            formula,
53            instructions_tx,
54            phantom: PhantomData,
55        }
56    }
57}