VmcCameraController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using log4net;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Ropin.Inspection.Api.Common;
  6. using Ropin.Inspection.Model.SearchModel.VMC;
  7. using Ropin.Inspection.Model.ViewModel.VMC;
  8. using Ropin.Inspection.Model;
  9. using Ropin.Inspection.Service.VMC.Interface;
  10. using System.Threading.Tasks;
  11. using System;
  12. using Ropin.Inspection.Model.Entities;
  13. using Ropin.Inspection.Common.Helper;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using Ropin.Inspection.Model.Common;
  17. namespace Ropin.Inspection.Api.Controllers.VMC
  18. {
  19. public class VmcCameraController : BaseController
  20. {
  21. private readonly IVmcCameraService _repository;
  22. private static readonly ILog log = LogManager.GetLogger(typeof(VmcCameraController));
  23. /// <summary>
  24. /// 构造函数
  25. /// </summary>
  26. /// <param name="repository"></param>
  27. public VmcCameraController(IVmcCameraService repository)
  28. {
  29. _repository = repository;
  30. }
  31. /// <summary>
  32. /// 新增
  33. /// </summary>
  34. /// <param name="content"></param>
  35. /// <returns></returns>
  36. [HttpPost("CreateAsync")]
  37. public async Task<ApiResult> CreateAsync(VmcCameraViewModel content)
  38. {
  39. if (content == null)
  40. {
  41. return new ApiResult(ReturnCode.ArgsError);
  42. }
  43. try
  44. {
  45. await _repository.CreateOneAsync(content);
  46. }
  47. catch (Exception ex)
  48. {
  49. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  50. }
  51. return new ApiResult(ReturnCode.Success);
  52. }
  53. /// <summary>
  54. /// 删除
  55. /// </summary>
  56. /// <param name="id"></param>
  57. /// <returns></returns>
  58. [HttpDelete("DeleteAsync/{id}")]
  59. public async Task<ApiResult> DeleteAsync(string id)
  60. {
  61. if (string.IsNullOrEmpty(id))
  62. {
  63. return new ApiResult(ReturnCode.GeneralError);
  64. }
  65. try
  66. {
  67. await _repository.DeleteAsync(id);
  68. }
  69. catch (Exception ex)
  70. {
  71. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  72. }
  73. return new ApiResult(ReturnCode.Success);
  74. }
  75. /// <summary>
  76. /// 禁用
  77. /// </summary>
  78. /// <param name="id"></param>
  79. /// <returns></returns>
  80. [HttpPut("ForbiddenAsync/{id}")]
  81. public async Task<ApiResult> ForbiddenAsync(string id)
  82. {
  83. if (string.IsNullOrEmpty(id))
  84. {
  85. return new ApiResult(ReturnCode.GeneralError);
  86. }
  87. try
  88. {
  89. await _repository.ForbiddenAsync(id);
  90. }
  91. catch (Exception ex)
  92. {
  93. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  94. }
  95. return new ApiResult(ReturnCode.Success);
  96. }
  97. /// <summary>
  98. /// 更新
  99. /// </summary>
  100. /// <param name="id"></param>
  101. /// <param name="updateModel"></param>
  102. /// <returns></returns>
  103. [HttpPut("UpdateAsync/{id}")]
  104. [AllowAnonymous]
  105. public async Task<ApiResult> UpdateAsync(string id, VmcCameraViewModel updateModel)
  106. {
  107. if (string.IsNullOrEmpty(id))
  108. {
  109. return new ApiResult(ReturnCode.GeneralError);
  110. }
  111. try
  112. {
  113. await _repository.UpdateAsync(updateModel, id);
  114. }
  115. catch (Exception ex)
  116. {
  117. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  118. }
  119. return new ApiResult(ReturnCode.Success);
  120. }
  121. /// <summary>
  122. /// 获取列表
  123. /// </summary>
  124. /// <returns></returns>
  125. [HttpPost("PageAsync")]
  126. [AllowAnonymous]
  127. public async Task<ApiResult> PageAsync(VmcCameraSearch searchModel)
  128. {
  129. if (searchModel == null)
  130. {
  131. return new ApiResult(ReturnCode.ArgsError);
  132. }
  133. try
  134. {
  135. var contentList = await _repository.GetConditionAsync(searchModel);
  136. if (contentList != null&&contentList.Count()>0)
  137. {
  138. DeviceChanneModel channeModel = await LiveGBSHelper.GetDeviceChannellist();
  139. if (channeModel != null && channeModel.ChannelList.Count > 0)
  140. {
  141. List<ChanneModel> ChannelList = channeModel.ChannelList;
  142. foreach (var item in contentList)
  143. {
  144. if (item != null)
  145. {
  146. ChanneModel channe = ChannelList.Find(c => c.DeviceID == item.C_Serial && c.ID == item.C_CameraCode);
  147. if (channe != null)
  148. {
  149. string status = channe.Status == "ON" ? "1" : "2";
  150. if (status != item.C_RunStatus)
  151. {
  152. item.C_RunStatus = status;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. return new ApiResult<PagesModel<VmcCameraViewModel>>(new PagesModel<VmcCameraViewModel>(contentList, searchModel));
  160. }
  161. catch (Exception ex)
  162. {
  163. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  164. }
  165. }
  166. /// <summary>
  167. /// 通过id获取详情
  168. /// </summary>
  169. /// <returns></returns>
  170. [HttpGet("DetailsAsync")]
  171. [AllowAnonymous]
  172. public async Task<ApiResult> DetailsAsync(string Id)
  173. {
  174. if (string.IsNullOrEmpty(Id))
  175. {
  176. return new ApiResult(ReturnCode.ArgsError);
  177. }
  178. try
  179. {
  180. VmcCameraViewModel data = await _repository.GetByIdAsync(Id);
  181. return new ApiResult<VmcCameraViewModel>(data);
  182. }
  183. catch (Exception ex)
  184. {
  185. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  186. }
  187. }
  188. /// <summary>
  189. /// 新增业主摄像头迁移
  190. /// </summary>
  191. /// <param name="content"></param>
  192. /// <returns></returns>
  193. [HttpPost("CreateCameraMigrateAsync")]
  194. public async Task<ApiResult> CreateCameraMigrateAsync(TVMC_CameraMigrate content)
  195. {
  196. if (content == null)
  197. {
  198. return new ApiResult(ReturnCode.ArgsError);
  199. }
  200. try
  201. {
  202. await _repository.CreateCameraMigrate(content);
  203. }
  204. catch (Exception ex)
  205. {
  206. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  207. }
  208. return new ApiResult(ReturnCode.Success);
  209. }
  210. /// <summary>
  211. /// 获取业主摄像头迁移列表
  212. /// </summary>
  213. /// <returns></returns>
  214. [HttpPost("CameraMigratePageAsync")]
  215. [AllowAnonymous]
  216. public async Task<ApiResult> CameraMigratePageAsync(VmcCameraMigrateSearch searchModel)
  217. {
  218. if (searchModel == null)
  219. {
  220. return new ApiResult(ReturnCode.ArgsError);
  221. }
  222. try
  223. {
  224. var contentList = await _repository.GetCameraMigrateConditionAsync(searchModel);
  225. return new ApiResult<PagesModel<VmcCameraMigrateViewModel>>(new PagesModel<VmcCameraMigrateViewModel>(contentList, searchModel));
  226. }
  227. catch (Exception ex)
  228. {
  229. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  230. }
  231. }
  232. /// <summary>
  233. /// 新增设备摄像头关联-单实体
  234. /// </summary>
  235. /// <param name="content"></param>
  236. /// <returns></returns>
  237. [HttpPost("CreateDevCameraAsync")]
  238. public async Task<ApiResult> CreateDevCameraAsync(TVMC_DevCamera content)
  239. {
  240. if (content == null)
  241. {
  242. return new ApiResult(ReturnCode.ArgsError);
  243. }
  244. try
  245. {
  246. await _repository.CreateDevCamera(content);
  247. }
  248. catch (Exception ex)
  249. {
  250. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  251. }
  252. return new ApiResult(ReturnCode.Success);
  253. }
  254. /// <summary>
  255. /// 新增设备摄像头关联-列表
  256. /// </summary>
  257. /// <param name="content"></param>
  258. /// <returns></returns>
  259. [HttpPost("CreateDevCameraListAsync")]
  260. public async Task<ApiResult> CreateDevCameraListAsync(VmcDevCamera content)
  261. {
  262. if (content == null||content.CameraList == null|| content.CameraList.Count==0)
  263. {
  264. return new ApiResult(ReturnCode.ArgsError);
  265. }
  266. try
  267. {
  268. await _repository.CreateDevCameraList(content);
  269. }
  270. catch (Exception ex)
  271. {
  272. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  273. }
  274. return new ApiResult(ReturnCode.Success);
  275. }
  276. /// <summary>
  277. /// 获取设备摄像头列表
  278. /// </summary>
  279. /// <returns></returns>
  280. [HttpPost("DevCameraPageAsync")]
  281. [AllowAnonymous]
  282. public async Task<ApiResult> DevCameraPageAsync(VmcDevSearch searchModel)
  283. {
  284. if (searchModel == null)
  285. {
  286. return new ApiResult(ReturnCode.ArgsError);
  287. }
  288. try
  289. {
  290. var contentList = await _repository.GetDevCameraConditionAsync(searchModel);
  291. return new ApiResult<PagesModel<VmcDevCameraViewModel>>(new PagesModel<VmcDevCameraViewModel>(contentList, searchModel));
  292. }
  293. catch (Exception ex)
  294. {
  295. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  296. }
  297. }
  298. /// <summary>
  299. /// 获取LiveGBS登录token
  300. /// </summary>
  301. /// <returns></returns>
  302. [HttpGet("GetLiveFBSToken")]
  303. [AllowAnonymous]
  304. public async Task<ApiResult> GetLiveFBSToken()
  305. {
  306. try
  307. {
  308. GBSToken token = await LiveGBSHelper.GetToken();
  309. return new ApiResult<GBSToken>(token);
  310. }
  311. catch (Exception ex)
  312. {
  313. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  314. }
  315. }
  316. /// <summary>
  317. /// 获取LiveGBS列表
  318. /// </summary>
  319. /// <returns></returns>
  320. [HttpPost("GetLiveFBSListAsync")]
  321. [AllowAnonymous]
  322. public async Task<ApiResult> GetLiveFBSListAsync(TbdmCodeSearchModel searchModel)
  323. {
  324. if (searchModel == null)
  325. {
  326. return new ApiResult(ReturnCode.ArgsError);
  327. }
  328. try
  329. {
  330. if (!string.IsNullOrEmpty(searchModel.C_Value))
  331. {
  332. switch (searchModel.C_Code)
  333. {
  334. case "VIDEO_TYPE_001": LiveGBSHelper.loginUrl = searchModel.C_Value;break;
  335. default:
  336. break;
  337. }
  338. }
  339. //LiveGBS
  340. if (searchModel.C_Code== "VIDEO_TYPE_001")
  341. {
  342. List<LiveGBSModel> list = new List<LiveGBSModel>();
  343. DeviceChanneModel channeModel = await LiveGBSHelper.GetDeviceChannellist();
  344. if (channeModel != null && channeModel.ChannelList.Count > 0)
  345. {
  346. foreach (var item in channeModel.ChannelList)
  347. {
  348. LiveGBSModel model = new LiveGBSModel();
  349. model.ChannelID = item.ID;
  350. model.DeviceID = item.DeviceID;
  351. model.DeviceName = item.DeviceName;
  352. model.DeviceOnline = item.DeviceOnline;
  353. model.Channel = item.Channel;
  354. model.Name = item.Name;
  355. model.SnapURL = item.SnapURL;
  356. model.Model = item.Model;
  357. model.Status = item.Status;
  358. ChannelVideo channelVideo = await LiveGBSHelper.GeChannelVideo(item.DeviceID, item.ID);
  359. if (channelVideo != null)
  360. {
  361. model.StreamID = channelVideo.StreamID;
  362. model.SMSID = channelVideo.SMSID;
  363. if (!string.IsNullOrEmpty(channelVideo.FLV))
  364. {
  365. string flv = "/sms" + channelVideo.FLV?.Split("sms")[1];
  366. model.FLV = flv;
  367. }
  368. if (!string.IsNullOrEmpty(channelVideo.HLS))
  369. {
  370. string hls = "/sms" + channelVideo.HLS?.Split("sms")[1];
  371. model.HLS = hls;
  372. }
  373. model.RTMP = channelVideo.RTMP;
  374. //model.VSnapURL = channelVideo.SnapURL;
  375. model.WEBRTC = channelVideo.WEBRTC;
  376. model.WS_FLV = channelVideo.WS_FLV;
  377. }
  378. list.Add(model);
  379. }
  380. }
  381. return new ApiResult<List<LiveGBSModel>>(list);
  382. }
  383. else
  384. {
  385. return new ApiResult<object>(null);
  386. }
  387. }
  388. catch (Exception ex)
  389. {
  390. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  391. }
  392. }
  393. /// <summary>
  394. /// 前端设备录像(历史视频)
  395. /// </summary>
  396. /// <returns></returns>
  397. [HttpPost("GetLiveFBSHistoryAsync")]
  398. [AllowAnonymous]
  399. public async Task<ApiResult> GetLiveFBSHistoryAsync(VmcCameraSearch searchModel)
  400. {
  401. if (searchModel == null)
  402. {
  403. return new ApiResult(ReturnCode.ArgsError);
  404. }
  405. try
  406. {
  407. bool IsPage = searchModel.IsPagination;
  408. searchModel.IsPagination = false;
  409. var contentList = await _repository.GetConditionAsync(searchModel);
  410. if (contentList == null)
  411. {
  412. return new ApiResult<IEnumerable<RecordList>>(null);
  413. }
  414. List<RecordList> list = new List<RecordList>();
  415. foreach (var content in contentList)
  416. {
  417. //LiveGBS
  418. if (content.C_Type == "VIDEO_TYPE_001")
  419. {
  420. if (!string.IsNullOrEmpty(content.C_Serial) && !string.IsNullOrEmpty(content.C_CameraCode))
  421. {
  422. LiveGBSHelper.loginUrl = content.TypeValue;
  423. string startTime = searchModel.VideoStartData;
  424. string EndTime = searchModel.VideoEndData;
  425. if (string.IsNullOrEmpty(startTime))
  426. {
  427. startTime = DateTime.Now.AddHours(-1).ToString("yyy-MM-ddTHH:mm:ss");
  428. }
  429. if (string.IsNullOrEmpty(EndTime))
  430. {
  431. EndTime = DateTime.Now.ToString("yyy-MM-ddTHH:mm:ss");
  432. }
  433. playbackRecordList datas = await LiveGBSHelper.playbackRecordList(content.C_Serial, content.C_CameraCode, startTime, EndTime);
  434. if (datas != null && datas.RecordList != null && datas.RecordList.Count > 0)
  435. {
  436. datas.RecordList.ForEach(record =>
  437. {
  438. record.CameraName = content.C_Name;
  439. record.DeviceID = content.C_Serial;
  440. record.ChannelID = content.C_CameraCode;
  441. });
  442. list.AddRange(datas.RecordList);
  443. }
  444. }
  445. }
  446. }
  447. searchModel.TotalCount=list.Count;
  448. if (IsPage)
  449. {
  450. var pageLIst= list.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize);
  451. return new ApiResult<PagesModel<RecordList>>(new PagesModel<RecordList>(pageLIst, searchModel));
  452. }
  453. else
  454. {
  455. return new ApiResult<PagesModel<RecordList>>(new PagesModel<RecordList>(list, searchModel));
  456. }
  457. }
  458. catch (Exception ex)
  459. {
  460. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  461. }
  462. }
  463. /// <summary>
  464. /// 前端设备录像-视频回放
  465. /// </summary>
  466. /// <returns></returns>
  467. [HttpPost("LiveFBSPlaybackStart")]
  468. [AllowAnonymous]
  469. public async Task<ApiResult> LiveFBSPlaybackStart(LiveFBSSearch searchModel)
  470. {
  471. if (searchModel == null)
  472. {
  473. return new ApiResult(ReturnCode.ArgsError);
  474. }
  475. try
  476. {
  477. string startTime = searchModel.VideoStartData;
  478. string EndTime = searchModel.VideoEndData;
  479. ChannelVideo datas = await LiveGBSHelper.playbackStart(searchModel.C_Serial, searchModel.C_CameraCode, startTime, EndTime,searchModel.Download,searchModel.DownloadSpeed);
  480. if (!string.IsNullOrEmpty(datas.FLV))
  481. {
  482. string flv = "/sms" + datas.FLV?.Split("sms")[1];
  483. datas.FLV = flv;
  484. }
  485. if (!string.IsNullOrEmpty(datas.HLS))
  486. {
  487. string hls = "/sms" + datas.HLS?.Split("sms")[1];
  488. datas.HLS = hls;
  489. }
  490. return new ApiResult<ChannelVideo>(datas);
  491. }
  492. catch (Exception ex)
  493. {
  494. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  495. }
  496. }
  497. /// <summary>
  498. /// 前端设备录像-回放控制
  499. /// </summary>
  500. /// <returns></returns>
  501. [HttpPost("LiveFBSPlaybackControl")]
  502. [AllowAnonymous]
  503. public async Task<ApiResult> LiveFBSPlaybackControl(LiveGBSPlayControl searchModel)
  504. {
  505. if (searchModel == null)
  506. {
  507. return new ApiResult(ReturnCode.ArgsError);
  508. }
  509. try
  510. {
  511. string datas = await LiveGBSHelper.playbackControl(searchModel.StreamId, searchModel.Command,searchModel.Range, searchModel.Scale);
  512. return new ApiResult<object>(datas);
  513. }
  514. catch (Exception ex)
  515. {
  516. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  517. }
  518. }
  519. /// <summary>
  520. /// 前端设备录像- 视频回放流停止
  521. /// </summary>
  522. /// <returns></returns>
  523. [HttpPost("LiveFBSPlaybackStop")]
  524. [AllowAnonymous]
  525. public async Task<ApiResult> LiveFBSPlaybackStop(LiveFBSSearch searchModel)
  526. {
  527. if (searchModel == null)
  528. {
  529. return new ApiResult(ReturnCode.ArgsError);
  530. }
  531. try
  532. {
  533. playbackStopPlaybackFileURL datas = await LiveGBSHelper.playbackStop(searchModel.StreamId);
  534. if (datas!=null&&!string.IsNullOrEmpty(datas.PlaybackFileURL))
  535. {
  536. string hls = "/sms" + datas.PlaybackFileURL.Split("sms")[1];
  537. datas.PlaybackFileURL = hls;
  538. }
  539. return new ApiResult<playbackStopPlaybackFileURL>(datas);
  540. }
  541. catch (Exception ex)
  542. {
  543. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  544. }
  545. }
  546. /// <summary>
  547. /// 前端设备录像- 单条回放流信息
  548. /// </summary>
  549. /// <returns></returns>
  550. [HttpPost("LiveFBSPlaybackStreaminfo")]
  551. [AllowAnonymous]
  552. public async Task<ApiResult> LiveFBSPlaybackStreaminfo(LiveFBSSearch searchModel)
  553. {
  554. if (searchModel == null)
  555. {
  556. return new ApiResult(ReturnCode.ArgsError);
  557. }
  558. try
  559. {
  560. playbackStreaminfoEntity datas = await LiveGBSHelper.playbackStreaminfo(searchModel.StreamId);
  561. if (datas != null && !string.IsNullOrEmpty(datas.PlaybackFileURL))
  562. {
  563. string hls = "/sms" + datas.PlaybackFileURL.Split("sms")[1];
  564. datas.PlaybackFileURL = hls;
  565. }
  566. return new ApiResult<playbackStreaminfoEntity>(datas);
  567. }
  568. catch (Exception ex)
  569. {
  570. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  571. }
  572. }
  573. }
  574. }