-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "msr-plugin-fieldbus-modbus" | ||
version = "0.0.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
log = "0.4" | ||
msr-core = { path = "../msr-core" } | ||
msr-plugin = { path = "../msr-plugin" } | ||
thiserror = "1" | ||
tokio = { version = "1", default_features = false, features = ["sync", "macros"] } | ||
tokio-modbus = "0.5" |
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,17 @@ | ||
use std::net::SocketAddr; | ||
|
||
#[derive(Debug)] | ||
pub enum Message { | ||
Connect(ConnCfg), | ||
Shutdown, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ConnCfg { | ||
Tcp(SocketAddr), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Event { | ||
ConnectionError(String), | ||
} |
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,69 @@ | ||
use crate::api::{ConnCfg, Event, Message}; | ||
use msr_plugin::MessageLoop; | ||
use tokio::sync::{broadcast, mpsc}; | ||
|
||
#[derive(Default)] | ||
pub struct Context { | ||
conn: Connection, | ||
} | ||
|
||
enum Connection { | ||
Disconnected, | ||
Connecting, | ||
Connected(tokio_modbus::client::Context), | ||
} | ||
|
||
impl Default for Connection { | ||
fn default() -> Self { | ||
Self::Disconnected | ||
} | ||
} | ||
|
||
pub fn create_message_loop( | ||
event_tx: broadcast::Sender<Event>, | ||
) -> (MessageLoop, mpsc::UnboundedSender<Message>) { | ||
let mut ctx = Context::default(); | ||
let (message_tx, mut message_rx) = mpsc::unbounded_channel(); | ||
let message_loop = async move { | ||
log::debug!("Entering modbus plugin message loop"); | ||
loop { | ||
tokio::select! { | ||
next_msg = message_rx.recv() => { | ||
if let Some(msg) = next_msg { | ||
log::debug!("Received message: {:?}", msg); | ||
match msg { | ||
Message::Connect(cfg) => { | ||
match cfg { | ||
ConnCfg::Tcp(addr) => { | ||
ctx.conn = Connection::Connecting; | ||
// TODO send event | ||
match tokio_modbus::client::tcp::connect(addr).await { | ||
Ok(mb_ctx) => { | ||
ctx.conn = Connection::Connected(mb_ctx); | ||
// TODO send event | ||
} | ||
Err(err) => { | ||
let ev = Event::ConnectionError(err.to_string()); | ||
if let Err(ev) = event_tx.send(ev) { | ||
log::debug!("No subscribers, dropping event: {:?}", ev); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Message::Shutdown => { | ||
break; | ||
} | ||
} | ||
} else { | ||
log::debug!("All message senders have been dropped"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
log::debug!("Exiting modbus plugin message loop"); | ||
}; | ||
(Box::pin(message_loop), message_tx) | ||
} |
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,44 @@ | ||
use msr_plugin::MessageLoop; | ||
use thiserror::Error; | ||
use tokio::sync::{broadcast, mpsc}; | ||
|
||
mod api; | ||
mod internal; | ||
|
||
pub use api::*; | ||
|
||
pub struct Plugin { | ||
message_loop: MessageLoop, | ||
message_tx: mpsc::UnboundedSender<Message>, | ||
broadcast_tx: broadcast::Sender<Event>, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum SetupError {} | ||
|
||
impl Plugin { | ||
pub fn setup() -> Result<Self, SetupError> { | ||
let (broadcast_tx, _) = broadcast::channel(100); | ||
let event_tx = broadcast_tx.clone(); | ||
let (message_loop, message_tx) = internal::create_message_loop(event_tx); | ||
Ok(Self { | ||
message_tx, | ||
message_loop, | ||
broadcast_tx, | ||
}) | ||
} | ||
} | ||
|
||
impl msr_plugin::Plugin for Plugin { | ||
type Message = Message; | ||
type Event = Event; | ||
fn message_sender(&self) -> mpsc::UnboundedSender<Self::Message> { | ||
self.message_tx.clone() | ||
} | ||
fn subscribe_events(&self) -> broadcast::Receiver<Self::Event> { | ||
self.broadcast_tx.subscribe() | ||
} | ||
fn run(self) -> MessageLoop { | ||
self.message_loop | ||
} | ||
} |