Skip to content

Commit

Permalink
Improve broker + Update core to 0.2.0 (#205)
Browse files Browse the repository at this point in the history
* improve broker

* import old files for futur devs

* update core 0.2.0 and prepare 0.5.4
  • Loading branch information
XdoctorwhoZ authored Dec 23, 2024
1 parent 2a1beb4 commit 5f46914
Show file tree
Hide file tree
Showing 7 changed files with 571 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "panduza-rust-platform"
version = "0.5.3"
version = "0.5.4"
edition = "2021"

[dependencies]

# Main base code for Panduza platform and plugins
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", tag = "0.2.0" }
# Main async framework for the platform
tokio = { version = "1.40.0", features = ["full", "tracing"] }
#
Expand Down Expand Up @@ -72,10 +72,10 @@ rust_decimal_macros = "1.35"
# crate-type = ["lib"] => because plugins need to be compile as simple lib when built-in
# ---
# Virtual Instruments
pza-plugin-vi = { git = "https://github.com/Panduza/pza-plugin-vi", tag = "0.1.1", optional = true }
pza-plugin-vi = { git = "https://github.com/Panduza/pza-plugin-vi", tag = "0.1.2", optional = true }
# ---
# Korad devices
pza-plugin-korad = { git = "https://github.com/Panduza/pza-plugin-korad", tag = "1.0.7", optional = true }
pza-plugin-korad = { git = "https://github.com/Panduza/pza-plugin-korad", tag = "1.0.8", optional = true }
# ---
# Hameg devices
# pza-plugin-hameg = { git = "https://github.com/Panduza/pza-plugin-hameg", tag = "1.0.4", optional = true }
Expand Down
66 changes: 66 additions & 0 deletions src/__platform/connection_info/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ---
// Documentation
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/
// ---

#[derive(Debug)]
pub enum ErrorType {
// COVER:REQ_CONN_INFO_0030_00
ContentBadFormat,
// COVER:REQ_CONN_INFO_0040_00
MandatoryFieldMissing,
// COVER:REQ_CONN_INFO_0050_00
FileDoesNotExist,
}

/// Error for connection info crate
#[derive(Debug)]
pub struct Error {
/// Type of the error
pub err_type: ErrorType,
/// Platform error message
pub plt_error: crate::Error,
}

impl Error {
pub fn new(err_type: ErrorType, plt_error: crate::Error) -> Self {
Self {
err_type: err_type,
plt_error: plt_error
}
}
}

#[macro_export]
macro_rules! connection_info_content_bad_format_error {
($msg:expr) => {
super::Error::new (
crate::platform::connection_info::error::ErrorType::ContentBadFormat,
crate::Error::new(file!(), line!(), $msg.to_string())
)
};
}

#[macro_export]
macro_rules! connection_info_mandatory_field_missing_error {
($msg:expr) => {
super::Error::new (
crate::platform::connection_info::error::ErrorType::MandatoryFieldMissing,
crate::Error::new(file!(), line!(), $msg.to_string())
)
};
}

#[macro_export]
macro_rules! connection_info_file_does_not_exist_error_result {
($msg:expr) => {
Err(
super::Error::new (
crate::platform::connection_info::error::ErrorType::FileDoesNotExist,
crate::Error::new(file!(), line!(), $msg.to_string())
)
)
};
}


84 changes: 84 additions & 0 deletions src/__platform/connection_info/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::env;
use std::io::Write;
use std::path::PathBuf;

use super::Info;
use super::Error;
use super::serde;
use crate::FunctionResult;

use crate::__platform_error;
use crate::connection_info_content_bad_format_error;
use crate::connection_info_file_does_not_exist_error_result;

// ---
// Documentation
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/
// ---

/// Return the system path of the connection.json file
///
/// COVER:PLATF_00001_00 - File System Paths
///
pub fn system_file_path() -> PathBuf {
// Define the paths
let filename = "connection.json";
let unix_path =
PathBuf::from("/etc/panduza").join(filename);
let windows_path =
PathBuf::from(dirs::public_dir().unwrap()).join("panduza").join(filename);

// Return the file path depeding on the OS
match env::consts::OS {
"windows" => { return windows_path; }
"unix" => { return unix_path; }
"linux" => { return unix_path; }
_ => {
tracing::warn!("Unsupported system ({:?}) but try with unix path anyway !", env::consts::OS);
return unix_path;
}
}
}

/// Create a new Info object from a JSON file
///
/// COVER:PLATF_00002_00 - File is JSON
///
pub async fn import_file(file_path: PathBuf) -> Result<Info, Error> {

// Check if the file exists
if !file_path.exists() {
return connection_info_file_does_not_exist_error_result!(file_path.to_str().unwrap());
}

// Try to read the file content
tokio::fs::read_to_string(&file_path).await
.map_err(|e| connection_info_content_bad_format_error!(e.to_string().as_str()) )
.and_then(|v| serde::deserialize(v.as_str()) )
}

/// Create a new Info file from a info data
///
/// COVER:PLATF_00002_00 - File is JSON
///
pub fn export_file(info: &Info) -> FunctionResult {

// Create the file directory
let mut filedir_path = std::path::PathBuf::from(info.file_path.clone());
filedir_path.pop();
std::fs::create_dir_all(filedir_path)
.map_err(|e| __platform_error!(e.to_string()) )?;

// Write new file
let mut file = std::fs::File::create(&info.file_path)
.map_err(|e| __platform_error!(e.to_string()) )?;

// Create the JSON string
let json_string = serde::serialize(info)?;

// Write file
file.write_all(json_string.as_bytes())
.map_err(|e| __platform_error!(e.to_string()) )?;

Ok(())
}
50 changes: 50 additions & 0 deletions src/__platform/connection_info/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::file::system_file_path;

// ---
// Documentation
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/
// ---

/// This object is responsible of the connection information
///
/// It must manage the data but also the file used to store them
///
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Info {
// Path of the file
pub file_path: String,

// broker info
pub broker_addr: String,
pub broker_port: u16,

// credential
pub credentials_user: Option<String>,
pub credentials_pass: Option<String>,

// Platform info
pub platform_name: String,

// Services info
pub services_retry_delay: u32,
pub services_enable_plbd: bool,
}

impl Info {

/// Create a new Info object with default values
///
pub fn default() -> Self {
Self {
file_path: system_file_path().to_str().unwrap().to_string(),
broker_addr: "localhost".to_string(),
broker_port: 1883,
credentials_user: None,
credentials_pass: None,
platform_name: "panduza_platform".to_string(),
services_retry_delay: 1,
services_enable_plbd: false,
}
}

}
31 changes: 31 additions & 0 deletions src/__platform/connection_info/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mod info;
mod file;
mod error;
mod serde;

use std::path::PathBuf;
use crate::FunctionResult;

// ---
// Documentation
// https://panduza.github.io/panduza-doc/docs/50_platform/architecture/connection_info/
// ---

pub type Info = info::Info;
pub type Error = error::Error;
pub type ErrorType = error::ErrorType;

#[inline(always)]
pub fn system_file_path() -> PathBuf {
file::system_file_path()
}

#[inline(always)]
pub async fn import_file(file_path: PathBuf) -> Result<Info, Error> {
file::import_file(file_path).await
}

#[inline(always)]
pub fn export_file(info: &Info) -> FunctionResult {
file::export_file(info)
}
Loading

0 comments on commit 5f46914

Please sign in to comment.