SystemID.cs 15 KB

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