use std::time::Duration;
use spvirit_server::PvaServer;
use spvirit_types::ScalarValue;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = PvaServer::builder()
.ai("SIM:TEMPERATURE", 22.5)
.ao("SIM:SETPOINT", 25.0)
.bo("SIM:ENABLE", false)
.build();
let store = server.store().clone();
tokio::spawn(async move {
let alpha = 0.10; let mut interval = tokio::time::interval(Duration::from_millis(200));
loop {
interval.tick().await;
let enabled = matches!(
store.get_value("SIM:ENABLE").await,
Some(ScalarValue::Bool(true))
);
if !enabled {
continue;
}
let sp = match store.get_value("SIM:SETPOINT").await {
Some(ScalarValue::F64(v)) => v,
_ => continue,
};
let temp = match store.get_value("SIM:TEMPERATURE").await {
Some(ScalarValue::F64(v)) => v,
_ => continue,
};
let new_temp = temp + alpha * (sp - temp);
store
.set_value("SIM:TEMPERATURE", ScalarValue::F64(new_temp))
.await;
}
});
server.run().await
}