-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
284 lines (268 loc) · 8.28 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* Copyright 2023-2024 Bruce Merry
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use csv::StringRecord;
use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::rc::Rc;
/// Duplicate of crate::fields::FieldType
#[derive(Deserialize, Debug, Clone)]
enum FieldType {
Charge,
Current,
Energy,
Frequency,
Power,
StateOfCharge,
Temperature,
Time,
Voltage,
Unitless,
}
use FieldType::*;
fn split_str<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
Ok(s.split(' ')
.map(|x| x.to_owned())
.filter(|x| !x.is_empty())
.collect())
}
#[derive(Deserialize, Clone)]
struct Field {
field_type: FieldType,
group: String,
name: String,
id: String,
scale: Option<f64>,
#[serde(deserialize_with = "split_str")]
sum_of: Vec<String>,
}
struct Record {
field: Rc<Field>,
positions: Vec<i32>,
}
impl Record {
fn new(
field: &Rc<Field>,
name1: &str,
name2: &str,
header_index: &HashMap<&str, usize>,
row: &StringRecord,
) -> Option<Self> {
let col1 = header_index[name1];
let col2 = header_index[name2];
let mut positions: Vec<i32> = vec![];
if let Some(value1) = row.get(col1).and_then(|x| x.parse().ok()) {
if value1 >= 0 {
positions.push(value1);
if let Some(value2) = row.get(col2).and_then(|x| x.parse().ok()) {
positions.push(value2);
}
}
Some(Self {
field: field.clone(),
positions,
})
} else {
None
}
}
}
fn write_fields_data<W>(w: &mut W, records: &[Record]) -> Result<(), Box<dyn Error>>
where
W: Write,
{
writeln!(w, "&[")?;
let mut by_id: HashMap<&str, usize> = HashMap::new();
for (i, record) in records.iter().enumerate() {
let field = &record.field;
let default_scale = match field.field_type {
Charge | Power | StateOfCharge | Unitless => Some(1.0),
Energy | Temperature => Some(0.1),
Frequency => Some(0.01),
Current | Voltage => None,
Time => Some(60.0),
};
let bias = match field.field_type {
Temperature => -100.0,
_ => 0.0,
};
let unit = match field.field_type {
Charge => "Ah",
Current => "A",
Energy => "kWh",
Frequency => "Hz",
Power => "W",
StateOfCharge => "%",
Temperature => "°C",
Time => "s",
Voltage => "V",
Unitless => "",
};
let scale = field.scale.or(default_scale).unwrap();
let sum_of: Vec<usize> = field
.sum_of
.iter()
.map(|id| {
*by_id
.get(id.as_str())
.unwrap_or_else(|| panic!("Prior field {id:?} not found"))
})
.collect();
writeln!(
w,
r#" crate::fields::Field {{
field_type: crate::fields::FieldType::{:?},
group: {:?},
name: {:?},
id: {:?},
scale: {scale:?},
bias: {bias:?},
unit: {unit:?},
sum_of: &{:?},
}},"#,
field.field_type,
field.group,
field.name,
field.id,
sum_of.as_slice()
)?;
by_id.insert(field.id.as_str(), i);
}
write!(w, "]")?;
Ok(())
}
fn write_fields<W>(w: &mut W, header: &str, records: &[Record]) -> Result<(), Box<dyn Error>>
where
W: Write,
{
writeln!(w, "{header}")?;
write!(w, "const FIELDS: &[crate::fields::Field] = ")?;
write_fields_data(w, records)?;
writeln!(w, ";")?;
writeln!(w, "#[allow(dead_code)]")?;
writeln!(w, "mod field_idx {{")?;
for (i, record) in records.iter().enumerate() {
writeln!(
w,
" pub const {}: usize = {};",
record.field.id.to_uppercase(),
i
)?;
}
writeln!(w, "}}")?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);
let pcap_path = out_path.join("pcap_fields.rs");
let modbus_path = out_path.join("modbus_fields.rs");
let mut reader = csv::Reader::from_reader(fs::File::open("fields.csv")?);
let mut header_index = HashMap::new();
let headers = reader.headers()?.clone();
for (i, header) in headers.iter().enumerate() {
header_index.insert(header, i);
}
let pcap_sizes = &[292, 302];
let mut pcap_records = HashMap::new();
for size in pcap_sizes {
pcap_records.insert(size, vec![]);
}
let mut modbus_records = vec![];
for row in reader.records() {
let row = row?;
let field: Field = row.deserialize(Some(&headers))?;
let field = Rc::new(field);
for (size, records) in pcap_records.iter_mut() {
let offset_name = format!("v{size}_offset");
let offset2_name = format!("v{size}_offset2");
if let Some(record) =
Record::new(&field, &offset_name, &offset2_name, &header_index, &row)
{
records.push(record);
}
}
if let Some(record) = Record::new(&field, "reg", "reg2", &header_index, &row) {
modbus_records.push(record);
}
}
{
let mut pcap_writer = fs::File::create(pcap_path)?;
let mut builder = phf_codegen::Map::new();
for (size, records) in pcap_records.iter() {
let mut buf = Vec::new();
writeln!(&mut buf, " FieldOffsets {{")?;
write!(&mut buf, " fields: ")?;
write_fields_data(&mut buf, records)?;
writeln!(&mut buf, ",")?;
write!(&mut buf, " offsets: &[")?;
for record in records.iter() {
writeln!(&mut buf, " &{:?},", record.positions.as_slice())?;
}
writeln!(&mut buf, " ],")?;
writeln!(&mut buf, " }}")?;
builder.entry(size, &String::from_utf8(buf)?);
}
writeln!(&mut pcap_writer, "struct FieldOffsets {{")?;
writeln!(
&mut pcap_writer,
" fields: &'static [crate::fields::Field<'static>],"
)?;
writeln!(
&mut pcap_writer,
" offsets: &'static [&'static [usize]],"
)?;
writeln!(&mut pcap_writer, "}}")?;
writeln!(
&mut pcap_writer,
"/// Field definitions for different packet sizes"
)?;
writeln!(
&mut pcap_writer,
"static FIELDS: phf::Map<usize, FieldOffsets> = {};",
builder.build()
)?;
}
{
let mut modbus_writer = fs::File::create(modbus_path)?;
write_fields(
&mut modbus_writer,
"/// Fields retrieved by modbus protocol",
&modbus_records,
)?;
writeln!(&mut modbus_writer, "/// Registers corresponding to fields")?;
writeln!(&mut modbus_writer, "const REGISTERS: &[&[u16]] = &[")?;
for record in modbus_records.into_iter() {
writeln!(
&mut modbus_writer,
" &{:?},",
record.positions.as_slice()
)?;
}
writeln!(&mut modbus_writer, "];")?;
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=fields.csv");
Ok(())
}