You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTCenter/Api/SrsController.cs

64 lines
2.4 KiB

using Application.Domain.Entities;
using Infrastructure.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace IoTCenter.Api.Controllers
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class SrsController : ControllerBase
{
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<LiveRecord> _liveRecordRepo;
public SrsController(IRepository<Device> deviceRepo,
IRepository<LiveRecord> liveRecordRepo)
{
this._deviceRepo = deviceRepo;
this._liveRecordRepo = liveRecordRepo;
}
/// <summary>
/// 录制完毕事件响应
///{"action":"on_dvr","client_id":107,"ip":"192.168.3.124","vhost":"__defaultVhost__","app":"live","stream":"main0a5d5deee43011b483884cbd8fd2b3dd","param":"","cwd":"/root/publish/apps/srs","file":"./objs/nginx/html/live.main0a5d5deee43011b483884cbd8fd2b3dd.1574832559180.mp4"}
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpPost]
public string OnDvr()
{
using (var reader = new StreamReader(Request.Body))
{
var json = reader.ReadToEndAsync().Result;
Debug.WriteLine("ondvr:");
Debug.WriteLine(json);
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
if (result["action"] == "on_dvr")
{
var streamId = result["stream"];
var device = this._deviceRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == streamId);
if (device != null)
{
this._liveRecordRepo.Add(new LiveRecord
{
DeviceNumber = device.Number,
DeviceName = device?.DisplayName,
Name = $"回放{DateTime.Now:yyyy-MM-dd HH:mm}",
Value = $"/video{result["file"].Substring(result["file"].LastIndexOf('/'))}"
});
this._liveRecordRepo.SaveChanges();
}
}
}
return string.Empty;
}
}
}