using DocumentFormat.OpenXml.Math;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using OpenAuth.App.ServiceApp;
using OpenAuth.Repository.Domain;
using System.Text;
using System.Text.Json;
namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
///
/// 机场运维
///
[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> GetDeviceBindingCode()
{
var result = new Response();
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;
}
///
/// 切换相机
///
///
[HttpPost]
public async Task> ExchangeCamera(Zhibo zhiboReq)
{
Response response = new Response();
try
{
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
{
camera_position = zhiboReq.position,
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
var topicRequest1 = $"thing/product/" + videoids[0] +"/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
{
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
response.Result = 0;
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response; ;
}
///
/// 设置直播镜头
///
///
[HttpPost]
public async Task> SetCamera(Zhibo zhiboReq)
{
Response response = new Response();
try
{
var videoids = zhiboReq.videoId.Split("/");
var topicRequest = $"thing/product/" + videoids[0] + "/services";
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_lens_change",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
video_type = zhiboReq.cameraType,
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
{
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
response.Result = 0;
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
///
/// 设置直播清晰度
///
///
[HttpPost]
public async Task> SetCameraVideo(Zhibo zhiboReq)
{
Response response = new Response();
try
{
var videoids = zhiboReq.videoId.Split("/");
var topicRequest = $"thing/product/" + videoids[0] + "/services";
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_set_quality",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
video_quality = zhiboReq.videoQuality,
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
{
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
response.Result = 0;
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
///
/// 停止直播
///
///
[HttpPost]
public async Task> EndLive( Zhibo zhiboReq)
{
Response response = new Response();
try
{
var videoids = zhiboReq.videoId.Split("/");
var topicRequest = $"thing/product/" + videoids[0] + "/services";
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_stop_push",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
{
var payload = args.ApplicationMessage.Payload;
var message = Encoding.UTF8.GetString(payload);
Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
await Task.CompletedTask;
});
response.Result = 0;
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
///
/// 开始直播
///
///
[HttpPost]
public async Task> StartLive(Zhibo zhiboReq)
{
Response response = new Response();
try
{
var videoids = zhiboReq.videoId.Split("/");
var topicRequest = $"thing/product/" + videoids[0] + "/services";
var requestData = new
{
bid = Guid.NewGuid().ToString(),
method = "live_start_push",
tid = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
data = new
{
url_type = zhiboReq.urlType,
url = zhiboReq.url,
video_quality = zhiboReq.videoQuality,
video_id = zhiboReq.videoId
}
};
string payload = JsonSerializer.Serialize(requestData);
await _mqttClientManager.PublishAsync(topicRequest, payload);
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;
});
response.Result = 0;
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
}
}