using Application.Domain.Entities; using Infrastructure.Data; using IoTCenter.Application.Domain; 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 _deviceRepo; private readonly IRepository _liveRecordRepo; public SrsController(IRepository deviceRepo, IRepository liveRecordRepo) { this._deviceRepo = deviceRepo; this._liveRecordRepo = liveRecordRepo; } /// /// 录制完毕事件响应 ///{"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"} /// /// [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>(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; } } }