SystemID.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. }
  116. public enum EVSE_InputConfig
  117. {
  118. Unknown, Y, D, L, S, W, T, E, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, AA
  119. }
  120. public enum EVSE_SafetyReg
  121. {
  122. Unknown,
  123. CE_Europoean,
  124. UL_NorthAmerica,
  125. GB_China,
  126. CNS_Taiwan,
  127. JARI_Japan,
  128. TR25_Singapor,
  129. KC_Korea,
  130. British_UK,
  131. EVready_French,
  132. M,
  133. P,
  134. I,
  135. F,
  136. L
  137. }
  138. public enum EVSE_network
  139. {
  140. None,
  141. Ethernet,
  142. Ble,
  143. EthWiFi,
  144. Eth4G,
  145. EthBle,
  146. EthWiFi4G,
  147. WiFi
  148. }
  149. public EVSE_TYPE Type { get; private set; }
  150. public EVSE_InputConfig InputConfig { get; private set; }
  151. public EVSE_SafetyReg SafetyReg { get; private set; }
  152. public double RatedPower { get; private set; }
  153. public ConnectorType[] ConnectorTypes { get; private set; }
  154. public EVSE_network Network { get; private set; }
  155. public int Generation { get; private set; }
  156. public string CustomCode { get; private set; }
  157. public EVSE_TYPE_CAT Type_Cat { get => GetTypeCat(); }
  158. private string _modelNameString;
  159. public string modelNameString
  160. {
  161. get => _modelNameString;
  162. set
  163. {
  164. if (TryParse(value, out var result))
  165. {
  166. this.Type = result.Type;
  167. this.InputConfig = result.InputConfig;
  168. this.SafetyReg = result.SafetyReg;
  169. this.RatedPower = result.RatedPower;
  170. this.ConnectorTypes = result.ConnectorTypes;
  171. this.Network = result.Network;
  172. this.Generation = result.Generation;
  173. this.CustomCode = result.CustomCode;
  174. _modelNameString = value;
  175. }
  176. }
  177. }
  178. public static bool TryParse(byte[] modeNameByte, out ModelName modelName)
  179. {
  180. modelName = null;
  181. string modelNameString;
  182. try
  183. {
  184. modelNameString = Encoding.ASCII.GetString(modeNameByte);
  185. }
  186. catch
  187. {
  188. return false;
  189. }
  190. if (modelNameString.Length != 14 && modelNameString.Length != 12)
  191. return false;
  192. return TryParse(modelNameString, out modelName);
  193. }
  194. public static bool TryParse(string modeNameString, out ModelName modelName)
  195. {
  196. modelName = null;
  197. ModelName model = new ModelName();
  198. if (modeNameString == null || modeNameString.Length != 14)
  199. return false;
  200. bool isAc = modeNameString[0] == 'A';
  201. var typeString = modeNameString.Substring(0, 2);
  202. switch (typeString)
  203. {
  204. case "AC":
  205. model.Type = EVSE_TYPE.AC_Cordset;
  206. break;
  207. case "AW":
  208. model.Type = EVSE_TYPE.AC_Wallmount;
  209. break;
  210. case "AP":
  211. model.Type = EVSE_TYPE.AC_Pedestal;
  212. break;
  213. case "DW":
  214. model.Type = EVSE_TYPE.DC_Wallmount;
  215. break;
  216. case "DS":
  217. model.Type = EVSE_TYPE.DC_Standalong;
  218. break;
  219. case "DM":
  220. model.Type = EVSE_TYPE.DC_Movable;
  221. break;
  222. case "DR":
  223. model.Type = EVSE_TYPE.DC_PowerRack_NoCabinet;
  224. break;
  225. case "DD":
  226. model.Type = EVSE_TYPE.DC_Disppenser;
  227. break;
  228. case "DO":
  229. model.Type = EVSE_TYPE.DC_OutputpowerCabinet;
  230. break;
  231. case "DQ":
  232. model.Type = EVSE_TYPE.DC_Infy_30KW_GB;
  233. break;
  234. case "DK":
  235. model.Type = EVSE_TYPE.DC_PowerRack;
  236. break;
  237. case "DX":
  238. model.Type = EVSE_TYPE.DC_OutputCommunicationBox;
  239. break;
  240. default:
  241. return false;
  242. }
  243. var inputConfigStr = modeNameString.Substring(2, 1);
  244. switch (inputConfigStr)
  245. {
  246. case "Y":
  247. model.InputConfig = EVSE_InputConfig.Y;
  248. break;
  249. case "D":
  250. model.InputConfig = EVSE_InputConfig.D;
  251. break;
  252. case "L":
  253. model.InputConfig = EVSE_InputConfig.L;
  254. break;
  255. case "S":
  256. model.InputConfig = EVSE_InputConfig.S;
  257. break;
  258. case "W":
  259. model.InputConfig = EVSE_InputConfig.W;
  260. break;
  261. case "T":
  262. model.InputConfig = EVSE_InputConfig.T;
  263. break;
  264. case "E":
  265. model.InputConfig = EVSE_InputConfig.E;
  266. break;
  267. case "0":
  268. model.InputConfig = EVSE_InputConfig.A0;
  269. break;
  270. case "1":
  271. model.InputConfig = EVSE_InputConfig.A1;
  272. break;
  273. case "2":
  274. model.InputConfig = EVSE_InputConfig.A2;
  275. break;
  276. case "3":
  277. model.InputConfig = EVSE_InputConfig.A3;
  278. break;
  279. case "4":
  280. model.InputConfig = EVSE_InputConfig.A4;
  281. break;
  282. case "5":
  283. model.InputConfig = EVSE_InputConfig.A5;
  284. break;
  285. case "6":
  286. model.InputConfig = EVSE_InputConfig.A6;
  287. break;
  288. case "7":
  289. model.InputConfig = EVSE_InputConfig.A7;
  290. break;
  291. case "8":
  292. model.InputConfig = EVSE_InputConfig.A8;
  293. break;
  294. case "9":
  295. model.InputConfig = EVSE_InputConfig.A9;
  296. break;
  297. case "A":
  298. model.InputConfig = EVSE_InputConfig.AA;
  299. break;
  300. default:
  301. return false;
  302. }
  303. var safeRegStr = modeNameString.Substring(3, 1);
  304. switch (safeRegStr)
  305. {
  306. case "E":
  307. model.SafetyReg = EVSE_SafetyReg.CE_Europoean;
  308. break;
  309. case "U":
  310. model.SafetyReg = EVSE_SafetyReg.UL_NorthAmerica;
  311. break;
  312. case "G":
  313. model.SafetyReg = EVSE_SafetyReg.GB_China;
  314. break;
  315. case "C":
  316. model.SafetyReg = EVSE_SafetyReg.CNS_Taiwan;
  317. break;
  318. case "J":
  319. model.SafetyReg = EVSE_SafetyReg.JARI_Japan;
  320. break;
  321. case "T":
  322. model.SafetyReg = EVSE_SafetyReg.TR25_Singapor;
  323. break;
  324. case "K":
  325. model.SafetyReg = EVSE_SafetyReg.KC_Korea;
  326. break;
  327. case "B":
  328. model.SafetyReg = EVSE_SafetyReg.British_UK;
  329. break;
  330. case "Z":
  331. model.SafetyReg = EVSE_SafetyReg.EVready_French;
  332. break;
  333. case "M":
  334. model.SafetyReg = EVSE_SafetyReg.M;
  335. break;
  336. case "P":
  337. model.SafetyReg = EVSE_SafetyReg.P;
  338. break;
  339. case "I":
  340. model.SafetyReg = EVSE_SafetyReg.I;
  341. break;
  342. case "F":
  343. model.SafetyReg = EVSE_SafetyReg.F;
  344. break;
  345. case "L":
  346. model.SafetyReg = EVSE_SafetyReg.L;
  347. break;
  348. default:
  349. return false;
  350. }
  351. var ratedPowerStr = modeNameString.Substring(4, 3);
  352. if (!int.TryParse(ratedPowerStr, out int ratedPowerNum) &&
  353. ratedPowerNum > 0)
  354. return false;
  355. double realnum = (double)ratedPowerNum / 10;
  356. var exp = ratedPowerNum % 10;
  357. model.RatedPower = (double)(realnum * (10 ^ exp));
  358. model.ConnectorTypes = new ConnectorType[3];
  359. var connectorListStr = modeNameString.Substring(7, 3);
  360. /*
  361. model.ConnectorTypes.Add(ConnectorType.CHadeMO);
  362. model.ConnectorTypes.Add(ConnectorType.CCS);
  363. model.ConnectorTypes.Add(ConnectorType.IEC);
  364. */
  365. for (int index = 0; index < 3; index++)
  366. {
  367. var connectorStr = connectorListStr.Substring(index, 1);
  368. switch (connectorStr)
  369. {
  370. case "0":
  371. //none connecter
  372. model.ConnectorTypes[index] = ConnectorType.None;
  373. break;
  374. case "1":
  375. case "2":
  376. case "3":
  377. case "4":
  378. case "5":
  379. case "6":
  380. case "7":
  381. case "8":
  382. model.ConnectorTypes[index] = ConnectorType.AC;
  383. break;
  384. case "J":
  385. case "K":
  386. model.ConnectorTypes[index] = ConnectorType.CHadeMO;
  387. break;
  388. case "U":
  389. case "V":
  390. case "T":
  391. case "N":
  392. case "R":
  393. model.ConnectorTypes[index] = ConnectorType.CCS1;
  394. break;
  395. case "E":
  396. case "F":
  397. case "D":
  398. case "M":
  399. case "P":
  400. model.ConnectorTypes[index] = ConnectorType.CCS2;
  401. break;
  402. case "G":
  403. model.ConnectorTypes[index] = ConnectorType.GBT;
  404. break;
  405. case "C":
  406. model.ConnectorTypes[index] = ConnectorType.ChaoJi;
  407. break;
  408. case "W":
  409. model.ConnectorTypes[index] = ConnectorType.Wireless;
  410. break;
  411. default:
  412. return false;
  413. }
  414. }
  415. var networkOpStr = modeNameString.Substring(10, 1);
  416. switch (networkOpStr)
  417. {
  418. case "0":
  419. model.Network = EVSE_network.None;
  420. break;
  421. case "E":
  422. model.Network = EVSE_network.Ethernet;
  423. break;
  424. case "B":
  425. model.Network = EVSE_network.Ble;
  426. break;
  427. case "W":
  428. model.Network = EVSE_network.EthWiFi;
  429. break;
  430. case "T":
  431. model.Network = EVSE_network.Eth4G;
  432. break;
  433. case "U":
  434. model.Network = EVSE_network.EthBle;
  435. break;
  436. case "D":
  437. case "A":
  438. model.Network = EVSE_network.EthWiFi4G;
  439. break;
  440. case "F":
  441. model.Network = EVSE_network.WiFi;
  442. break;
  443. default:
  444. return false;
  445. }
  446. var genStr = modeNameString.Substring(11, 1);
  447. if (!int.TryParse(genStr, out var genInt))
  448. return false;
  449. model.Generation = genInt;
  450. model.CustomCode = modeNameString.Substring(12, 2);
  451. model._modelNameString = modeNameString;
  452. modelName = model;
  453. return true;
  454. }
  455. public ModelName() { }
  456. public override string ToString()
  457. {
  458. return modelNameString;
  459. }
  460. private EVSE_TYPE_CAT GetTypeCat()
  461. {
  462. string typeString = Type.ToString();
  463. if (typeString.Contains("AC"))
  464. return EVSE_TYPE_CAT.AC;
  465. else if (typeString.Contains("DC"))
  466. return EVSE_TYPE_CAT.DC;
  467. return EVSE_TYPE_CAT.Hybrid;
  468. }
  469. }
  470. }