平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

306 lines
11 KiB

  1. using MoYu.FriendlyException;
  2. using MoYu.RemoteRequest.Extensions;
  3. using Newtonsoft.Json.Linq;
  4. using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
  5. using SafeCampus.Application.Manager.DeepelephManager;
  6. using SafeCampus.Web.Core.Controllers.Application.Business.Dto;
  7. using System.Collections.Generic;
  8. namespace SafeCampus.Web.Core.Controllers.Application.Business;
  9. /// <summary>
  10. /// 设备管理接口
  11. /// </summary>
  12. [Route("/business/[controller]")]
  13. [ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 100,Tag = "摄像头设备流媒体")]
  14. public class DeviceApi:IDynamicApiController
  15. {
  16. private readonly IDeepelephManager _deepelephManager;
  17. private readonly ISimpleCacheService _simpleCacheService;
  18. public DeviceApi(IDeepelephManager deepelephManager, ISimpleCacheService simpleCacheService)
  19. {
  20. _deepelephManager = deepelephManager;
  21. _simpleCacheService = simpleCacheService;
  22. }
  23. /// <summary>
  24. /// 获取设备ip
  25. /// </summary>
  26. /// <returns></returns>
  27. public async Task<string> FieldValidNetwork()
  28. {
  29. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  30. var str = await $"{appSettings.SXAPIURL}/device/console/v1/device/field_valid_network"
  31. .SetBody(new { token = _deepelephManager.GetToken(), tenantCode=appSettings.TenantCode, poiId = appSettings.PoiId, })
  32. .SetContentType("application/json")
  33. .PostAsAsync<string>();
  34. var model = JsonConvert.DeserializeObject<JObject>(str);
  35. if ((bool)model["success"])
  36. {
  37. return model["data"]["ipv4"].ToString();
  38. }
  39. throw Oops.Oh(model["message"].ToString());
  40. }
  41. /// <summary>
  42. /// 分页查询摄像头列表
  43. /// </summary>
  44. /// <param name="search">查询条件</param>
  45. /// <returns></returns>
  46. [AllowAnonymous]
  47. public async Task<dynamic> Brief(BriefSearch search)
  48. {
  49. if (string.IsNullOrEmpty(search.sensorId)||string.IsNullOrEmpty(search.sensorName))
  50. {
  51. var data = _simpleCacheService.Get<JToken>(SafeCampusConst.DeviceList);
  52. if (data != null)
  53. {
  54. return new
  55. {
  56. pageNum = search.PageNum,
  57. pageSize = data["data"]["size"],
  58. total = data["data"]["total"],
  59. pages = data["data"]["pages"],
  60. list = data["data"]["items"]
  61. };
  62. }
  63. }
  64. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  65. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/page_query/brief"
  66. .SetBody(new
  67. {
  68. token = _deepelephManager.GetToken(),
  69. tenantCode = appSettings.TenantCode,
  70. poiId = appSettings.PoiId,
  71. search.sensorId,
  72. search.sensorName,
  73. pageNo = 1,
  74. pageSize=500,
  75. })
  76. .SetContentType("application/json")
  77. .PostAsAsync<string>();
  78. var model = JsonConvert.DeserializeObject<JObject>(str);
  79. if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
  80. if (model["data"]!=null)
  81. {
  82. if (!string.IsNullOrEmpty(search.sensorId) &&!string.IsNullOrEmpty(search.sensorName))
  83. {
  84. _simpleCacheService.Set(SafeCampusConst.DeviceList, model["data"]);
  85. }
  86. return new
  87. {
  88. pageNum = search.PageNum,
  89. pageSize = model["data"]["size"],
  90. total = model["data"]["total"],
  91. pages = model["data"]["pages"],
  92. list = model["data"]["items"]
  93. };
  94. }
  95. return new
  96. {
  97. pageNum = search.PageNum,
  98. pageSize =0,
  99. total = 0,
  100. pages = 0,
  101. list = new List<string>()
  102. };
  103. }
  104. /// <summary>
  105. /// 摄像头数据同步
  106. /// </summary>
  107. /// <returns></returns>
  108. [Obsolete]
  109. public async Task<bool> DataSync()
  110. {
  111. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  112. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/page_query/brief"
  113. .SetBody(new
  114. {
  115. token = _deepelephManager.GetToken(),
  116. tenantCode = appSettings.TenantCode,
  117. poiId = appSettings.PoiId,
  118. pageNo = 1,
  119. pageSize = 500,
  120. })
  121. .SetContentType("application/json")
  122. .PostAsAsync<string>();
  123. var model = JsonConvert.DeserializeObject<JObject>(str);
  124. if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
  125. _simpleCacheService.Set(SafeCampusConst.DeviceList, model["data"]);
  126. return true;
  127. }
  128. /// <summary>
  129. /// 根据id查询摄像头
  130. /// </summary>
  131. /// <param name="sensorId">摄像头id</param>
  132. /// <returns></returns>
  133. public async Task<dynamic> GetCameraById(string sensorId)
  134. {
  135. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  136. var str = await $"{appSettings.SXAPIURL}/api/v1/camera/get"
  137. .SetBody(new
  138. {
  139. token = _deepelephManager.GetToken(),
  140. tenantCode = appSettings.TenantCode,
  141. fieldId = appSettings.PoiId,
  142. sensorId,
  143. })
  144. .SetContentType("application/json")
  145. .PostAsAsync<string>();
  146. var model = JsonConvert.DeserializeObject<JObject>(str);
  147. if ((bool)model["success"])
  148. {
  149. return model["data"];
  150. }
  151. throw Oops.Oh(model["message"].ToString());
  152. }
  153. /// <summary>
  154. /// 直播流拉取
  155. /// 拉流的时候,选pullStreamUrls和rtsPullStreamUrls里面的链接,pushStreamUrls和h5rtsPullStreamUrls直接忽略。
  156. /// </summary>
  157. /// <param name="sensorId">摄像头id</param>
  158. /// <returns></returns>
  159. [AllowAnonymous]
  160. public async Task<dynamic> GetStartVideoLive(string sensorId)
  161. {
  162. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  163. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/start_video_live"
  164. .SetBody(new
  165. {
  166. token = _deepelephManager.GetToken(),
  167. tenantCode = appSettings.TenantCode,
  168. poiId = appSettings.PoiId,
  169. cameraId=sensorId,
  170. })
  171. .SetContentType("application/json")
  172. .PostAsAsync<string>();
  173. var model = JsonConvert.DeserializeObject<JObject>(str);
  174. if ((bool)model["success"])
  175. {
  176. return model["data"];
  177. }
  178. throw Oops.Oh(model["message"].ToString());
  179. }
  180. /// <summary>
  181. /// 直播流停止拉取
  182. /// </summary>
  183. /// <param name="sensorId">摄像头id</param>
  184. /// <param name="videoToken">视频token</param>
  185. /// <param name="streamId">流ID</param>
  186. /// <returns></returns>
  187. [AllowAnonymous]
  188. public async Task<dynamic> GetStopVideoLive(string sensorId,string videoToken,string streamId)
  189. {
  190. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  191. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/stop_video_live"
  192. .SetBody(new
  193. {
  194. token = _deepelephManager.GetToken(),
  195. tenantCode = appSettings.TenantCode,
  196. poiId = appSettings.PoiId,
  197. cameraId = sensorId,
  198. videoToken,
  199. streamId
  200. })
  201. .SetContentType("application/json")
  202. .PostAsAsync<string>();
  203. var model = JsonConvert.DeserializeObject<JObject>(str);
  204. if ((bool)model["success"])
  205. {
  206. return model["data"]["streamId"].ToString();
  207. }
  208. throw Oops.Oh(model["message"].ToString());
  209. }
  210. /// <summary>
  211. /// 录像流拉取
  212. /// !!!注意,录入摄像头时,需要填写存储设备相关信息,才能正常拉录像!!!
  213. /// </summary>
  214. /// <param name="sensorId">摄像头id</param>
  215. /// <param name="startTime">开始时间</param>
  216. /// <param name="endTime">结束时间</param>
  217. /// <returns></returns>
  218. public async Task<dynamic> GetStartVideoPlayback(string sensorId,DateTime startTime,DateTime endTime)
  219. {
  220. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  221. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/start_video_playback"
  222. .SetBody(new
  223. {
  224. token = _deepelephManager.GetToken(),
  225. tenantCode = appSettings.TenantCode,
  226. poiId = appSettings.PoiId,
  227. cameraId = sensorId,
  228. startTime=new DateTimeOffset(startTime).ToUnixTimeMilliseconds(),
  229. endTime=new DateTimeOffset(endTime).ToUnixTimeMilliseconds()
  230. })
  231. .SetContentType("application/json")
  232. .PostAsAsync<string>();
  233. var model = JsonConvert.DeserializeObject<JObject>(str);
  234. if ((bool)model["success"])
  235. {
  236. return model["data"];
  237. }
  238. throw Oops.Oh(model["message"].ToString());
  239. }
  240. /// <summary>
  241. /// 录像流停止
  242. /// </summary>
  243. /// <param name="sensorId">摄像头id</param>
  244. /// <param name="videoToken">视频token</param>
  245. /// <param name="streamId">流ID</param>
  246. /// <returns></returns>
  247. public async Task<dynamic> GetStopVideoPayback(string sensorId,string videoToken,string streamId)
  248. {
  249. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  250. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/stop_video_playback"
  251. .SetBody(new
  252. {
  253. token = _deepelephManager.GetToken(),
  254. tenantCode = appSettings.TenantCode,
  255. poiId = appSettings.PoiId,
  256. cameraId = sensorId,
  257. videoToken,
  258. streamId
  259. })
  260. .SetContentType("application/json")
  261. .PostAsAsync<string>();
  262. var model = JsonConvert.DeserializeObject<JObject>(str);
  263. if ((bool)model["success"])
  264. {
  265. return model["data"]["streamId"].ToString();
  266. }
  267. throw Oops.Oh(model["message"].ToString());
  268. }
  269. /// <summary>
  270. /// 摄像头快照获取
  271. /// </summary>
  272. /// <param name="sensorId">摄像头id不传入时,默认下发门店下所有摄像头</param>
  273. /// <returns></returns>
  274. public async Task<dynamic> GetTakeSnapshot(string sensorId)
  275. {
  276. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  277. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/take_snapshot"
  278. .SetBody(new
  279. {
  280. token = _deepelephManager.GetToken(),
  281. tenantCode = appSettings.TenantCode,
  282. poiId = appSettings.PoiId,
  283. cameraId = sensorId,
  284. })
  285. .SetContentType("application/json")
  286. .PostAsAsync<string>();
  287. var model = JsonConvert.DeserializeObject<JObject>(str);
  288. if ((bool)model["success"])
  289. {
  290. return model["data"];
  291. }
  292. throw Oops.Oh(model["message"].ToString());
  293. }
  294. }