CameraDataController.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. namespace NoName_CarplateRecognizeCamera_Lab.Controllers
  9. {
  10. [Route("api/[controller]")]
  11. public class CameraDataController : Controller
  12. {
  13. static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("Application");
  14. public static Action<SimplaCarPlateRecognizeResult> OnCarPlateRead;
  15. [HttpPost]
  16. public async Task<IActionResult> Post([FromBody]SimplaCarPlateRecognizeResult value)
  17. {
  18. try
  19. {
  20. if (value == null)
  21. {
  22. logger.Info("null or invalid car plate data");
  23. // for test purpose
  24. //OnCarPlateRead?.Invoke(new SimplaCarPlateRecognizeResult()
  25. //{
  26. // AlarmInfoPlate = new AlarmInfoPlate()
  27. // {
  28. // result = new result() { PlateResult = new PlateResult() { license = "test泰BD8734" } }
  29. // }
  30. //});
  31. }
  32. else
  33. {
  34. logger.Info("Read carPlate: " + value.AlarmInfoPlate.result.PlateResult.license);
  35. OnCarPlateRead?.Invoke(value);
  36. if (!string.IsNullOrEmpty(CameraApp.SmsServiceUrl))
  37. {
  38. logger.Info(" will send SMS");
  39. var formatedUrl = string.Format(CameraApp.SmsServiceUrl, value.AlarmInfoPlate.result.PlateResult.license);
  40. var webRequest = HttpWebRequest.Create(formatedUrl);
  41. var webResponse = (webRequest.GetResponse() as HttpWebResponse);
  42. logger.Info(" SMS service returned with Code: " + webResponse.StatusCode + ", ContentLen: " + webResponse.ContentLength);
  43. }
  44. }
  45. }
  46. catch (Exception exx)
  47. {
  48. logger.Error("CameraDataController Post(...) exceptioned: " + exx);
  49. }
  50. return Ok();
  51. }
  52. //var formatedUrl = "http://wc.shaojun.xyz:8585/api/shortMessage/carPlateNumber/" + HttpUtility.UrlEncode(recognized.AlarmInfoPlate.result.PlateResult.license) + "/phoneNumber/" + HttpUtility.UrlEncode("13817741205,13774352835");
  53. //var webRequest =
  54. // HttpWebRequest.Create(formatedUrl);
  55. //var webResponse = (webRequest.GetResponse() as HttpWebResponse);
  56. //Console.WriteLine(DateTime.Now.ToString("HH:mm:ss fff") + " remote service returned with HttpStatusCode: " + webResponse.StatusCode + ", Content - Len: " + webResponse.ContentLength);
  57. }
  58. }