Commit db229b6c by dongshufeng

change example

parent 5d889fbf
......@@ -19,7 +19,6 @@ pub fn read_point_terminal(records: &mut StringRecordsIter<&[u8]>,
} else {
return Err(format!("Wrong point input, row {row} col {col}"));
}
} else if meas_phase.is_some() {
meas_phase.as_mut().unwrap().push(MeasPhase::from(str))
}
......@@ -44,7 +43,8 @@ pub fn read_point_terminal(records: &mut StringRecordsIter<&[u8]>,
Ok(points)
}
pub fn read_terminal_cn_dev(records: &mut StringRecordsIter<&[u8]>) -> Result<Vec<Vec<u64>>, String> {
pub fn read_terminal_cn_dev(records: &mut StringRecordsIter<&[u8]>, mut dev_type: Option<&mut HashMap<u64, u16>>)
-> Result<Vec<Vec<u64>>, String> {
let mut terminals: Vec<Vec<u64>> = Vec::new();
// 按行读取csv
let mut row = 0;
......@@ -54,18 +54,26 @@ pub fn read_terminal_cn_dev(records: &mut StringRecordsIter<&[u8]>) -> Result<Ve
terminals.push(vec![0u64; 3]);
let mut col = 0;
for str in record.iter() {
if let Ok(id) = str.parse() {
terminals[row][col] = id;
} else {
return Err(format!("Wrong terminal input, row {row} col {col}: {str}"));
if col < 3 {
if let Ok(id) = str.parse() {
terminals[row][col] = id;
} else {
return Err(format!("Wrong terminal input, row {row} col {col}: {str}"));
}
} else if col == 3 && dev_type.is_some() {
if let Ok(type_u16) = str.parse::<u16>() {
dev_type.as_mut().unwrap().insert(terminals[row][2], type_u16);
} else {
return Err(format!("Wrong terminal input, row {row} col {col}: {str}"));
}
}
col += 1;
if col == 3 {
if col == 4 {
break;
}
}
if col != 3 {
return Err(format!("Wrong terminal input, expected col at least 3, actual {col}"));
if col != 4 {
return Err(format!("Wrong terminal input, expected col 4, actual {col}"));
}
}
Some(Err(e)) => {
......@@ -81,8 +89,7 @@ pub fn read_terminal_cn_dev(records: &mut StringRecordsIter<&[u8]>) -> Result<Ve
}
pub fn read_static_topo(records: &mut StringRecordsIter<&[u8]>,
mut normal_open: Option<&mut HashMap<u64, bool>>,
mut dev_type: Option<&mut HashMap<u64, u16>>)
mut normal_open: Option<&mut HashMap<u64, bool>>)
-> Result<Vec<Vec<u64>>, String> {
let mut edges = Vec::new();
let mut row = 0;
......@@ -104,9 +111,6 @@ pub fn read_static_topo(records: &mut StringRecordsIter<&[u8]>,
} else if col == 3 {
if let Ok(type_u16) = str.parse::<u16>() {
is_switch = type_u16 == swich_type;
if dev_type.is_some() {
dev_type.as_mut().unwrap().insert(edges[row][2], type_u16);
}
} else {
return Err(format!("Wrong static topology input, row {row} col {col}: {str}"));
}
......
......@@ -41,12 +41,12 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
let mut records = rdr.records();
// 开始读取输入的static topology DataFrame
if input.dfs[i] == STATIC_TOPO_DF_NAME {
match read_static_topo(&mut records, Some(&mut normal_open), None) {
match read_static_topo(&mut records, Some(&mut normal_open)) {
Ok(v) => edges = v,
Err(s) => error = Some(s),
}
} else if input.dfs[i] == TERMINAL_DF_NAME {
match read_terminal_cn_dev(&mut records) {
match read_terminal_cn_dev(&mut records, None) {
Ok(v) => terminals = v,
Err(s) => error = Some(s),
}
......
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use arrow_schema::{DataType, Field, Schema};
......@@ -53,14 +56,9 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
break;
}
}
} else if input.dfs[i] == STATIC_TOPO_DF_NAME {
with_static = true;
match read_static_topo(&mut records, None, Some(&mut dev_type)) {
Ok(_) => {}
Err(s) => error = Some(s),
}
} else if input.dfs[i] == TERMINAL_DF_NAME {
match read_terminal_cn_dev(&mut records) {
with_static = true;
match read_terminal_cn_dev(&mut records, Some(&mut dev_type)) {
Ok(v) => terminals = v,
Err(s) => error = Some(s),
}
......@@ -124,6 +122,21 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
Field::new("terminal", DataType::UInt64, false),
Field::new("phase", DataType::Utf8, false),
]);
// create file
let mut base = PathBuf::from("/");
base.push("shunt_meas.csv");
let mut file = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&base)
.expect("Could not create file");
if let Err(e) = file.write_all(csv_str.as_bytes()) {
log::warn!("!!Failed to write file, err: {:?}", e);
} else {
let _ = file.sync_all();
}
// write graph
let csv_bytes = vec![(SHUNT_MEAS_DF_NAME.to_string(), csv_str.into_bytes())];
let output = PluginOutput {
error_msg: None,
......
......@@ -58,7 +58,7 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
}
if outgoing.contains(&TERMINAL_DF_NAME.to_string()) {
is_matched = true;
let mut terminal_csv_str = String::from("terminal,cn,dev\n");
let mut terminal_csv_str = String::from("terminal,cn,dev,type\n");
let mut terminal_to_cn = HashMap::with_capacity(2 * island.cns.len());
// 先建立CN对应的节点
for cn in &island.cns {
......@@ -70,7 +70,12 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
for terminal in &dev.terminals {
let terminal_id = terminal.id;
if let Some(cn_id) = terminal_to_cn.get(&terminal_id) {
terminal_csv_str.push_str(format!("{terminal_id},{cn_id},{id}\n").as_str());
let dev_type = if let Some(def) = defines.get(&dev.define_id) {
def.rsr_type as u16
} else {
0u16
};
terminal_csv_str.push_str(format!("{terminal_id},{cn_id},{id},{dev_type}\n").as_str());
}
}
}
......@@ -79,6 +84,7 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
Field::new("terminal", DataType::UInt64, false),
Field::new("cn", DataType::UInt64, false),
Field::new("dev", DataType::UInt64, false),
Field::new("type", DataType::UInt32, false),
]));
}
// if let Err(e) = file.write_all(csv_str.as_bytes()) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论