Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Created March 10, 2020 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/0fc37bda378ea94f694a2de8c1ca7a6e to your computer and use it in GitHub Desktop.
Save chrismckelt/0fc37bda378ea94f694a2de8c1ca7a6e to your computer and use it in GitHub Desktop.
Azure IoT Edge demo PipeMessage.cs
// This method is called whenever the module is sent a message from the EdgeHub. It just pipe the messages without any change.It prints all the incoming messages.
static async Task<MessageResponse> PipeMessage(Message message, object userContext) {
int counterValue=Interlocked.Increment(ref _counter);
var moduleClient=userContext as ModuleClient;
byte[] messageBytes=message.GetBytes();
string messageString=Encoding.UTF8.GetString(messageBytes);
if (!string.IsNullOrEmpty(messageString)) {
try {
Log.Information($"Receiving Message: {messageString.TrimStart('"').TrimEnd('"').Replace('\\', ' ')}");
Payload payload=JsonConvert.DeserializeObject<Payload>(messageString.TrimStart('"').TrimEnd('"').Replace("\\", String.Empty));
await SaveData(payload); // save in database
}
catch (Exception ex) {
Log.Error($"Error processing message: {ex}");
}
}
return await Task.FromResult(MessageResponse.Completed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment