make it more readable
This commit is contained in:
parent
3e01c8c47f
commit
672a46aed0
87
src/main.rs
87
src/main.rs
@ -112,20 +112,56 @@ impl Automation {
|
||||
qos: mqtt_client::QoS::AtMostOnce,
|
||||
}});
|
||||
}
|
||||
|
||||
fn message_in(&mut self, message: mqtt_client::MqttMessage) {
|
||||
// println!("INFO : mqtt_automation: {}: {}", message.topic, message.payload);
|
||||
if message.topic.eq("clock/time/hour") {
|
||||
|
||||
if message.payload.eq("7") && (self.clock_dow < 5) {
|
||||
self.lamp01_set(true);
|
||||
}
|
||||
|
||||
} else if message.topic.eq("/cool/devices/KNMITemp/values") {
|
||||
|
||||
let payload = json_parser::Json::Text(message.payload);
|
||||
match json_parser::get_u32(payload, Vec::from([String::from("gr")])) {
|
||||
Ok(gr) => {
|
||||
if gr > 30 {
|
||||
self.lamp01_set(false);
|
||||
}
|
||||
},
|
||||
Err(e) =>
|
||||
print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string())
|
||||
}
|
||||
|
||||
}
|
||||
else if message.topic.eq("clock/date/dow") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) =>
|
||||
println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e),
|
||||
Ok(n) => self.clock_dow = n
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
impl mqtt_client::MqttTool for Automation {
|
||||
fn new(client: rumqttc::Client, tx: Sender<mqtt_client::MqttMessage>) -> Automation {
|
||||
|
||||
match client.subscribe("clock/time/hour", rumqttc::QoS::AtMostOnce) {
|
||||
Err(e) => println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e),
|
||||
match client.subscribe("clock/time/hour", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("clock/date/dow", rumqttc::QoS::AtMostOnce) {
|
||||
Err(e) => println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e),
|
||||
match client.subscribe("clock/date/dow", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("/cool/devices/KNMITemp/values", rumqttc::QoS::AtMostOnce) {
|
||||
Err(e) => println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e),
|
||||
match client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
|
||||
@ -144,33 +180,7 @@ impl mqtt_client::MqttTool for Automation {
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
},
|
||||
Ok(message) => {
|
||||
println!("INFO : mqtt_automation: {}: {}", message.topic, message.payload);
|
||||
if message.topic.eq("clock/time/hour") {
|
||||
|
||||
if message.payload.eq("7") && (self.clock_dow < 5) {
|
||||
self.lamp01_set(true);
|
||||
}
|
||||
|
||||
} else if message.topic.eq("/cool/devices/KNMITemp/values") {
|
||||
|
||||
match json_parser::get_u32(json_parser::Json::Text(message.payload), Vec::from([String::from("gr")])) {
|
||||
Ok(gr) => {
|
||||
if gr > 30 {
|
||||
self.lamp01_set(false);
|
||||
}
|
||||
},
|
||||
Err(e) => print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string())
|
||||
}
|
||||
|
||||
}
|
||||
else if message.topic.eq("clock/date/dow") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) => println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e),
|
||||
Ok(n) => self.clock_dow = n
|
||||
}
|
||||
|
||||
}
|
||||
self.message_in(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -206,7 +216,8 @@ fn read_config() -> Result<Settings,SettingsError> {
|
||||
Err(_) => {
|
||||
println!("INFO : read_config: could not find '~/.config/mqttAutomation.yml'. try '/etc/mqttAutomation.yml");
|
||||
match fs::read_to_string("/etc/mqttAutomation.yml") {
|
||||
Err(_) => println!("ERROR: read_config: could not find any config file"),
|
||||
Err(_) =>
|
||||
println!("ERROR: read_config: could not find any config file"),
|
||||
Ok(str) => config_str = str
|
||||
}
|
||||
},
|
||||
@ -234,7 +245,13 @@ fn main() {
|
||||
// get setting
|
||||
match read_config() {
|
||||
Ok(conf) =>
|
||||
mqtt_client::run::<Automation>(conf.mqtt.host, conf.mqtt.port, conf.mqtt.client, conf.mqtt.user, conf.mqtt.pass),
|
||||
mqtt_client::run::<Automation>(
|
||||
conf.mqtt.host,
|
||||
conf.mqtt.port,
|
||||
conf.mqtt.client,
|
||||
conf.mqtt.user,
|
||||
conf.mqtt.pass
|
||||
),
|
||||
Err(_) => {}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user