EvApi.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. {
  94. return new EvApiResult<ButtonStatus>()
  95. {
  96. Result = null,
  97. Response = result
  98. };
  99. }
  100. var values = JsonConvert.DeserializeObject<ButtonStatus>(result);
  101. return new EvApiResult<ButtonStatus>()
  102. {
  103. Result = values,
  104. Response = result,
  105. };
  106. }
  107. internal static async Task<EvApiResult<Dictionary<string, string>>> GetVersion()
  108. {
  109. var getResult = await EvHttpClient.GetQueryActionOpt1String();
  110. var toReturn = new Dictionary<string, string>();
  111. string result = getResult.Msg;
  112. if (!getResult.IsSuccess)
  113. {
  114. return new EvApiResult<Dictionary<string, string>>()
  115. {
  116. Result = null,
  117. Response = getResult.Msg,
  118. };
  119. }
  120. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
  121. foreach (var pair in values)
  122. {
  123. if (pair.Value is string v)
  124. {
  125. toReturn.Add(pair.Key, v);
  126. }
  127. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  128. {
  129. try
  130. {
  131. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  132. for (int index = 0; index < versionList.Count; index++)
  133. {
  134. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  135. }
  136. }
  137. catch
  138. {
  139. }
  140. }
  141. }
  142. return new EvApiResult<Dictionary<string, string>>()
  143. {
  144. Result = toReturn,
  145. Response = result
  146. };
  147. }
  148. internal static async Task<EvApiResult<string>> GetTelcomModemImei()
  149. {
  150. var getResult = await EvHttpClient.GetQueryActionOpt3String();
  151. string result = getResult.Msg;
  152. if (!getResult.IsSuccess)
  153. {
  154. return new EvApiResult<string>()
  155. {
  156. Result = null,
  157. Response = getResult.Msg,
  158. };
  159. }
  160. Regex rx = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\"");
  161. var matches = rx.Matches(result);
  162. string imeiString = string.Empty;
  163. if (matches.Count != 0)
  164. {
  165. var match = matches[0];
  166. if (match.Groups.Count != 3)
  167. {
  168. imeiString = string.Empty;
  169. }
  170. else
  171. {
  172. if (match.Groups[2].Value is string imei)
  173. {
  174. imeiString = imei;
  175. }
  176. else
  177. {
  178. imeiString = "";
  179. }
  180. }
  181. }
  182. else
  183. {
  184. imeiString = "";
  185. }
  186. return new EvApiResult<string>()
  187. {
  188. Result = imeiString,
  189. Response = result
  190. };
  191. }
  192. internal static async Task<EvApiResult<EvApiWifiResult>> GetWifiRssi()
  193. {
  194. var getResult = await EvHttpClient.GetQueryActionOpt3String();
  195. string result = getResult.Msg;
  196. if (!getResult.IsSuccess)
  197. {
  198. return new EvApiResult<EvApiWifiResult>()
  199. {
  200. Result = new EvApiWifiResult()
  201. {
  202. Rssi = 0,
  203. ErrorCode = 0
  204. },
  205. Response = getResult.Msg,
  206. };
  207. }
  208. Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
  209. var matches_mode = rx_mode.Matches(result);
  210. int rssi = 0;
  211. int errorCode = -1;
  212. if (matches_mode.Count != 0)
  213. {
  214. var match = matches_mode[0];
  215. if (match.Groups.Count != 3)
  216. {
  217. errorCode = 0;
  218. }
  219. else
  220. {
  221. if (int.TryParse(match.Groups[2].Value, out var wifiMode))
  222. {
  223. if (wifiMode != 1)
  224. {
  225. errorCode = 1;
  226. }
  227. }
  228. }
  229. }
  230. else
  231. {
  232. errorCode = 2;
  233. }
  234. if (errorCode != -1)
  235. {
  236. return new EvApiResult<EvApiWifiResult>()
  237. {
  238. Result = new EvApiWifiResult()
  239. {
  240. Rssi = rssi,
  241. ErrorCode = errorCode
  242. },
  243. Response = result
  244. };
  245. }
  246. Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
  247. var matches = rx.Matches(result);
  248. if (matches.Count != 0)
  249. {
  250. var match = matches[0];
  251. if (match.Groups.Count != 3)
  252. {
  253. errorCode = 3;
  254. }
  255. else
  256. {
  257. if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
  258. {
  259. rssi = rssiSignal;
  260. }
  261. else
  262. {
  263. errorCode = 4;
  264. }
  265. }
  266. }
  267. else
  268. {
  269. errorCode = 5;
  270. }
  271. return new EvApiResult<EvApiWifiResult>()
  272. {
  273. Result = new EvApiWifiResult()
  274. {
  275. Rssi = rssi,
  276. ErrorCode = errorCode
  277. },
  278. Response = result
  279. };
  280. }
  281. internal static async Task<EvApiResult<Dictionary<int, string>>> GetConnectorStatus()
  282. {
  283. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  284. Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
  285. string result = getResult.Msg;
  286. if (!getResult.IsSuccess)
  287. {
  288. return new EvApiResult<Dictionary<int, string>>()
  289. {
  290. Result = null,
  291. Response = getResult.Msg
  292. };
  293. }
  294. Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
  295. var matches = rx.Matches(result);
  296. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  297. {
  298. var match = matches[matchIndex];
  299. if (match.Groups.Count != 3)
  300. {
  301. //LogWriter.Log($"Connector {matchIndex} status string mismatched");
  302. return new EvApiResult<Dictionary<int, string>>()
  303. {
  304. Result = null,
  305. Response = result
  306. };
  307. //InfoLog += $"Connector {matchIndex} status string mismatched\n";
  308. //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
  309. }
  310. else
  311. {
  312. connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
  313. }
  314. }
  315. return new EvApiResult<Dictionary<int, string>>()
  316. {
  317. Result = connectorStatusPair,
  318. Response = result,
  319. };
  320. }
  321. internal static async Task<EvApiResult<bool>> FactorySet()
  322. {
  323. var getResult = await EvHttpClient.GetFactorySetResultString();
  324. var result = getResult.Msg;
  325. if (!getResult.IsSuccess)
  326. {
  327. return new EvApiResult<bool>()
  328. {
  329. Result = false,
  330. Response = getResult.Msg
  331. };
  332. }
  333. Regex rx = new Regex("(result)\":\"([a-zA-Z]*)\"");
  334. var matches = rx.Matches(result);
  335. if (matches.Count > 0 &&
  336. matches[0].Success &&
  337. matches[0].Groups.Count == 3 &&
  338. matches[0].Groups[2].Value.ToLower() == "success")
  339. {
  340. return new EvApiResult<bool>()
  341. {
  342. Result = true,
  343. Response = result
  344. };
  345. }
  346. return new EvApiResult<bool>()
  347. {
  348. Result = false,
  349. Response = result
  350. };
  351. }
  352. internal static async Task<EvApiResult<bool>> SignalUpdateFirmware()
  353. {
  354. var result = await EvHttpClient.GetSignalUpdateFirmwareResultString();
  355. if (!result.IsSuccess)
  356. {
  357. return new EvApiResult<bool>()
  358. {
  359. Result = false,
  360. Response = result.Msg
  361. };
  362. }
  363. return new EvApiResult<bool>()
  364. {
  365. Result = !string.IsNullOrEmpty(result.Msg),
  366. Response = result.Msg,
  367. };
  368. }
  369. /// <summary>
  370. /// Not Tested
  371. /// </summary>
  372. /// <param name="fileName"></param>
  373. /// <returns></returns>
  374. [Obsolete]
  375. internal static async Task<EvApiResult<bool>> Uploadfirmware(string fileName)
  376. {
  377. var result = await EvHttpClient.GetUploadfirmwareResultString(new List<string> { fileName });
  378. if (!result.IsSuccess)
  379. {
  380. return new EvApiResult<bool>()
  381. {
  382. Result = false,
  383. Response = result.Msg
  384. };
  385. }
  386. return new EvApiResult<bool>()
  387. {
  388. Result = !string.IsNullOrEmpty(result.Msg),
  389. Response = result.Msg,
  390. };
  391. }
  392. /// <summary>
  393. /// Not tested
  394. /// </summary>
  395. /// <param name="fileNames"></param>
  396. /// <returns></returns>
  397. [Obsolete]
  398. internal static async Task<EvApiResult<bool>> Uploadfirmware(List<string> fileNames)
  399. {
  400. var result = await EvHttpClient.GetUploadfirmwareResultString(fileNames);
  401. if (!result.IsSuccess)
  402. {
  403. return new EvApiResult<bool>()
  404. {
  405. Result = false,
  406. Response = result.Msg
  407. };
  408. }
  409. return new EvApiResult<bool>()
  410. {
  411. Result = !string.IsNullOrEmpty(result.Msg),
  412. Response = result.Msg,
  413. };
  414. }
  415. }
  416. }