@@ -4,8 +4,6 @@ namespace IotEdgePerf.Transmitter.Edge
4
4
using System . Threading . Tasks ;
5
5
using System . Text ;
6
6
7
- using System . Collections . Generic ;
8
-
9
7
using Newtonsoft . Json ;
10
8
11
9
using Microsoft . Azure . Devices . Client ;
@@ -17,21 +15,12 @@ namespace IotEdgePerf.Transmitter.Edge
17
15
using Serilog . Events ;
18
16
19
17
using IotEdgePerf . Transmitter ;
20
- using IotEdgePerf . Transmitter . ConfigData ;
21
- using IotEdgePerf . Transmitter . Commands ;
22
-
18
+ using IotEdgePerf . Shared ;
19
+
23
20
class Program
24
21
{
25
- static ModuleClient _ioTHubModuleClient ;
26
- static string _moduleOutput = "output1" ;
27
-
28
- static string _deviceId = Environment . GetEnvironmentVariable ( "IOTEDGE_DEVICEID" ) ;
29
- static string _iotHubHostname = Environment . GetEnvironmentVariable ( "IOTEDGE_IOTHUBHOSTNAME" ) ;
30
- static LoggingLevelSwitch _logLevelSwitch = new LoggingLevelSwitch ( ) ;
31
-
32
- static TransmitterLogic _transmitter ;
33
22
34
- static TwinCollection _twin ;
23
+ static LoggingLevelSwitch _logLevelSwitch = new LoggingLevelSwitch ( ) ;
35
24
36
25
static void GetLogLevelFromEnv ( )
37
26
{
@@ -70,99 +59,47 @@ static async Task Main(string[] args)
70
59
//.WriteTo.File("logs/myapp.txt", rollingInterval: RollingInterval.Day)
71
60
. CreateLogger ( ) ;
72
61
73
- Init ( ) . Wait ( ) ;
62
+ TransmitterLogic transmitter = await Init ( ) ;
74
63
75
64
while ( true )
76
65
{
77
- await _transmitter . SendAsync ( ) ;
66
+ await transmitter . TransmitterLoopAsync ( ) ;
78
67
}
79
68
}
80
69
81
-
82
-
83
70
/// <summary>
84
71
/// Initializes the ModuleClient and sets up the callback to receive
85
72
/// messages containing temperature information
86
73
/// </summary>
87
- static async Task Init ( )
74
+ static async Task < TransmitterLogic > Init ( )
88
75
{
89
76
MqttTransportSettings mqttSetting = new MqttTransportSettings ( TransportType . Mqtt_Tcp_Only ) ;
90
77
ITransportSettings [ ] settings = { mqttSetting } ;
78
+ ModuleClient ioTHubModuleClient ;
79
+ string moduleOutput = "output1" ;
91
80
92
81
// Open a connection to the Edge runtime
93
- _ioTHubModuleClient = await ModuleClient . CreateFromEnvironmentAsync ( settings ) ;
94
- await _ioTHubModuleClient . OpenAsync ( ) ;
82
+ ioTHubModuleClient = await ModuleClient . CreateFromEnvironmentAsync ( settings ) ;
83
+ await ioTHubModuleClient . OpenAsync ( ) ;
95
84
96
- // Read module twin
97
- var moduleTwin = await _ioTHubModuleClient . GetTwinAsync ( ) ;
98
- await OnDesiredPropertiesUpdate ( moduleTwin . Properties . Desired , _transmitter ) ;
99
-
100
85
Log . Information ( "IoT Hub module client initialized." ) ;
101
- Log . Information ( $ "Device id: '{ _deviceId } '") ;
102
- Log . Information ( $ "IoT HUB: '{ _iotHubHostname } '") ;
86
+ Log . Information ( $ "Device id: '{ Environment . GetEnvironmentVariable ( "IOTEDGE_DEVICEID" ) } '") ;
87
+ Log . Information ( $ "IoT HUB: '{ Environment . GetEnvironmentVariable ( "IOTEDGE_IOTHUBHOSTNAME" ) } '") ;
103
88
104
89
// applies initial configuration from twins
105
- var transmitterConfig = TransmitterConfigData . GetFromObject ( _twin [ "config" ] ) ;
106
- _transmitter = new TransmitterLogic ( transmitterConfig ) ;
107
-
108
- // transmitter events handlers
109
- _transmitter . SendMessage += SdkSendMessage ;
110
- _transmitter . SendMessageBatch += null ;
111
- _transmitter . CreateMessage += CreateMessage ;
90
+ var moduleTwin = await ioTHubModuleClient . GetTwinAsync ( ) ;
91
+
92
+ TransmitterLogic transmitter = new TransmitterLogic (
93
+ TransmitterConfigData . GetFromObject ( moduleTwin . Properties . Desired [ "config" ] ) ,
94
+ new Transport . Sdk ( ioTHubModuleClient , moduleOutput ) ,
95
+ new MessageProvider . RandomMessage ( )
96
+ ) ;
112
97
113
98
// twin and dm handlers
114
- await _ioTHubModuleClient . SetDesiredPropertyUpdateCallbackAsync ( OnDesiredPropertiesUpdate , _transmitter ) ;
115
- await _ioTHubModuleClient . SetMethodHandlerAsync ( "Start" , OnStartDm , _transmitter ) ;
116
- }
117
-
118
-
119
-
120
- private async static Task SdkSendMessage ( string message )
121
- {
122
- Message azIotMessage = new Message ( Encoding . ASCII . GetBytes ( message ) ) ;
123
- await _ioTHubModuleClient . SendEventAsync ( _moduleOutput , azIotMessage ) ;
124
- }
125
-
126
- private async static Task SdkSendMessageBatch ( string [ ] messageBatch )
127
- {
128
- List < Message > azIotMessageBatch = new List < Message > ( ) ;
99
+ await ioTHubModuleClient . SetDesiredPropertyUpdateCallbackAsync ( OnDesiredPropertiesUpdate , transmitter ) ;
100
+ await ioTHubModuleClient . SetMethodHandlerAsync ( "Start" , OnStartDm , transmitter ) ;
129
101
130
- foreach ( var message in messageBatch )
131
- {
132
- azIotMessageBatch . Add ( new Message ( Encoding . ASCII . GetBytes ( message ) ) ) ;
133
- }
134
-
135
- await _ioTHubModuleClient . SendEventBatchAsync ( _moduleOutput , azIotMessageBatch ) ;
136
- }
137
-
138
- private static object CreateMessage ( int length )
139
- {
140
- var applicationObject = new
141
- {
142
- payload = RandomString ( length )
143
- } ;
144
-
145
- return applicationObject ;
146
- }
147
-
148
- /// <summary>
149
- /// Creates a string of specified length with random chars
150
- /// (from "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
151
- /// </summary>
152
- /// <param name="length">Number of chars</param>
153
- /// <returns>the string</returns>
154
- private static String RandomString ( int length )
155
- {
156
- var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
157
- var stringChars = new char [ length ] ;
158
- var random = new Random ( ) ;
159
-
160
- for ( int i = 0 ; i < length ; i ++ )
161
- {
162
- stringChars [ i ] = chars [ random . Next ( chars . Length ) ] ;
163
- }
164
-
165
- return new String ( stringChars ) ;
102
+ return transmitter ;
166
103
}
167
104
168
105
private static Task < MethodResponse > OnStartDm ( MethodRequest methodRequest , object userContext )
@@ -184,13 +121,10 @@ private static Task<MethodResponse> OnStartDm(MethodRequest methodRequest, objec
184
121
185
122
static Task OnDesiredPropertiesUpdate ( TwinCollection desiredProperties , object userContext )
186
123
{
187
- TransmitterLogic transmitter = ( TransmitterLogic ) userContext ;
188
-
189
124
try
190
125
{
191
126
Log . Debug ( "Desired property change:\n {0}" , JsonConvert . SerializeObject ( desiredProperties ) ) ;
192
- _twin = desiredProperties ;
193
-
127
+ TransmitterLogic transmitter = ( TransmitterLogic ) userContext ;
194
128
transmitter . ApplyConfiguration ( TransmitterConfigData . GetFromObject ( desiredProperties [ "config" ] ) ) ;
195
129
}
196
130
0 commit comments