|
@@ -1,11 +1,12 @@
|
|
|
use crate::cmd::config::app_config;
|
|
|
-use crate::ems::pcs::pcs_conf;
|
|
|
+use crate::connect_or_retry;
|
|
|
use crate::ems::{Consumer, Producer, Service};
|
|
|
use crate::internal::modbus::code::ModbusCode;
|
|
|
-use crate::internal::modbus::slice_sequential;
|
|
|
+use crate::internal::modbus::{read_csv_to_code, slice_sequential};
|
|
|
use crate::internal::utils;
|
|
|
use anyhow::bail;
|
|
|
use log::{error, info};
|
|
|
+use std::net::SocketAddr;
|
|
|
use std::sync::Arc;
|
|
|
use tokio::sync::Mutex;
|
|
|
use tokio_modbus::client::Reader;
|
|
@@ -19,15 +20,10 @@ pub struct Pcs {
|
|
|
pub ctx: Arc<Mutex<tokio_modbus::client::Context>>,
|
|
|
}
|
|
|
|
|
|
-/// Modbus-TCP连接
|
|
|
-/// FIXME 考虑重连机制
|
|
|
async fn connect_modbus_tcp() -> anyhow::Result<tokio_modbus::client::Context> {
|
|
|
- let pcs_config = &app_config().emu.pcs;
|
|
|
- let ctx = tokio_modbus::client::tcp::connect(
|
|
|
- format!("{}:{}", pcs_config.host, pcs_config.port).parse()?,
|
|
|
- )
|
|
|
- .await?;
|
|
|
- Ok(ctx)
|
|
|
+ let config = &app_config().emu.pcs;
|
|
|
+ let addr: SocketAddr = format!("{}:{}", config.host, config.port).parse()?;
|
|
|
+ connect_or_retry!(tokio_modbus::client::tcp::connect(addr), "ModbusTCP")
|
|
|
}
|
|
|
|
|
|
impl Pcs {
|
|
@@ -35,18 +31,8 @@ impl Pcs {
|
|
|
pub async fn new(producer: Producer, consumer: Consumer) -> anyhow::Result<Self> {
|
|
|
let id = utils::generate_random_str(12);
|
|
|
utils::log::init_log("inpower_iot_mgc_rs::ems::pcs::*", "pcs/pcs.log").await;
|
|
|
- let ctx = match connect_modbus_tcp().await {
|
|
|
- Ok(c) => c,
|
|
|
- Err(e) => {
|
|
|
- error!(
|
|
|
- "【PCS】Modbus Tcp {}连接失败: {}",
|
|
|
- &app_config().emu.pcs.host,
|
|
|
- e.to_string()
|
|
|
- );
|
|
|
- return Err(e);
|
|
|
- }
|
|
|
- };
|
|
|
- let modbus_code = match pcs_conf() {
|
|
|
+ let ctx = connect_modbus_tcp().await?;
|
|
|
+ let modbus_code = match read_csv_to_code("./config/pcs.csv") {
|
|
|
Ok(c) => c,
|
|
|
Err(e) => {
|
|
|
error!("【PCS】协议生成失败: {}", e.to_string());
|