EvApi.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. Result = false,
  37. Response = getResult.Msg
  38. };
  39. }
  40. bool isAllPassed = true;
  41. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  42. {
  43. var match = matches[matchIndex];
  44. if (match.Groups.Count != 3)
  45. {
  46. isAllPassed = false;
  47. break;
  48. }
  49. else
  50. {
  51. if (match.Groups[2].Value != "1")
  52. {
  53. isAllPassed = false;
  54. break;
  55. }
  56. }
  57. }
  58. return new EvApiResult<bool>()
  59. {
  60. Result = isAllPassed,
  61. Response = getResult.Msg,
  62. };
  63. }
  64. internal static async Task<EvApiResult<bool>> CheckGetQueryAction()
  65. {
  66. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  67. if (!getResult.IsSuccess ||
  68. string.IsNullOrEmpty(getResult.Msg))
  69. {
  70. return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
  71. }
  72. return new EvApiResult<bool>()
  73. {
  74. Result = !string.IsNullOrEmpty(getResult.Msg),
  75. Response = getResult.Msg
  76. };
  77. }
  78. internal static async Task<EvApiResult<ButtonStatus>> GetButtonStatus()
  79. {
  80. var getResult = await EvHttpClient.GetButtonStatusString();
  81. var result = getResult.Msg;
  82. if(!getResult.IsSuccess)
  83. {
  84. return new EvApiResult<ButtonStatus>()
  85. {
  86. Result = null,
  87. Response = getResult.Msg
  88. };
  89. }
  90. if (!result.Contains("Button1") ||
  91. !result.Contains("Button2") ||
  92. !result.Contains("EmergencyButton"))
  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 = string.Empty,
  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. Result = new EvApiWifiResult() {
  200. Rssi = 0,
  201. ErrorCode = 0
  202. },
  203. Response = getResult.Msg,
  204. };
  205. }
  206. Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
  207. var matches_mode = rx_mode.Matches(result);
  208. int rssi = 0;
  209. int errorCode = -1;
  210. if (matches_mode.Count != 0)
  211. {
  212. var match = matches_mode[0];
  213. if (match.Groups.Count != 3)
  214. {
  215. errorCode = 0;
  216. }
  217. else
  218. {
  219. if (int.TryParse(match.Groups[2].Value, out var wifiMode))
  220. {
  221. if (wifiMode != 1)
  222. {
  223. errorCode = 1;
  224. }
  225. }
  226. }
  227. }
  228. else
  229. {
  230. errorCode = 2;
  231. }
  232. if (errorCode != -1)
  233. {
  234. return new EvApiResult<EvApiWifiResult>()
  235. {
  236. Result = new EvApiWifiResult()
  237. {
  238. Rssi = rssi,
  239. ErrorCode = errorCode
  240. },
  241. Response = result
  242. };
  243. }
  244. Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
  245. var matches = rx.Matches(result);
  246. if (matches.Count != 0)
  247. {
  248. var match = matches[0];
  249. if (match.Groups.Count != 3)
  250. {
  251. errorCode = 3;
  252. }
  253. else
  254. {
  255. if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
  256. {
  257. rssi = rssiSignal;
  258. }
  259. else
  260. {
  261. errorCode = 4;
  262. }
  263. }
  264. }
  265. else
  266. {
  267. errorCode = 5;
  268. }
  269. return new EvApiResult<EvApiWifiResult>()
  270. {
  271. Result = new EvApiWifiResult()
  272. {
  273. Rssi = rssi,
  274. ErrorCode = errorCode
  275. },
  276. Response = result
  277. };
  278. }
  279. internal static async Task<EvApiResult<Dictionary<int, string>>> GetConnectorStatus()
  280. {
  281. var getResult = await EvHttpClient.GetQueryActionOpt2String();
  282. Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
  283. string result = getResult.Msg;
  284. if (!getResult.IsSuccess)
  285. {
  286. return new EvApiResult<Dictionary<int, string>>() {
  287. Result = connectorStatusPair,
  288. Response = getResult.Msg
  289. };
  290. }
  291. Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
  292. var matches = rx.Matches(result);
  293. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  294. {
  295. var match = matches[matchIndex];
  296. if (match.Groups.Count != 3)
  297. {
  298. //LogWriter.Log($"Connector {matchIndex} status string mismatched");
  299. return new EvApiResult<Dictionary<int, string>>()
  300. {
  301. Result = null,
  302. Response = result
  303. };
  304. //InfoLog += $"Connector {matchIndex} status string mismatched\n";
  305. //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
  306. }
  307. else
  308. {
  309. connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
  310. }
  311. }
  312. return new EvApiResult<Dictionary<int, string>>()
  313. {
  314. Result = connectorStatusPair,
  315. Response = result,
  316. };
  317. }
  318. internal static async Task<EvApiResult<bool>> FactorySet()
  319. {
  320. var getResult = await EvHttpClient.GetFactorySetResultString();
  321. var result = getResult.Msg;
  322. if (!getResult.IsSuccess)
  323. {
  324. return new EvApiResult<bool>() {
  325. Result = false,
  326. Response = getResult.Msg
  327. };
  328. }
  329. Regex rx = new Regex("(result)\":\"([a-zA-Z]*)\"");
  330. var matches = rx.Matches(result);
  331. if (matches.Count > 0 &&
  332. matches[0].Success &&
  333. matches[0].Groups.Count == 3 &&
  334. matches[0].Groups[2].Value.ToLower() == "success")
  335. {
  336. return new EvApiResult<bool>()
  337. {
  338. Result = true,
  339. Response = result
  340. };
  341. }
  342. return new EvApiResult<bool>()
  343. {
  344. Result = false,
  345. Response = result
  346. };
  347. }
  348. internal static async Task<EvApiResult<bool>> SignalUpdateFirmware()
  349. {
  350. var result = await EvHttpClient.GetSignalUpdateFirmwareResultString();
  351. if (!result.IsSuccess)
  352. {
  353. return new EvApiResult<bool>() {
  354. Result = false,
  355. Response = result.Msg
  356. };
  357. }
  358. return new EvApiResult<bool>()
  359. {
  360. Result = !string.IsNullOrEmpty(result.Msg),
  361. Response = result.Msg,
  362. };
  363. }
  364. /// <summary>
  365. /// Not Tested
  366. /// </summary>
  367. /// <param name="fileName"></param>
  368. /// <returns></returns>
  369. [Obsolete]
  370. internal static async Task<EvApiResult<bool>> Uploadfirmware(string fileName)
  371. {
  372. var result = await EvHttpClient.GetUploadfirmwareResultString(new List<string> { fileName });
  373. if (!result.IsSuccess)
  374. {
  375. return new EvApiResult<bool>()
  376. {
  377. Result = false,
  378. Response = result.Msg
  379. };
  380. }
  381. return new EvApiResult<bool>() {
  382. Result = !string.IsNullOrEmpty(result.Msg),
  383. Response = result.Msg,
  384. };
  385. }
  386. /// <summary>
  387. /// Not tested
  388. /// </summary>
  389. /// <param name="fileNames"></param>
  390. /// <returns></returns>
  391. [Obsolete]
  392. internal static async Task<EvApiResult<bool>> Uploadfirmware(List<string> fileNames)
  393. {
  394. var result = await EvHttpClient.GetUploadfirmwareResultString(fileNames);
  395. if (!result.IsSuccess)
  396. {
  397. return new EvApiResult<bool>()
  398. {
  399. Result = false,
  400. Response = result.Msg
  401. };
  402. }
  403. return new EvApiResult<bool>()
  404. {
  405. Result = !string.IsNullOrEmpty(result.Msg),
  406. Response = result.Msg,
  407. };
  408. }
  409. }
  410. }