fix double double client bug
This commit is contained in:
parent
8ae77bbeb1
commit
bffcf75eee
@ -1,5 +1,6 @@
|
|||||||
mqtt:
|
mqtt:
|
||||||
host: "localhost"
|
host: "localhost"
|
||||||
port: 1883
|
port: 1883
|
||||||
|
client: "mqttAutomation01"
|
||||||
user: "mqttAutomation"
|
user: "mqttAutomation"
|
||||||
pass: "password"
|
pass: "password"
|
||||||
|
|||||||
35
src/main.rs
35
src/main.rs
@ -125,6 +125,7 @@ fn mqtt_automation(publish: &Sender<MqttMessage>, message: MqttMessage) {
|
|||||||
struct SettingsMQTT {
|
struct SettingsMQTT {
|
||||||
host: String,
|
host: String,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
client: String,
|
||||||
user: String,
|
user: String,
|
||||||
pass: String
|
pass: String
|
||||||
}
|
}
|
||||||
@ -184,18 +185,27 @@ fn main() {
|
|||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
conf = Settings {
|
conf = Settings {
|
||||||
mqtt: SettingsMQTT {host:String::new(),port:0,user:String::new(),pass:String::new()}
|
mqtt: SettingsMQTT {host:String::new(),port:0,client:String::new(),user:String::new(),pass:String::new()}
|
||||||
};
|
};
|
||||||
conf_ok = false;
|
conf_ok = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf_ok {
|
if conf_ok {
|
||||||
let mut mqttoptions = MqttOptions::new("rumqtt-sync", conf.mqtt.host, conf.mqtt.port);
|
let mut mqttoptions = MqttOptions::new(conf.mqtt.client, conf.mqtt.host, conf.mqtt.port);
|
||||||
mqttoptions.set_keep_alive(Duration::from_secs(5));
|
mqttoptions.set_keep_alive(Duration::from_secs(5));
|
||||||
mqttoptions.set_credentials(conf.mqtt.user, conf.mqtt.pass);
|
mqttoptions.set_credentials(conf.mqtt.user, conf.mqtt.pass);
|
||||||
let (client, mut connection) = Client::new(mqttoptions, 10);
|
let (client, mut connection) = Client::new(mqttoptions, 10);
|
||||||
|
|
||||||
|
match client.subscribe("clock/hour", QoS::AtMostOnce) {
|
||||||
|
Err(e) => println!("ERROR: main: faild to subscribe to clock/hour ({})", e),
|
||||||
|
Ok(_) => {}
|
||||||
|
}
|
||||||
|
match client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) {
|
||||||
|
Err(e) => println!("ERROR: main: faild to subscribe to .../KNMITemp/values ({})", e),
|
||||||
|
Ok(_) => {}
|
||||||
|
}
|
||||||
|
|
||||||
// thread publisher
|
// thread publisher
|
||||||
let publisher = thread::Builder::new()
|
let publisher = thread::Builder::new()
|
||||||
.name("publisher".to_string())
|
.name("publisher".to_string())
|
||||||
@ -222,7 +232,6 @@ fn main() {
|
|||||||
for (_i, notification) in connection.iter().enumerate() {
|
for (_i, notification) in connection.iter().enumerate() {
|
||||||
let mut delay: bool = false;
|
let mut delay: bool = false;
|
||||||
match notification {
|
match notification {
|
||||||
// Err(e) => println!("ERROR: mqtt: {}", e),
|
|
||||||
Err(e) => match e {
|
Err(e) => match e {
|
||||||
rumqttc::ConnectionError::MqttState(state) => match state {
|
rumqttc::ConnectionError::MqttState(state) => match state {
|
||||||
rumqttc::StateError::Io(e) => {
|
rumqttc::StateError::Io(e) => {
|
||||||
@ -270,17 +279,19 @@ fn main() {
|
|||||||
Packet::Connect(_) => println!("INFO : mqtt: connected"),
|
Packet::Connect(_) => println!("INFO : mqtt: connected"),
|
||||||
Packet::ConnAck(_) => println!("INFO : mqtt: connaction acknolaged"),
|
Packet::ConnAck(_) => println!("INFO : mqtt: connaction acknolaged"),
|
||||||
Packet::Publish(msg) => {
|
Packet::Publish(msg) => {
|
||||||
let payload = match String::from_utf8(msg.payload.to_vec()) {
|
let mut payload: String = String::from("");
|
||||||
Err(e) => panic!("ERROR: pqtt_recive: faild to decode payload ({})", e),
|
match String::from_utf8(msg.payload.to_vec()) {
|
||||||
Ok(v) => v
|
Err(e) => println!("ERROR: pqtt_recive: faild to decode payload ({})", e),
|
||||||
|
Ok(v) => payload = v
|
||||||
};
|
};
|
||||||
|
if payload.len() > 0 {
|
||||||
|
let message: MqttMessage = { MqttMessage {
|
||||||
|
topic: msg.topic,
|
||||||
|
payload: payload
|
||||||
|
}};
|
||||||
|
|
||||||
let message: MqttMessage = { MqttMessage {
|
mqtt_automation(&mqtt_publish, message);
|
||||||
topic: msg.topic,
|
}
|
||||||
payload: payload
|
|
||||||
}};
|
|
||||||
|
|
||||||
mqtt_automation(&mqtt_publish, message);
|
|
||||||
},
|
},
|
||||||
Packet::PubAck(_) => {}, //println!("INFO : mqtt_recive: in PubAck"),
|
Packet::PubAck(_) => {}, //println!("INFO : mqtt_recive: in PubAck"),
|
||||||
Packet::PubRec(_) => {}, //println!("INFO : mqtt_recive: in PubRec"),
|
Packet::PubRec(_) => {}, //println!("INFO : mqtt_recive: in PubRec"),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user