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
use crate::db::Db;
use crate::reading::Message;
use crate::settings::Settings;
use clap::Arg;
use failure::Error;
use log::{debug, info};
use multiqueue::{broadcast_queue, BroadcastReceiver, BroadcastSender};
use std::sync::{Arc, Mutex};
pub mod consts;
pub mod db;
pub mod logging;
pub mod reading;
pub mod receiver;
pub mod services;
pub mod settings;
pub mod templates;
pub mod threading;
pub mod value;
pub mod web;
type Result<T> = std::result::Result<T, Error>;
const DEFAULT_SETTINGS_PATH: &str = "settings.yml";
const DEFAULT_DB_PATH: &str = "my-iot.sqlite3";
fn main() -> Result<()> {
logging::init();
let matches = clap::App::new("My IoT")
.version(clap::crate_version!())
.author(clap::crate_authors!("\n"))
.about(clap::crate_description!())
.arg(
Arg::with_name("settings")
.short("s")
.long("settings")
.takes_value(true)
.help(&format!("Settings file path (default: {})", DEFAULT_SETTINGS_PATH)),
)
.arg(
Arg::with_name("db")
.long("--db")
.takes_value(true)
.help(&format!("Database file path (default: {})", DEFAULT_DB_PATH)),
)
.get_matches();
info!("Reading settings…");
let settings = settings::read(matches.value_of("settings").unwrap_or(DEFAULT_SETTINGS_PATH))?;
debug!("Settings: {:?}", &settings);
info!("Opening database…");
let db = Arc::new(Mutex::new(Db::new(matches.value_of("db").unwrap_or(DEFAULT_DB_PATH))?));
info!("Starting services…");
let (tx, rx) = broadcast_queue(1024);
spawn_services(&settings, &db, &tx, &rx)?;
info!("Starting readings receiver…");
receiver::start(&rx, db.clone())?;
drop(tx);
rx.unsubscribe();
info!("Starting web server on port {}…", settings.http_port);
web::start_server(settings, db.clone())
}
fn spawn_services(
settings: &Settings,
db: &Arc<Mutex<Db>>,
tx: &BroadcastSender<Message>,
rx: &BroadcastReceiver<Message>,
) -> Result<()> {
for (service_id, settings) in settings.services.iter() {
info!("Spawning service `{}`…", service_id);
debug!("Settings `{}`: {:?}", service_id, settings);
services::new(service_id, settings)?.spawn(db.clone(), &tx, &rx)?;
}
Ok(())
}