private void Frm_ObjectDetail_Load(object sender, EventArgs e) { Global_GUI.RestoreWindowState(this); this.Show(); try { Global_GUI.ConfigureFOLV(this.folv_ObjectDetail, typeof(ClsPrediction), null, null); Global_GUI.UpdateFOLV(this.folv_ObjectDetail, this.PredictionObjectDetails); if (!String.IsNullOrEmpty(this.ImageFileName) && this.ImageFileName.Contains("\\") && File.Exists(this.ImageFileName)) { OriginalBMP = new ClsImageQueueItem(this.ImageFileName, 0); this.pictureBox1.Image = Image.FromStream(OriginalBMP.ToStream()); //load actual image as background, so that an overlay can be added as the image } } catch (Exception) { throw; } }
public async Task <MqttClientPublishResult> PublishAsync(string topic, string payload, bool retain, ClsImageQueueItem CurImg) { using var Trace = new Trace(); //This c# 8.0 using feature will auto dispose when the function is done. MqttClientPublishResult res = null; Stopwatch sw = Stopwatch.StartNew(); this.LastRetain = retain; this.LastPayload = payload; this.LastTopic = topic; if (string.IsNullOrWhiteSpace(topic)) { this.LastTopic = Guid.NewGuid().ToString(); } if (!this.IsConnected) { await this.Connect(); } if (!this.IsConnected) { return(res); } await Task.Run(async() => { try { Log($"Debug: MQTT: Sending topic '{this.LastTopic}' with payload '{this.LastPayload}' to server '{this.server}:{this.portint}'..."); if (cres != null && mqttClient.IsConnected && cres.ResultCode == MqttClientConnectResultCode.Success) { IsConnected = true; MqttApplicationMessage ma; if (CurImg != null) { //using FileStream image_data = System.IO.File.OpenRead(CurImg.image_path); ma = new MqttApplicationMessageBuilder() .WithTopic(this.LastTopic) .WithPayload(CurImg.ToStream()) .WithAtLeastOnceQoS() .WithRetainFlag(this.LastRetain) .Build(); res = await mqttClient.PublishAsync(ma, CancellationToken.None); if (res.ReasonCode == MqttClientPublishReasonCode.Success) { Log($"Debug: MQTT: ...Sent image in {sw.ElapsedMilliseconds}ms, Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } else { Log($"Error: MQTT: sending image: ({sw.ElapsedMilliseconds}ms) Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } } else { ma = new MqttApplicationMessageBuilder() .WithTopic(this.LastTopic) .WithPayload(this.LastPayload) .WithAtLeastOnceQoS() .WithRetainFlag(this.LastRetain) .Build(); res = await mqttClient.PublishAsync(ma, CancellationToken.None); //Success = 0, // NoMatchingSubscribers = 0x10, // UnspecifiedError = 0x80, // ImplementationSpecificError = 0x83, // NotAuthorized = 0x87, // TopicNameInvalid = 0x90, // PacketIdentifierInUse = 0x91, // QuotaExceeded = 0x97, // PayloadFormatInvalid = 0x99 if (res.ReasonCode == MqttClientPublishReasonCode.Success) { Log($"Debug: MQTT: ...Sent in {sw.ElapsedMilliseconds}ms, Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } else { Log($"Error: MQTT: sending: ({sw.ElapsedMilliseconds}ms) Reason: '{res.ReasonCode}' ({Convert.ToInt32(res.ReasonCode)} - '{res.ReasonString}')"); } } } else if (cres != null) { IsConnected = false; Log($"Error: MQTT: connecting: ({sw.ElapsedMilliseconds}ms) Result: '{cres.ResultCode}' - '{cres.ReasonString}'"); } else { IsConnected = false; Log($"Error: MQTT: Error connecting: ({sw.ElapsedMilliseconds}ms) cres=null"); } } catch (Exception ex) { Log($"Error: MQTT: Unexpected Problem: Topic '{this.LastTopic}' Payload '{this.LastPayload}': " + ex.Msg()); } finally { } }); return(res); }