EvApi.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using AwInitilizer.Model;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace AwInitilizer.Assist
  10. {
  11. public class EvApiResult<T>
  12. {
  13. public T Result { get; set; }
  14. public string Response { get; set; }
  15. }
  16. public class EvApiWifiResult
  17. {
  18. public int Rssi { get; set; }
  19. public int ErrorCode { get; set; }
  20. }
  21. public static class EvApi
  22. {
  23. internal static async Task<EvApiResult<bool>> ChekCsuBootCompelete()
  24. {
  25. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  26. if (!getResult.IsSuccess ||
  27. string.IsNullOrEmpty(getResult.Msg))
  28. {
  29. return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
  30. }
  31. Regex rx = new Regex("(SystemStatus)\\\": ([0-9]*)");
  32. var matches = rx.Matches(getResult.Msg);
  33. if (matches.Count == 0)
  34. {
  35. return new EvApiResult<bool>()
  36. {
  37. Result = false,
  38. Response = getResult.Msg
  39. };
  40. }
  41. bool isAllPassed = true;
  42. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  43. {
  44. var match = matches[matchIndex];
  45. if (match.Groups.Count != 3)
  46. {
  47. isAllPassed = false;
  48. break;
  49. }
  50. else
  51. {
  52. if (match.Groups[2].Value != "1")
  53. {
  54. isAllPassed = false;
  55. break;
  56. }
  57. }
  58. }
  59. return new EvApiResult<bool>()
  60. {
  61. Result = isAllPassed,
  62. Response = getResult.Msg,
  63. };
  64. }
  65. internal static async Task<EvApiResult<bool>> CheckGetQueryAction()
  66. {
  67. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  68. if (!getResult.IsSuccess ||
  69. string.IsNullOrEmpty(getResult.Msg))
  70. {
  71. return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
  72. }
  73. return new EvApiResult<bool>()
  74. {
  75. Result = !string.IsNullOrEmpty(getResult.Msg),
  76. Response = getResult.Msg
  77. };
  78. }
  79. internal static async Task<EvApiResult<ButtonStatus>> GetButtonStatus()
  80. {
  81. var getResult = await EvHttpClient.GetButtonStatusString();
  82. var result = getResult.Msg;
  83. if (!getResult.IsSuccess)
  84. {
  85. return new EvApiResult<ButtonStatus>()
  86. {
  87. Result = null,
  88. Response = getResult.Msg
  89. };
  90. }
  91. if (!result.Contains("Button1") ||
  92. !result.Contains("Button2") ||
  93. !result.Contains("EmergencyButton"))
  94. {
  95. return new EvApiResult<ButtonStatus>()
  96. {
  97. Result = null,
  98. Response = result
  99. };
  100. }
  101. var values = JsonConvert.DeserializeObject<ButtonStatus>(result);
  102. return new EvApiResult<ButtonStatus>()
  103. {
  104. Result = values,
  105. Response = result,
  106. };
  107. }
  108. internal static async Task<EvApiResult<Dictionary<string, string>>> GetVersion()
  109. {
  110. var getResult = await EvHttpClient.GetQueryActionOpt1String();
  111. var toReturn = new Dictionary<string, string>();
  112. string result = getResult.Msg;
  113. if (!getResult.IsSuccess)
  114. {
  115. return new EvApiResult<Dictionary<string, string>>()
  116. {
  117. Result = toReturn,
  118. Response = getResult.Msg,
  119. };
  120. }
  121. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
  122. foreach (var pair in values)
  123. {
  124. if (pair.Value is string v)
  125. {
  126. toReturn.Add(pair.Key, v);
  127. }
  128. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  129. {
  130. try
  131. {
  132. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  133. for (int index = 0; index < versionList.Count; index++)
  134. {
  135. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  136. }
  137. }
  138. catch
  139. {
  140. }
  141. }
  142. }
  143. return new EvApiResult<Dictionary<string, string>>()
  144. {
  145. Result = toReturn,
  146. Response = result
  147. };
  148. }
  149. internal static async Task<EvApiResult<string>> GetTelcomModemImei()
  150. {
  151. var getResult = await EvHttpClient.GetQueryActionOpt3String();
  152. string result = getResult.Msg;
  153. if (!getResult.IsSuccess)
  154. {
  155. return new EvApiResult<string>()
  156. {
  157. Result = string.Empty,
  158. Response = getResult.Msg,
  159. };
  160. }
  161. Regex rx = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\"");
  162. var matches = rx.Matches(result);
  163. string imeiString = string.Empty;
  164. if (matches.Count != 0)
  165. {
  166. var match = matches[0];
  167. if (match.Groups.Count != 3)
  168. {
  169. imeiString = string.Empty;
  170. }
  171. else
  172. {
  173. if (match.Groups[2].Value is string imei)
  174. {
  175. imeiString = imei;
  176. }
  177. else
  178. {
  179. imeiString = "";
  180. }
  181. }
  182. }
  183. else
  184. {
  185. imeiString = "";
  186. }
  187. return new EvApiResult<string>()
  188. {
  189. Result = imeiString,
  190. Response = result
  191. };
  192. }
  193. internal static async Task<EvApiResult<EvApiWifiResult>> GetWifiRssi()
  194. {
  195. var getResult = await EvHttpClient.GetQueryActionOpt3String();
  196. string result = getResult.Msg;
  197. if (!getResult.IsSuccess)
  198. {
  199. return new EvApiResult<EvApiWifiResult>()
  200. {
  201. Result = new EvApiWifiResult()
  202. {
  203. Rssi = 0,
  204. ErrorCode = 0
  205. },
  206. Response = getResult.Msg,
  207. };
  208. }
  209. Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
  210. var matches_mode = rx_mode.Matches(result);
  211. int rssi = 0;
  212. int errorCode = -1;
  213. if (matches_mode.Count != 0)
  214. {
  215. var match = matches_mode[0];
  216. if (match.Groups.Count != 3)
  217. {
  218. errorCode = 0;
  219. }
  220. else
  221. {
  222. if (int.TryParse(match.Groups[2].Value, out var wifiMode))
  223. {
  224. if (wifiMode != 1)
  225. {
  226. errorCode = 1;
  227. }
  228. }
  229. }
  230. }
  231. else
  232. {
  233. errorCode = 2;
  234. }
  235. if (errorCode != -1)
  236. {
  237. return new EvApiResult<EvApiWifiResult>()
  238. {
  239. Result = new EvApiWifiResult()
  240. {
  241. Rssi = rssi,
  242. ErrorCode = errorCode
  243. },
  244. Response = result
  245. };
  246. }
  247. Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
  248. var matches = rx.Matches(result);
  249. if (matches.Count != 0)
  250. {
  251. var match = matches[0];
  252. if (match.Groups.Count != 3)
  253. {
  254. errorCode = 3;
  255. }
  256. else
  257. {
  258. if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
  259. {
  260. rssi = rssiSignal;
  261. }
  262. else
  263. {
  264. errorCode = 4;
  265. }
  266. }
  267. }
  268. else
  269. {
  270. errorCode = 5;
  271. }
  272. return new EvApiResult<EvApiWifiResult>()
  273. {
  274. Result = new EvApiWifiResult()
  275. {
  276. Rssi = rssi,
  277. ErrorCode = errorCode
  278. },
  279. Response = result
  280. };
  281. }
  282. internal static async Task<EvApiResult<Dictionary<int, string>>> GetConnectorStatus()
  283. {
  284. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  285. Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
  286. string result = getResult.Msg;
  287. if (!getResult.IsSuccess)
  288. {
  289. return new EvApiResult<Dictionary<int, string>>()
  290. {
  291. Result = connectorStatusPair,
  292. Response = getResult.Msg
  293. };
  294. }
  295. Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
  296. var matches = rx.Matches(result);
  297. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  298. {
  299. var match = matches[matchIndex];
  300. if (match.Groups.Count != 3)
  301. {
  302. //LogWriter.Log($"Connector {matchIndex} status string mismatched");
  303. return new EvApiResult<Dictionary<int, string>>()
  304. {
  305. Result = null,
  306. Response = result
  307. };
  308. //InfoLog += $"Connector {matchIndex} status string mismatched\n";
  309. //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
  310. }
  311. else
  312. {
  313. connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
  314. }
  315. }
  316. return new EvApiResult<Dictionary<int, string>>()
  317. {
  318. Result = connectorStatusPair,
  319. Response = result,
  320. };
  321. }
  322. internal static async Task<EvApiResult<bool>> FactorySet()
  323. {
  324. var getResult = await EvHttpClient.GetFactorySetResultString();
  325. var result = getResult.Msg;
  326. if (!getResult.IsSuccess)
  327. {
  328. return new EvApiResult<bool>()
  329. {
  330. Result = false,
  331. Response = getResult.Msg
  332. };
  333. }
  334. Regex rx = new Regex("(result)\":\"([a-zA-Z]*)\"");
  335. var matches = rx.Matches(result);
  336. if (matches.Count > 0 &&
  337. matches[0].Success &&
  338. matches[0].Groups.Count == 3 &&
  339. matches[0].Groups[2].Value.ToLower() == "success")
  340. {
  341. return new EvApiResult<bool>()
  342. {
  343. Result = true,
  344. Response = result
  345. };
  346. }
  347. return new EvApiResult<bool>()
  348. {
  349. Result = false,
  350. Response = result
  351. };
  352. }
  353. internal static async Task<EvApiResult<bool>> SignalUpdateFirmware()
  354. {
  355. var result = await EvHttpClient.GetSignalUpdateFirmwareResultString();
  356. if (!result.IsSuccess)
  357. {
  358. return new EvApiResult<bool>()
  359. {
  360. Result = false,
  361. Response = result.Msg
  362. };
  363. }
  364. return new EvApiResult<bool>()
  365. {
  366. Result = !string.IsNullOrEmpty(result.Msg),
  367. Response = result.Msg,
  368. };
  369. }
  370. /// <summary>
  371. /// Not Tested
  372. /// </summary>
  373. /// <param name="fileName"></param>
  374. /// <returns></returns>
  375. [Obsolete]
  376. internal static async Task<EvApiResult<bool>> Uploadfirmware(string fileName)
  377. {
  378. var result = await EvHttpClient.GetUploadfirmwareResultString(new List<string> { fileName });
  379. if (!result.IsSuccess)
  380. {
  381. return new EvApiResult<bool>()
  382. {
  383. Result = false,
  384. Response = result.Msg
  385. };
  386. }
  387. return new EvApiResult<bool>()
  388. {
  389. Result = !string.IsNullOrEmpty(result.Msg),
  390. Response = result.Msg,
  391. };
  392. }
  393. /// <summary>
  394. /// Not tested
  395. /// </summary>
  396. /// <param name="fileNames"></param>
  397. /// <returns></returns>
  398. [Obsolete]
  399. internal static async Task<EvApiResult<bool>> Uploadfirmware(List<string> fileNames)
  400. {
  401. var result = await EvHttpClient.GetUploadfirmwareResultString(fileNames);
  402. if (!result.IsSuccess)
  403. {
  404. return new EvApiResult<bool>()
  405. {
  406. Result = false,
  407. Response = result.Msg
  408. };
  409. }
  410. return new EvApiResult<bool>()
  411. {
  412. Result = !string.IsNullOrEmpty(result.Msg),
  413. Response = result.Msg,
  414. };
  415. }
  416. }
  417. }