Skip to content

Commit

Permalink
testing for exhibition 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed May 29, 2023
1 parent 0e71a33 commit d9173f9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 16,4 @@ libc = "^0.2"
bindgen = "^0.51"
log = { version = "^0.4", features = ["std", "serde"] }
env_logger = "^0.10"
concurrent-queue = "^2.2"
24 changes: 19 additions & 5 deletions src/ds_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@ pub mod ds_db {
use std::{collections::HashMap, thread::{self, JoinHandle}, sync::{Arc, Mutex}, any::Any, time::Instant};

use chrono::Utc;
use concurrent_queue::ConcurrentQueue;
use log::{
info,
debug,
Expand All @@ -13,9 14,11 @@ pub mod ds_db {
use crate::{
ds_config::ds_config::{DsDbConf, DsPointConf},
s7_parse_point::s7_parse_point::{ParsePoint, ParsePointType, S7ParsePointBool, S7ParsePointInt, S7ParsePointReal},
s7_client::s7_client::S7Client
s7_client::s7_client::S7Client, ds_point::ds_point::DsPoint
};

pub(crate) const MAX_QUEUE_SIZE: usize = 1024 * 16;

#[derive(Debug)]
pub struct DsDb {
pub name: String,
Expand All @@ -28,6 31,8 @@ pub mod ds_db {
localPoints: HashMap<String, ParsePointType>,
handle: Option<JoinHandle<()>>,
cancel: bool,
// sender: Arc<ConcurrentQueue<DsPoint>>,
// pub receiver: Arc<ConcurrentQueue<DsPoint>>,
}
impl DsDb {
///
Expand Down Expand Up @@ -81,6 86,7 @@ pub mod ds_db {
}
}
}
// let sender = Arc::new(ConcurrentQueue::bounded(MAX_QUEUE_SIZE));
DsDb {
name: config.name,
description: config.description,
Expand All @@ -92,6 98,8 @@ pub mod ds_db {
localPoints: dbPoints,
handle: None,
cancel: false,
// sender: sender.clone(),
// receiver: sender,
}

}
Expand All @@ -108,9 116,9 @@ pub mod ds_db {
let me1 = this.clone();
let delay = this.clone().lock().unwrap().delay as u64;
let handle = thread::Builder::new().name("DsDb.thread".to_string()).spawn(move || {
let me = me.lock().unwrap();
let cancel = me.cancel;
while !cancel {
// let sender = me.clone().lock().unwrap().sender.clone();
while !me.clone().lock().unwrap().cancel {
let me = me.lock().unwrap();
let t = Instant::now();
// let t = Utc::now();
if client.isConnected {
Expand All @@ -125,6 133,10 @@ pub mod ds_db {
ParsePointType::Bool(mut point) => {
point.addRaw(&bytes);
debug!("{} point Bool: {:#?}", logPref, point);
if point.isChanged() {
// let dsPoint = DsPoint::new(point.name, point.path, config)
// sender.push(value)
}
},
ParsePointType::Int(mut point) => {
point.addRaw(&bytes);
Expand All @@ -146,11 158,13 @@ pub mod ds_db {

}
let dt = Instant::now() - t;
debug!("{} {:#?} elapsed: {:?} ({:?})", logPref, me.name , dt, dt.as_millis());
// debug!("{} {:#?} elapsed: {:?} ({:?})", logPref, me.name , dt, dt.as_millis());
let wait: i128 = (delay as i128) - (dt.as_millis() as i128);
if wait > 0 {
std::thread::sleep(std::time::Duration::from_millis(wait as u64));
}
let dt = Instant::now() - t;
debug!("{} {:#?} elapsed: {:?} ({:?})", logPref, me.name , dt, dt.as_millis());
}
info!("{} exit", logPref);
}).unwrap();
Expand Down
28 changes: 16 additions & 12 deletions src/ds_point.rs
Original file line number Diff line number Diff line change
@@ -1,30 1,34 @@
#![allow(non_snake_case)]

pub mod ds_point {
use chrono::{DateTime, Utc};

use crate::ds_config::ds_config::DsPointConf;

#[derive(Debug)]
pub struct DsPoint {
pub struct DsPoint<T> {
pub name: String,
pub path: String,
pub dataType: Option<String>,
pub offset: Option<u32>,
pub comment: Option<String>,
pub vrt: Option<u8>,
pub value: T,
history: u8,
alarm: u8,
pub timestamp: DateTime<Utc>,
}
impl DsPoint {



impl DsPoint<bool> {
pub fn new(
name: String,
path: String,
config: DsPointConf,
) -> DsPoint {
) -> DsPoint<bool> {
DsPoint {
name: name,
path: path,
dataType: config.dataType,
offset: config.offset,
vrt: config.vrt,
comment: config.comment,
value: todo!(),
history: todo!(),
alarm: todo!(),
timestamp: todo!(),
}

}
Expand Down
25 changes: 25 additions & 0 deletions src/s7_parse_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 26,14 @@ pub mod s7_parse_point {
pub trait ParsePoint<T> {
fn addRaw(&mut self, bytes: &Vec<u8>);
fn convert(&self, bytes: &Vec<u8>, start: usize, bit: usize) -> Result<T, TryFromSliceError>;
fn isChanged(&self) -> bool;
}


#[derive(Debug, Clone)]
pub struct S7ParsePointBool {
value: bool,
isChanged: bool,
pub path: String,
pub name: String,
pub dataType: Option<String>,
Expand All @@ -53,6 55,7 @@ pub mod s7_parse_point {
) -> S7ParsePointBool {
S7ParsePointBool {
value: false,
isChanged: false,
path: path,
name: name,
dataType: config.dataType,
Expand All @@ -74,6 77,7 @@ pub mod s7_parse_point {
Ok(newVal) => {
if newVal != self.value {
self.value = newVal;
self.isChanged = true;
}
},
Err(e) => {
Expand All @@ -95,6 99,11 @@ pub mod s7_parse_point {
}
}
}
///
/// returns true if value of point wath updated
fn isChanged(&self) -> bool {
self.isChanged
}
}

///
Expand All @@ -107,6 116,7 @@ pub mod s7_parse_point {
#[derive(Debug, Clone)]
pub struct S7ParsePointInt {
value: i16,
isChanged: bool,
pub path: String,
pub name: String,
pub dataType: Option<String>,
Expand All @@ -129,6 139,7 @@ pub mod s7_parse_point {
) -> S7ParsePointInt {
S7ParsePointInt {
value: 0,
isChanged: false,
path: path,
name: name,
dataType: config.dataType,
Expand All @@ -142,6 153,7 @@ pub mod s7_parse_point {
}
}
impl ParsePoint<i16> for S7ParsePointInt {
///
fn addRaw(&mut self, bytes: &Vec<u8>) {
let result = self.convert(bytes, self.offset.unwrap() as usize, 0);
match result {
Expand All @@ -155,6 167,7 @@ pub mod s7_parse_point {
}
}
}
///
fn convert(&self, bytes: &Vec<u8>, start: usize, _bit: usize) -> Result<i16, TryFromSliceError> {
match bytes[start..(start 2)].try_into() {
Ok(v) => Ok(i16::from_be_bytes(v)),
Expand All @@ -164,11 177,17 @@ pub mod s7_parse_point {
}
}
}
///
/// returns true if value of point wath updated
fn isChanged(&self) -> bool {
self.isChanged
}
}
///
#[derive(Debug, Clone)]
pub struct S7ParsePointReal {
value: f32,
isChanged: bool,
pub path: String,
pub name: String,
pub dataType: Option<String>,
Expand All @@ -192,6 211,7 @@ pub mod s7_parse_point {
) -> S7ParsePointReal {
S7ParsePointReal {
value: 0.0,
isChanged: false,
path: path,
name: name,
dataType: config.dataType,
Expand Down Expand Up @@ -228,6 248,11 @@ pub mod s7_parse_point {
Err(e)
}
}
}
///
/// returns true if value of point wath updated
fn isChanged(&self) -> bool {
self.isChanged
}
}
///
Expand Down

0 comments on commit d9173f9

Please sign in to comment.