ButtonStatusCheckPorcedure.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.Threading.Tasks;
  13. using System.Windows;
  14. namespace AwInitilizer.Procedure
  15. {
  16. public class ButtonStatusCheckPorcedure : ProcedureBase
  17. {
  18. public ButtonStatusCheckPorcedure() : base()
  19. {
  20. Name = "Button press test";
  21. Content = "Interaction to test button status";
  22. }
  23. internal override async Task<bool> Run()
  24. {
  25. Logger.Print("Button Unpress Status check Start");
  26. //stage 1 - unpress all
  27. MessageBox.Show("Please make sure All button is Unpressed\nPress Ok while complete", "Starting Button test");
  28. ButtonStatus status = await GetButtonStatus();
  29. if (status == null)
  30. {
  31. InfoLog += "Get Butoon state failed\n";
  32. Logger.Print("Get Butoon state failed");
  33. return false;
  34. }
  35. InfoLog += "Unpress check result\n";
  36. InfoLog += $"Button1:{status.Button1},Button2:{status.Button2},EmgerncyButton:{status.EmergencyButton}\n";
  37. bool isAllMatched = true;
  38. if (status != null)
  39. {
  40. isAllMatched = true;
  41. if (status.Button1 != 1)
  42. {
  43. Logger.Print("Button1 status ERROR, unpress is ecpected", isError: true);
  44. isAllMatched = false;
  45. }
  46. if (status.Button2 != 1)
  47. {
  48. Logger.Print("Button3 status ERROR, unpress is ecpected", isError: true);
  49. isAllMatched = false;
  50. }
  51. if (status.EmergencyButton != 1)
  52. {
  53. Logger.Print("EmergencyButton status ERROR, unpress is ecpected", isError: true);
  54. isAllMatched = false;
  55. }
  56. if (isAllMatched)
  57. {
  58. Logger.Print("Unpress Check passed");
  59. }
  60. else
  61. {
  62. return false;
  63. }
  64. }
  65. else
  66. {
  67. Logger.Print("Get button press status Failed", isError: true);
  68. return false;
  69. }
  70. //stage 2 - check press sequence
  71. Logger.Print("Button Unpress Status check Start");
  72. MessageBox.Show("Press Button1,Button2,EmergencyButton in order\neach press continuous 2 secondes\nPress Ok to start", "Starting Button test");
  73. bool isError = false;
  74. //Button1,Button2,EmergencyButton in order 0,1,2
  75. ButtonStatus pressStatus = null;
  76. for (int testType = 0; testType < 3; testType++)
  77. {
  78. //retry 20 times, 20*0.5 = 10 seconds
  79. int testCnt;
  80. for (testCnt = 0; testCnt < 20; testCnt++)
  81. {
  82. await Task.Delay(500);
  83. pressStatus = await GetButtonStatus();
  84. if (pressStatus == null)
  85. {
  86. InfoLog += "Get Butoon state failed\n";
  87. Logger.Print("Get Butoon state failed");
  88. return false;
  89. }
  90. //if any button is pressed
  91. if (pressStatus.Button1 != 0 ||
  92. pressStatus.Button2 != 0 ||
  93. pressStatus.EmergencyButton != 0)
  94. {
  95. if (pressStatus.Button1 == (testType == 0 ? 1 : 0) &&
  96. pressStatus.Button2 == (testType == 1 ? 1 : 0) &&
  97. pressStatus.EmergencyButton == (testType == 2 ? 1 : 0))
  98. {
  99. //Status correct
  100. }
  101. else
  102. {
  103. //Status error
  104. isError = true;
  105. break;
  106. }
  107. //wait release
  108. for (testCnt = 0; testCnt < 20; testCnt++)
  109. {
  110. await Task.Delay(500);
  111. status = await GetButtonStatus();
  112. if (status == null)
  113. {
  114. InfoLog += "Get Butoon state failed\n";
  115. Logger.Print("Get Butoon state failed");
  116. return false;
  117. }
  118. if (status.Button1 == 0 &&
  119. status.Button2 == 0 &&
  120. status.EmergencyButton == 0)
  121. {
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. string btn;
  128. switch (testType)
  129. {
  130. case 0:
  131. btn = "Button1";
  132. break;
  133. case 1:
  134. btn = "Button2";
  135. break;
  136. case 2:
  137. btn = "EmergencyButton";
  138. break;
  139. default:
  140. btn = "";
  141. break;
  142. };
  143. InfoLog += $"Press check stage {btn} result\n";
  144. InfoLog += $"Button1:{pressStatus.Button1},Button2:{pressStatus.Button2},EmgerncyButton:{pressStatus.EmergencyButton}\n";
  145. if (testCnt >= 20)
  146. {
  147. Logger.Print($"{btn} press TIMEOUT", isError: true);
  148. break;
  149. }
  150. else if (isError)
  151. {
  152. Logger.Print($"{btn} press state ERROR", isError: true);
  153. Logger.Print($"Button1 {GetBtnStatusString(pressStatus.Button1)}, Button2 {GetBtnStatusString(pressStatus.Button2)}, EmergencyButton {GetBtnStatusString(pressStatus.EmergencyButton)}", isError: true);
  154. break;
  155. }
  156. else
  157. {
  158. Logger.Print($"{btn} press state passed");
  159. }
  160. }
  161. if(isError)
  162. return false;
  163. return true;
  164. }
  165. internal async Task<ButtonStatus> GetButtonStatus()
  166. {
  167. try
  168. {
  169. using (WebClientTimeout webClient = new WebClientTimeout())
  170. {
  171. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_button_action.php"))
  172. // 使用 StreamReader 讀取 stream 內的字元
  173. using (StreamReader reader = new StreamReader(stream))
  174. {
  175. // 將 StreamReader 所讀到的字元轉為 string
  176. string request = reader.ReadToEnd();
  177. InfoLog += $"Get respone : {request}\n";
  178. var values = JsonConvert.DeserializeObject<ButtonStatus>(request);
  179. return values;
  180. }
  181. }
  182. }
  183. catch (Exception e)
  184. {
  185. InfoLog += "Get Button Status Failed\n";
  186. InfoLog += e.Message;
  187. InfoLog += "\n";
  188. Logger.Print("Get Button Status Failed", isError: true);
  189. Logger.Print(e.Message + "", isError: true);
  190. }
  191. return null;
  192. }
  193. private string GetBtnStatusString(int status)
  194. {
  195. if (status == 1)
  196. return "Pressed";
  197. else if (status == 0)
  198. return "Unpressed";
  199. else
  200. return status.ToString();
  201. }
  202. }
  203. }