初始化
This commit is contained in:
29
iotweb/Controllers/ValuesController.cs
Normal file
29
iotweb/Controllers/ValuesController.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MQTTnet.Server;
|
||||
namespace iotweb.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class ValuesController : ControllerBase
|
||||
{
|
||||
private readonly MqttServer _mqttServer;
|
||||
|
||||
public ValuesController(MqttServer mqttServer)
|
||||
{
|
||||
_mqttServer = mqttServer;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet()]
|
||||
public async Task<IActionResult> GetConnectedClients()
|
||||
{
|
||||
var clients = await _mqttServer.GetClientsAsync();
|
||||
return Ok(clients.Select(c => new
|
||||
{
|
||||
Id = c.Id,
|
||||
Endpoint = c.Endpoint
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
33
iotweb/Controllers/WeatherForecastController.cs
Normal file
33
iotweb/Controllers/WeatherForecastController.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace iotweb.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
92
iotweb/Program.cs
Normal file
92
iotweb/Program.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using Microsoft.AspNetCore.Http.Connections;
|
||||
using MQTTnet.AspNetCore;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace iotweb
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.WebHost.UseKestrel(o =>
|
||||
{
|
||||
o.ListenAnyIP(1883, l => l.UseMqtt());
|
||||
o.ListenAnyIP(5000);
|
||||
});
|
||||
|
||||
|
||||
builder.Services.AddMqttServer(optionsBuilder =>
|
||||
{
|
||||
optionsBuilder.WithDefaultEndpoint();
|
||||
});
|
||||
|
||||
builder.Services.AddMqttConnectionHandler();
|
||||
builder.Services.AddConnections();
|
||||
builder.Services.AddSingleton<MqttController>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthorization();
|
||||
app.UseEndpoints((endpoints) =>
|
||||
{
|
||||
endpoints.MapConnectionHandler<MqttConnectionHandler>("/mqtt", ConfigureWebSocketOptions);
|
||||
});
|
||||
|
||||
app.UseMqttServer(
|
||||
server =>
|
||||
{
|
||||
server.ValidatingConnectionAsync += Server_ValidatingConnectionAsync; ;
|
||||
server.ClientConnectedAsync += Server_ClientConnectedAsync;
|
||||
});
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
private static void ConfigureWebSocketOptions(HttpConnectionDispatcherOptions options)
|
||||
{
|
||||
options.WebSockets.SubProtocolSelector = new Func<IList<string>, string>((IList<string> protocols) =>
|
||||
{
|
||||
return protocols.FirstOrDefault() ?? "mqtt";
|
||||
});
|
||||
}
|
||||
|
||||
private static Task Server_ClientConnectedAsync(ClientConnectedEventArgs arg)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task Server_ValidatingConnectionAsync(ValidatingConnectionEventArgs arg)
|
||||
{
|
||||
Console.WriteLine(arg.ClientId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
sealed class MqttController
|
||||
{
|
||||
public MqttController()
|
||||
{
|
||||
// Inject other services via constructor.
|
||||
}
|
||||
|
||||
public Task OnClientConnected(ClientConnectedEventArgs eventArgs)
|
||||
{
|
||||
Console.WriteLine($"Client '{eventArgs.ClientId}' connected.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public Task ValidateConnection(ValidatingConnectionEventArgs eventArgs)
|
||||
{
|
||||
Console.WriteLine($"Client '{eventArgs.ClientId}' wants to connect. Accepting!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
31
iotweb/Properties/launchSettings.json
Normal file
31
iotweb/Properties/launchSettings.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:2022",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"applicationUrl": "http://localhost:5286",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
iotweb/WeatherForecast.cs
Normal file
13
iotweb/WeatherForecast.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace iotweb
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
8
iotweb/appsettings.Development.json
Normal file
8
iotweb/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
17
iotweb/appsettings.json
Normal file
17
iotweb/appsettings.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://localhost:5286"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
15
iotweb/iotweb.csproj
Normal file
15
iotweb/iotweb.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
|
||||
<PackageReference Include="MQTTnet.AspNetCore" Version="5.0.1.1416" />
|
||||
<PackageReference Include="MQTTnet.Server" Version="5.0.1.1416" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
7
iotweb/iotweb.csproj.user
Normal file
7
iotweb/iotweb.csproj.user
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
6
iotweb/iotweb.http
Normal file
6
iotweb/iotweb.http
Normal file
@@ -0,0 +1,6 @@
|
||||
@iotweb_HostAddress = http://localhost:5286
|
||||
|
||||
GET {{iotweb_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
Reference in New Issue
Block a user