ButtonStatusCheckPorcedure.cs 12 KB

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