ButtonStatusCheckPorcedure.cs 13 KB

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