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.
 
 
 

91 lines
2.6 KiB

  1. using JT808.Gateway.Abstractions.Dtos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json;
  6. namespace JT808.Gateway.Abstractions
  7. {
  8. public abstract class JT808MsgIdHttpHandlerBase
  9. {
  10. public Dictionary<string, Func<string,byte[]>> HandlerDict { get; }
  11. /// <summary>
  12. /// 初始化消息处理业务
  13. /// </summary>
  14. protected JT808MsgIdHttpHandlerBase()
  15. {
  16. HandlerDict = new Dictionary<string, Func<string, byte[]>>();
  17. }
  18. protected void CreateRoute(string url, Func<string, byte[]> func)
  19. {
  20. if (!HandlerDict.ContainsKey(url))
  21. {
  22. HandlerDict.Add(url, func);
  23. }
  24. else
  25. {
  26. // 替换
  27. HandlerDict[url] = func;
  28. }
  29. }
  30. protected byte[] CreateHttpResponse(dynamic dynamicObject)
  31. {
  32. byte[] data = JsonSerializer.SerializeToUtf8Bytes(dynamicObject);
  33. return data;
  34. }
  35. public byte[] DefaultHttpResponse()
  36. {
  37. byte[] json = JsonSerializer.SerializeToUtf8Bytes(new JT808DefaultResultDto());
  38. return json;
  39. }
  40. public byte[] EmptyHttpResponse()
  41. {
  42. byte[] json = JsonSerializer.SerializeToUtf8Bytes(new JT808ResultDto<string>()
  43. {
  44. Code = JT808ResultCode.Empty,
  45. Message = "内容为空",
  46. Data = "Content Empty"
  47. });
  48. return json;
  49. }
  50. public byte[] NotFoundHttpResponse()
  51. {
  52. byte[] json = JsonSerializer.SerializeToUtf8Bytes(new JT808ResultDto<string>()
  53. {
  54. Code = JT808ResultCode.NotFound,
  55. Message = "没有该服务",
  56. Data = "没有该服务"
  57. });
  58. return json;
  59. }
  60. public byte[] AuthFailHttpResponse()
  61. {
  62. byte[] json = JsonSerializer.SerializeToUtf8Bytes(new JT808ResultDto<string>()
  63. {
  64. Code = JT808ResultCode.AuthFail,
  65. Message = "token认证失败",
  66. Data = "token认证失败"
  67. });
  68. return json;
  69. }
  70. public byte[] ErrorHttpResponse(Exception ex)
  71. {
  72. byte[] json = JsonSerializer.SerializeToUtf8Bytes(new JT808ResultDto<string>()
  73. {
  74. Code = JT808ResultCode.Error,
  75. Message = ex.StackTrace,
  76. Data = ex.Message
  77. });
  78. return json;
  79. }
  80. }
  81. }