You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.5 KiB
C#

3 months ago
using DocumentFormat.OpenXml.Math;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using OpenAuth.App.ServiceApp;
using OpenAuth.Repository.Domain;
using System.Text.Json;
namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
/// <summary>
/// 机场运维
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class AirportMaintenanceController : ControllerBase
{
private readonly AirportMaintenanceApp _app;
private readonly MqttClientManager _mqttClientManager;
public AirportMaintenanceController(AirportMaintenanceApp app, MqttClientManager mqttClientManager)
{
_app = app;
_mqttClientManager = mqttClientManager;
}
//机场注册 注册码生成
[HttpPost]
public async Task<Response<LasaDeviceBindingCode>> GetDeviceBindingCode()
{
var result = new Response<LasaDeviceBindingCode>();
try
{
result = await _app.GetDeviceBindingCode();
//发送MQTT消息
if (result.Code == 200 && result.Result != null)
{
var topicRequest = $"thing/product/{result.Result.DeviceBindingCode}/requests";
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "airport_organization_bind",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
bind_devices = new[]
{
new
{
device_binding_code = result.Result.DeviceBindingCode,
organization_id = result.Result.OrgId
}
}
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
//在这个地方在接收一下?
}
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
}
}