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
use chrono::*;
use frame::*;
use std::io;
#[derive(Debug)]
pub struct HcInfos {
pub date: DateTime<Local>,
pub periode: String,
pub hc: i32,
pub hp: i32,
pub iinst: i32,
pub papp: i32,
pub alerte: bool
}
struct HcInfosBuilder {
date: Option<DateTime<Local>>,
periode: Option<String>,
hc: Option<i32>,
hp: Option<i32>,
iinst: Option<i32>,
papp: Option<i32>,
alerte: bool
}
macro_rules! get {
($e:expr, $msg:expr) => (match $e { Some(e) => e, None => return Err(TeleinfoError::FrameError($msg.to_string())) })
}
impl HcInfos {
pub fn read<T: io::Read>(mut input: &mut T) -> Result<HcInfos, TeleinfoError> {
let frame = Frame::next_frame(&mut input)?;
return HcInfos::from(frame);
}
fn from(frame: Frame) -> Result<HcInfos, TeleinfoError> {
let mut builder = HcInfosBuilder::new();
let now: DateTime<Local> = Local::now();
builder.date(now);
for tag in frame.tags {
match tag {
Tag::PTEC(p) => {
builder.periode(match p {
PeriodeTarifaire::HP => "HP",
PeriodeTarifaire::HC => "HC",
_ => panic!("PeriodeTarifaire does not match HC")
});
},
Tag::HCHC(v) => {
builder.hc(v);
},
Tag::HCHP(v) => {
builder.hp(v);
},
Tag::IINST(v) => {
builder.iinst(v);
},
Tag::PAPP(v) => {
builder.papp(v);
},
Tag::ADPS(_) => {
builder.alerte(true);
},
_ => ()
};
}
builder.build()
}
}
impl HcInfosBuilder {
fn new() -> HcInfosBuilder {
HcInfosBuilder {
date: None,
periode: None,
hc: None,
hp: None,
iinst: None,
papp: None,
alerte: false
}
}
fn date(&mut self, date: DateTime<Local>) -> &mut HcInfosBuilder {
self.date = Some(date);
self
}
fn periode(&mut self, periode: &str) -> &mut HcInfosBuilder {
self.periode = Some(periode.to_string());
self
}
fn hc(&mut self, hc: i32) -> &mut HcInfosBuilder {
self.hc = Some(hc);
self
}
fn hp(&mut self, hp: i32) -> &mut HcInfosBuilder {
self.hp = Some(hp);
self
}
fn iinst(&mut self, iinst: i32) -> &mut HcInfosBuilder {
self.iinst = Some(iinst);
self
}
fn papp(&mut self, papp: i32) -> &mut HcInfosBuilder {
self.papp = Some(papp);
self
}
fn alerte(&mut self, alerte: bool) -> &mut HcInfosBuilder {
self.alerte = alerte;
self
}
fn build(self) -> Result<HcInfos, TeleinfoError> {
let infos = HcInfos {
date: get!(self.date, "Missing date"),
periode: get!(self.periode, "Missing periode"),
hc: get!(self.hc, "Missing hc"),
hp: get!(self.hp, "Missing hp"),
iinst: get!(self.iinst, "Missing iinst"),
papp: get!(self.papp, "Missing papp"),
alerte: self.alerte
};
Ok(infos)
}
}