EvApi.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. using AwInitilizer.Model;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. namespace AwInitilizer.Assist
  12. {
  13. public class EvApiResult<T>
  14. {
  15. public T Result { get; set; }
  16. public string Response { get; set; }
  17. }
  18. public class EvApiWifiResult
  19. {
  20. public int Rssi { get; set; }
  21. public int ErrorCode { get; set; }
  22. }
  23. public static class EvApi
  24. {
  25. internal static async Task<EvApiResult<bool>> ChekCsuBootCompelete()
  26. {
  27. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  28. if (!getResult.IsSuccess ||
  29. string.IsNullOrEmpty(getResult.Msg))
  30. {
  31. return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
  32. }
  33. Regex rx = new Regex("(SystemStatus)\\\": ([0-9]*)");
  34. var matches = rx.Matches(getResult.Msg);
  35. if (matches.Count == 0)
  36. {
  37. return new EvApiResult<bool>()
  38. {
  39. Result = false,
  40. Response = getResult.Msg
  41. };
  42. }
  43. bool isAllPassed = true;
  44. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  45. {
  46. var match = matches[matchIndex];
  47. if (match.Groups.Count != 3)
  48. {
  49. isAllPassed = false;
  50. break;
  51. }
  52. else
  53. {
  54. if (match.Groups[2].Value != "1")
  55. {
  56. isAllPassed = false;
  57. break;
  58. }
  59. }
  60. }
  61. return new EvApiResult<bool>()
  62. {
  63. Result = isAllPassed,
  64. Response = getResult.Msg,
  65. };
  66. }
  67. internal static async Task<EvApiResult<bool>> CheckGetQueryAction()
  68. {
  69. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  70. if (!getResult.IsSuccess ||
  71. string.IsNullOrEmpty(getResult.Msg))
  72. {
  73. return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
  74. }
  75. return new EvApiResult<bool>()
  76. {
  77. Result = !string.IsNullOrEmpty(getResult.Msg),
  78. Response = getResult.Msg
  79. };
  80. }
  81. internal static async Task<EvApiResult<ButtonStatus>> GetButtonStatus()
  82. {
  83. var getResult = await EvHttpClient.GetButtonStatusString();
  84. var result = getResult.Msg;
  85. if (!getResult.IsSuccess)
  86. {
  87. return new EvApiResult<ButtonStatus>()
  88. {
  89. Result = null,
  90. Response = getResult.Msg
  91. };
  92. }
  93. if (!result.Contains("Button1") ||
  94. !result.Contains("Button2"))
  95. {
  96. return new EvApiResult<ButtonStatus>()
  97. {
  98. Result = null,
  99. Response = result
  100. };
  101. }
  102. var values = JsonConvert.DeserializeObject<ButtonStatus>(result);
  103. return new EvApiResult<ButtonStatus>()
  104. {
  105. Result = values,
  106. Response = result,
  107. };
  108. }
  109. internal static async Task<EvApiResult<Dictionary<string, string>>> GetVersion()
  110. {
  111. var getResult = await EvHttpClient.GetQueryActionOpt1String();
  112. var toReturn = new Dictionary<string, string>();
  113. string result = getResult.Msg;
  114. if (!getResult.IsSuccess)
  115. {
  116. return new EvApiResult<Dictionary<string, string>>()
  117. {
  118. Result = null,
  119. Response = getResult.Msg,
  120. };
  121. }
  122. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
  123. foreach (var pair in values)
  124. {
  125. if (pair.Value is string v)
  126. {
  127. toReturn.Add(pair.Key, v);
  128. }
  129. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  130. {
  131. try
  132. {
  133. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  134. for (int index = 0; index < versionList.Count; index++)
  135. {
  136. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  137. }
  138. }
  139. catch
  140. {
  141. }
  142. }
  143. }
  144. return new EvApiResult<Dictionary<string, string>>()
  145. {
  146. Result = toReturn,
  147. Response = result
  148. };
  149. }
  150. internal static async Task<EvApiResult<string>> GetTelcomModemImei()
  151. {
  152. var getResult = await EvHttpClient.GetQueryActionOpt3String();
  153. string result = getResult.Msg;
  154. if (!getResult.IsSuccess)
  155. {
  156. return new EvApiResult<string>()
  157. {
  158. Result = null,
  159. Response = getResult.Msg,
  160. };
  161. }
  162. Regex rx = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\"");
  163. var matches = rx.Matches(result);
  164. string imeiString = string.Empty;
  165. if (matches.Count != 0)
  166. {
  167. var match = matches[0];
  168. if (match.Groups.Count != 3)
  169. {
  170. imeiString = string.Empty;
  171. }
  172. else
  173. {
  174. if (match.Groups[2].Value is string imei)
  175. {
  176. imeiString = imei;
  177. }
  178. else
  179. {
  180. imeiString = "";
  181. }
  182. }
  183. }
  184. else
  185. {
  186. imeiString = "";
  187. }
  188. return new EvApiResult<string>()
  189. {
  190. Result = imeiString,
  191. Response = result
  192. };
  193. }
  194. internal static async Task<EvApiResult<EvApiWifiResult>> GetWifiRssi()
  195. {
  196. var getResult = await EvHttpClient.GetQueryActionOpt3String();
  197. string result = getResult.Msg;
  198. if (!getResult.IsSuccess)
  199. {
  200. return new EvApiResult<EvApiWifiResult>()
  201. {
  202. Result = new EvApiWifiResult()
  203. {
  204. Rssi = 0,
  205. ErrorCode = 0
  206. },
  207. Response = getResult.Msg,
  208. };
  209. }
  210. Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
  211. var matches_mode = rx_mode.Matches(result);
  212. int rssi = 0;
  213. int errorCode = -1;
  214. if (matches_mode.Count != 0)
  215. {
  216. var match = matches_mode[0];
  217. if (match.Groups.Count != 3)
  218. {
  219. errorCode = 0;
  220. }
  221. else
  222. {
  223. if (int.TryParse(match.Groups[2].Value, out var wifiMode))
  224. {
  225. if (wifiMode != 1)
  226. {
  227. errorCode = 1;
  228. }
  229. }
  230. }
  231. }
  232. else
  233. {
  234. errorCode = 2;
  235. }
  236. if (errorCode != -1)
  237. {
  238. return new EvApiResult<EvApiWifiResult>()
  239. {
  240. Result = new EvApiWifiResult()
  241. {
  242. Rssi = rssi,
  243. ErrorCode = errorCode
  244. },
  245. Response = result
  246. };
  247. }
  248. Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
  249. var matches = rx.Matches(result);
  250. if (matches.Count != 0)
  251. {
  252. var match = matches[0];
  253. if (match.Groups.Count != 3)
  254. {
  255. errorCode = 3;
  256. }
  257. else
  258. {
  259. if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
  260. {
  261. rssi = rssiSignal;
  262. }
  263. else
  264. {
  265. errorCode = 4;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. errorCode = 5;
  272. }
  273. return new EvApiResult<EvApiWifiResult>()
  274. {
  275. Result = new EvApiWifiResult()
  276. {
  277. Rssi = rssi,
  278. ErrorCode = errorCode
  279. },
  280. Response = result
  281. };
  282. }
  283. internal static async Task<EvApiResult<Dictionary<int, string>>> GetConnectorStatus()
  284. {
  285. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  286. Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
  287. string result = getResult.Msg;
  288. if (!getResult.IsSuccess)
  289. {
  290. return new EvApiResult<Dictionary<int, string>>()
  291. {
  292. Result = null,
  293. Response = getResult.Msg
  294. };
  295. }
  296. Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
  297. var matches = rx.Matches(result);
  298. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  299. {
  300. var match = matches[matchIndex];
  301. if (match.Groups.Count != 3)
  302. {
  303. //LogWriter.Log($"Connector {matchIndex} status string mismatched");
  304. return new EvApiResult<Dictionary<int, string>>()
  305. {
  306. Result = null,
  307. Response = result
  308. };
  309. //InfoLog += $"Connector {matchIndex} status string mismatched\n";
  310. //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
  311. }
  312. else
  313. {
  314. connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
  315. }
  316. }
  317. return new EvApiResult<Dictionary<int, string>>()
  318. {
  319. Result = connectorStatusPair,
  320. Response = result,
  321. };
  322. }
  323. internal static async Task<EvApiResult<bool>> FactorySet()
  324. {
  325. var getResult = await EvHttpClient.GetFactorySetResultString();
  326. var result = getResult.Msg;
  327. if (!getResult.IsSuccess)
  328. {
  329. return new EvApiResult<bool>()
  330. {
  331. Result = false,
  332. Response = getResult.Msg
  333. };
  334. }
  335. Regex rx = new Regex("(result)\":\"([a-zA-Z]*)\"");
  336. var matches = rx.Matches(result);
  337. if (matches.Count > 0 &&
  338. matches[0].Success &&
  339. matches[0].Groups.Count == 3 &&
  340. matches[0].Groups[2].Value.ToLower() == "success")
  341. {
  342. return new EvApiResult<bool>()
  343. {
  344. Result = true,
  345. Response = result
  346. };
  347. }
  348. return new EvApiResult<bool>()
  349. {
  350. Result = false,
  351. Response = result
  352. };
  353. }
  354. internal static async Task<EvApiResult<bool>> SignalUpdateFirmware()
  355. {
  356. var result = await EvHttpClient.GetSignalUpdateFirmwareResultString();
  357. if (!result.IsSuccess)
  358. {
  359. return new EvApiResult<bool>()
  360. {
  361. Result = false,
  362. Response = result.Msg
  363. };
  364. }
  365. return new EvApiResult<bool>()
  366. {
  367. Result = !string.IsNullOrEmpty(result.Msg),
  368. Response = result.Msg,
  369. };
  370. }
  371. internal static async Task<EvApiResult<bool>> SetAuthorisationMode(bool isAuthRequired)
  372. {
  373. var result = await EvHttpClient.SetSystemAction("AuthorisationMode", isAuthRequired ? "0" : "1");
  374. if (!result.IsSuccess)
  375. {
  376. return new EvApiResult<bool>()
  377. {
  378. Result = false,
  379. Response = result.Msg
  380. };
  381. }
  382. return new EvApiResult<bool>()
  383. {
  384. Result = !string.IsNullOrEmpty(result.Msg),
  385. Response = result.Msg,
  386. };
  387. }
  388. internal static async Task<EvApiResult<string>> GetTelcomModemFwRev()
  389. {
  390. var key = "TelcomModemFwRev";
  391. var result = await GetVersion();
  392. if (result.Result is null)
  393. {
  394. return new EvApiResult<string> { Result = null, Response = result.Response };
  395. }
  396. var versionPairs = result.Result;
  397. if (versionPairs is null || !versionPairs.ContainsKey(key))
  398. {
  399. return new EvApiResult<string> { Result = string.Empty, Response = result.Response };
  400. }
  401. return new EvApiResult<string> { Result = versionPairs[key], Response = result.Response };
  402. }
  403. internal static async Task<EvApiResult<string>> GetTelcomSubModemFwRev()
  404. {
  405. var key = "TelcomSubModemFwRev";
  406. var result = await GetVersion();
  407. if (result.Result is null)
  408. {
  409. return new EvApiResult<string> { Result = null, Response = result.Response };
  410. }
  411. var versionPairs = result.Result;
  412. if (versionPairs is null || !versionPairs.ContainsKey(key))
  413. {
  414. return new EvApiResult<string> { Result = string.Empty, Response = result.Response };
  415. }
  416. return new EvApiResult<string> { Result = versionPairs[key], Response = result.Response };
  417. }
  418. internal static async Task<EvApiResult<SimStatusString>> GetTelcomSimStatus()
  419. {
  420. var result = await EvHttpClient.GetQueryActionOpt3String();
  421. if (!result.IsSuccess)
  422. {
  423. return new EvApiResult<SimStatusString>()
  424. {
  425. Result = null,
  426. Response = result.Msg
  427. };
  428. }
  429. var simStatus = GetIntValue("TelcomSimStatus", result.Msg);
  430. var simIccid = GetStringValue("TelcomSimIccid", result.Msg);
  431. var simImsi = GetStringValue("TelcomSimImsi", result.Msg);
  432. return new EvApiResult<SimStatusString>()
  433. {
  434. Result = new SimStatusString() { IsInstalled = simStatus == 1, ICCID = simIccid, IMSI = simImsi },
  435. Response = result.Msg,
  436. };
  437. }
  438. internal static async Task<EvApiResult<SimStatusString>> GetTelcomSubSimStatus()
  439. {
  440. var result = await EvHttpClient.GetQueryActionOpt3String();
  441. if (!result.IsSuccess)
  442. {
  443. return new EvApiResult<SimStatusString>()
  444. {
  445. Result = null,
  446. Response = result.Msg
  447. };
  448. }
  449. var simStatus = GetIntValue("TelcomSubSimStatus", result.Msg);
  450. var simIccid = GetStringValue("TelcomSubSimIccid", result.Msg);
  451. var simImsi = GetStringValue("TelcomSubSimImsi", result.Msg);
  452. return new EvApiResult<SimStatusString>()
  453. {
  454. Result = new SimStatusString() { IsInstalled = simStatus == 1, ICCID = simIccid, IMSI = simImsi },
  455. Response = result.Msg,
  456. };
  457. }
  458. /// <summary>
  459. /// Not Tested
  460. /// </summary>
  461. /// <param name="fileName"></param>
  462. /// <returns></returns>
  463. [Obsolete]
  464. internal static async Task<EvApiResult<bool>> Uploadfirmware(string fileName)
  465. {
  466. var result = await EvHttpClient.GetUploadfirmwareResultString(new List<string> { fileName });
  467. if (!result.IsSuccess)
  468. {
  469. return new EvApiResult<bool>()
  470. {
  471. Result = false,
  472. Response = result.Msg
  473. };
  474. }
  475. return new EvApiResult<bool>()
  476. {
  477. Result = !string.IsNullOrEmpty(result.Msg),
  478. Response = result.Msg,
  479. };
  480. }
  481. /// <summary>
  482. /// Not tested
  483. /// </summary>
  484. /// <param name="fileNames"></param>
  485. /// <returns></returns>
  486. internal static async Task<EvApiResult<bool>> Uploadfirmware(List<string> fileNames)
  487. {
  488. var result = await EvHttpClient.GetUploadfirmwareResultString(fileNames);
  489. if (!result.IsSuccess)
  490. {
  491. return new EvApiResult<bool>()
  492. {
  493. Result = false,
  494. Response = result.Msg
  495. };
  496. }
  497. return new EvApiResult<bool>()
  498. {
  499. Result = !string.IsNullOrEmpty(result.Msg),
  500. Response = result.Msg,
  501. };
  502. }
  503. private static int? GetIntValue(string key, string source)
  504. {
  505. Regex rx = new Regex($"({key})\\\": ([0-9]*)");
  506. var matches = rx.Matches(source);
  507. if (matches.Count != 0)
  508. {
  509. var match = matches[0];
  510. if (match.Groups.Count != 3)
  511. {
  512. return null;
  513. }
  514. else
  515. {
  516. if (int.TryParse(match.Groups[2].Value, out var val))
  517. {
  518. return val;
  519. }
  520. else
  521. {
  522. return null;
  523. }
  524. }
  525. }
  526. return null;
  527. }
  528. private static string GetStringValue(string key, string source)
  529. {
  530. Regex rx = new Regex($"({key})\\\": \"([a-zA-Z0-9]*)\"");
  531. var matches = rx.Matches(source);
  532. if (matches.Count != 0)
  533. {
  534. var match = matches[0];
  535. if (match.Groups.Count != 3)
  536. {
  537. return null;
  538. }
  539. else
  540. {
  541. return match.Groups[2].Value;
  542. }
  543. }
  544. return null;
  545. }
  546. }
  547. }