use std::sync::{Arc, Mutex};
use std::time::Duration;
use serde_json::value::RawValue;
use tokio::net::TcpListener;
use crate::engine::IncomingRequest;
use crate::rpc::CallError;
use crate::transport::{
Auth, AuthOutcome, BasicAuthPassword, BoxFuture, Csms, CsmsHandle, Ctx, Handle, Handler,
SecurityProfile, Station, TransportError,
};
use crate::types::{DateTime, Identity};
use crate::version::Version;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Exchange {
pub identity: Identity,
pub action: String,
pub payload: String,
}
pub type Answer = dyn Fn(&Ctx, &IncomingRequest) -> Result<Box<RawValue>, CallError> + Send + Sync;
struct Recording {
seen: Mutex<Vec<Exchange>>,
identity: Identity,
answer: Box<Answer>,
}
impl Handler for Recording {
fn on_request(
&self,
ctx: Ctx,
request: IncomingRequest,
) -> BoxFuture<'_, Result<Box<RawValue>, CallError>> {
Box::pin(async move {
if let Ok(mut seen) = self.seen.lock() {
seen.push(Exchange {
identity: ctx.identity().clone(),
action: request.action.clone(),
payload: request.payload.get().to_owned(),
});
}
(self.answer)(&ctx, &request)
})
}
}
fn default_answer(ctx: &Ctx, request: &IncomingRequest) -> Result<Box<RawValue>, CallError> {
let now = DateTime::now().to_string();
let body = match request.action.as_str() {
"BootNotification" => {
format!(r#"{{"currentTime":"{now}","interval":300,"status":"Accepted"}}"#)
}
"Heartbeat" => format!(r#"{{"currentTime":"{now}"}}"#),
"Authorize" => r#"{"idTokenInfo":{"status":"Accepted"}}"#.to_owned(),
"TransactionEvent"
| "StatusNotification"
| "NotifyEvent"
| "NotifyReport"
| "MeterValues"
| "SecurityEventNotification"
| "LogStatusNotification"
| "FirmwareStatusNotification" => "{}".to_owned(),
"Reset" | "ChangeAvailability" | "UnlockConnector" | "TriggerMessage" | "SetVariables"
| "GetVariables" | "ClearCache" => r#"{"status":"Accepted"}"#.to_owned(),
_ => return Err(CallError::not_implemented(&request.action)),
};
let _ = ctx;
RawValue::from_string(body)
.map_err(|error| CallError::internal(format!("mock answer is not JSON: {error}")))
}
pub struct MockCsms {
url: String,
port: u16,
handle: CsmsHandle,
recording: Arc<Recording>,
}
impl MockCsms {
pub async fn start() -> Result<Self, TransportError> {
Self::builder().start().await
}
#[must_use]
pub fn builder() -> MockCsmsBuilder {
MockCsmsBuilder {
versions: vec![Version::V2_1, Version::V2_0_1, Version::V1_6],
answer: None,
}
}
#[must_use]
pub fn url(&self) -> &str {
&self.url
}
#[must_use]
pub fn port(&self) -> u16 {
self.port
}
#[must_use]
pub fn handle(&self) -> &CsmsHandle {
&self.handle
}
#[must_use]
pub fn exchanges(&self) -> Vec<Exchange> {
self.recording
.seen
.lock()
.map(|seen| seen.clone())
.unwrap_or_default()
}
#[must_use]
pub fn saw(&self, action: &str) -> bool {
self.exchanges()
.iter()
.any(|exchange| exchange.action == action)
}
pub async fn wait_for(&self, action: &str, within: Duration) -> bool {
let deadline = tokio::time::Instant::now() + within;
while tokio::time::Instant::now() < deadline {
if self.saw(action) {
return true;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
self.saw(action)
}
}
pub struct MockCsmsBuilder {
versions: Vec<Version>,
answer: Option<Box<Answer>>,
}
impl MockCsmsBuilder {
#[must_use]
pub fn versions(mut self, versions: impl IntoIterator<Item = Version>) -> Self {
self.versions = versions.into_iter().collect();
self
}
#[must_use]
pub fn answer(
mut self,
answer: impl Fn(&Ctx, &IncomingRequest) -> Result<Box<RawValue>, CallError>
+ Send
+ Sync
+ 'static,
) -> Self {
self.answer = Some(Box::new(answer));
self
}
pub async fn start(self) -> Result<MockCsms, TransportError> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
let recording = Arc::new(Recording {
seen: Mutex::new(Vec::new()),
identity: Identity::new("mock-csms").expect("a valid identity"),
answer: self.answer.unwrap_or_else(|| Box::new(default_answer)),
});
let csms = Csms::builder()
.bind(addr)
.versions(self.versions)
.authenticate(|_: Auth| async { AuthOutcome::accept() })
.handler(SharedHandler(recording.clone()))
.ping_interval(None)
.build()?;
let handle = csms.handle();
tokio::spawn(async move {
let _ = csms.serve_on(listener).await;
});
Ok(MockCsms {
url: format!("ws://127.0.0.1:{}/ocpp", addr.port()),
port: addr.port(),
handle,
recording,
})
}
}
pub struct MockStation {
handle: Handle,
recording: Arc<Recording>,
}
impl MockStation {
pub fn connect(url: &str, identity: &str) -> Result<Self, TransportError> {
let recording = Arc::new(Recording {
seen: Mutex::new(Vec::new()),
identity: Identity::new(identity)
.map_err(|error| TransportError::Configuration(error.to_string()))?,
answer: Box::new(default_answer),
});
let handle = Station::builder()
.identity(identity)?
.url(url)
.security_profile(SecurityProfile::BasicAuth)
.password(
BasicAuthPassword::utf8("mock-station-secret")
.map_err(TransportError::Credential)?,
)
.ping_interval(None)
.handler(SharedHandler(recording.clone()))
.build()?
.spawn()?;
Ok(Self { handle, recording })
}
#[must_use]
pub fn handle(&self) -> &Handle {
&self.handle
}
#[must_use]
pub fn identity(&self) -> &Identity {
&self.recording.identity
}
pub async fn boot(&self, version: Version) -> Result<Box<RawValue>, CallError> {
let payload = match version {
Version::V1_6 => r#"{"chargePointModel":"Model-1","chargePointVendor":"ACME"}"#,
_ => {
r#"{"reason":"PowerUp","chargingStation":{"model":"Model-1","vendorName":"ACME"}}"#
}
};
let payload = RawValue::from_string(payload.to_owned())
.map_err(|error| CallError::internal(error.to_string()))?;
self.handle
.call_raw(
"BootNotification",
payload,
crate::engine::CallOptions::default(),
)
.await
.map_err(|failure| match failure {
crate::engine::CallFailure::Rejected(error) => error,
other => CallError::internal(other.to_string()),
})?
.into_payload()
.ok_or_else(|| CallError::internal("BootNotification is not a SEND"))
}
#[must_use]
pub fn exchanges(&self) -> Vec<Exchange> {
self.recording
.seen
.lock()
.map(|seen| seen.clone())
.unwrap_or_default()
}
#[must_use]
pub fn saw(&self, action: &str) -> bool {
self.exchanges()
.iter()
.any(|exchange| exchange.action == action)
}
pub async fn shutdown(&self, deadline: Duration) {
self.handle.shutdown(deadline).await;
}
}
struct SharedHandler(Arc<Recording>);
impl Handler for SharedHandler {
fn on_request(
&self,
ctx: Ctx,
request: IncomingRequest,
) -> BoxFuture<'_, Result<Box<RawValue>, CallError>> {
self.0.on_request(ctx, request)
}
}