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 _deviceRepo; private readonly IRepository _liveRecordRepo; public ServiceController(IRepository deviceRepo, IRepository liveRecordRepo) { this._deviceRepo = deviceRepo; this._liveRecordRepo = liveRecordRepo; } /// /// 录制完毕事件响应 ///{"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"} /// /// public IActionResult OnDvr() { using (var reader = new StreamReader(Request.Body)) { var json = reader.ReadToEndAsync().Result; var result = JsonConvert.DeserializeObject>(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); } } }