-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from PoorRican/update-testing
Major update testing and improve error handling
- Loading branch information
Showing
23 changed files
with
479 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
//! Type aliases for functions and closures to assist `ActionBuilder`. | ||
//! These aliases allow for strongly structuring the dynamic initialization of subscriber/command instances. | ||
use crate::action::{Command, Comparison, PublisherInstance, SubscriberType, CommandType}; | ||
use crate::helpers::Deferred; | ||
use crate::action::CommandType; | ||
use crate::io::IOType; | ||
|
||
// Command Factories | ||
pub type BaseCommandFactory = fn(IOType, IOType) -> CommandType; | ||
|
||
// ********************** | ||
// Subscriber Factories * | ||
// ********************** | ||
|
||
/// Type alias for a function or closure that returns a `ThresholdNotifier` instance | ||
pub type ThresholdNotifierFactory = fn(String, IOType, Comparison, BaseCommandFactory) -> Deferred<SubscriberType>; | ||
pub type BaseCommandFactory = fn(IOType, IOType) -> CommandType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,5 @@ | ||
mod action; | ||
mod device_log; | ||
|
||
pub use action::*; | ||
|
||
use std::sync::{Arc, Mutex}; | ||
use crate::action::{BaseCommandFactory, Comparison, ThresholdNotifier, SimpleNotifier, Publisher, | ||
PublisherInstance}; | ||
use crate::helpers::{Deferrable, Deferred}; | ||
use crate::io::{Device, GenericInput, IdType, InputType, IOKind, IOType}; | ||
use crate::settings::Settings; | ||
use crate::storage::OwnedLog; | ||
|
||
/// Init input and `OwnedLog`, then set owner on log. Return deferred log and deferred input. | ||
pub fn input_log_builder( | ||
name: &str, | ||
id: &IdType, | ||
kind: &Option<IOKind>, | ||
settings: Option<Arc<Settings>>, | ||
) -> (Deferred<OwnedLog>, Deferred<InputType>) { | ||
let log = Arc::new(Mutex::new(OwnedLog::new(*id, settings))); | ||
let input = GenericInput::new(name.to_string(), *id, *kind, log.clone()); | ||
|
||
let wrapped = input.deferred(); | ||
log.lock().unwrap().set_owner(wrapped.clone()); | ||
|
||
(log, wrapped) | ||
} | ||
|
||
pub fn pubsub_builder(input: Deferred<InputType>, name: String, threshold: IOType, trigger: Comparison, | ||
factory: BaseCommandFactory) { | ||
let binding = PublisherInstance::default(); | ||
let publisher = binding.deferred(); | ||
|
||
// attempt to add publisher. Existing publisher is not overwritten. | ||
let _ = input.try_lock().unwrap().add_publisher(publisher.clone()); | ||
|
||
let notifier = ThresholdNotifier::new( | ||
name.clone(), | ||
threshold, | ||
trigger, | ||
factory, | ||
); | ||
let deferred = notifier.deferred(); | ||
let mut binding = publisher.try_lock().unwrap(); | ||
binding.subscribe(deferred); | ||
|
||
println!("Initialized and setup up subscriber: {}", name); | ||
} | ||
pub use device_log::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::helpers::{Deferrable, Deferred}; | ||
use crate::io::{ | ||
DeferredDevice, Device, DeviceType, GenericInput, GenericOutput, IODirection, IOKind, IdType, | ||
}; | ||
use crate::settings::Settings; | ||
use crate::storage::OwnedLog; | ||
use std::sync::{Arc, Mutex, Weak}; | ||
|
||
pub struct DeviceLogBuilder { | ||
device: DeferredDevice, | ||
log: Deferred<OwnedLog>, | ||
} | ||
|
||
impl DeviceLogBuilder { | ||
pub fn new( | ||
name: &str, | ||
id: &IdType, | ||
kind: &Option<IOKind>, | ||
direction: &IODirection, | ||
settings: Option<Arc<Settings>>, | ||
) -> Self { | ||
let log: Deferred<OwnedLog> = Arc::new(Mutex::new(OwnedLog::new(*id, settings))); | ||
let device = match direction { | ||
IODirection::Output => { | ||
let output = GenericOutput::new(name.to_string(), *id, *kind, log.clone()); | ||
DeviceType::Output(output) | ||
} | ||
IODirection::Input => { | ||
let input = GenericInput::new(name.to_string(), *id, *kind, log.clone()); | ||
DeviceType::Input(input) | ||
} | ||
}; | ||
|
||
let wrapped = device.deferred(); | ||
let downgraded: Weak<Mutex<DeviceType>> = Arc::downgrade(&wrapped.clone()); | ||
log.lock().unwrap().set_owner(downgraded); | ||
Self { | ||
device: wrapped, | ||
log, | ||
} | ||
} | ||
|
||
pub fn get(&self) -> (DeferredDevice, Deferred<OwnedLog>) { | ||
(self.device.clone(), self.log.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.