From c6e7e75b5701643b95f9f3aa81ba05e86a7ca1d9 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 27 Nov 2019 18:05:59 +0800 Subject: [PATCH] update Former-commit-id: fa49ed844da2a9896f308f94c9f2eb7f128c3875 --- .../IoT.Shared/Application/Models/Methods.cs | 3 + .../Services/IoTCenter/IoTCenterHub.cs | 75 ++++++++++++++++++- .../Services/IoTNode/IoTNodeClient.cs | 6 ++ projects/IoTCenter/Program.cs | 1 + projects/IoTCenter/Views/Home/Device.cshtml | 2 + .../Onvif/Controllers/OnvifController.cs | 22 +++++- .../DeviceServices/Onvif/OnvifService.cs | 23 ++++-- 7 files changed, 121 insertions(+), 11 deletions(-) diff --git a/projects/IoT.Shared/Application/Models/Methods.cs b/projects/IoT.Shared/Application/Models/Methods.cs index c837f42a..927ccbad 100644 --- a/projects/IoT.Shared/Application/Models/Methods.cs +++ b/projects/IoT.Shared/Application/Models/Methods.cs @@ -86,5 +86,8 @@ public const string DeleteIoTTiggerRequest = nameof(DeleteIoTTiggerRequest); public const string DeleteIoTTiggerResponse = nameof(DeleteIoTTiggerResponse); + + public const string UpdateDvr = nameof(UpdateDvr); + public const string UpdateCamera = nameof(UpdateCamera); } } \ No newline at end of file diff --git a/projects/IoT.Shared/Services/IoTCenter/IoTCenterHub.cs b/projects/IoT.Shared/Services/IoTCenter/IoTCenterHub.cs index 675591e1..2daa9257 100644 --- a/projects/IoT.Shared/Services/IoTCenter/IoTCenterHub.cs +++ b/projects/IoT.Shared/Services/IoTCenter/IoTCenterHub.cs @@ -6,10 +6,13 @@ using Infrastructure.Extensions; using Infrastructure.Web.SignalR; using IoT.Shared.Services; using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; using Vibrant.InfluxDB.Client; using Vibrant.InfluxDB.Client.Rows; @@ -32,6 +35,7 @@ namespace IoTCenter.Services private readonly IRepository _iotTimerRepo; private readonly IRepository _iotTiggerRepo; private readonly DataService _dataService; + private readonly IHttpClientFactory _httpClientFactory; public IoTCenterHub(IConfiguration cfg, IEventPublisher eventPublisher, @@ -46,7 +50,8 @@ namespace IoTCenter.Services IRepository sceneCommandRepo, IRepository iotTimerRepo, IRepository iotTiggerRepo, - DataService dataService) + DataService dataService, + IHttpClientFactory httpClientFactory) { this._cfg = cfg; this._eventPublisher = eventPublisher; @@ -62,6 +67,7 @@ namespace IoTCenter.Services this._iotTimerRepo = iotTimerRepo; this._iotTiggerRepo = iotTiggerRepo; this._dataService = dataService; + this._httpClientFactory = httpClientFactory; } public void ServerToClient(string method, string message, string toClient, string fromClient = null) @@ -272,11 +278,78 @@ namespace IoTCenter.Services { this.ServerToClient(method, message, to, from); } + else if (method == Methods.UpdateDvr) + { + var model = message.FromJson(); + this.UpdateDvr(model); + } + } + catch (Exception ex) + { + ex.PrintStack(); + } + } + + private void UpdateDvr(Data model) + { + try + { + var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).FirstOrDefault(o => o.Id == model.DeviceId); + var number = device.Node.Number; + var url = this._cfg.GetValue("srs", "http://localhost:1985"); + var result = ""; + var method = Methods.UpdateCamera; + if (model.Value == "是") + { + var url2 = url + $"/api/v1/raw?rpc=update&scope=dvr&value=__defaultVhost__¶m=enable&data=live/main{device.Number}"; + if (CallSrs(url2)) + { + this.ServerToClient(method, model.ToJson(), number); + } + url2 = url + $"/api/v1/raw?rpc=update&scope=dvr&value=__defaultVhost__¶m=enable&data=live/sub{device.Number}"; + if (CallSrs(url2)) + { + this.ServerToClient(method, model.ToJson(), number); + } + } + else + { + var url2 = url + $"/api/v1/raw?rpc=update&scope=dvr&value=__defaultVhost__¶m=disable&data=live/main{device.Number}"; + if (CallSrs(url2)) + { + this.ServerToClient(method, model.ToJson(), number); + } + url2 = url + $"/api/v1/raw?rpc=update&scope=dvr&value=__defaultVhost__¶m=disable&data=live/sub{device.Number}"; + if (CallSrs(url2)) + { + this.ServerToClient(method, model.ToJson(), number); + } + } + } + catch (Exception ex) + { + ex.PrintStack(); + } + } + + private bool CallSrs(string url) + { + var result = false; + try + { + var httpClient = this._httpClientFactory.CreateClient(); + var response = httpClient.GetStringAsync(url).Result; + var json = JsonConvert.DeserializeObject>(response); + if (json["code"] == "0") + { + result = true; + } } catch (Exception ex) { ex.PrintStack(); } + return result; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] diff --git a/projects/IoT.Shared/Services/IoTNode/IoTNodeClient.cs b/projects/IoT.Shared/Services/IoTNode/IoTNodeClient.cs index c87ed68d..8a3ebce9 100644 --- a/projects/IoT.Shared/Services/IoTNode/IoTNodeClient.cs +++ b/projects/IoT.Shared/Services/IoTNode/IoTNodeClient.cs @@ -366,6 +366,12 @@ namespace IoT.Shared.Services var id = message.FromJson(); this.ExecCommand(id); } + else if (method == Methods.UpdateCamera) + { + var model = message.FromJson(); + dataService.Edit(model); + this.ClientToServer(method, model, null); + } } catch (Exception ex) { diff --git a/projects/IoTCenter/Program.cs b/projects/IoTCenter/Program.cs index bacd24cc..242b6723 100644 --- a/projects/IoTCenter/Program.cs +++ b/projects/IoTCenter/Program.cs @@ -33,6 +33,7 @@ namespace IoTCenter new EFConfigurationValue { Id = "influxdb:url", Value= $"http://{host}:8086"}, new EFConfigurationValue { Id = "influxdb:usr", Value= "admin"}, new EFConfigurationValue { Id = "influxdb:pwd", Value= "admin"}, + new EFConfigurationValue { Id = "srs", Value= "http://localhost:1985"}, // new EFConfigurationValue { Id = "name", Value= "物联网管控平台"}, new EFConfigurationValue { Id = "logo", Value= "/images/logo.png",Type= InputType.ImageUrl}, diff --git a/projects/IoTCenter/Views/Home/Device.cshtml b/projects/IoTCenter/Views/Home/Device.cshtml index ac6a1e93..0e8a6326 100644 --- a/projects/IoTCenter/Views/Home/Device.cshtml +++ b/projects/IoTCenter/Views/Home/Device.cshtml @@ -198,6 +198,8 @@

{{model.DisplayName}}

+ +
diff --git a/projects/IoTNode/DeviceServices/Onvif/Controllers/OnvifController.cs b/projects/IoTNode/DeviceServices/Onvif/Controllers/OnvifController.cs index 0afe60e6..067df7c3 100644 --- a/projects/IoTNode/DeviceServices/Onvif/Controllers/OnvifController.cs +++ b/projects/IoTNode/DeviceServices/Onvif/Controllers/OnvifController.cs @@ -102,7 +102,7 @@ namespace IoTNode.Controllers { return this.AsyncAction(() => { - this._deviceService.StartPush(number); + this._deviceService.SetPush(number, true); }); } @@ -111,7 +111,25 @@ namespace IoTNode.Controllers { return this.AsyncAction(() => { - this._deviceService.StopPush(number); + this._deviceService.SetPush(number, false); + }); + } + + [HttpGet, Route("/[controller]/[action]"), SwaggerOperation("开始录制")] + public ApiResponse StartRecord([SwaggerParameter("设备编号")]string number) + { + return this.AsyncAction(() => + { + this._deviceService.SetRecordStart(number, true); + }); + } + + [HttpGet, Route("/[controller]/[action]"), SwaggerOperation("停止录制")] + public ApiResponse StopRecord([SwaggerParameter("设备编号")]string number) + { + return this.AsyncAction(() => + { + this._deviceService.SetRecordStart(number, false); }); } } diff --git a/projects/IoTNode/DeviceServices/Onvif/OnvifService.cs b/projects/IoTNode/DeviceServices/Onvif/OnvifService.cs index 560afe85..8bf2d21c 100644 --- a/projects/IoTNode/DeviceServices/Onvif/OnvifService.cs +++ b/projects/IoTNode/DeviceServices/Onvif/OnvifService.cs @@ -568,7 +568,7 @@ namespace IoTNode.DeviceServices.Onvif return hc.GetByteDigest(new Uri(url), camera.UserName, camera.Password); } - public void StartPush(string number) + public void SetPush(string number, bool state) { using (var scope = _applicationServices.CreateScope()) { @@ -579,15 +579,22 @@ namespace IoTNode.DeviceServices.Onvif var data = device.Data.FirstOrDefault(o => o.Key == "Push"); if (data != null) { - data.Value = "是"; + data.Value = state ? "是" : "否"; this.UpdateData(repo, device, data); - this.StartPushToServer(device); + if (state) + { + this.StartPushToServer(device); + } + else + { + this.StopPushToServer(device.Number); + } } } } } - public void StopPush(string number) + public void SetRecordStart(string number, bool state) { using (var scope = _applicationServices.CreateScope()) { @@ -595,12 +602,12 @@ namespace IoTNode.DeviceServices.Onvif var device = repo.Table().Include(o => o.Data).FirstOrDefault(o => o.Number == number); if (device != null) { - var data = device.Data.FirstOrDefault(o => o.Key == "Push"); + var data = device.Data.FirstOrDefault(o => o.Key == "Record"); if (data != null) { - data.Value = "否"; - this.UpdateData(repo, device, data); - this.StopPushToServer(device.Number); + data.Value = state ? "是" : "否"; + //this.UpdateData(repo, device, data); + this.SendToServer(Methods.UpdateDvr, data); } } }