@@ -67,32 +67,30 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
67
67
{
68
68
if ( isConnected )
69
69
{
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 ) ;
89
75
}
90
76
else
91
77
{
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 ) ) ;
93
79
}
94
80
}
95
81
}
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
+ }
96
94
97
95
private async Task ConnectToBroker ( )
98
96
{
@@ -103,7 +101,25 @@ private async Task ConnectToBroker()
103
101
}
104
102
catch ( Exception ex )
105
103
{
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 ) ) ;
107
123
}
108
124
}
109
125
}
0 commit comments