-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
251 lines (218 loc) · 7.32 KB
/
main.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
use chrono::prelude::*;
use log::{error, info, warn};
use std::sync::{Arc, Mutex};
use bytes::Bytes;
use http_body_util::Full;
use hyper::{body::Incoming as IncomingBody};
use hyper::server::conn::http1;
use hyper::service::{Service as HyperService};
use hyper::{Request, Response};
use std::future::Future;
use std::pin::Pin;
mod combustion;
use combustion::CombustionFinder;
mod push;
use push::Pusher;
fn as_farenheit(c: f32) -> f32 {
c * 1.8 + 32.0
}
#[derive(Clone, Copy, Debug)]
enum SvcStatus {
DISCOVERING,
CONNECTING,
CONNECTED,
RUNNING,
}
impl std::fmt::Display for SvcStatus {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let value = match self {
SvcStatus::DISCOVERING => "discovering".to_string(),
SvcStatus::CONNECTING => "connecting".to_string(),
SvcStatus::CONNECTED => "connected".to_string(),
SvcStatus::RUNNING => "running".to_string(),
};
write!(f, "{}", value)
}
}
#[derive(Clone, Copy, Debug)]
enum S3Status {
UNINIT,
WRITING,
ERROR,
}
#[derive(Debug)]
struct SvcInner {
raw_temp: f32,
status: SvcStatus,
s3_status: S3Status,
}
impl std::fmt::Display for S3Status {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let value = match self {
S3Status::UNINIT => "uninitialized".to_string(),
S3Status::WRITING => "writing".to_string(),
S3Status::ERROR => "error".to_string(),
};
write!(f, "{}", value)
}
}
#[derive(Debug, Clone)]
struct Svc {
inner: Arc<Mutex<SvcInner>>,
}
impl Svc {
pub fn new() -> Svc {
Svc{
inner: Arc::new(Mutex::new(SvcInner{
raw_temp: 0.0,
status: SvcStatus::DISCOVERING,
s3_status: S3Status::UNINIT,
})),
}
}
pub fn get_status(&self) -> SvcStatus {
self.inner.lock().unwrap().status
}
pub fn get_raw_temp(&self) -> f32 {
self.inner.lock().unwrap().raw_temp
}
pub fn get_s3_status(&self) -> S3Status {
self.inner.lock().unwrap().s3_status
}
pub fn set_status(&self, status: SvcStatus) {
self.inner.lock().unwrap().status = status;
}
pub fn set_raw_temp(&self, raw_temp: f32) {
self.inner.lock().unwrap().raw_temp = raw_temp;
}
pub fn set_s3_status(&self, status: S3Status) {
self.inner.lock().unwrap().s3_status = status;
}
}
impl HyperService<Request<IncomingBody>> for Svc {
type Response = Response<Full<Bytes>>;
type Error = hyper::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn call(&self, req: Request<IncomingBody>) -> Self::Future {
fn mk_response(s: String) -> Result<Response<Full<Bytes>>, hyper::Error> {
Ok(Response::builder().body(Full::new(Bytes::from(s))).unwrap())
}
let res = match req.uri().path() {
"/" => {
mk_response(format!("{{ \"temp\": {}, \"status\": \"{}\", \"s3\": \"{}\" }}", self.get_raw_temp(), self.get_status(), self.get_s3_status()))
},
_ => return Box::pin(async { mk_response("Whoopsie".into()) }),
};
Box::pin(async { res })
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
env_logger::init();
let flags = xflags::parse_or_exit! {
/// Bucket to upload data into
optional bucket: String
};
info!("Using bucket {:?}", flags.bucket);
// Listen for Ctrl-C
let (done_tx, mut done) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
tokio::signal::ctrl_c().await.expect("ctrlc");
done_tx.send(true).expect("Send ctrlc");
});
let svc = Svc::new();
// Start an HTTP server to serve requests for current temp data
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3000).into();
let listener: tokio::net::TcpListener = tokio::net::TcpListener::bind(addr).await.expect("Tcp listener");
tokio::spawn({
let svc = svc.clone();
async move {
loop {
let (tcp, _) = listener.accept().await.expect("Accept");
let io = hyper_util::rt::tokio::TokioIo::new(tcp);
let svc_clone = svc.clone();
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new().serve_connection(io, svc_clone).await
{
error!("Error serving connection: {:?}", err);
}
});
}
}
});
info!("Discovering devices");
let finder = CombustionFinder::new().await?;
let mut combustion = match finder.discover(&mut done).await {
Ok(d) => d,
Err(e) => {
error!("Could not find combustion device: {:?}", e);
return Err(e);
}
};
svc.set_status(SvcStatus::CONNECTING);
info!("Connecting to device");
if let Err(e) = combustion.connect().await {
error!("Could not connect to device: {:?}", e);
return Err(e);
}
svc.set_status(SvcStatus::CONNECTED);
// Create the Pusher
let (tx, mut rx) = tokio::sync::mpsc::channel(100);
tokio::spawn({
let svc = svc.clone();
async move {
let mut pusher = Pusher::new();
if let Some(bucket) = flags.bucket {
let prefix = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true);
info!("Starting S3 pusher to {}/{}", bucket, prefix);
pusher.init(bucket, prefix).await;
}
let mut i = 0;
while let Some(t) = rx.recv().await {
// Do something
if let Err(e) = pusher.push(t).await {
error!("Failed to push t={} i={}: {}", t, i, e);
svc.set_s3_status(S3Status::ERROR);
} else {
svc.set_s3_status(S3Status::WRITING);
}
i += 1;
}
}
});
// Poll the thermometer and push the temps into the S3 Pusher
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(5000));
let mut i = 0;
loop {
tokio::select! {
_ = interval.tick() => {
match combustion.get_raw_temp().await? {
Some(raw_temp_c) => {
info!("Raw temp deg C={} degF={}", raw_temp_c, as_farenheit(raw_temp_c));
svc.set_status(SvcStatus::RUNNING);
svc.set_raw_temp(as_farenheit(raw_temp_c));
if let Err(e) = tx.send(raw_temp_c).await {
error!("Failed to send raw temp={} entry={} to pusher: {}", raw_temp_c, i, e);
}
i += 1;
},
None => {
warn!("Couldn't fetch temp");
}
}
}
_d = &mut done => {
info!("Done!");
break;
}
}
}
if let Err(e) = combustion.disconnect().await {
error!("Failed to disconnect device: {:?}", e);
}
info!("Done");
Ok(())
}