Created
September 29, 2016 22:14
-
-
Save jplane/35fbe2a928948a6f75393e17859745f6 to your computer and use it in GitHub Desktop.
Processing messages from an IoT Hub using EventProcessorHost and friends...
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
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