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/Controllers/ServiceController.cs

52 lines
2.2 KiB

using Application.Domain.Entities;
using Infrastructure.Data;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace IoTCenter.Controllers
{
public class ServiceController : Controller
{
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<LiveRecord> _liveRecordRepo;
public ServiceController(IRepository<Device> deviceRepo, IRepository<LiveRecord> liveRecordRepo)
{
this._deviceRepo = deviceRepo;
this._liveRecordRepo = liveRecordRepo;
}
/// <summary>
/// 录制完毕事件响应
///{"action":"on_dvr","client_id":128,"ip":"127.0.0.1","vhost":"__defaultVhost__","app":"live","stream":"mainb57e867ae29d11b483864cbd8fd0f747","param":"","cwd":"/root/publish/apps/srs","file":"./objs/nginx/html/live/mainb57e867ae29d11b483864cbd8fd0f747.1565084670048.flv"}
/// </summary>
/// <returns></returns>
public IActionResult OnDvr()
{
using (var reader = new StreamReader(Request.Body))
{
var json = reader.ReadToEndAsync().Result;
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
if (result["action"] == "on_dvr")
{
var streamId = result["stream"];
var deviceNumber = streamId.StartsWith("main") ? streamId.Substring(4) : streamId.Substring(3);
var device = this._deviceRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == deviceNumber);
this._liveRecordRepo.Add(new LiveRecord
{
DeviceNumber = deviceNumber,
DeviceName = device?.DisplayName,
Name = $"回放{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}",
Value = $"/video{result["file"].Substring(result["file"].LastIndexOf('/'))}"
});
this._liveRecordRepo.SaveChanges();
}
}
return Content(string.Empty);
}
}
}