ModelName.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using PhihongEv.Lib.Convert;
  2. using PhihongEv.Lib.Model;
  3. using System;
  4. using System.Text;
  5. namespace PhihongEv.Lib
  6. {
  7. public class ModelName
  8. {
  9. public MajorType Type { get; private set; }
  10. public InputConfig InputConfig { get; private set; }
  11. public SafetyRegulation SafetyReg { get; private set; }
  12. public decimal RatedPower { get; private set; }
  13. public ConnectorType[] ConnectorTypes { get; private set; }
  14. public NetworkOption Network { get; private set; }
  15. public Generation Generation { get; private set; }
  16. public string CustomCode { get; private set; }
  17. public MajorTypeCategory Type_Cat { get => Type.ToCategory(); }
  18. private string _modelNameString;
  19. public string modelNameString
  20. {
  21. get => _modelNameString;
  22. set
  23. {
  24. if (TryParse(value, out var result))
  25. {
  26. this.Type = result.Type;
  27. this.InputConfig = result.InputConfig;
  28. this.SafetyReg = result.SafetyReg;
  29. this.RatedPower = result.RatedPower;
  30. this.ConnectorTypes = result.ConnectorTypes;
  31. this.Network = result.Network;
  32. this.Generation = result.Generation;
  33. this.CustomCode = result.CustomCode;
  34. _modelNameString = value;
  35. }
  36. }
  37. }
  38. public static bool TryParse(byte[] modeNameByte, out ModelName modelName)
  39. {
  40. modelName = null;
  41. string modelNameString;
  42. try
  43. {
  44. modelNameString = Encoding.UTF8.GetString(modeNameByte,0,modeNameByte.Length);
  45. }
  46. catch
  47. {
  48. return false;
  49. }
  50. if (modelNameString.Length != 14)
  51. return false;
  52. return TryParse(modelNameString, out modelName);
  53. }
  54. public static bool TryParse(string modeNameString, out ModelName modelName)
  55. {
  56. modelName = null;
  57. ModelName model = new ModelName();
  58. if (modeNameString == null || modeNameString.Length != 14)
  59. return false;
  60. var typeString = modeNameString.Substring(0, 2);
  61. model.Type = typeString.ToMajorType();
  62. if (model.Type == null)
  63. return false;
  64. var inputConfigChr = modeNameString.Substring(2,1);
  65. model.InputConfig = inputConfigChr.ToInputConfig();
  66. if (model.InputConfig == null)
  67. return false;
  68. var safeRegChr = modeNameString.Substring(3,1);
  69. model.SafetyReg = safeRegChr.ToSafetyRegulation();
  70. if (model.SafetyReg == null)
  71. return false;
  72. var ratedPowerStr = modeNameString.Substring(4, 3);
  73. model.RatedPower = GetRatedPowerFromStr(ratedPowerStr);
  74. model.ConnectorTypes = new ConnectorType[3];
  75. var connectorListStr = modeNameString.Substring(7, 3);
  76. /*
  77. model.ConnectorTypes.Add(ConnectorType.CHadeMO);
  78. model.ConnectorTypes.Add(ConnectorType.CCS);
  79. model.ConnectorTypes.Add(ConnectorType.IEC);
  80. */
  81. for (int index = 0; index < 3; index++)
  82. {
  83. var connectorStr = connectorListStr.Substring(index,1);
  84. model.ConnectorTypes[index] = connectorStr.ToConnectorType();
  85. if (model.ConnectorTypes[index] == null)
  86. return false;
  87. }
  88. var networkOpChr = modeNameString.Substring(10,1);
  89. model.Network = networkOpChr.ToNetworkOption();
  90. if (model.Network == null)
  91. {
  92. return false;
  93. }
  94. var genStr = modeNameString.Substring(11, 1);
  95. model.Generation = genStr.ToGeneration();
  96. if (model.Generation == null)
  97. {
  98. return false;
  99. }
  100. model.CustomCode = modeNameString.Substring(12, 2);
  101. model._modelNameString = modeNameString;
  102. modelName = model;
  103. return true;
  104. }
  105. public override string ToString()
  106. {
  107. return modelNameString;
  108. }
  109. public int GetConnectorViewIndex(int arrayIndex)
  110. {
  111. if (arrayIndex < 0 || arrayIndex > 3)
  112. {
  113. throw new Exception("connector Array index out of range");
  114. }
  115. //assume same index from start
  116. int viewIndex = arrayIndex;
  117. while (arrayIndex >= 0)
  118. {
  119. arrayIndex--;
  120. //for those none connector before index, view index - 1
  121. if (
  122. arrayIndex >= 0 &&
  123. ConnectorTypes[arrayIndex] == ConnectorType.None)
  124. {
  125. viewIndex--;
  126. }
  127. }
  128. return viewIndex;
  129. }
  130. public int GetConnectorArrayIndex(int viewIndex)
  131. {
  132. if (viewIndex < 0 || viewIndex > 3)
  133. {
  134. throw new Exception("connector Array index out of range");
  135. }
  136. int arrayIndex = 0;
  137. while (viewIndex >= 0)
  138. {
  139. //find next not non connector
  140. while (ConnectorTypes[arrayIndex] == ConnectorType.None)
  141. {
  142. arrayIndex++;
  143. }
  144. //find connector, match and find next
  145. viewIndex--;
  146. }
  147. return arrayIndex;
  148. }
  149. private static decimal GetRatedPowerFromStr(string fromStr)
  150. {
  151. if (!int.TryParse(fromStr, out int ratedPowerNum) &&
  152. ratedPowerNum > 0)
  153. return -1;
  154. decimal realnum = (decimal)ratedPowerNum / 10;
  155. var exp = ratedPowerNum % 10;
  156. return (decimal)(realnum * (10 ^ exp));
  157. }
  158. }
  159. }