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.

39 lines
1.5 KiB

  1. using JT809.Protocol.Enums;
  2. using JT809.Protocol.Exceptions;
  3. using JT809.Protocol.Formatters;
  4. using JT809.Protocol.Interfaces;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. namespace JT809.Protocol
  10. {
  11. public static class JT809ConfigExtensions
  12. {
  13. private readonly static ConcurrentDictionary<string, JT809Serializer> jT809SerializerDict = new ConcurrentDictionary<string, JT809Serializer>(StringComparer.OrdinalIgnoreCase);
  14. public static object GetMessagePackFormatterByType(this IJT809Config jT809Config, Type type)
  15. {
  16. if (!jT809Config.FormatterFactory.FormatterDict.TryGetValue(type.GUID, out var formatter))
  17. {
  18. throw new JT809Exception(JT809ErrorCode.NotGlobalRegisterFormatterAssembly, type.FullName);
  19. }
  20. return formatter;
  21. }
  22. public static IJT809MessagePackFormatter<T> GetMessagePackFormatter<T>(this IJT809Config jT809Config)
  23. {
  24. return (IJT809MessagePackFormatter<T>)GetMessagePackFormatterByType(jT809Config, typeof(T));
  25. }
  26. public static JT809Serializer GetSerializer(this IJT809Config jT808Config)
  27. {
  28. if(!jT809SerializerDict.TryGetValue(jT808Config.ConfigId,out var serializer))
  29. {
  30. serializer = new JT809Serializer(jT808Config);
  31. jT809SerializerDict.TryAdd(jT808Config.ConfigId, serializer);
  32. }
  33. return serializer;
  34. }
  35. }
  36. }