From 6a2bd07b0e13662bdc21cfba78bc1743d23ba7c3 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 6 Aug 2019 17:46:38 +0800 Subject: [PATCH] update Former-commit-id: 047dc5700d8eb39c12b074d91f4587fee2f2c949 --- .../Application/Domain/Entities/Device.cs | 3 -- .../Application/Models/EditDeviceModel.cs | 3 -- .../Controllers/Onvif/OnvifController.cs | 18 +++++++++ .../DeviceServices/FBee/FBeeService.cs | 39 ++++++------------- .../DeviceServices/Onvif/OnvifService.cs | 35 ++++++++++++++--- projects/IoT/IoTNode/Program.cs | 6 +-- projects/IoT/IoTNode/Startup.cs | 4 +- .../IoTCenter/Controllers/AppController.cs | 13 +++++++ tools/nginx-1.16.0/conf/nginx.conf | 2 +- 9 files changed, 78 insertions(+), 45 deletions(-) diff --git a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs index a90da967..fc0b950f 100644 --- a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs @@ -42,9 +42,6 @@ namespace Application.Domain.Entities [Display(Name = "密码")] public string Password { get; set; } - [Display(Name = "启用")] - public bool Enable { get; set; } - [Display(Name = "中转")] public bool IsTransfer { get; set; } diff --git a/projects/IoT/IoT.Shared/Application/Models/EditDeviceModel.cs b/projects/IoT/IoT.Shared/Application/Models/EditDeviceModel.cs index 3a26ce6e..87d01e91 100644 --- a/projects/IoT/IoT.Shared/Application/Models/EditDeviceModel.cs +++ b/projects/IoT/IoT.Shared/Application/Models/EditDeviceModel.cs @@ -37,9 +37,6 @@ namespace Application.Models [Display(Name = "密码")] public string Password { get; set; } - [Display(Name = "启用")] - public bool Enable { get; set; } - [Display(Name = "编号")] [Required(ErrorMessage = nameof(RequiredAttribute))] public string Number { get; set; } diff --git a/projects/IoT/IoT.Shared/Controllers/Onvif/OnvifController.cs b/projects/IoT/IoT.Shared/Controllers/Onvif/OnvifController.cs index 939d93a8..3b935981 100644 --- a/projects/IoT/IoT.Shared/Controllers/Onvif/OnvifController.cs +++ b/projects/IoT/IoT.Shared/Controllers/Onvif/OnvifController.cs @@ -96,5 +96,23 @@ namespace IoT.Shared.Controllers.Onvif this._deviceService.Stop(number); }); } + + [HttpGet, Route("/[controller]/[action]"), SwaggerOperation("开始推流")] + public ApiResponse StartPush([SwaggerParameter("网关编号")]string gateway, [SwaggerParameter("设备编号")]string number) + { + return this.AsyncAction(() => + { + this._deviceService.StartPush(number); + }); + } + + [HttpGet, Route("/[controller]/[action]"), SwaggerOperation("停止推流")] + public ApiResponse StopPush([SwaggerParameter("网关编号")]string gateway, [SwaggerParameter("设备编号")]string number) + { + return this.AsyncAction(() => + { + this._deviceService.StopPush(number); + }); + } } } \ No newline at end of file diff --git a/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs b/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs index 46170bcd..ea4299d8 100644 --- a/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs +++ b/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs @@ -160,7 +160,6 @@ namespace IoT.Shared.DeviceServices.FBee { Number = sn, Name = "网关", - Enable = true, Icon = "gateway", CategoryNumber = "0", InfoNumber = deviceInfoNumber, @@ -178,42 +177,28 @@ namespace IoT.Shared.DeviceServices.FBee var gateways = deviceRepo.ReadOnlyTable().Where(o => o.Info.DeviceType == DeviceType.Gateway).ToList(); foreach (var gateway in gateways) { - if (gateway.Enable) + if (Clients.Any(o => o.Key == gateway.Number)) { - if (Clients.Any(o => o.Key == gateway.Number)) + Clients.TryGetValue(gateway.Number, out TcpClientWrapper client); + if (client.Ip != gateway.Ip) { - Clients.TryGetValue(gateway.Number, out TcpClientWrapper client); - if (client.Ip != gateway.Ip) - { - this.Connect(client); - } - else - { - if (!client.Client.Connected) - { - this.Connect(client); - } - } + this.Connect(client); } else { - var client = new TcpClientWrapper { Sn = gateway.Number, Ip = gateway.Ip, Client = new TcpClient() }; - Clients.TryAdd(gateway.Number, client); - this.Connect(client); + if (!client.Client.Connected) + { + this.Connect(client); + } } - this.X81(gateway.Number); } else { - if (Clients.Any(o => o.Key == gateway.Number)) - { - Clients.TryRemove(gateway.Number, out TcpClientWrapper client); - if (client.Client.Connected) - { - client.Client.Close(); - } - } + var client = new TcpClientWrapper { Sn = gateway.Number, Ip = gateway.Ip, Client = new TcpClient() }; + Clients.TryAdd(gateway.Number, client); + this.Connect(client); } + this.X81(gateway.Number); } } } diff --git a/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs b/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs index 6e542fa2..db2ddeb4 100644 --- a/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs +++ b/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs @@ -1,7 +1,6 @@ using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Extensions; -using Infrastructure.Models; using IoT.Shared.Infrastructure; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; @@ -124,6 +123,8 @@ namespace IoT.Shared.DeviceServices.Onvif }; var fileName = $"ffmpeg-{Helper.Instance.GetRunTime()}{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "")}"; var file = Path.Combine(this._env.WebRootPath, fileName); + device.AddorUpdateData("Push", "否", DeviceDataType.String, "推流"); + device.AddorUpdateData("Record", "否", DeviceDataType.String, "录像"); device.AddorUpdateData("ffmpeg.file", file, DeviceDataType.String, "ffmpeg路径", hidden: true); device.AddorUpdateData("ffmpeg.args", this._configuration["ffmpeg.args"], DeviceDataType.String, "ffmpeg.args", hidden: true); device.AddorUpdateData("mainrtmp", $"rtmp://{this._configuration["stream.rtmp"]}/live/main{ipCamera.Id}", DeviceDataType.String, "主码流rtmp"); @@ -221,12 +222,12 @@ namespace IoT.Shared.DeviceServices.Onvif using (var scope = _applicationServices.CreateScope()) { var repo = scope.ServiceProvider.GetService>(); - var cameras = repo.ReadOnlyTable().Include(o => o.Data).Where(o => o.Enable).ToList(); + var cameras = repo.ReadOnlyTable().Include(o => o.Data).Where(o => o.Name == "摄像头").ToList(); foreach (var key in this._list.Keys) { var camera = cameras.FirstOrDefault(o => o.Number == key); var remove = false; - if (!camera.Enable) + if (camera.Data.FirstOrDefault(o => o.Key == "Push").Value != "是") { remove = true; } @@ -243,7 +244,7 @@ namespace IoT.Shared.DeviceServices.Onvif { try { - if (camera.Enable) + if (camera.Data.FirstOrDefault(o => o.Key == "Push").Value == "是") { if (camera.Data.FirstOrDefault(o => o.Key == "NeedAuth").Value == "否" || camera.Data.FirstOrDefault(o => o.Key == "HasAuth").Value == "是") { @@ -280,7 +281,7 @@ namespace IoT.Shared.DeviceServices.Onvif { var repo = scope.ServiceProvider.GetService>(); var camera = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Number == key); - if (camera != null && camera.Enable)// && camera.Publish && (!camera.NeedAuth || (camera.NeedAuth && camera.HasAuth))) + if (camera != null && camera.Data.FirstOrDefault(o => o.Key == "Push").Value == "是")// && camera.Publish && (!camera.NeedAuth || (camera.NeedAuth && camera.HasAuth))) { if (camera.Data.FirstOrDefault(o => o.Key == "NeedAuth").Value == "否" || camera.Data.FirstOrDefault(o => o.Key == "HasAuth").Value == "是") { @@ -377,7 +378,7 @@ namespace IoT.Shared.DeviceServices.Onvif { if (!string.IsNullOrEmpty(e.Data)) { - //Console.WriteLine(e.Data); + Console.WriteLine(e.Data); if (e.Data.IndexOf("forcing output") > -1) { process.Kill(); @@ -538,6 +539,28 @@ namespace IoT.Shared.DeviceServices.Onvif return hc.GetByteDigest(url, camera.UserName, camera.Password); } + public void StartPush(string number) + { + using (var scope = _applicationServices.CreateScope()) + { + var repo = scope.ServiceProvider.GetService>(); + var device = repo.Table().Include(o => o.Data).FirstOrDefault(o => o.Number == number); + var data = device.Data.FirstOrDefault(o => o.Key == "Push"); + data.Value = "是"; + } + } + + public void StopPush(string number) + { + using (var scope = _applicationServices.CreateScope()) + { + var repo = scope.ServiceProvider.GetService>(); + var device = repo.Table().Include(o => o.Data).FirstOrDefault(o => o.Number == number); + var data = device.Data.FirstOrDefault(o => o.Key == "Push"); + data.Value = "否"; + } + } + private byte[] ScreenShot(Device camera) { var url = camera.GetDataValue("SubSnapshotUri"); diff --git a/projects/IoT/IoTNode/Program.cs b/projects/IoT/IoTNode/Program.cs index 9525773f..b504a2d3 100644 --- a/projects/IoT/IoTNode/Program.cs +++ b/projects/IoT/IoTNode/Program.cs @@ -13,7 +13,7 @@ namespace IoTNode public static void Main(string[] args) { var host = "localhost"; - var stream = "49.4.92.112"; + var stream = "localhost"; WebHost.CreateDefaultBuilder(args) .Run(new List { new EFConfigurationValue { Id = "id", Value= "根据设备编号生成的授权码" }, @@ -42,8 +42,8 @@ namespace IoTNode new EFConfigurationValue { Id = "camera.usr", Value="admin"}, new EFConfigurationValue { Id = "camera.pwd", Value="dsideal123"}, new EFConfigurationValue { Id = "stream.rtmp", Value=stream}, - new EFConfigurationValue { Id = "stream.flv", Value=$"{stream}:8080"}, - new EFConfigurationValue { Id = "stream.hls", Value=$"{stream}:8080"}, + new EFConfigurationValue { Id = "stream.flv", Value=$"{stream}/live"}, + new EFConfigurationValue { Id = "stream.hls", Value=$"{stream}/live"}, new EFConfigurationValue { Id = "ffmpeg.args", Value=" -y -threads {0} -rtsp_transport tcp -use_wallclock_as_timestamps 1 -stimeout 3000000 -i \"{1}\" -fflags +genpts -c copy -f flv \"{2}\""}, // new EFConfigurationValue { Id = "name", Value= "物联网节点"}, diff --git a/projects/IoT/IoTNode/Startup.cs b/projects/IoT/IoTNode/Startup.cs index d32598dc..53131a89 100644 --- a/projects/IoT/IoTNode/Startup.cs +++ b/projects/IoT/IoTNode/Startup.cs @@ -27,7 +27,7 @@ namespace IoTNode services.AddSingleton(); //services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + //services.AddSingleton(); base.ConfigureServices(services); } @@ -39,7 +39,7 @@ namespace IoTNode app.ApplicationServices.GetService().Start(); app.ApplicationServices.GetService()?.Start(); app.ApplicationServices.GetService()?.Start(); - app.ApplicationServices.GetService()?.Start(); + //app.ApplicationServices.GetService()?.Start(); }); } diff --git a/projects/IoTCenter/Controllers/AppController.cs b/projects/IoTCenter/Controllers/AppController.cs index abf4c887..674f6a92 100644 --- a/projects/IoTCenter/Controllers/AppController.cs +++ b/projects/IoTCenter/Controllers/AppController.cs @@ -13,6 +13,7 @@ using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Vibrant.InfluxDB.Client; using Vibrant.InfluxDB.Client.Rows; @@ -164,5 +165,17 @@ namespace IoTCenter.Controllers } return Json(list); } + + //{"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 body = reader.ReadToEnd(); + + // Do something + } + return Content(string.Empty); + } } } \ No newline at end of file diff --git a/tools/nginx-1.16.0/conf/nginx.conf b/tools/nginx-1.16.0/conf/nginx.conf index 50f33eae..e036c8ed 100644 --- a/tools/nginx-1.16.0/conf/nginx.conf +++ b/tools/nginx-1.16.0/conf/nginx.conf @@ -26,7 +26,7 @@ http { listen 80; server_name localhost; - location ^~ /srs/ { + location ^~ /live/ { proxy_pass http://localhost:8080/; }