Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix socket disconnect error reported by mosquitto #14

Merged
merged 5 commits into from
Oct 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions led_status_helper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct Config {
mqtt_port: Option<u16>,
mqtt_topic: String,
temp_unit: Option<char>,
wait_secs: Option<u64>,
}

fn generate_mq_client_id() -> String {
Expand Down Expand Up @@ -141,13 +142,7 @@ fn main() {
};
let redis_conn = redis_client.get_connection().unwrap();

let status = generate_status(
&redis_conn,
&config.temp_unit.unwrap_or('F'),
&config.redis_namespace.unwrap_or("".to_string()),
);

let mut mq_request_handler = {
let mut mq_cli = {
// Specify client connection options
let opts: MqttOptions = MqttOptions::new()
.set_keep_alive(5)
Expand All @@ -163,15 +158,21 @@ fn main() {
MqttClient::start(opts, None).expect("MQTT client couldn't start")
};

mq_request_handler
.publish(
&config.mqtt_topic,
QoS::Level0,
status.unwrap().clone().into_bytes(),
)
.unwrap();

// let the other thread do its thing before we exit
std::thread::sleep(std::time::Duration::from_millis(1000));
let _ = mq_request_handler.disconnect();
let wait_secs = config.wait_secs.unwrap_or(10);

loop {
let status = generate_status(
&redis_conn,
&config.temp_unit.unwrap_or('F'),
&config.redis_namespace.clone().unwrap_or("".to_string()),
);
mq_cli
.publish(
&config.mqtt_topic,
QoS::Level0,
status.unwrap().clone().into_bytes(),
)
.unwrap();
std::thread::sleep(std::time::Duration::from_secs(wait_secs));
}
}