use std::sync::Arc;
use openadr::core::SystemClock;
use openadr::model::{
EventPayloadDescriptor, EventRequest, Interval, IntervalPeriod, ObjectType, ProgramRequest,
ReadingType, ReportDescriptor, StartTime, Value, ValuesMap, Ven,
};
use openadr::vtn::{Vtn, auth::StaticTokenAuth, notify::Fanout, store::MemoryStorage};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_env_filter("openadr=debug,info")
.init();
let auth = StaticTokenAuth::new("http://localhost:3000/openadr3/3.1.0/auth/token")
.with_business_logic("bl-token", "business-logic".parse()?)
.with_ven("ven-token", "ven-1".parse()?);
let storage = MemoryStorage::shared();
seed(&storage).await?;
let vtn = Vtn::builder()
.storage(storage)
.authenticator(Arc::new(auth))
.build();
println!("VTN on http://localhost:3000/openadr3/3.1.0");
println!(" business logic: Bearer bl-token");
println!(" VEN: Bearer ven-token");
println!(" seeded: programme `day-ahead`, a price event running now, and a grant of");
println!(" `group1` to ven-1 — run `cargo run --features ven --example ven`");
vtn.serve("0.0.0.0:3000").await?;
Ok(())
}
async fn seed(
storage: &openadr::vtn::store::SharedStorage,
) -> Result<(), Box<dyn std::error::Error>> {
use openadr::core::Clock;
let now = SystemClock.now();
storage
.create_ven(
Ven {
id: "pending".parse()?,
created_date_time: now,
modification_date_time: now,
object_type: ObjectType::Ven,
client_id: "ven-1".parse()?,
ven_name: "water-heater-7".parse()?,
targets: vec!["group1".parse()?],
attributes: None,
},
&Fanout::none(),
)
.await?;
let program = storage
.create_program(
ProgramRequest::new("day-ahead".parse()?),
now,
&Fanout::none(),
)
.await?;
let mut event = EventRequest::new(program.id.clone())
.with_targets(vec!["group1".parse()?])
.with_interval_period(IntervalPeriod::new(StartTime::Now, "PT3H".parse()?))
.with_intervals(vec![Interval::new(
0,
vec![ValuesMap::new(
"PRICE".parse()?,
vec![
Value::Number("0.21".parse()?),
Value::Number("0.34".parse()?),
Value::Number("0.12".parse()?),
],
)],
)]);
event.event_name = Some("live-price".into());
event.payload_descriptors = Some(vec![
EventPayloadDescriptor::new("PRICE".parse()?)
.with_units("KWH".parse()?)
.with_currency("EUR"),
]);
event.report_descriptors = Some(vec![
ReportDescriptor {
reading_type: Some(ReadingType::Forecast),
historical: false,
num_intervals: 3,
frequency: 3,
repeat: 1,
start_interval: 0,
..ReportDescriptor::new("USAGE".parse()?)
},
ReportDescriptor {
num_intervals: 1,
frequency: 1,
repeat: 3,
start_interval: 0,
..ReportDescriptor::new("USAGE".parse()?)
},
]);
storage.create_event(event, now, &Fanout::none()).await?;
Ok(())
}