Skip to content

Commit

Permalink
Update Core to 0.2.0 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ authored Dec 23, 2024
1 parent da4ad25 commit c949389
Show file tree
Hide file tree
Showing 17 changed files with 365 additions and 225 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pza-plugin-korad"
edition = "2021"
version = "1.0.7"
version = "1.0.8"

[lib]
path = "src/lib.rs"
Expand All @@ -10,7 +10,7 @@ crate-type = ["lib", "cdylib"]
[dependencies]

# The panduza core library
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.10", features = [
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.2.0", features = [
"serial",
] }
# The panduza connectors library
Expand All @@ -25,7 +25,7 @@ futures = "0.3.17"

[package.metadata.cargo-post.dependencies]
# Only for env paths
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.10" }
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core" }

[build-dependencies]
#
Expand Down
4 changes: 3 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ pub mod driver;
pub mod fake;
pub mod identity;
pub mod measure;
pub mod props;
pub mod settings;

pub use settings::Settings as ControlSettings;
32 changes: 22 additions & 10 deletions src/common/control.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub use super::ControlSettings;

mod current;
mod options;
mod voltage;
use crate::common::driver::KoradDriver;
use panduza_platform_core::{
protocol::AsciiCmdRespProtocol, spawn_on_command, BooleanAttServer, Container, Error, Instance,
Logger,
log_debug_mount_end, log_debug_mount_start, protocol::AsciiCmdRespProtocol, spawn_on_command,
BooleanAttServer, Container, Error, Instance, Logger,
};
use std::sync::Arc;
use tokio::sync::Mutex;
Expand All @@ -15,18 +17,28 @@ use tokio::sync::Mutex;
pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
mut instance: Instance,
driver: Arc<Mutex<KoradDriver<SD>>>,
control_settings: ControlSettings,
) -> Result<(), Error> {
//
// Start logging
let logger = instance.logger.clone();
logger.info("Mounting 'control' class...");

//
// Create attribute
let mut itf_control = instance.create_class("control").finish().await;
let logger = itf_control.logger().clone();
log_debug_mount_start!(logger);

current::mount(instance.clone(), itf_control.clone(), driver.clone()).await?;
voltage::mount(instance.clone(), itf_control.clone(), driver.clone()).await?;
current::mount(
instance.clone(),
itf_control.clone(),
driver.clone(),
control_settings.clone(),
)
.await?;
voltage::mount(
instance.clone(),
itf_control.clone(),
driver.clone(),
control_settings.clone(),
)
.await?;
options::mount(instance.clone(), itf_control.clone(), driver.clone()).await?;

//
Expand All @@ -53,7 +65,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(

//
// End of mount
logger.info("Mounting 'control' class -> OK");
log_debug_mount_end!(logger);
Ok(())
}

Expand Down
39 changes: 26 additions & 13 deletions src/common/control/current.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,76 @@
use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::{log_info, Container, Error, SiAttServer};
use panduza_platform_core::{
log_debug_mount_end, log_debug_mount_start, log_info, Container, Error, SiAttServer,
};
use panduza_platform_core::{spawn_on_command, Class, Instance, Logger};
use std::sync::Arc;
use tokio::sync::Mutex;

///
use super::ControlSettings;

/// control/current
///
pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
mut instance: Instance,
mut class: Class,
driver: Arc<Mutex<KoradDriver<SD>>>,
control_settings: ControlSettings,
) -> Result<(), Error> {
//
// Start logging
let logger = instance.logger.clone();
logger.info("Mounting 'control/current' class...");

//
// Create the attribute
let att_server = class
.create_attribute("current")
.with_rw()
.with_info(r#"Allow to read & write the current limit value of the power supply"#)
.finish_as_si("A", 0, 5, 3)
.finish_as_si(
"A",
control_settings.min_current(),
control_settings.max_current(),
3,
)
.await?;
let logger = att_server.logger();
log_debug_mount_start!(logger);

//
// Init with a first value
let v = driver.lock().await.get_iset().await?;
let mut v = driver.lock().await.get_iset().await?;
if v < control_settings.min_current() as f32 {
v = control_settings.min_current() as f32;
driver.lock().await.set_iset(v).await?;
}
if v > control_settings.max_current() as f32 {
v = control_settings.max_current() as f32;
driver.lock().await.set_iset(v).await?;
}
att_server.set_from_f32(v).await?;

//
// Execute action on each command received
let logger_2 = logger.clone();
let att_server_2 = att_server.clone();
spawn_on_command!(
"on_command => control/current",
instance,
att_server_2,
on_command(logger_2.clone(), att_server_2.clone(), driver.clone())
on_command(att_server_2.clone(), driver.clone())
);

//
// End of mount
logger.info("Mounting 'control/current' class -> OK");
log_debug_mount_end!(logger);
Ok(())
}

///
/// control/current => triggered when command is received
///
async fn on_command<SD: AsciiCmdRespProtocol>(
logger: Logger,
mut att_server: SiAttServer,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
while let Some(command_result) = att_server.pop_cmd_as_f32().await {
let logger = att_server.logger();
match command_result {
Ok(v) => {
log_info!(logger, "'control/current' - command received '{:?}'", v);
Expand Down
36 changes: 24 additions & 12 deletions src/common/control/voltage.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::{log_debug, log_info, Container, Error, Logger, SiAttServer};
use panduza_platform_core::{
log_debug_mount_end, log_debug_mount_start, log_info, Container, Error, Logger, SiAttServer,
};
use panduza_platform_core::{spawn_on_command, Class, Instance};
use std::sync::Arc;
use tokio::sync::Mutex;

///
use super::ControlSettings;

/// control/voltage
///
pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
mut instance: Instance,
mut class: Class,
driver: Arc<Mutex<KoradDriver<SD>>>,
control_settings: ControlSettings,
) -> Result<(), Error> {
//
// Start logging
let logger = instance
.logger
.new_for_attribute(Some("control".to_string()), "voltage");
log_debug!(logger, "Mounting...");

//
// Create the attribute
let att_server = class
.create_attribute("voltage")
.with_rw()
.with_info(r#"Allow to read & write the voltage limit value of the power supply"#)
.finish_as_si("V", 0, 30, 2)
.finish_as_si(
"V",
control_settings.min_voltage(),
control_settings.max_voltage(),
2,
)
.await?;
let logger = att_server.logger();
log_debug_mount_start!(logger);

//
// Init with a first value
let v = driver.lock().await.get_vset().await?;
let mut v = driver.lock().await.get_vset().await?;
if v < control_settings.min_voltage() as f32 {
v = control_settings.min_voltage() as f32;
driver.lock().await.set_vset(v).await?;
}
if v > control_settings.max_voltage() as f32 {
v = control_settings.max_voltage() as f32;
driver.lock().await.set_vset(v).await?;
}
att_server.set_from_f32(v).await?;

//
Expand All @@ -47,7 +59,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(

//
// End of mount
log_debug!(logger, "Mounting => OK");
log_debug_mount_end!(logger);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/measure/ampermeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
.create_attribute("value")
.with_ro()
.with_info(r#"Hold values returned from the ampermeter"#)
.finish_as_si("A", 0, 3, 3)
.finish_as_si("A", 0.0, 3.0, 3)
.await?;

//
Expand Down
2 changes: 1 addition & 1 deletion src/common/measure/voltmeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
.create_attribute("value")
.with_ro()
.with_info(r#"Hold values returned from the ampermeter"#)
.finish_as_si("V", 0, 30, 2)
.finish_as_si("V", 0.0, 30.0, 2)
.await?;

//
Expand Down
78 changes: 0 additions & 78 deletions src/common/props.rs

This file was deleted.

47 changes: 47 additions & 0 deletions src/common/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use panduza_platform_core::std::prop::min_max::Settings as MinMaxSettings;
use panduza_platform_core::{Error, InstanceSettings};

///
///
#[derive(Debug, Clone)]
pub struct Settings {
pub min_max_voltage: MinMaxSettings,
pub min_max_current: MinMaxSettings,
}

impl Settings {
///
///
pub fn new() -> Self {
Self {
min_max_voltage: MinMaxSettings::new("voltage", "voltage limit", 0, 30, None),
min_max_current: MinMaxSettings::new("current", "current limit", 0, 3, None),
}
}

pub fn min_voltage(&self) -> f64 {
return self.min_max_voltage.min;
}
pub fn max_voltage(&self) -> f64 {
return self.min_max_voltage.max;
}
pub fn min_current(&self) -> f64 {
return self.min_max_current.min;
}
pub fn max_current(&self) -> f64 {
return self.min_max_current.max;
}

///
///
pub fn override_with_instance_settings(
&mut self,
settings: &Option<InstanceSettings>,
) -> Result<(), Error> {
self.min_max_voltage
.override_with_instance_settings(settings)?;
self.min_max_current
.override_with_instance_settings(settings)?;
Ok(())
}
}
Loading

0 comments on commit c949389

Please sign in to comment.