Skip to content

Instantly share code, notes, and snippets.

@nathan-miller-actigraph
Created February 19, 2019 17:48
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 nathan-miller-actigraph/6cd18c99ee2ee4ca05fb5f74598287ae to your computer and use it in GitHub Desktop.
Save nathan-miller-actigraph/6cd18c99ee2ee4ca05fb5f74598287ae to your computer and use it in GitHub Desktop.
.NET core 2.1 AWS IoT and IotData test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Amazon.Runtime;
using Amazon.IoT;
using Amazon.IotData;
namespace myapp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
services.AddAWSService<IAmazonIoT>();
services.AddAWSService<IAmazonIotData>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using Amazon.IoT;
using Amazon.IoT.Model;
using Amazon.IotData;
using Amazon.IotData.Model;
namespace myapp.Controllers
{
[Route("api/[controller]")]
public class ThingController : Controller
{
IAmazonIoT IoTClient { get; set; }
IAmazonIotData IotDataClient { get; set; }
public ThingController(IAmazonIoT iotClient, IAmazonIotData iotDataClient)
{
this.IoTClient = iotClient;
this.IotDataClient = iotDataClient;
}
[HttpGet("[action]")]
public async Task<IActionResult> Get(string serial)
{
GetThingShadowRequest request = new GetThingShadowRequest()
{
ThingName = serial
};
GetThingShadowResponse response = await this.IotDataClient.GetThingShadowAsync(request, new CancellationToken());
StreamReader reader = new StreamReader(response.Payload);
string shadow = reader.ReadToEnd();
Console.WriteLine(shadow);
return Ok(shadow);
}
[HttpGet("[action]")]
public async Task<IActionResult> List()
{
ListThingsResponse response = await this.IoTClient.ListThingsAsync(new CancellationToken());
return Ok(response.Things.Select(thing => thing.ThingName));
}
[HttpGet("[action]")]
public async Task<IActionResult> Search(string fragment)
{
SearchIndexRequest request = new SearchIndexRequest()
{
MaxResults = 25,
QueryString = $"thingName:{fragment}*"
};
SearchIndexResponse response = await this.IoTClient.SearchIndexAsync(request, new CancellationToken());
return Ok(response.Things);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment