Skip to content

Instantly share code, notes, and snippets.

@jplane
Created September 29, 2016 22:23
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/73b964dcaf79e63482bce67016b58b10 to your computer and use it in GitHub Desktop.
Save jplane/73b964dcaf79e63482bce67016b58b10 to your computer and use it in GitHub Desktop.
Web API controller to query IoT device metadata
public class MapController : ApiController
{
[Route("api/map/{id}")]
public object Get(int id)
{
var uri = new Uri(ConfigurationManager.AppSettings["docdbUri"]);
var key = ConfigurationManager.AppSettings["docdbKey"];
var dbName = ConfigurationManager.AppSettings["docdbDatabaseName"];
var collName = ConfigurationManager.AppSettings["docdbCollName"];
var client = new DocumentClient(uri, key);
var query = client.CreateDocumentQuery<BusInfo>(UriFactory.CreateDocumentCollectionUri(dbName, collName),
new FeedOptions { MaxItemCount = 1 })
.Where(bi => bi.VehicleId == id);
var result = query.AsEnumerable().FirstOrDefault();
return result != null
? new
{
tripid = result.TripId,
route = result.RouteShortName,
vehicleid = result.VehicleId,
timeliness = result.Timeliness.ToString()
}
: null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment