private async void btTest_ClickAsync(object sender, EventArgs e) { btTest.Enabled = false; btnSave.Enabled = false; btnCancel.Enabled = false; try { AppSettings.Settings.mqtt_serverandport = tb_ServerPort.Text.Trim(); AppSettings.Settings.mqtt_password = tb_Password.Text.Trim(); AppSettings.Settings.mqtt_username = tb_Username.Text.Trim(); AppSettings.Settings.mqtt_UseTLS = cb_UseTLS.Checked; using (Global_GUI.CursorWait cw = new Global_GUI.CursorWait()) { Global.Log("------ TESTING MQTT --------"); string topic = AITOOL.ReplaceParams(this.cam, null, tb_Topic.Text.Trim()); string payload = AITOOL.ReplaceParams(this.cam, null, tb_Payload.Text.Trim()); List <string> topics = Global.Split(topic, ";|"); List <string> payloads = Global.Split(payload, ";|"); MQTTClient mq = new MQTTClient(); MqttClientPublishResult pr = null; for (int i = 0; i < topics.Count; i++) { pr = await mq.PublishAsync(topics[i], payloads[i], cam.Action_mqtt_retain_message); } Global.Log("------ DONE TESTING MQTT --------"); if (pr != null && (pr.ReasonCode == MqttClientPublishReasonCode.Success)) { MessageBox.Show("Success! See Log for details."); } else if (pr != null) { MessageBox.Show($"Failed. See log for details. Reason={pr.ReasonCode}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show($"Failed. See log for details.", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch { } finally { btTest.Enabled = true; btnSave.Enabled = true; btnCancel.Enabled = true; } }
private async void btTest_ClickAsync(object sender, EventArgs e) { AppSettings.Settings.mqtt_serverandport = tb_ServerPort.Text.Trim(); AppSettings.Settings.mqtt_password = tb_Password.Text.Trim(); AppSettings.Settings.mqtt_username = tb_Username.Text.Trim(); AppSettings.Settings.mqtt_UseTLS = cb_UseTLS.Checked; MQTTClient mq = new MQTTClient(); MqttClientPublishResult pr = await mq.PublishAsync(tb_Topic.Text.Trim(), tb_Payload.Text.Trim()); if (pr != null && (pr.ReasonCode == MqttClientPublishReasonCode.Success)) { MessageBox.Show("Success."); } else if (pr != null) { MessageBox.Show($"Failed. See log. Reason={pr.ReasonCode}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show($"Failed. See log.", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private async void btTest_ClickAsync(object sender, EventArgs e) { this.btTest.Enabled = false; this.btnSave.Enabled = false; this.btnCancel.Enabled = false; try { AppSettings.Settings.mqtt_serverandport = this.tb_ServerPort.Text.Trim(); AppSettings.Settings.mqtt_password = this.tb_Password.Text.Trim(); AppSettings.Settings.mqtt_username = this.tb_Username.Text.Trim(); AppSettings.Settings.mqtt_UseTLS = this.cb_UseTLS.Checked; using (Global_GUI.CursorWait cw = new Global_GUI.CursorWait()) { Log("------ TESTING MQTT --------"); string topic = AITOOL.ReplaceParams(this.cam, null, null, this.tb_Topic.Text.Trim()); string payload = AITOOL.ReplaceParams(this.cam, null, null, this.tb_Payload.Text.Trim()); List <string> topics = Global.Split(topic, ";|"); List <string> payloads = Global.Split(payload, ";|"); MQTTClient mq = new MQTTClient(); MqttClientPublishResult pr = null; ClsImageQueueItem CurImg = null; for (int i = 0; i < topics.Count; i++) { if (this.cam.Action_mqtt_send_image) { if (topics[i].IndexOf("/image", StringComparison.OrdinalIgnoreCase) >= 0) { if (!string.IsNullOrEmpty(this.cam.last_image_file_with_detections) && File.Exists(this.cam.last_image_file_with_detections)) { CurImg = new ClsImageQueueItem(this.cam.last_image_file_with_detections, 0); } else if (!string.IsNullOrEmpty(this.cam.last_image_file) && File.Exists(this.cam.last_image_file)) { CurImg = new ClsImageQueueItem(this.cam.last_image_file, 0); } else { CurImg = null; } } else { CurImg = null; } } pr = await mq.PublishAsync(topics[i], payloads[i], this.cam.Action_mqtt_retain_message, CurImg); } Log("------ DONE TESTING MQTT --------"); if (pr != null && (pr.ReasonCode == MqttClientPublishReasonCode.Success)) { MessageBox.Show("Success! See Log for details."); } else if (pr != null) { MessageBox.Show($"Failed. See log for details. Reason={pr.ReasonCode}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show($"Failed. See log for details.", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch { } finally { this.btTest.Enabled = true; this.btnSave.Enabled = true; this.btnCancel.Enabled = true; } }