SystemID.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AwInitilizer.Model
  7. {
  8. public enum ConnectorType
  9. {
  10. Unknown, None, CHadeMO, CCS1, CCS2, GBT, AC
  11. }
  12. public class SystemID
  13. {
  14. public ModelName ModelName { get; set; }
  15. public string SerialNumber { get; set; }
  16. public string sourceString { get; set; }
  17. public static bool TryParse(byte[] byteData, out SystemID connectInfo)
  18. {
  19. connectInfo = null;
  20. if (byteData == null || byteData.Length < 14)
  21. return false;
  22. string systemIDString = null;
  23. try
  24. {
  25. systemIDString = Encoding.ASCII.GetString(byteData);
  26. }
  27. catch
  28. {
  29. return false;
  30. }
  31. if (!string.IsNullOrEmpty(systemIDString))
  32. {
  33. return TryParse(systemIDString, out connectInfo);
  34. }
  35. return false;
  36. }
  37. public static bool TryParse(string byteString, out SystemID connectInfo)
  38. {
  39. connectInfo = null;
  40. SystemID tmp = new SystemID();
  41. tmp.sourceString = byteString;
  42. if (tmp.sourceString.Length < 14)
  43. return false;
  44. if (!ModelName.TryParse(tmp.sourceString.Substring(0, 14), out var model))
  45. return false;
  46. tmp.ModelName = model;
  47. tmp.SerialNumber = tmp.sourceString.Substring(14);
  48. connectInfo = tmp;
  49. return true;
  50. }
  51. public string ToWifiPwd()
  52. {
  53. return SerialNumber + ModelName.ToString();
  54. }
  55. public override string ToString()
  56. {
  57. return sourceString;
  58. }
  59. public override bool Equals(object obj)
  60. {
  61. return obj.ToString() == this.ToString();
  62. }
  63. public override int GetHashCode()
  64. {
  65. return this.ToString().GetHashCode();
  66. }
  67. public static bool operator ==(SystemID a, SystemID b)
  68. {
  69. string stra = null, strb = null;
  70. if (Object.Equals(a, null))
  71. stra = null;
  72. else
  73. stra = a.ToString();
  74. if (Object.Equals(b, null))
  75. strb = null;
  76. else
  77. strb = b.ToString();
  78. return string.Equals(a, b);
  79. }
  80. public static bool operator !=(SystemID a, SystemID b)
  81. {
  82. string stra = null, strb = null;
  83. if (Object.Equals(a, null))
  84. stra = null;
  85. else
  86. stra = a.ToString();
  87. if (Object.Equals(b, null))
  88. strb = null;
  89. else
  90. strb = b.ToString();
  91. return !string.Equals(a, b);
  92. }
  93. }
  94. public class ModelName
  95. {
  96. public enum EVSE_TYPE_CAT
  97. {
  98. Unknown,
  99. AC,
  100. DC,
  101. Hybrid
  102. }
  103. public enum EVSE_TYPE
  104. {
  105. Unknown,
  106. AC_Cordset,
  107. AC_Wallmount,
  108. AC_Pedestal,
  109. DC_Wallmount,
  110. DC_Standalong,
  111. DC_Movable,
  112. DC_Roadside,
  113. DC_Disppenser,
  114. DC_Outputpowercabinet
  115. }
  116. public enum EVSE_InputConfig
  117. {
  118. Y, D, L, S, W, T, E , N0, N1, N2, N3, N4, N5, N6, N7, N8, N9,
  119. }
  120. public enum EVSE_SafetyReg
  121. {
  122. Europoean,
  123. NorthAmerica,
  124. China,
  125. Taiwan,
  126. Japan,
  127. Singapor,
  128. Korea,
  129. UK,
  130. French,
  131. M,
  132. P,
  133. I,
  134. F,
  135. L
  136. }
  137. public enum EVSE_network
  138. {
  139. None,
  140. Ethernet,
  141. Ble,
  142. EthWiFi,
  143. Eth4G,
  144. EthBle,
  145. EthWiFi4G,
  146. }
  147. public EVSE_TYPE Type { get; private set; }
  148. public EVSE_InputConfig InputConfig { get; private set; }
  149. public EVSE_SafetyReg SafetyReg { get; private set; }
  150. public double RatedPower { get; private set; }
  151. public ConnectorType[] ConnectorTypes { get; private set; }
  152. public EVSE_network Network { get; private set; }
  153. public int Generation { get; private set; }
  154. public string CustomCode { get; private set; }
  155. public EVSE_TYPE_CAT Type_Cat { get => GetTypeCat(); }
  156. private string _modelNameString;
  157. public string modelNameString
  158. {
  159. get => _modelNameString;
  160. set
  161. {
  162. if (TryParse(value, out var result))
  163. {
  164. this.Type = result.Type;
  165. this.InputConfig = result.InputConfig;
  166. this.SafetyReg = result.SafetyReg;
  167. this.RatedPower = result.RatedPower;
  168. this.ConnectorTypes = result.ConnectorTypes;
  169. this.Network = result.Network;
  170. this.Generation = result.Generation;
  171. this.CustomCode = result.CustomCode;
  172. _modelNameString = value;
  173. }
  174. }
  175. }
  176. public static bool TryParse(byte[] modeNameByte, out ModelName modelName)
  177. {
  178. modelName = null;
  179. string modelNameString;
  180. try
  181. {
  182. modelNameString = Encoding.ASCII.GetString(modeNameByte);
  183. }
  184. catch
  185. {
  186. return false;
  187. }
  188. if (modelNameString.Length != 14 && modelNameString.Length != 12)
  189. return false;
  190. return TryParse(modelNameString, out modelName);
  191. }
  192. public static bool TryParse(string modeNameString, out ModelName modelName)
  193. {
  194. modelName = null;
  195. ModelName model = new ModelName();
  196. if (modeNameString == null || modeNameString.Length != 14)
  197. return false;
  198. bool isAc = modeNameString[0] == 'A';
  199. var typeString = modeNameString.Substring(0, 2);
  200. switch (typeString)
  201. {
  202. case "AC":
  203. model.Type = EVSE_TYPE.AC_Cordset;
  204. break;
  205. case "AW":
  206. model.Type = EVSE_TYPE.AC_Wallmount;
  207. break;
  208. case "AP":
  209. model.Type = EVSE_TYPE.AC_Pedestal;
  210. break;
  211. case "DW":
  212. model.Type = EVSE_TYPE.DC_Wallmount;
  213. break;
  214. case "DS":
  215. model.Type = EVSE_TYPE.DC_Standalong;
  216. break;
  217. case "DM":
  218. model.Type = EVSE_TYPE.DC_Movable;
  219. break;
  220. case "DR":
  221. model.Type = EVSE_TYPE.DC_Roadside;
  222. break;
  223. case "DD":
  224. model.Type = EVSE_TYPE.DC_Disppenser;
  225. break;
  226. case "DO":
  227. model.Type = EVSE_TYPE.DC_Outputpowercabinet;
  228. break;
  229. default:
  230. return false;
  231. }
  232. var inputConfigStr = modeNameString.Substring(2, 1);
  233. switch (inputConfigStr)
  234. {
  235. case "Y":
  236. model.InputConfig = EVSE_InputConfig.Y;
  237. break;
  238. case "D":
  239. model.InputConfig = EVSE_InputConfig.D;
  240. break;
  241. case "L":
  242. model.InputConfig = EVSE_InputConfig.L;
  243. break;
  244. case "S":
  245. model.InputConfig = EVSE_InputConfig.S;
  246. break;
  247. case "W":
  248. model.InputConfig = EVSE_InputConfig.W;
  249. break;
  250. case "T":
  251. model.InputConfig = EVSE_InputConfig.T;
  252. break;
  253. case "E":
  254. model.InputConfig = EVSE_InputConfig.E;
  255. break;
  256. case "0":
  257. model.InputConfig = EVSE_InputConfig.N0;
  258. break;
  259. case "1":
  260. model.InputConfig = EVSE_InputConfig.N1;
  261. break;
  262. case "2":
  263. model.InputConfig = EVSE_InputConfig.N2;
  264. break;
  265. case "3":
  266. model.InputConfig = EVSE_InputConfig.N3;
  267. break;
  268. case "4":
  269. model.InputConfig = EVSE_InputConfig.N4;
  270. break;
  271. case "5":
  272. model.InputConfig = EVSE_InputConfig.N5;
  273. break;
  274. case "6":
  275. model.InputConfig = EVSE_InputConfig.N6;
  276. break;
  277. case "7":
  278. model.InputConfig = EVSE_InputConfig.N7;
  279. break;
  280. case "8":
  281. model.InputConfig = EVSE_InputConfig.N8;
  282. break;
  283. case "9":
  284. model.InputConfig = EVSE_InputConfig.N9;
  285. break;
  286. default:
  287. return false;
  288. }
  289. var safeRegStr = modeNameString.Substring(3, 1);
  290. switch (safeRegStr)
  291. {
  292. case "E":
  293. model.SafetyReg = EVSE_SafetyReg.Europoean;
  294. break;
  295. case "U":
  296. model.SafetyReg = EVSE_SafetyReg.NorthAmerica;
  297. break;
  298. case "G":
  299. model.SafetyReg = EVSE_SafetyReg.China;
  300. break;
  301. case "C":
  302. model.SafetyReg = EVSE_SafetyReg.Taiwan;
  303. break;
  304. case "J":
  305. model.SafetyReg = EVSE_SafetyReg.Japan;
  306. break;
  307. case "T":
  308. model.SafetyReg = EVSE_SafetyReg.Singapor;
  309. break;
  310. case "K":
  311. model.SafetyReg = EVSE_SafetyReg.Korea;
  312. break;
  313. case "B":
  314. model.SafetyReg = EVSE_SafetyReg.UK;
  315. break;
  316. case "Z":
  317. model.SafetyReg = EVSE_SafetyReg.French;
  318. break;
  319. case "M":
  320. model.SafetyReg = EVSE_SafetyReg.M;
  321. break;
  322. case "P":
  323. model.SafetyReg = EVSE_SafetyReg.P;
  324. break;
  325. case "I":
  326. model.SafetyReg = EVSE_SafetyReg.I;
  327. break;
  328. case "F":
  329. model.SafetyReg = EVSE_SafetyReg.F;
  330. break;
  331. case "L":
  332. model.SafetyReg = EVSE_SafetyReg.L;
  333. break;
  334. default:
  335. return false;
  336. }
  337. var ratedPowerStr = modeNameString.Substring(4, 3);
  338. if (!int.TryParse(ratedPowerStr, out int ratedPowerNum) &&
  339. ratedPowerNum > 0)
  340. return false;
  341. double realnum = (double)ratedPowerNum / 10;
  342. var exp = ratedPowerNum % 10;
  343. model.RatedPower = (double)(realnum * (10 ^ exp));
  344. model.ConnectorTypes = new ConnectorType[3];
  345. var connectorListStr = modeNameString.Substring(7, 3);
  346. /*
  347. model.ConnectorTypes.Add(ConnectorType.CHadeMO);
  348. model.ConnectorTypes.Add(ConnectorType.CCS);
  349. model.ConnectorTypes.Add(ConnectorType.IEC);
  350. */
  351. for (int index = 0; index < 3; index++)
  352. {
  353. var connectorStr = connectorListStr.Substring(index, 1);
  354. switch (connectorStr)
  355. {
  356. case "0":
  357. //none connecter
  358. model.ConnectorTypes[index] = ConnectorType.None;
  359. break;
  360. case "1":
  361. case "2":
  362. case "3":
  363. case "4":
  364. case "5":
  365. case "6":
  366. model.ConnectorTypes[index] = ConnectorType.AC;
  367. break;
  368. case "J":
  369. model.ConnectorTypes[index] = ConnectorType.CHadeMO;
  370. break;
  371. case "U":
  372. case "V":
  373. model.ConnectorTypes[index] = ConnectorType.CCS1;
  374. break;
  375. case "E":
  376. case "F":
  377. model.ConnectorTypes[index] = ConnectorType.CCS2;
  378. break;
  379. case "G":
  380. model.ConnectorTypes[index] = ConnectorType.GBT;
  381. break;
  382. default:
  383. return false;
  384. }
  385. }
  386. var networkOpStr = modeNameString.Substring(10, 1);
  387. switch (networkOpStr)
  388. {
  389. case "0":
  390. model.Network = EVSE_network.None;
  391. break;
  392. case "E":
  393. model.Network = EVSE_network.Ethernet;
  394. break;
  395. case "B":
  396. model.Network = EVSE_network.Ble;
  397. break;
  398. case "W":
  399. model.Network = EVSE_network.EthWiFi;
  400. break;
  401. case "T":
  402. model.Network = EVSE_network.Eth4G;
  403. break;
  404. case "U":
  405. model.Network = EVSE_network.EthBle;
  406. break;
  407. case "D":
  408. case "A":
  409. model.Network = EVSE_network.EthWiFi4G;
  410. break;
  411. default:
  412. return false;
  413. }
  414. var genStr = modeNameString.Substring(11, 1);
  415. if (!int.TryParse(genStr, out var genInt))
  416. return false;
  417. model.Generation = genInt;
  418. model.CustomCode = modeNameString.Substring(12, 2);
  419. model._modelNameString = modeNameString;
  420. modelName = model;
  421. return true;
  422. }
  423. public ModelName() { }
  424. public override string ToString()
  425. {
  426. return modelNameString;
  427. }
  428. private EVSE_TYPE_CAT GetTypeCat()
  429. {
  430. string typeString = Type.ToString();
  431. if (typeString.Contains("AC"))
  432. return EVSE_TYPE_CAT.AC;
  433. else if (typeString.Contains("DC"))
  434. return EVSE_TYPE_CAT.DC;
  435. return EVSE_TYPE_CAT.Hybrid;
  436. }
  437. }
  438. }