ButtonStatusCheckPorcedure.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. object response = false;
  32. int pollingCnt = 0;
  33. for (pollingCnt = 0; pollingCnt < 14; pollingCnt++)
  34. {
  35. response = await GetButtonStatus();
  36. if (response!=null)
  37. break;
  38. await Task.Delay(TimeSpan.FromSeconds(30));
  39. }
  40. ReportLog.Add(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<14", pollingCnt * 0.5));
  41. //timeout
  42. if (pollingCnt >= 14)
  43. {
  44. Logger.Print("Wait EVSE timeout", isError: true);
  45. return false;
  46. }
  47. //stage 1 - unpress all
  48. //ShowDialog(
  49. // "Please make sure All button is Unpressed\nPress Ok while complete",
  50. // "Starting Button test",
  51. // "OK","", cancelAble: true);
  52. ShowDialog(
  53. (string)Application.Current.FindResource("BtnPressUnpressHint"),
  54. (string)Application.Current.FindResource("BtnPressHintTitle"),
  55. (string)Application.Current.FindResource("Confirm"),
  56. "", cancelAble: true);
  57. ButtonStatus status = await GetButtonStatus();
  58. if (status == null)
  59. {
  60. InfoLog += "Get Butoon state failed\n";
  61. Logger.Print("Get Butoon state failed");
  62. return false;
  63. }
  64. InfoLog += "Unpress check result\n";
  65. InfoLog += $"Button1:{status.Button1},Button2:{status.Button2},EmgerncyButton:{status.EmergencyButton}\n";
  66. LogPair.Add("UnpressButtonTest", string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton));
  67. ReportLog.Add(string.Format("Button unpress test result : {0}, Expect:000",
  68. string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton)
  69. ));
  70. bool isAllMatched = true;
  71. if (status != null)
  72. {
  73. isAllMatched = true;
  74. if (status.Button1 != 0)
  75. {
  76. Logger.Print("Button1 status ERROR, unpress is ecpected", isError: true);
  77. isAllMatched = false;
  78. }
  79. if (status.Button2 != 0)
  80. {
  81. Logger.Print("Button3 status ERROR, unpress is ecpected", isError: true);
  82. isAllMatched = false;
  83. }
  84. if (status.EmergencyButton != 0)
  85. {
  86. Logger.Print("EmergencyButton status ERROR, unpress is ecpected", isError: true);
  87. isAllMatched = false;
  88. }
  89. if (isAllMatched)
  90. {
  91. Logger.Print("Unpress Check passed");
  92. }
  93. else
  94. {
  95. return false;
  96. }
  97. }
  98. else
  99. {
  100. Logger.Print("Get button press status Failed", isError: true);
  101. return false;
  102. }
  103. //stage 2 - check press sequence
  104. Logger.Print("Button press Status check Start");
  105. //MessageBox.Show("Press Button1,Button2,EmergencyButton in order\neach press continuous 2 secondes\nPress Ok to start", "Starting Button test");
  106. bool isError = false;
  107. //Button1,Button2,EmergencyButton in order 0,1,2
  108. ButtonStatus pressStatus = null;
  109. for (int testType = 0; testType < 3; testType++)
  110. {
  111. string btn, btnLang, imgUrl;
  112. switch (testType)
  113. {
  114. case 0:
  115. btn = "Green Button";
  116. btnLang = (string)Application.Current.FindResource("BtnPressGreenBtn");
  117. imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Green.png";
  118. break;
  119. case 1:
  120. btn = "Blue Button";
  121. btnLang = (string)Application.Current.FindResource("BtnPressBlueBtn");
  122. imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Blue.png";
  123. break;
  124. case 2:
  125. btn = "EmergencyButton";
  126. btnLang = (string)Application.Current.FindResource("BtnPressEmergencyBtn");
  127. imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Emergency.png";
  128. break;
  129. default:
  130. btn = "";
  131. btnLang = "";
  132. imgUrl = null;
  133. break;
  134. };
  135. //MessageBox.Show($"press {btn}");
  136. //ShowDialog($"PRESS {btn.ToUpper()}", "Button press test", "", imgUrl, cancelAble: false);
  137. ShowDialog(
  138. string.Format((string)Application.Current.FindResource("BtnPressPressHint"), btnLang),
  139. (string)Application.Current.FindResource("BtnPressHintTitle"),
  140. "", imgUrl, cancelAble: false); ;
  141. //retry 20 times, 20*0.5 = 10 seconds
  142. int testCnt;
  143. for (testCnt = 0; testCnt < 20; testCnt++)
  144. {
  145. await Task.Delay(500);
  146. pressStatus = await GetButtonStatus();
  147. if (pressStatus == null)
  148. {
  149. InfoLog += "Get Butoon state failed\n";
  150. Logger.Print("Get Butoon state failed");
  151. return false;
  152. }
  153. //if any button is pressed
  154. if (pressStatus.Button1 != 0 ||
  155. pressStatus.Button2 != 0 ||
  156. pressStatus.EmergencyButton != 0)
  157. {
  158. bool success = false;
  159. if (pressStatus.Button1 == (testType == 0 ? 1 : 0) &&
  160. pressStatus.Button2 == (testType == 1 ? 1 : 0) &&
  161. pressStatus.EmergencyButton == (testType == 2 ? 1 : 0))
  162. {
  163. //Status correct
  164. success = true;
  165. }
  166. else
  167. {
  168. //Status error
  169. isError = true;
  170. success = false;
  171. break;
  172. }
  173. if (success)
  174. {
  175. //wait release
  176. for (var releaseCnt = 0; releaseCnt < 20; releaseCnt++)
  177. {
  178. await Task.Delay(500);
  179. status = await GetButtonStatus();
  180. if (status == null)
  181. {
  182. InfoLog += "Get Butoon state failed\n";
  183. Logger.Print("Get Butoon state failed");
  184. return false;
  185. }
  186. if (status.Button1 == 0 &&
  187. status.Button2 == 0 &&
  188. status.EmergencyButton == 0)
  189. {
  190. break;
  191. }
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. DismisDialog();
  198. InfoLog += $"Press check stage {btn} result\n";
  199. InfoLog += $"Button1:{pressStatus.Button1},Button2:{pressStatus.Button2},EmgerncyButton:{pressStatus.EmergencyButton}\n";
  200. LogPair.Add($"{btn}PressTest", string.Format("{0}{1}{2}", pressStatus.Button1, pressStatus.Button2, pressStatus.EmergencyButton));
  201. ReportLog.Add(string.Format("{0} Button press test result : {1}, Expect:{2}{3}{4}",
  202. btn,
  203. string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton),
  204. testType == 0 ? "1" : "0",
  205. testType == 1 ? "1" : "0",
  206. testType == 2 ? "1" : "0"
  207. ));
  208. if (testCnt >= 20)
  209. {
  210. isError = true;
  211. Logger.Print($"{btn} press TIMEOUT", isError: true);
  212. break;
  213. }
  214. else if (isError)
  215. {
  216. Logger.Print($"{btn} press state ERROR", isError: true);
  217. Logger.Print($"Button1 {GetBtnStatusString(pressStatus.Button1)}, Button2 {GetBtnStatusString(pressStatus.Button2)}, EmergencyButton {GetBtnStatusString(pressStatus.EmergencyButton)}", isError: true);
  218. break;
  219. }
  220. else
  221. {
  222. Logger.Print($"{btn} press state passed");
  223. }
  224. }
  225. if(isError)
  226. return false;
  227. return true;
  228. }
  229. internal async Task<ButtonStatus> GetButtonStatus(bool isConnectTest = false)
  230. {
  231. try
  232. {
  233. using (WebClientTimeout webClient = new WebClientTimeout())
  234. {
  235. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_button_action.php"))
  236. // 使用 StreamReader 讀取 stream 內的字元
  237. using (StreamReader reader = new StreamReader(stream))
  238. {
  239. // 將 StreamReader 所讀到的字元轉為 string
  240. string request = reader.ReadToEnd();
  241. InfoLog += $"Get respone : {request}\n";
  242. var values = JsonConvert.DeserializeObject<ButtonStatus>(request);
  243. return values;
  244. }
  245. }
  246. }
  247. catch (Exception e)
  248. {
  249. if (!isConnectTest)
  250. {
  251. InfoLog += "Get Button Status Failed\n";
  252. InfoLog += e.Message;
  253. InfoLog += "\n";
  254. Logger.Print("Get Button Status Failed", isError: true);
  255. Logger.Print(e.Message + "", isError: true);
  256. }
  257. }
  258. return null;
  259. }
  260. private string GetBtnStatusString(int status)
  261. {
  262. if (status == 1)
  263. return "Pressed";
  264. else if (status == 0)
  265. return "Unpressed";
  266. else
  267. return status.ToString();
  268. }
  269. private void ShowDialog(string msg,string title, string btnText,string imgUrl, bool cancelAble)
  270. {
  271. hintDialog = new HintDialog();
  272. hintDialog.Owner = Application.Current.MainWindow;
  273. hintDialog.Message = msg;
  274. hintDialog.Title = title;
  275. hintDialog.IsCancelAble = cancelAble;
  276. hintDialog.BtnText = btnText;
  277. hintDialog.ImgPath = imgUrl;
  278. if (cancelAble)
  279. {
  280. hintDialog.ShowDialog();
  281. }
  282. else
  283. {
  284. hintDialog.Show();
  285. }
  286. }
  287. private void DismisDialog()
  288. {
  289. if (hintDialog == null)
  290. return;
  291. hintDialog.Close();
  292. }
  293. }
  294. }