LASAPlatform/OpenAuth.WebApi/Controllers/ServiceControllers/AirportMaintenanceControlle...

370 lines
13 KiB
C#
Raw Normal View History

2025-06-12 14:33:37 +08:00
using DocumentFormat.OpenXml.Math;
2025-06-19 14:47:46 +08:00
using DocumentFormat.OpenXml.Spreadsheet;
2025-06-12 14:33:37 +08:00
using Infrastructure;
2025-06-18 15:44:13 +08:00
using Microsoft.AspNetCore.Authorization;
2025-06-12 14:33:37 +08:00
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using OpenAuth.App.ServiceApp;
using OpenAuth.Repository.Domain;
2025-06-16 15:12:06 +08:00
using System.Text;
2025-06-12 14:33:37 +08:00
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;
2025-06-19 14:47:46 +08:00
private readonly MqttMessageCenter _mqttCenter;
2025-06-12 14:33:37 +08:00
2025-06-19 14:47:46 +08:00
public AirportMaintenanceController(AirportMaintenanceApp app, MqttClientManager mqttClientManager, MqttMessageCenter mqttCenter)
2025-06-12 14:33:37 +08:00
{
_app = app;
_mqttClientManager = mqttClientManager;
2025-06-19 14:47:46 +08:00
_mqttCenter = mqttCenter;
2025-06-12 14:33:37 +08:00
}
2025-06-12 15:58:24 +08:00
/// <summary>
/// 机场注册 注册码生成
/// </summary>
/// <returns></returns>
2025-06-12 14:33:37 +08:00
[HttpPost]
2025-06-18 15:44:13 +08:00
[Obsolete]
2025-06-12 14:33:37 +08:00
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;
}
2025-06-18 15:44:13 +08:00
/// <summary>
/// 机场注册 注册码生成
/// </summary>
/// <returns></returns>
[HttpPost]
[AllowAnonymous]
public async Task<Response<LasaGateway>> GetGateway()
{
var result = new Response<LasaGateway>();
try
{
result = await _app.GetGateway();
2025-06-19 14:47:46 +08:00
//自动更新主题
string sn = result.Result.GatewaySn;
var topics = new List<string>();
topics.AddRange(new[]
{
$"thing/product/{sn}/osd",
$"thing/product/{sn}/events",
$"thing/product/{sn}/requests",
$"thing/product/{sn}/services"
});
await _mqttCenter.SubscribeAsync(topics.ToArray());
2025-06-18 15:44:13 +08:00
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
2025-06-13 09:09:13 +08:00
/// <summary>
/// 切换相机
/// </summary>
/// <returns></returns>
[HttpPost]
2025-06-16 15:52:11 +08:00
public async Task<Response<int>> ExchangeCamera(Zhibo zhiboReq)
2025-06-13 09:09:13 +08:00
{
Response<int> response = new Response<int>();
try
{
2025-06-18 15:44:13 +08:00
var videoids = zhiboReq.videoId.Split("/");
var topicRequest = $"thing/product/" + videoids[0] + "/services";
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_camera_change",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
2025-06-13 09:09:13 +08:00
{
2025-06-18 15:44:13 +08:00
camera_position = zhiboReq.position,
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
2025-06-13 09:09:13 +08:00
2025-06-18 15:44:13 +08:00
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
2025-06-13 09:09:13 +08:00
2025-06-16 15:12:06 +08:00
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
{
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
2025-06-18 15:44:13 +08:00
await Task.CompletedTask;
2025-06-16 15:12:06 +08:00
});
response.Result = 0;
2025-06-13 09:09:13 +08:00
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response; ;
2025-06-18 15:44:13 +08:00
2025-06-13 09:09:13 +08:00
}
/// <summary>
/// 设置直播镜头
/// </summary>
/// <returns></returns>
[HttpPost]
2025-06-16 15:52:11 +08:00
public async Task<Response<int>> SetCamera(Zhibo zhiboReq)
2025-06-13 09:09:13 +08:00
{
Response<int> response = new Response<int>();
try
{
2025-06-16 15:52:11 +08:00
var videoids = zhiboReq.videoId.Split("/");
2025-06-16 15:12:06 +08:00
var topicRequest = $"thing/product/" + videoids[0] + "/services";
2025-06-13 09:09:13 +08:00
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_lens_change",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
2025-06-16 15:52:11 +08:00
video_type = zhiboReq.cameraType,
video_id = zhiboReq.videoId
2025-06-13 09:09:13 +08:00
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
2025-06-16 15:12:06 +08:00
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
2025-06-13 09:09:13 +08:00
{
2025-06-16 15:12:06 +08:00
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
2025-06-13 09:09:13 +08:00
2025-06-16 15:12:06 +08:00
response.Result = 0;
2025-06-13 09:09:13 +08:00
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
2025-06-18 15:44:13 +08:00
return response;
2025-06-13 09:09:13 +08:00
}
/// <summary>
/// 设置直播清晰度
/// </summary>
/// <returns></returns>
[HttpPost]
2025-06-16 15:52:11 +08:00
public async Task<Response<int>> SetCameraVideo(Zhibo zhiboReq)
2025-06-13 09:09:13 +08:00
{
Response<int> response = new Response<int>();
try
{
2025-06-16 15:52:11 +08:00
var videoids = zhiboReq.videoId.Split("/");
2025-06-16 15:12:06 +08:00
var topicRequest = $"thing/product/" + videoids[0] + "/services";
2025-06-13 09:09:13 +08:00
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_set_quality",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
2025-06-16 15:52:11 +08:00
video_quality = zhiboReq.videoQuality,
video_id = zhiboReq.videoId
2025-06-13 09:09:13 +08:00
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
2025-06-16 15:12:06 +08:00
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
2025-06-13 09:09:13 +08:00
{
2025-06-16 15:12:06 +08:00
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
2025-06-13 09:09:13 +08:00
2025-06-16 15:12:06 +08:00
response.Result = 0;
2025-06-13 09:09:13 +08:00
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
2025-06-18 15:44:13 +08:00
return response;
2025-06-13 09:09:13 +08:00
}
/// <summary>
/// 停止直播
/// </summary>
/// <returns></returns>
[HttpPost]
2025-06-18 15:44:13 +08:00
public async Task<Response<int>> EndLive(Zhibo zhiboReq)
2025-06-13 09:09:13 +08:00
{
Response<int> response = new Response<int>();
try
{
2025-06-16 15:52:11 +08:00
var videoids = zhiboReq.videoId.Split("/");
2025-06-16 15:12:06 +08:00
var topicRequest = $"thing/product/" + videoids[0] + "/services";
var requestData = new
2025-06-18 15:44:13 +08:00
{
bid = Guid.NewGuid().ToString(),
method = "live_stop_push",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
2025-06-13 09:09:13 +08:00
{
2025-06-18 15:44:13 +08:00
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
2025-06-13 09:09:13 +08:00
2025-06-16 15:12:06 +08:00
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
2025-06-13 09:09:13 +08:00
{
2025-06-16 15:12:06 +08:00
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
2025-06-13 09:09:13 +08:00
2025-06-16 15:12:06 +08:00
response.Result = 0;
2025-06-18 15:44:13 +08:00
2025-06-13 09:09:13 +08:00
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
/// <summary>
/// 开始直播
/// </summary>
/// <returns></returns>
[HttpPost]
2025-06-16 15:52:11 +08:00
public async Task<Response<int>> StartLive(Zhibo zhiboReq)
2025-06-13 09:09:13 +08:00
{
Response<int> response = new Response<int>();
try
{
2025-06-16 15:52:11 +08:00
var videoids = zhiboReq.videoId.Split("/");
2025-06-16 15:12:06 +08:00
var topicRequest = $"thing/product/" + videoids[0] + "/services";
2025-06-13 09:09:13 +08:00
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_start_push",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
2025-06-16 15:52:11 +08:00
url_type = zhiboReq.urlType,
url = zhiboReq.url,
video_quality = zhiboReq.videoQuality,
video_id = zhiboReq.videoId
2025-06-13 09:09:13 +08:00
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
2025-06-16 15:12:06 +08:00
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
{
var payload1 = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload1);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
2025-06-13 09:09:13 +08:00
2025-06-16 15:12:06 +08:00
response.Result = 0;
2025-06-13 09:09:13 +08:00
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
2025-06-12 14:33:37 +08:00
}
}