-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
146 lines (131 loc) · 4.28 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
mod eink;
mod eink_calls;
use crate::eink::Color::White;
use crate::eink_calls::Call;
use std::path;
use std::process::exit;
use std::fs::File;
use std::io::{Read, Write};
use rand;
use rand::Rng;
use rumqtt::{MqttOptions, MqttClient};
use rumqtt::QoS::AtMostOnce;
use rumqtt::client::Notification::Publish;
use serde::Deserialize;
use toml;
#[derive(Deserialize, Debug)]
struct Config {
host: String,
port: u16,
id: String,
username: Option<String>,
password: Option<String>,
channels: Channels,
}
#[derive(Deserialize, Debug)]
struct Channels {
calls: String,
error: String,
}
fn read_string_or_create_empty(path: &str) -> Result<String, &str> {
if path::Path::new(path).exists() {
match File::open(path) {
Ok(mut file) => {
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
return Ok(content);
},
Err(_) => Err("Failed to load file!")
}
}else {
let default_config = format!(
"host = \"localhost\"\n\
port = 1883\n\
id = \"rpi-epaper-mqtt-receiver-{}\"\n\
#username = \"user\"\n\
#password = \"pass\"\n\
\n\
[channels]\n\
calls = \"cmnd/rpi-epaper-mqtt-receiver/calls\"\n\
error = \"stat/rpi-epaper-mqtt-receiver/error\"\n",
rand::thread_rng().gen_range(0, 10000)
);
match File::create(path) {
Ok(mut file) => {
write!(file, "{}", default_config);
file.flush().unwrap();
},
Err(e) => panic!(e)
}
return Ok(String::from(default_config)); // Empty new file
}
}
fn read_config_or_panic(path: &str) -> Config {
match read_string_or_create_empty(path.clone()) {
Ok(content) => {
match toml::from_str::<Config>(content.as_str()) {
Ok(config) => config,
Err(err) => {
println!("Error: {}", err);
exit(1);
}
}
},
Err(reason) => {
println!("Error: {}", reason);
exit(1);
}
}
}
fn print_config(config: &mut Config) {
println!("Address: {}:{}", config.host, config.port);
println!("Id: {}", config.id);
if let Some(user) = &config.username {
println!("Username: {}", user);
}else {
println!("Username: not set");
}
if let Some(_) = config.password {
println!("Password: set");
}else {
println!("Password: not set");
}
println!("Calls: {}", config.channels.calls);
println!("Error: {}", config.channels.error);
}
fn main() {
println!("Loading toml...");
let mut config = read_config_or_panic("config.toml");
print_config(&mut config);
let mqtt_options = MqttOptions::new(&config.id, &config.host, config.port);
let (mut mqtt_client, notifications) = MqttClient::start(mqtt_options).unwrap();
println!("Initializing display...");
let mut eink = eink::EInk::new(White);
eink.delay(500);
println!("Start listening on MQTT...");
mqtt_client.subscribe(config.channels.calls, AtMostOnce);
for n in notifications {
if let Publish(message) = n {
// Decode mqtt payload
if let Ok(plain_text) = String::from_utf8(message.payload.to_vec()) {
// Parse and serialize
match serde_json::from_str::<Vec<Call>>(&plain_text) {
Ok(calls) => {
for call in calls {
// Execute call and check for error
if let Err(reason) = call.call(&mut eink) {
mqtt_client.publish(&config.channels.error, AtMostOnce, false,
reason.as_str());
}
}
},
Err(err) => {
// Handle parsing/serialization error
mqtt_client.publish(&config.channels.error, AtMostOnce, false,
format!("Couldn't parse or serialize json: {}", err));
}
}
}
}
}
}