Skip to content

Instantly share code, notes, and snippets.

@jplane
Created September 29, 2016 22:14
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 jplane/35fbe2a928948a6f75393e17859745f6 to your computer and use it in GitHub Desktop.
Save jplane/35fbe2a928948a6f75393e17859745f6 to your computer and use it in GitHub Desktop.
Processing messages from an IoT Hub using EventProcessorHost and friends...
public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
// configure REST client
var client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["webUrl"]);
var save = false;
// process incoming messages
foreach (var msg in messages)
{
// get BusInfo from incoming message
var json = Encoding.UTF8.GetString(msg.GetBytes());
var bus = JsonConvert.DeserializeObject<BusInfo>(json);
// create metadata object from busInfo to POST to web app
var info = new
{
id = bus.VehicleId,
lat = bus.Latitude,
lng = bus.Longitude,
timepoint = bus.Timepoint
};
var content = new StringContent(JsonConvert.SerializeObject(info));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// POST
await client.PostAsync("api/listener", content);
save = true;
}
// if we successfully processed the messages, make note of that for all other consumers
if (save)
{
await context.CheckpointAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment