smgw_lmn/simulator/
gateway.rs1use super::{
2 ENERGY, ID, MEASURED, PROFILE, Protocol, STATUS, sml, std,
3 tcp::{CLIENT_SAP, MAX_APDU, METER_SAP, READ_ONLY, SESSION_LIMIT, Timed, loopback},
4};
5use crate::dlms::{
6 BlockingReader, BudgetedTransport, ClockRange, Counts, Source,
7 protocol::{
8 client::{Client, ClientConfig, ClientError},
9 security::RustCryptoProvider,
10 transport::WrapperTransport,
11 },
12 read_profile,
13};
14use core::num::NonZeroUsize;
15use smgw_core::taf15::{self, Capture, Channel, Period, Profile};
16use std::{
17 io,
18 net::{SocketAddr, TcpStream},
19 time::Instant,
20};
21
22pub const SOURCE: Source<'static> = Source {
24 channel: Channel {
25 meter_id: ID,
26 obis: *ENERGY.as_bytes(),
27 unit: "Wh",
28 },
29 profile: PROFILE,
30 measured_time: Some(MEASURED),
31 status: Some(STATUS),
32};
33
34pub fn new_profile(first_utc: i64) -> Result<Profile<'static, 3>, taf15::Error> {
36 Profile::new(
37 "import-energy",
38 SOURCE.channel,
39 Period::QuarterHour,
40 first_utc,
41 )
42}
43
44pub fn read_meter(
47 address: SocketAddr,
48 protocol: Protocol,
49 destination: &mut Profile<'static, 3>,
50) -> io::Result<Counts> {
51 loopback(address)?;
52 if destination.channel() != SOURCE.channel
53 || destination.period() != smgw_core::taf15::Period::QuarterHour
54 {
55 return Err(io::Error::new(
56 io::ErrorKind::InvalidInput,
57 "expected the commissioned simulation channel and period",
58 ));
59 }
60 let deadline = Instant::now() + SESSION_LIMIT;
61 let socket = TcpStream::connect_timeout(&address, SESSION_LIMIT)?;
62 if protocol == Protocol::Sml {
63 return sml::gateway::read(&mut Timed::new(socket, deadline)?, destination);
64 }
65 let window = destination.window();
66 let range = ClockRange::from_unix(*window.start(), *window.end()).map_err(io::Error::other)?;
67 let mut received = Profile::<3>::new(
68 "import-energy",
69 SOURCE.channel,
70 destination.period(),
71 *window.start(),
72 )
73 .map_err(io::Error::other)?;
74 loopback(socket.peer_addr()?)?;
75 let mut link =
77 WrapperTransport::over(socket, CLIENT_SAP, METER_SAP)?.with_max_apdu(MAX_APDU as u16);
78 link.set_deadline(Some(deadline));
79 let budget = BudgetedTransport::new(link, NonZeroUsize::new(256).unwrap());
80 let config = ClientConfig {
81 max_pdu_size: 64,
82 conformance: READ_ONLY,
83 ..ClientConfig::default()
84 };
85 let mut client =
86 Client::<_, RustCryptoProvider>::connect(budget, config, RustCryptoProvider::default())
87 .map_err(io::Error::other)?
88 .with_response_capacity(MAX_APDU);
89 let mut scratch = [0; MAX_APDU];
90 let mut reader = BlockingReader::new(SOURCE.channel.meter_id, &mut client, &mut scratch);
91 read_profile(&mut reader, &SOURCE, range, &mut received).map_err(io::Error::other)?;
92 client.release().map_err(|error| match error {
94 ClientError::Io(error) => error,
95 other => io::Error::other(other),
96 })?;
97 destination.capture_batch(|batch| {
98 let mut counts = Counts {
99 inserted: 0,
100 duplicates: 0,
101 };
102 for record in received.records() {
103 match batch
104 .capture(SOURCE.channel, *record)
105 .map_err(io::Error::other)?
106 {
107 Capture::Inserted => counts.inserted += 1,
108 Capture::Duplicate => counts.duplicates += 1,
109 }
110 }
111 Ok(counts)
112 })
113}