Skip to content

Commit 55035a7

Browse files
authored
Merge pull request #6 from CorefluxCommunity/worker_cleanUp
+ ExecuteAsync deep refactor
2 parents f17f298 + 10ba927 commit 55035a7

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

ClientSampleBackgroundService/Worker.cs

+37-21
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,30 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6767
{
6868
if (isConnected)
6969
{
70-
//Get the last value of the topic received
71-
var t = mqttInstance.GetDataAsync("CF/GetTest").GetAwaiter();
72-
_logger.LogInformation("Was able to subscribe CF/GetTest with {string} ", t.GetResult());
73-
Task.Delay(100).Wait();
74-
//Set the new value of topic CF/Test
75-
var t1 = mqttInstance.SetDataAsync("CF/Test", "test", qoslevel: 1);
76-
t1.Wait(100);
77-
var result = t1.GetAwaiter().GetResult();
78-
if (result != null)
79-
{
80-
if (result.ReasonFeedback == MQTTPublishFeedback.FeedbackType.PublishSucess)
81-
{
82-
_logger.LogInformation(string.Format("Message publish to CF/Test with success @ {0} ", DateTimeOffset.Now));
83-
}
84-
else
85-
{
86-
_logger.LogInformation(string.Format("Failed to publish message to CF/Test @ {0} ", DateTimeOffset.Now));
87-
}
88-
}
70+
// Send data.
71+
MQTTPublishFeedback data = await PublishData();
72+
73+
// Verify Publishing Information.
74+
VerifyPublishingFeedBack(data);
8975
}
9076
else
9177
{
92-
_logger.LogInformation(string.Format("Failed to publish message to CF/Test. Isn't connected. @ {0} ", DateTimeOffset.Now));
78+
_logger.LogInformation(string.Format("Failed to publish message to CF/Test. Not connected. @ {0} ", DateTimeOffset.Now));
9379
}
9480
}
9581
}
82+
private async Task<MQTTPublishFeedback> PublishData()
83+
{
84+
try
85+
{
86+
return await mqttInstance.SetDataAsync("CF/Test", "test", qoslevel: 1);
87+
}
88+
catch (Exception ex)
89+
{
90+
_logger.LogInformation(string.Format("Failed to publish. Time {0}. Exception {1}", DateTimeOffset.Now, ex.ToString()));
91+
}
92+
return null; // Returns null because something happend :)!
93+
}
9694

9795
private async Task ConnectToBroker()
9896
{
@@ -103,7 +101,25 @@ private async Task ConnectToBroker()
103101
}
104102
catch (Exception ex)
105103
{
106-
_logger.LogInformation(string.Format("Failed to find the broker {0}. Exception {1}", DateTimeOffset.Now, ex.ToString()));
104+
_logger.LogInformation(string.Format("Connection failed. Time {0}. Exception {1}", DateTimeOffset.Now, ex.ToString()));
105+
}
106+
}
107+
private void VerifyPublishingFeedBack(MQTTPublishFeedback data)
108+
{
109+
if (data != null)
110+
{
111+
if (data.ReasonFeedback == MQTTPublishFeedback.FeedbackType.PublishSucess)
112+
{
113+
_logger.LogInformation(string.Format("Message published to CF/Test with success @ {0} ", DateTimeOffset.Now));
114+
}
115+
else
116+
{
117+
_logger.LogInformation(string.Format("Failed to publish message to CF/Test @ {0} ", DateTimeOffset.Now));
118+
}
119+
}
120+
else
121+
{
122+
_logger.LogInformation(string.Format("Failed to publish message to CF/Test. @ {0} ", DateTimeOffset.Now));
107123
}
108124
}
109125
}

0 commit comments

Comments
 (0)