ButtonStatusCheckPorcedure.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using AwInitilizer.Assist;
  2. using AwInitilizer.Model;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Security.AccessControl;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace AwInitilizer.Procedure
  16. {
  17. public class ButtonStatusCheckPorcedure : ProcedureBase
  18. {
  19. private HintDialog hintDialog = new HintDialog();
  20. public ButtonStatusCheckPorcedure() : base()
  21. {
  22. Name = "Button Press Test";
  23. Content = "Interaction to test button status";
  24. }
  25. internal override async Task<bool> Run()
  26. {
  27. Logger.Print("Button Unpress Status check Start");
  28. //stage 0 - wait system ready
  29. //wait restart
  30. Logger.Print("Waiting response..");
  31. bool response = false;
  32. int pollinfCnt = 0;
  33. for (pollinfCnt = 0; pollinfCnt < 14; pollinfCnt++)
  34. {
  35. await Task.Delay(TimeSpan.FromSeconds(30));
  36. response = await CheckAllIdel();
  37. if (response)
  38. break;
  39. }
  40. //timeout
  41. if (pollinfCnt >= 14)
  42. {
  43. Logger.Print("Wait EVSE timeout", isError: true);
  44. return false;
  45. }
  46. //stage 1 - unpress all
  47. ShowDialog("Please make sure All button is Unpressed\nPress Ok while complete", "Starting Button test", "OK","", cancelAble: true);
  48. ButtonStatus status = await GetButtonStatus();
  49. if (status == null)
  50. {
  51. InfoLog += "Get Butoon state failed\n";
  52. Logger.Print("Get Butoon state failed");
  53. return false;
  54. }
  55. InfoLog += "Unpress check result\n";
  56. InfoLog += $"Button1:{status.Button1},Button2:{status.Button2},EmgerncyButton:{status.EmergencyButton}\n";
  57. LogPair.Add("UnpressButtonTest", string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton));
  58. bool isAllMatched = true;
  59. if (status != null)
  60. {
  61. isAllMatched = true;
  62. if (status.Button1 != 0)
  63. {
  64. Logger.Print("Button1 status ERROR, unpress is ecpected", isError: true);
  65. isAllMatched = false;
  66. }
  67. if (status.Button2 != 0)
  68. {
  69. Logger.Print("Button3 status ERROR, unpress is ecpected", isError: true);
  70. isAllMatched = false;
  71. }
  72. if (status.EmergencyButton != 0)
  73. {
  74. Logger.Print("EmergencyButton status ERROR, unpress is ecpected", isError: true);
  75. isAllMatched = false;
  76. }
  77. if (isAllMatched)
  78. {
  79. Logger.Print("Unpress Check passed");
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. else
  87. {
  88. Logger.Print("Get button press status Failed", isError: true);
  89. return false;
  90. }
  91. //stage 2 - check press sequence
  92. Logger.Print("Button Unpress Status check Start");
  93. //MessageBox.Show("Press Button1,Button2,EmergencyButton in order\neach press continuous 2 secondes\nPress Ok to start", "Starting Button test");
  94. bool isError = false;
  95. //Button1,Button2,EmergencyButton in order 0,1,2
  96. ButtonStatus pressStatus = null;
  97. for (int testType = 0; testType < 3; testType++)
  98. {
  99. string btn,imgUrl;
  100. switch (testType)
  101. {
  102. case 0:
  103. btn = "Green Button";
  104. imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Green.png";
  105. break;
  106. case 1:
  107. btn = "Blue Button";
  108. imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Blue.png";
  109. break;
  110. case 2:
  111. btn = "EmergencyButton";
  112. imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Emergency.png";
  113. break;
  114. default:
  115. btn = "";
  116. imgUrl = null;
  117. break;
  118. };
  119. //MessageBox.Show($"press {btn}");
  120. ShowDialog($"PRESS {btn.ToUpper()}", "Button press test", "", imgUrl, cancelAble: false);
  121. //retry 20 times, 20*0.5 = 10 seconds
  122. int testCnt;
  123. for (testCnt = 0; testCnt < 20; testCnt++)
  124. {
  125. await Task.Delay(500);
  126. pressStatus = await GetButtonStatus();
  127. if (pressStatus == null)
  128. {
  129. InfoLog += "Get Butoon state failed\n";
  130. Logger.Print("Get Butoon state failed");
  131. return false;
  132. }
  133. //if any button is pressed
  134. if (pressStatus.Button1 != 0 ||
  135. pressStatus.Button2 != 0 ||
  136. pressStatus.EmergencyButton != 0)
  137. {
  138. bool success = false;
  139. if (pressStatus.Button1 == (testType == 0 ? 1 : 0) &&
  140. pressStatus.Button2 == (testType == 1 ? 1 : 0) &&
  141. pressStatus.EmergencyButton == (testType == 2 ? 1 : 0))
  142. {
  143. //Status correct
  144. success = true;
  145. }
  146. else
  147. {
  148. //Status error
  149. isError = true;
  150. success = false;
  151. break;
  152. }
  153. if (success)
  154. {
  155. //wait release
  156. for (var releaseCnt = 0; releaseCnt < 20; releaseCnt++)
  157. {
  158. await Task.Delay(500);
  159. status = await GetButtonStatus();
  160. if (status == null)
  161. {
  162. InfoLog += "Get Butoon state failed\n";
  163. Logger.Print("Get Butoon state failed");
  164. return false;
  165. }
  166. if (status.Button1 == 0 &&
  167. status.Button2 == 0 &&
  168. status.EmergencyButton == 0)
  169. {
  170. break;
  171. }
  172. }
  173. break;
  174. }
  175. }
  176. }
  177. DismisDialog();
  178. InfoLog += $"Press check stage {btn} result\n";
  179. InfoLog += $"Button1:{pressStatus.Button1},Button2:{pressStatus.Button2},EmgerncyButton:{pressStatus.EmergencyButton}\n";
  180. LogPair.Add($"{btn}PressTest", string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton));
  181. if (testCnt >= 20)
  182. {
  183. isError = true;
  184. Logger.Print($"{btn} press TIMEOUT", isError: true);
  185. break;
  186. }
  187. else if (isError)
  188. {
  189. Logger.Print($"{btn} press state ERROR", isError: true);
  190. Logger.Print($"Button1 {GetBtnStatusString(pressStatus.Button1)}, Button2 {GetBtnStatusString(pressStatus.Button2)}, EmergencyButton {GetBtnStatusString(pressStatus.EmergencyButton)}", isError: true);
  191. break;
  192. }
  193. else
  194. {
  195. Logger.Print($"{btn} press state passed");
  196. }
  197. }
  198. if(isError)
  199. return false;
  200. return true;
  201. }
  202. internal async Task<ButtonStatus> GetButtonStatus(bool isConnectTest = false)
  203. {
  204. try
  205. {
  206. using (WebClientTimeout webClient = new WebClientTimeout())
  207. {
  208. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_button_action.php"))
  209. // 使用 StreamReader 讀取 stream 內的字元
  210. using (StreamReader reader = new StreamReader(stream))
  211. {
  212. // 將 StreamReader 所讀到的字元轉為 string
  213. string request = reader.ReadToEnd();
  214. InfoLog += $"Get respone : {request}\n";
  215. var values = JsonConvert.DeserializeObject<ButtonStatus>(request);
  216. return values;
  217. }
  218. }
  219. }
  220. catch (Exception e)
  221. {
  222. if (!isConnectTest)
  223. {
  224. InfoLog += "Get Button Status Failed\n";
  225. InfoLog += e.Message;
  226. InfoLog += "\n";
  227. Logger.Print("Get Button Status Failed", isError: true);
  228. Logger.Print(e.Message + "", isError: true);
  229. }
  230. }
  231. return null;
  232. }
  233. private async Task<bool> CheckAllIdel()
  234. {
  235. try
  236. {
  237. using (Assist.WebClientTimeout webClient = new Assist.WebClientTimeout())
  238. {
  239. NameValueCollection parameters = new NameValueCollection();
  240. parameters.Add("opt", "2");
  241. webClient.QueryString = parameters;
  242. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  243. // 使用 StreamReader 讀取 stream 內的字元
  244. using (StreamReader reader = new StreamReader(stream))
  245. {
  246. // 將 StreamReader 所讀到的字元轉為 string
  247. string request = reader.ReadToEnd();
  248. InfoLog += $"get status respons:\n{request}\n";
  249. LogPair.Add($"EvseStatus", request);
  250. Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
  251. var matches = rx.Matches(request);
  252. bool isAllPassed = true;
  253. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  254. {
  255. var match = matches[matchIndex];
  256. if (match.Groups.Count != 3)
  257. {
  258. InfoLog += $"Connector {matchIndex} status string mismatched\n";
  259. Logger.Print($"Connector {matchIndex} status string mismatched", isError: true);
  260. isAllPassed = false;
  261. }
  262. else
  263. {
  264. if (match.Groups[2].Value != "1")
  265. {
  266. InfoLog += $"Connector {matchIndex} status not Idel\n";
  267. Logger.Print($"Connector {matchIndex} status not Idel", isError: true);
  268. isAllPassed = false;
  269. }
  270. }
  271. }
  272. return isAllPassed;
  273. }
  274. }
  275. return true;
  276. }
  277. catch (Exception e)
  278. {
  279. return false;
  280. }
  281. }
  282. private string GetBtnStatusString(int status)
  283. {
  284. if (status == 1)
  285. return "Pressed";
  286. else if (status == 0)
  287. return "Unpressed";
  288. else
  289. return status.ToString();
  290. }
  291. private void ShowDialog(string msg,string title, string btnText,string imgUrl, bool cancelAble)
  292. {
  293. hintDialog = new HintDialog();
  294. hintDialog.Owner = Application.Current.MainWindow;
  295. hintDialog.Message = msg;
  296. hintDialog.Title = title;
  297. hintDialog.IsCancelAble = cancelAble;
  298. hintDialog.BtnText = btnText;
  299. hintDialog.ImgPath = imgUrl;
  300. if (cancelAble)
  301. {
  302. hintDialog.ShowDialog();
  303. }
  304. else
  305. {
  306. hintDialog.Show();
  307. }
  308. }
  309. private void DismisDialog()
  310. {
  311. if (hintDialog == null)
  312. return;
  313. hintDialog.Close();
  314. }
  315. }
  316. }