mod apdu;
mod layout;
mod publisher;
mod subscriber;
pub const fn smp_cnt_wrap(samples_per_second: u32) -> u32 {
match samples_per_second {
0 => 1,
n if n > 65_536 => 65_536,
n => n,
}
}
pub use apdu::{Asdu, AsduOffsets, AsduView, Field, SavPdu, SavPduView, SmpSynch, TAG_SAV_PDU};
pub use layout::{Channel, ChannelType, ChannelValue, SampleLayout};
pub use publisher::{Publisher, PublisherConfig, SmpMod, SvProfile};
pub use subscriber::{Sample, SimulationMode, StreamConfig, StreamKey, StreamState, Subscriber, SubscriberEvent};
#[allow(clippy::indexing_slicing, clippy::cast_precision_loss)] pub mod le {
use crate::common::Quality;
pub const APPID: u16 = 0x4000;
pub const SAMPLES_PER_CYCLE_PROTECTION: u32 = 80;
pub const SAMPLES_PER_CYCLE_MEASUREMENT: u32 = 256;
pub const SCALE_CURRENT: f32 = 0.001;
pub const SCALE_VOLTAGE: f32 = 0.01;
pub const SAMPLE_LEN: usize = 64;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PhsMeas1 {
pub currents: [i32; 4],
pub current_quality: [Quality; 4],
pub voltages: [i32; 4],
pub voltage_quality: [Quality; 4],
}
impl PhsMeas1 {
pub fn decode(sample: &[u8]) -> Option<PhsMeas1> {
if sample.len() != SAMPLE_LEN {
return None;
}
let mut currents = [0i32; 4];
let mut voltages = [0i32; 4];
let mut cq = [Quality::GOOD; 4];
let mut vq = [Quality::GOOD; 4];
for i in 0..8 {
let o = i * 8;
let v = i32::from_be_bytes([sample[o], sample[o + 1], sample[o + 2], sample[o + 3]]);
let q = Quality::from_bits_msb(u32::from_be_bytes([sample[o + 4], sample[o + 5], sample[o + 6], sample[o + 7]]));
if i < 4 {
currents[i] = v;
cq[i] = q;
} else {
voltages[i - 4] = v;
vq[i - 4] = q;
}
}
Some(PhsMeas1 { currents, current_quality: cq, voltages, voltage_quality: vq })
}
pub fn encode(&self) -> [u8; SAMPLE_LEN] {
let mut out = [0u8; SAMPLE_LEN];
for i in 0..8 {
let (v, q) = if i < 4 { (self.currents[i], self.current_quality[i]) } else { (self.voltages[i - 4], self.voltage_quality[i - 4]) };
let o = i * 8;
out[o..o + 4].copy_from_slice(&v.to_be_bytes());
out[o + 4..o + 8].copy_from_slice(&q.to_bits_msb().to_be_bytes());
}
out
}
pub fn currents_a(&self) -> [f32; 4] {
self.currents.map(|v| v as f32 * SCALE_CURRENT)
}
pub fn voltages_v(&self) -> [f32; 4] {
self.voltages.map(|v| v as f32 * SCALE_VOLTAGE)
}
}
}