SystemID.cs 16 KB

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