|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using MoYu.FriendlyException;
- using MoYu.RemoteRequest.Extensions;
- using Newtonsoft.Json.Linq;
- using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
- using SafeCampus.Application.Manager.DeepelephManager;
- using SafeCampus.Web.Core.Controllers.Application.Business.Dto;
- using System.Collections.Generic;
-
- namespace SafeCampus.Web.Core.Controllers.Application.Business;
- /// <summary>
- /// 设备管理接口
- /// </summary>
- [Route("/business/[controller]")]
- [ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 100,Tag = "摄像头设备流媒体")]
- public class DeviceApi:IDynamicApiController
- {
- private readonly IDeepelephManager _deepelephManager;
- private readonly ISimpleCacheService _simpleCacheService;
-
- public DeviceApi(IDeepelephManager deepelephManager, ISimpleCacheService simpleCacheService)
- {
- _deepelephManager = deepelephManager;
- _simpleCacheService = simpleCacheService;
- }
-
- /// <summary>
- /// 获取设备ip
- /// </summary>
- /// <returns></returns>
- public async Task<string> FieldValidNetwork()
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/device/field_valid_network"
- .SetBody(new { token = _deepelephManager.GetToken(), tenantCode=appSettings.TenantCode, poiId = appSettings.PoiId, })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"]["ipv4"].ToString();
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- /// <summary>
- /// 分页查询摄像头列表
- /// </summary>
- /// <param name="search">查询条件</param>
- /// <returns></returns>
- [AllowAnonymous]
- public async Task<dynamic> Brief(BriefSearch search)
- {
- if (string.IsNullOrEmpty(search.sensorId)||string.IsNullOrEmpty(search.sensorName))
- {
- var data = _simpleCacheService.Get<JToken>(SafeCampusConst.DeviceList);
- if (data != null)
- {
- return new
- {
- pageNum = search.PageNum,
- pageSize = data["data"]["size"],
- total = data["data"]["total"],
- pages = data["data"]["pages"],
- list = data["data"]["items"]
- };
- }
- }
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/page_query/brief"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
- search.sensorId,
- search.sensorName,
- pageNo = 1,
- pageSize=500,
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
- if (model["data"]!=null)
- {
- if (!string.IsNullOrEmpty(search.sensorId) &&!string.IsNullOrEmpty(search.sensorName))
- {
- _simpleCacheService.Set(SafeCampusConst.DeviceList, model["data"]);
- }
- return new
- {
- pageNum = search.PageNum,
- pageSize = model["data"]["size"],
- total = model["data"]["total"],
- pages = model["data"]["pages"],
- list = model["data"]["items"]
- };
- }
- return new
- {
- pageNum = search.PageNum,
- pageSize =0,
- total = 0,
- pages = 0,
- list = new List<string>()
- };
-
- }
- /// <summary>
- /// 摄像头数据同步
- /// </summary>
- /// <returns></returns>
- [Obsolete]
- public async Task<bool> DataSync()
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/page_query/brief"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
-
- pageNo = 1,
- pageSize = 500,
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
- _simpleCacheService.Set(SafeCampusConst.DeviceList, model["data"]);
- return true;
- }
- /// <summary>
- /// 根据id查询摄像头
- /// </summary>
- /// <param name="sensorId">摄像头id</param>
- /// <returns></returns>
- public async Task<dynamic> GetCameraById(string sensorId)
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/api/v1/camera/get"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- fieldId = appSettings.PoiId,
- sensorId,
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"];
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- /// <summary>
- /// 直播流拉取
- /// 拉流的时候,选pullStreamUrls和rtsPullStreamUrls里面的链接,pushStreamUrls和h5rtsPullStreamUrls直接忽略。
- /// </summary>
- /// <param name="sensorId">摄像头id</param>
- /// <returns></returns>
- [AllowAnonymous]
- public async Task<dynamic> GetStartVideoLive(string sensorId)
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/start_video_live"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
- cameraId=sensorId,
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"];
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- /// <summary>
- /// 直播流停止拉取
- /// </summary>
- /// <param name="sensorId">摄像头id</param>
- /// <param name="videoToken">视频token</param>
- /// <param name="streamId">流ID</param>
- /// <returns></returns>
- [AllowAnonymous]
- public async Task<dynamic> GetStopVideoLive(string sensorId,string videoToken,string streamId)
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/stop_video_live"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
- cameraId = sensorId,
- videoToken,
- streamId
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"]["streamId"].ToString();
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- /// <summary>
- /// 录像流拉取
- /// !!!注意,录入摄像头时,需要填写存储设备相关信息,才能正常拉录像!!!
- /// </summary>
- /// <param name="sensorId">摄像头id</param>
- /// <param name="startTime">开始时间</param>
- /// <param name="endTime">结束时间</param>
- /// <returns></returns>
- public async Task<dynamic> GetStartVideoPlayback(string sensorId,DateTime startTime,DateTime endTime)
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/start_video_playback"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
- cameraId = sensorId,
- startTime=new DateTimeOffset(startTime).ToUnixTimeMilliseconds(),
- endTime=new DateTimeOffset(endTime).ToUnixTimeMilliseconds()
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"];
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- /// <summary>
- /// 录像流停止
- /// </summary>
- /// <param name="sensorId">摄像头id</param>
- /// <param name="videoToken">视频token</param>
- /// <param name="streamId">流ID</param>
- /// <returns></returns>
- public async Task<dynamic> GetStopVideoPayback(string sensorId,string videoToken,string streamId)
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/stop_video_playback"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
- cameraId = sensorId,
- videoToken,
- streamId
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"]["streamId"].ToString();
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- /// <summary>
- /// 摄像头快照获取
- /// </summary>
- /// <param name="sensorId">摄像头id不传入时,默认下发门店下所有摄像头</param>
- /// <returns></returns>
- public async Task<dynamic> GetTakeSnapshot(string sensorId)
- {
- var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
- var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/take_snapshot"
- .SetBody(new
- {
- token = _deepelephManager.GetToken(),
- tenantCode = appSettings.TenantCode,
- poiId = appSettings.PoiId,
- cameraId = sensorId,
- })
- .SetContentType("application/json")
- .PostAsAsync<string>();
- var model = JsonConvert.DeserializeObject<JObject>(str);
- if ((bool)model["success"])
- {
- return model["data"];
- }
-
- throw Oops.Oh(model["message"].ToString());
- }
- }
|