Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core 0.1.9 #6

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[package]
name = "pza-plugin-korad"
edition = "2021"
version = "1.0.5"
version = "1.0.6"

[lib]
path = "src/lib.rs"
crate-type = ["lib", "cdylib"]

[dependencies]

# The panduza core library
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.6", features = [
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.9", features = [
"serial",
] }
# The panduza connectors library
Expand All @@ -24,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.6" }
panduza-platform-core = { git = "https://github.com/Panduza/panduza-platform-core", tag = "0.1.9" }

[build-dependencies]
#
Expand Down
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod driver;
pub mod fake;
pub mod identity;
pub mod measure;
pub mod props;
11 changes: 6 additions & 5 deletions src/common/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mod options;
mod voltage;
use crate::common::driver::KoradDriver;
use panduza_platform_core::{
protocol::AsciiCmdRespProtocol, spawn_on_command, BooleanAttServer, InstanceLogger, Error,
Instance,
protocol::AsciiCmdRespProtocol, spawn_on_command, BooleanAttServer, Container, Error, Instance,
Logger,
};
use std::sync::Arc;
use tokio::sync::Mutex;
Expand All @@ -23,7 +23,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(

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

current::mount(instance.clone(), itf_control.clone(), driver.clone()).await?;
voltage::mount(instance.clone(), itf_control.clone(), driver.clone()).await?;
Expand All @@ -38,13 +38,14 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
.await?;

let v = driver.lock().await.get_out().await?;
att_oe.set(v).await.unwrap();
att_oe.set(v).await?;

//
// Execute action on each command received
let logger_2 = instance.logger.clone();
let att_oe_2 = att_oe.clone();
spawn_on_command!(
"on_command => control",
instance,
att_oe_2,
on_command(logger_2.clone(), att_oe_2.clone(), driver.clone())
Expand All @@ -60,7 +61,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
///
///
async fn on_command<SD: AsciiCmdRespProtocol>(
logger: InstanceLogger,
logger: Logger,
mut att_oe: BooleanAttServer,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
Expand Down
7 changes: 4 additions & 3 deletions src/common/control/current.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::{log_info, Error, SiAttServer};
use panduza_platform_core::{spawn_on_command, Class, InstanceLogger, Instance};
use panduza_platform_core::{log_info, Container, Error, SiAttServer};
use panduza_platform_core::{spawn_on_command, Class, Instance, Logger};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand Down Expand Up @@ -37,6 +37,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
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())
Expand All @@ -52,7 +53,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
/// control/current => triggered when command is received
///
async fn on_command<SD: AsciiCmdRespProtocol>(
logger: InstanceLogger,
logger: Logger,
mut att_server: SiAttServer,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
Expand Down
4 changes: 2 additions & 2 deletions src/common/control/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod ocp;
mod ovp;
use crate::common::driver::KoradDriver;
use panduza_platform_core::{protocol::AsciiCmdRespProtocol, Class, Instance, Error};
use panduza_platform_core::{protocol::AsciiCmdRespProtocol, Class, Container, Error, Instance};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand All @@ -18,7 +18,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
) -> Result<(), Error> {
//
// Create attribute
let itf_options = interface.create_class("options").finish();
let itf_options = interface.create_class("options").finish().await;

ovp::mount(instance.clone(), itf_options.clone(), driver.clone()).await?;
ocp::mount(instance.clone(), itf_options.clone(), driver.clone()).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/common/control/options/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::Error;
use panduza_platform_core::{
spawn_on_command, BidirMsgAtt, BooleanCodec, Device, InstanceLogger, Interface,
spawn_on_command, BidirMsgAtt, BooleanCodec, Device, Logger, Interface,
};
use std::sync::Arc;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -47,7 +47,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol>(
///
///
async fn on_command(
logger: InstanceLogger,
logger: Logger,
mut value_value_attr: BidirMsgAtt<BooleanCodec>,
) -> Result<(), Error> {
while let Some(command) = value_value_attr.pop_cmd().await {
Expand Down
4 changes: 2 additions & 2 deletions src/common/control/options/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::Error;
use panduza_platform_core::{
spawn_on_command, BidirMsgAtt, BooleanCodec, Device, InstanceLogger, Interface,
spawn_on_command, BidirMsgAtt, BooleanCodec, Device, Logger, Interface,
};
use std::sync::Arc;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -47,7 +47,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol>(
///
///
async fn on_command(
logger: InstanceLogger,
logger: Logger,
mut value_value_attr: BidirMsgAtt<BooleanCodec>,
) -> Result<(), Error> {
while let Some(command) = value_value_attr.pop_cmd().await {
Expand Down
4 changes: 2 additions & 2 deletions src/common/control/options/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::Error;
use panduza_platform_core::{
spawn_on_command, BidirMsgAtt, Device, InstanceLogger, EnumCodec, EnumSettings, Interface,
spawn_on_command, BidirMsgAtt, Device, Logger, EnumCodec, EnumSettings, Interface,
};
use std::sync::Arc;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -50,7 +50,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol>(
///
///
async fn on_command(
logger: InstanceLogger,
logger: Logger,
mut value_value_attr: BidirMsgAtt<EnumCodec>,
) -> Result<(), Error> {
while let Some(command) = value_value_attr.pop_cmd().await {
Expand Down
7 changes: 4 additions & 3 deletions src/common/control/options/ocp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::Error;
use panduza_platform_core::{
log_debug, spawn_on_command, BooleanAttServer, Class, Instance, InstanceLogger,
log_debug, spawn_on_command, BooleanAttServer, Class, Instance, Logger,
};
use panduza_platform_core::{Container, Error};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand Down Expand Up @@ -36,6 +36,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
let logger_2 = instance.logger.clone();
let att_voltage_2 = att_voltage.clone();
spawn_on_command!(
"on_command => control/options/ocp",
instance,
att_voltage_2,
on_command(logger_2.clone(), att_voltage_2.clone(), driver.clone())
Expand All @@ -54,7 +55,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
///
///
async fn on_command<SD: AsciiCmdRespProtocol + 'static>(
logger: InstanceLogger,
logger: Logger,
mut value_value_attr: BooleanAttServer,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
Expand Down
9 changes: 4 additions & 5 deletions src/common/control/options/ovp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::Error;
use panduza_platform_core::{
spawn_on_command, BooleanAttServer, Class, InstanceLogger, Instance,
};
use panduza_platform_core::{spawn_on_command, BooleanAttServer, Class, Instance, Logger};
use panduza_platform_core::{Container, Error};
use std::sync::Arc;
use tokio::sync::Mutex;
///
Expand Down Expand Up @@ -31,6 +29,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
let logger_2 = instance.logger.clone();
let att_voltage_2 = att_voltage.clone();
spawn_on_command!(
"on_command => control/options/ovp",
instance,
att_voltage_2,
on_command(logger_2.clone(), att_voltage_2.clone(), driver.clone())
Expand All @@ -45,7 +44,7 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
///
///
async fn on_command<SD: AsciiCmdRespProtocol>(
logger: InstanceLogger,
logger: Logger,
mut value_value_attr: BooleanAttServer,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
Expand Down
19 changes: 11 additions & 8 deletions src/common/control/voltage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::driver::KoradDriver;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::{log_info, Error, SiAttServer};
use panduza_platform_core::{spawn_on_command, Class, InstanceLogger, Instance};
use panduza_platform_core::{log_debug, log_info, Container, Error, Logger, SiAttServer};
use panduza_platform_core::{spawn_on_command, Class, Instance};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand All @@ -15,8 +15,10 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
) -> Result<(), Error> {
//
// Start logging
let logger = instance.logger.clone();
logger.info("Mounting 'control/voltage' class...");
let logger = instance
.logger
.new_for_attribute(Some("control".to_string()), "voltage");
log_debug!(logger, "Mounting...");

//
// Create the attribute
Expand All @@ -37,36 +39,37 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
let logger_2 = logger.clone();
let att_server_2 = att_server.clone();
spawn_on_command!(
"on_command => control/voltage",
instance,
att_server_2,
on_command(logger_2.clone(), att_server_2.clone(), driver.clone())
);

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

///
/// control/voltage => triggered when command is received
///
async fn on_command<SD: AsciiCmdRespProtocol>(
logger: InstanceLogger,
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 {
match command_result {
Ok(v) => {
log_info!(logger, "'control/voltage' - command received '{:?}'", v);
log_info!(logger, "command received '{:?}'", v);
driver.lock().await.set_vset(v).await?;
att_server.set_from_f32(v).await?;
let real_value = driver.lock().await.get_vset().await?;
att_server.set_from_f32(real_value).await?;
}
Err(e) => {
let alert = format!("'control/voltage' - warning on received command '{:?}'", e);
let alert = format!("warning on received command '{:?}'", e);
logger.warn(&alert);
att_server.send_alert(alert).await;
let real_value = driver.lock().await.get_vset().await?;
Expand Down
54 changes: 51 additions & 3 deletions src/common/driver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use async_trait::async_trait;
use panduza_platform_core::protocol::AsciiCmdRespProtocol;
use panduza_platform_core::{log_trace, Error, InstanceLogger};
use panduza_platform_core::std::attribute::idn::IdnReader;
use panduza_platform_core::{log_trace, Error, Logger};
use std::time::Instant;

///
Expand All @@ -12,14 +14,42 @@ pub struct KoradDriver<SD> {

/// Logger for the driver
///
logger: InstanceLogger,
logger: Logger,
}

#[async_trait]
impl<SD: AsciiCmdRespProtocol> IdnReader for KoradDriver<SD> {
async fn read_idn(&mut self) -> Result<String, Error> {
//
// Measure perfs
let start = Instant::now();

//
// Perform request
let cmd = "*IDN?".to_string();
let response = self.driver.ask(&cmd).await?;

//
// Log
log_trace!(
self.logger,
"ASK <=> {:?} - {:?} - {:.2?}",
cmd,
response,
start.elapsed()
);

//
// End
Ok(response)
}
}

impl<SD: AsciiCmdRespProtocol> KoradDriver<SD> {
///
/// Create a new driver
///
pub fn new(driver: SD, logger: InstanceLogger) -> Self {
pub fn new(driver: SD, logger: Logger) -> Self {
Self {
driver: driver,
logger: logger,
Expand Down Expand Up @@ -176,8 +206,26 @@ impl<SD: AsciiCmdRespProtocol> KoradDriver<SD> {
///
///
pub async fn get_out(&mut self) -> Result<bool, Error> {
//
// Measure perfs
let start = Instant::now();

//
//
let cmd = "STATUS?".to_string();
let response = self.driver.ask(&cmd).await?;

//
// Log
log_trace!(
self.logger,
"ASK <=> {:?} - {:?} (as bytes/{:?}) - {:.2?}",
cmd,
response,
response.as_bytes(),
start.elapsed()
);

let byte = response.as_bytes()[0];
if (byte & (1 << 6)) == 0 {
Ok(false)
Expand Down
Loading
Loading