use std::fmt;
use crate::types::{service_types::EventNotificationList, status_code::StatusCode};
use super::subscription::MonitoredItem;
pub trait OnSubscriptionNotification {
fn on_data_change(&mut self, _data_change_items: &[&MonitoredItem]) {}
fn on_event(&mut self, _events: &EventNotificationList) {}
}
pub trait OnConnectionStatusChange {
fn on_connection_status_change(&mut self, connected: bool);
}
pub trait OnSessionClosed {
fn on_session_closed(&mut self, status_code: StatusCode);
}
pub struct DataChangeCallback {
cb: Box<dyn Fn(&[&MonitoredItem]) + Send + Sync + 'static>,
}
impl OnSubscriptionNotification for DataChangeCallback {
fn on_data_change(&mut self, data_change_items: &[&MonitoredItem]) {
(self.cb)(data_change_items);
}
}
impl DataChangeCallback {
pub fn new<CB>(cb: CB) -> Self
where
CB: Fn(&[&MonitoredItem]) + Send + Sync + 'static,
{
Self { cb: Box::new(cb) }
}
}
pub struct EventCallback {
cb: Box<dyn Fn(&EventNotificationList) + Send + Sync + 'static>,
}
impl OnSubscriptionNotification for EventCallback {
fn on_event(&mut self, events: &EventNotificationList) {
(self.cb)(events);
}
}
impl EventCallback {
pub fn new<CB>(cb: CB) -> Self
where
CB: Fn(&EventNotificationList) + Send + Sync + 'static,
{
Self { cb: Box::new(cb) }
}
}
pub struct ConnectionStatusCallback {
cb: Box<dyn FnMut(bool) + Send + Sync + 'static>,
}
impl fmt::Debug for ConnectionStatusCallback {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[callback]")
}
}
impl OnConnectionStatusChange for ConnectionStatusCallback {
fn on_connection_status_change(&mut self, connected: bool) {
if connected {
debug!("Received OPC UA connected event");
} else {
debug!("Received OPC UA disconnected event");
}
(self.cb)(connected);
}
}
impl ConnectionStatusCallback {
pub fn new<CB>(cb: CB) -> Self
where
CB: FnMut(bool) + Send + Sync + 'static,
{
Self { cb: Box::new(cb) }
}
}
pub struct SessionClosedCallback {
cb: Box<dyn FnMut(StatusCode) + Send + Sync + 'static>,
}
impl OnSessionClosed for SessionClosedCallback {
fn on_session_closed(&mut self, status_code: StatusCode) {
(self.cb)(status_code);
}
}
impl SessionClosedCallback {
pub fn new<CB>(cb: CB) -> Self
where
CB: FnMut(StatusCode) + Send + Sync + 'static,
{
Self { cb: Box::new(cb) }
}
}