use core::fmt;
use crate::codec::{Decode, Encode, Error, ErrorKind, Reader, Result, Writer};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Medium {
Abstract,
Electricity,
ElectricityDc,
HeatCostAllocator,
Cooling,
Heat,
Gas,
ColdWater,
HotWater,
Other,
Reserved(u8),
}
impl Medium {
#[must_use]
pub const fn as_u8(self) -> u8 {
match self {
Self::Abstract => 0,
Self::Electricity => 1,
Self::ElectricityDc => 2,
Self::HeatCostAllocator => 4,
Self::Cooling => 5,
Self::Heat => 6,
Self::Gas => 7,
Self::ColdWater => 8,
Self::HotWater => 9,
Self::Other => 15,
Self::Reserved(v) => v,
}
}
#[must_use]
pub const fn from_u8(v: u8) -> Self {
match v {
0 => Self::Abstract,
1 => Self::Electricity,
2 => Self::ElectricityDc,
4 => Self::HeatCostAllocator,
5 => Self::Cooling,
6 => Self::Heat,
7 => Self::Gas,
8 => Self::ColdWater,
9 => Self::HotWater,
15 => Self::Other,
other => Self::Reserved(other),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Obis(pub [u8; 6]);
impl Obis {
#[must_use]
pub const fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> Self {
Self([a, b, c, d, e, f])
}
#[must_use]
pub const fn from_bytes(b: [u8; 6]) -> Self {
Self(b)
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 6] {
&self.0
}
#[must_use]
pub const fn a(&self) -> u8 {
self.0[0]
}
#[must_use]
pub const fn b(&self) -> u8 {
self.0[1]
}
#[must_use]
pub const fn c(&self) -> u8 {
self.0[2]
}
#[must_use]
pub const fn d(&self) -> u8 {
self.0[3]
}
#[must_use]
pub const fn e(&self) -> u8 {
self.0[4]
}
#[must_use]
pub const fn f(&self) -> u8 {
self.0[5]
}
#[must_use]
pub const fn medium(&self) -> Medium {
Medium::from_u8(self.0[0])
}
#[must_use]
pub const fn is_manufacturer_specific(&self) -> bool {
(self.0[2] >= 128 && self.0[2] <= 199)
|| (self.0[3] >= 128 && self.0[3] <= 254)
|| (self.0[4] >= 128 && self.0[4] <= 254)
}
pub fn parse(s: &str) -> core::result::Result<Self, ObisParseError> {
let mut groups = [0u16; 6];
let mut n = 0usize;
let mut have_digit = false;
let mut cur: u16 = 0;
for ch in s.bytes() {
match ch {
b'0'..=b'9' => {
if !have_digit {
cur = 0;
}
have_digit = true;
cur = cur
.checked_mul(10)
.and_then(|v| v.checked_add(u16::from(ch - b'0')))
.ok_or(ObisParseError)?;
if cur > 255 {
return Err(ObisParseError);
}
}
b'-' | b':' | b'.' | b'*' | b'&' => {
if !have_digit || n >= 6 {
return Err(ObisParseError);
}
groups[n] = cur;
n += 1;
have_digit = false;
}
_ => return Err(ObisParseError),
}
}
if !have_digit || n >= 6 {
return Err(ObisParseError);
}
groups[n] = cur;
n += 1;
let g = |i: usize| groups.get(i).copied().unwrap_or(0) as u8;
match n {
3 => Ok(Self::new(1, 0, g(0), g(1), g(2), 255)),
5 => Ok(Self::new(g(0), g(1), g(2), g(3), g(4), 255)),
6 => Ok(Self::new(g(0), g(1), g(2), g(3), g(4), g(5))),
_ => Err(ObisParseError),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ObisParseError;
impl fmt::Display for ObisParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("not an OBIS code")
}
}
#[cfg(feature = "std")]
impl std::error::Error for ObisParseError {}
impl core::str::FromStr for Obis {
type Err = ObisParseError;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
Self::parse(s)
}
}
impl fmt::Display for Obis {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let [a, b, c, d, e, g] = self.0;
write!(f, "{a}-{b}:{c}.{d}.{e}*{g}")
}
}
impl fmt::Debug for Obis {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Obis({self})")
}
}
impl From<[u8; 6]> for Obis {
fn from(v: [u8; 6]) -> Self {
Self(v)
}
}
impl From<Obis> for [u8; 6] {
fn from(v: Obis) -> Self {
v.0
}
}
impl Encode for Obis {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_bytes(&self.0)
}
fn encoded_len(&self) -> usize {
6
}
}
impl<'a> Decode<'a> for Obis {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self(r.array::<6>()?))
}
}
impl Obis {
pub fn from_octet_string(bytes: &[u8], offset: usize) -> Result<Self> {
let arr: [u8; 6] = bytes.try_into().map_err(|_| Error::new(ErrorKind::InvalidLength, offset))?;
Ok(Self(arr))
}
}
#[cfg(feature = "obis-names")]
pub mod names {
use super::Obis;
pub const CLOCK: Obis = Obis::new(0, 0, 1, 0, 0, 255);
pub const LOGICAL_DEVICE_NAME: Obis = Obis::new(0, 0, 42, 0, 0, 255);
pub const CURRENT_ASSOCIATION: Obis = Obis::new(0, 0, 40, 0, 0, 255);
pub const SECURITY_SETUP: Obis = Obis::new(0, 0, 43, 0, 0, 255);
pub const SAP_ASSIGNMENT: Obis = Obis::new(0, 0, 41, 0, 0, 255);
pub const DEVICE_ID_1: Obis = Obis::new(0, 0, 96, 1, 0, 255);
pub const ERROR_REGISTER: Obis = Obis::new(0, 0, 97, 97, 0, 255);
pub const EVENT_LOG: Obis = Obis::new(0, 0, 99, 98, 0, 255);
pub const HDLC_SETUP: Obis = Obis::new(0, 0, 22, 0, 0, 255);
pub const DISCONNECT_CONTROL: Obis = Obis::new(0, 0, 96, 3, 10, 255);
pub const PUSH_SETUP: Obis = Obis::new(0, 0, 25, 9, 0, 255);
pub const IMAGE_TRANSFER: Obis = Obis::new(0, 0, 44, 0, 0, 255);
pub const ACTIVE_ENERGY_IMPORT_TOTAL: Obis = Obis::new(1, 0, 1, 8, 0, 255);
pub const ACTIVE_ENERGY_IMPORT_T1: Obis = Obis::new(1, 0, 1, 8, 1, 255);
pub const ACTIVE_ENERGY_IMPORT_T2: Obis = Obis::new(1, 0, 1, 8, 2, 255);
pub const ACTIVE_ENERGY_EXPORT_TOTAL: Obis = Obis::new(1, 0, 2, 8, 0, 255);
pub const REACTIVE_ENERGY_IMPORT_TOTAL: Obis = Obis::new(1, 0, 3, 8, 0, 255);
pub const REACTIVE_ENERGY_EXPORT_TOTAL: Obis = Obis::new(1, 0, 4, 8, 0, 255);
pub const ACTIVE_POWER_IMPORT: Obis = Obis::new(1, 0, 1, 7, 0, 255);
pub const ACTIVE_POWER_EXPORT: Obis = Obis::new(1, 0, 2, 7, 0, 255);
pub const VOLTAGE_L1: Obis = Obis::new(1, 0, 32, 7, 0, 255);
pub const VOLTAGE_L2: Obis = Obis::new(1, 0, 52, 7, 0, 255);
pub const VOLTAGE_L3: Obis = Obis::new(1, 0, 72, 7, 0, 255);
pub const CURRENT_L1: Obis = Obis::new(1, 0, 31, 7, 0, 255);
pub const CURRENT_L2: Obis = Obis::new(1, 0, 51, 7, 0, 255);
pub const CURRENT_L3: Obis = Obis::new(1, 0, 71, 7, 0, 255);
pub const LOAD_PROFILE_1: Obis = Obis::new(1, 0, 99, 1, 0, 255);
pub const LOAD_PROFILE_2: Obis = Obis::new(1, 0, 99, 2, 0, 255);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::SliceWriter;
#[test]
fn textual_notation_round_trips() {
#[cfg(feature = "std")]
{
use std::string::ToString;
let o = Obis::new(1, 0, 1, 8, 0, 255);
assert_eq!(o.to_string(), "1-0:1.8.0*255");
assert_eq!(Obis::parse(&o.to_string()).unwrap(), o);
}
}
#[test]
fn the_three_accepted_shapes() {
assert_eq!(Obis::parse("1-0:1.8.0*255").unwrap(), Obis::new(1, 0, 1, 8, 0, 255));
assert_eq!(Obis::parse("1-0:1.8.0").unwrap(), Obis::new(1, 0, 1, 8, 0, 255));
assert_eq!(Obis::parse("1.8.0").unwrap(), Obis::new(1, 0, 1, 8, 0, 255));
}
#[test]
fn a_group_count_that_is_not_three_five_or_six_is_refused() {
assert!(Obis::parse("1.8").is_err());
assert!(Obis::parse("1-0:1.8").is_err());
assert!(Obis::parse("1-0:1.8.0*255*1").is_err());
}
#[test]
fn a_group_above_255_is_refused_rather_than_truncated() {
assert!(Obis::parse("1-0:1.8.256").is_err());
assert!(Obis::parse("1-0:1.8.99999").is_err());
}
#[test]
fn junk_is_refused() {
for s in ["", "-", "1-0:1.8.x", "1--0:1.8.0", "1-0:1.8.", "abc"] {
assert!(Obis::parse(s).is_err(), "{s:?} should not parse");
}
}
#[test]
fn wire_form_is_six_bytes_in_order() {
let o = Obis::new(1, 0, 1, 8, 0, 255);
let mut buf = [0u8; 6];
let mut w = SliceWriter::new(&mut buf);
o.encode(&mut w).unwrap();
assert_eq!(buf, [1, 0, 1, 8, 0, 255]);
assert_eq!(Obis::from_bytes(buf), o);
}
#[test]
fn manufacturer_ranges_are_recognised() {
assert!(Obis::new(1, 0, 128, 8, 0, 255).is_manufacturer_specific());
assert!(!Obis::new(1, 0, 1, 8, 0, 255).is_manufacturer_specific());
}
#[test]
fn media_classify() {
assert_eq!(Obis::new(0, 0, 1, 0, 0, 255).medium(), Medium::Abstract);
assert_eq!(Obis::new(7, 0, 3, 0, 0, 255).medium(), Medium::Gas);
assert_eq!(Obis::new(3, 0, 3, 0, 0, 255).medium(), Medium::Reserved(3));
}
}