机场注册

main
zhangbin 3 months ago
parent 40ed1bd3a1
commit 413cbf6d75

@ -0,0 +1,73 @@
using OpenAuth.App.BaseApp.Base;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.Interface;
using SqlSugar;
using Infrastructure;
using OpenAuth.App.ServiceApp.Response;
namespace OpenAuth.App.ServiceApp
{
public class AirportMaintenanceApp : SqlSugarBaseApp<LasaDronePort, SugarDbContext>
{
public AirportMaintenanceApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<LasaDronePort> repository, IAuth auth) : base(unitWork, repository, auth)
{
}
//获取设备绑定码
public async Task<Response<LasaDeviceBindingCode>> GetDeviceBindingCode()
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var info = await db.LasaDeviceBindingCode.AsQueryable().Where(r => r.BindStatus == 0).FirstAsync();
if (info != null)
{
return new Response<LasaDeviceBindingCode>
{
Code = 200,
Message = "获取设备绑定码成功",
Result = info
};
}
else
{
//如果设备绑定码不存在,则创建一个新的设备绑定码
var newBindingCode = new LasaDeviceBindingCode
{
Id = Guid.NewGuid().ToString(),
DeviceBindingCode = Guid.NewGuid().ToString("N").Substring(0, 8), // 生成一个新的绑定码
OrgId = "371300", // 默认组织ID
OrgName = "临沂市", // 默认组织名称
BindStatus = 0 // 未绑定状态
};
await db.LasaDeviceBindingCode.InsertAsync(newBindingCode);
if (db.Commit())
{
return new Response<LasaDeviceBindingCode>
{
Code = 200,
Message = "获取设备绑定码成功",
Result = newBindingCode
};
}
else
{
return new Response<LasaDeviceBindingCode>
{
Code = 500,
Message = "获取设备绑定码失败",
};
}
}
}
}
}
}

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp.Response
{
public class DeviceBindingCodeResp
{
/// <summary>
/// 设备绑定码
/// </summary>
public string DeviceBindingCode { get; set; }
/// <summary>
/// 组织id
/// </summary>
public string OrgId { get; set; }
/// <summary>
/// 组织名称
/// </summary>
public string OrgName { get; set; }
}
}

@ -0,0 +1,35 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.Repository.Domain
{
[SugarTable("lasa_devicebindingcode")]
public class LasaDeviceBindingCode
{
/// <summary>
/// 主键
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; }
/// <summary>
/// 设备绑定码
/// </summary>
public string DeviceBindingCode { get; set; }
/// <summary>
/// 组织id
/// </summary>
public string OrgId { get; set; }
/// <summary>
/// 组织名称
/// </summary>
public string OrgName { get; set; }
/// <summary>
/// 绑定状态
/// </summary>
public int BindStatus { get; set; }
}
}

@ -67,6 +67,7 @@ namespace OpenAuth.Repository
public SugarRepositiry<LasaWorkspace> LasaWorkspace { get; set; }
public SugarRepositiry<LasaSpaceUser> LasaSpaceUser { get; set; }
public SugarRepositiry<LasaSpaceDevice> LasaSpaceDevice { get; set; }
public SugarRepositiry<LasaDeviceBindingCode> LasaDeviceBindingCode { get; set; }
#endregion
}
}

@ -0,0 +1,72 @@
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;
}
}
}
Loading…
Cancel
Save