Created
March 10, 2020 02:19
-
-
Save chrismckelt/0fc37bda378ea94f694a2de8c1ca7a6e to your computer and use it in GitHub Desktop.
Azure IoT Edge demo PipeMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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