Skip to content

Commit

Permalink
Improve Support for KAxxx (#5)
Browse files Browse the repository at this point in the history
* update serial driver
* prepare 1.0.5
  • Loading branch information
XdoctorwhoZ authored Dec 4, 2024
1 parent 6bf14ee commit 098ba5e
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "pza-plugin-korad"
edition = "2021"
version = "1.0.4"
version = "1.0.5"

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

[build-dependencies]
#
Expand Down
4 changes: 3 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod control;
pub mod driver;
pub mod fake;
pub mod real;
pub mod identity;
pub mod measure;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ 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,
log_debug, spawn_on_command, BooleanAttServer, Class, Instance, InstanceLogger,
};
use std::sync::Arc;
use tokio::sync::Mutex;

///
///
/// Mount OCP Attribute
///
pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
mut instance: Instance,
mut class: Class,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
//
// Start logging
let logger = instance.logger.clone();
log_debug!(logger, "Mounting 'control/options/ocp' class...");

//
// Mount the attribute
let att_voltage = class
.create_attribute("ocp")
.with_wo()
Expand All @@ -37,6 +41,10 @@ pub async fn mount<SD: AsciiCmdRespProtocol + 'static>(
on_command(logger_2.clone(), att_voltage_2.clone(), driver.clone())
);

//
//
log_debug!(logger, "Mounting 'control/options/ocp' class -> OK");

//
// Function ok
Ok(())
Expand All @@ -53,7 +61,9 @@ async fn on_command<SD: AsciiCmdRespProtocol + 'static>(
while let Some(command) = value_value_attr.pop_cmd().await {
//
// Log
logger.debug(format!("OCP command received '{:?}'", command));
log_debug!(logger, "OCP command received '{:?}'", command);
//
//
driver.lock().await.set_ocp(command).await?;
}
Ok(())
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 10 additions & 9 deletions src/common/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,17 @@ impl<SD: AsciiCmdRespProtocol> KoradDriver<SD> {
///
///
pub async fn set_ocp(&mut self, value: bool) -> Result<(), Error> {
match value {
true => {
let cmd = "OCP1".to_string();
self.driver.send(&cmd).await
}
false => {
let cmd = "OCP0".to_string();
self.driver.send(&cmd).await
}
//
// Prepare command
let mut cmd = "OCP0".to_string();
if value {
cmd = "OCP1".to_string();
}

//
// Send
log_trace!(self.logger, "SEND => {:?}", cmd);
self.driver.send(&cmd).await
}

///
Expand Down
8 changes: 8 additions & 0 deletions src/common/real/identity.rs → src/common/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub async fn mount<SD: AsciiCmdRespProtocol>(
mut instance: Instance,
driver: Arc<Mutex<KoradDriver<SD>>>,
) -> Result<(), Error> {
//
//
instance.logger.debug("Mounting 'identity'...");

//
// Create attribute
let att_identity = instance
Expand All @@ -25,5 +29,9 @@ pub async fn mount<SD: AsciiCmdRespProtocol>(
let idn = driver.lock().await.get_idn().await?;
att_identity.set(idn).await?;

//
//
instance.logger.debug("Mounting 'identity' => OK");

Ok(())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions src/common/real.rs

This file was deleted.

11 changes: 5 additions & 6 deletions src/ka3005p/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl KA3005PDevice {
SerialSettings::new()
.set_port_name_from_json_or_usb_settings(&json_settings, &usb_settings)
.map_err(|e| Error::Generic(e.to_string()))?
.set_baudrate(DEVICE_SERIAL_BAUDRATE)
.set_time_lock_duration(Duration::from_millis(300)), // require delay between 2 commands
.set_baudrate(DEVICE_SERIAL_BAUDRATE),
);

Ok(())
Expand All @@ -83,7 +82,7 @@ impl KA3005PDevice {
"Serial Settings not provided".to_string(),
))?;

let driver = SerialTimeLockDriver::open(settings)?;
let driver = SerialTimeLockDriver::open(settings, Duration::from_millis(300))?;

let kdriver = KoradDriver::new(driver, instance.logger.clone());

Expand All @@ -107,9 +106,9 @@ impl DriverOperations for KA3005PDevice {

let driver = self.mount_driver(instance.clone())?;

crate::common::real::identity::mount(instance.clone(), driver.clone()).await?;
crate::common::real::control::mount(instance.clone(), driver.clone()).await?;
crate::common::real::measure::mount(instance.clone(), driver.clone()).await?;
crate::common::identity::mount(instance.clone(), driver.clone()).await?;
crate::common::control::mount(instance.clone(), driver.clone()).await?;
crate::common::measure::mount(instance.clone(), driver.clone()).await?;

Ok(())
}
Expand Down
8 changes: 5 additions & 3 deletions src/kd3005p/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_trait::async_trait;

use crate::common::driver::KoradDriver;
use panduza_platform_core::drivers::serial::eol::Driver as SerialEolDriver;
// use panduza_platform_core::drivers::serial::time_lock::Driver as SerialTimeLockDriver;
use panduza_platform_core::drivers::serial::Settings as SerialSettings;
use panduza_platform_core::drivers::usb::Settings as UsbSettings;
use panduza_platform_core::{DriverOperations, Error};
Expand Down Expand Up @@ -94,6 +95,7 @@ impl KD3005PDevice {
))?;

let driver = SerialEolDriver::open(settings, vec![b'\n'])?;
// let driver = SerialTimeLockDriver::open(settings, Duration::from_millis(300))?;

let kdriver = KoradDriver::new(driver, instance.logger.clone());

Expand Down Expand Up @@ -121,9 +123,9 @@ impl DriverOperations for KD3005PDevice {

let driver = self.mount_driver(instance.clone())?;

crate::common::real::identity::mount(instance.clone(), driver.clone()).await?;
crate::common::real::control::mount(instance.clone(), driver.clone()).await?;
crate::common::real::measure::mount(instance.clone(), driver.clone()).await?;
crate::common::identity::mount(instance.clone(), driver.clone()).await?;
crate::common::control::mount(instance.clone(), driver.clone()).await?;
crate::common::measure::mount(instance.clone(), driver.clone()).await?;

//
//
Expand Down
6 changes: 3 additions & 3 deletions src/kd3005p_fake/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl DriverOperations for KD3005PFakeDevice {
async fn mount(&mut self, instance: Instance) -> Result<(), Error> {
let driver = self.mount_driver(instance.clone())?;

crate::common::real::identity::mount(instance.clone(), driver.clone()).await?;
crate::common::real::control::mount(instance.clone(), driver.clone()).await?;
crate::common::real::measure::mount(instance.clone(), driver.clone()).await?;
crate::common::identity::mount(instance.clone(), driver.clone()).await?;
crate::common::control::mount(instance.clone(), driver.clone()).await?;
crate::common::measure::mount(instance.clone(), driver.clone()).await?;

Ok(())
}
Expand Down

0 comments on commit 098ba5e

Please sign in to comment.