123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using AwInitilizer.Assist;
- using AwInitilizer.Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Security.AccessControl;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- namespace AwInitilizer.Procedure
- {
- public class ButtonStatusCheckPorcedure : ProcedureBase
- {
- private HintDialog hintDialog = new HintDialog();
- public ButtonStatusCheckPorcedure() : base()
- {
- Name = "Button Press Test";
- Content = "Interaction to test button status";
- }
- internal override async Task<bool> Run()
- {
- Logger.Print("Button Unpress Status check Start");
- //stage 0 - wait system ready
- //wait restart
- Logger.Print("Waiting response..");
- object response = false;
- int pollinfCnt = 0;
- for (pollinfCnt = 0; pollinfCnt < 14; pollinfCnt++)
- {
- response = await GetButtonStatus();
- if (response!=null)
- break;
- await Task.Delay(TimeSpan.FromSeconds(30));
- }
- //timeout
- if (pollinfCnt >= 14)
- {
- Logger.Print("Wait EVSE timeout", isError: true);
- return false;
- }
- //stage 1 - unpress all
- //ShowDialog(
- // "Please make sure All button is Unpressed\nPress Ok while complete",
- // "Starting Button test",
- // "OK","", cancelAble: true);
- ShowDialog(
- (string)Application.Current.FindResource("BtnPressUnpressHint"),
- (string)Application.Current.FindResource("BtnPressHintTitle"),
- (string)Application.Current.FindResource("Confirm"),
- "", cancelAble: true);
- ButtonStatus status = await GetButtonStatus();
- if (status == null)
- {
- InfoLog += "Get Butoon state failed\n";
- Logger.Print("Get Butoon state failed");
- return false;
- }
- InfoLog += "Unpress check result\n";
- InfoLog += $"Button1:{status.Button1},Button2:{status.Button2},EmgerncyButton:{status.EmergencyButton}\n";
- LogPair.Add("UnpressButtonTest", string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton));
- bool isAllMatched = true;
- if (status != null)
- {
- isAllMatched = true;
- if (status.Button1 != 0)
- {
- Logger.Print("Button1 status ERROR, unpress is ecpected", isError: true);
- isAllMatched = false;
- }
- if (status.Button2 != 0)
- {
- Logger.Print("Button3 status ERROR, unpress is ecpected", isError: true);
- isAllMatched = false;
- }
- if (status.EmergencyButton != 0)
- {
- Logger.Print("EmergencyButton status ERROR, unpress is ecpected", isError: true);
- isAllMatched = false;
- }
- if (isAllMatched)
- {
- Logger.Print("Unpress Check passed");
- }
- else
- {
- return false;
- }
- }
- else
- {
- Logger.Print("Get button press status Failed", isError: true);
- return false;
- }
- //stage 2 - check press sequence
- Logger.Print("Button Unpress Status check Start");
- //MessageBox.Show("Press Button1,Button2,EmergencyButton in order\neach press continuous 2 secondes\nPress Ok to start", "Starting Button test");
- bool isError = false;
- //Button1,Button2,EmergencyButton in order 0,1,2
- ButtonStatus pressStatus = null;
- for (int testType = 0; testType < 3; testType++)
- {
- string btn, btnLang, imgUrl;
- switch (testType)
- {
- case 0:
- btn = "Green Button";
- btnLang = (string)Application.Current.FindResource("BtnPressGreenBtn");
- imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Green.png";
- break;
- case 1:
- btn = "Blue Button";
- btnLang = (string)Application.Current.FindResource("BtnPressBlueBtn");
- imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Blue.png";
- break;
- case 2:
- btn = "EmergencyButton";
- btnLang = (string)Application.Current.FindResource("BtnPressEmergencyBtn");
- imgUrl = "pack://application:,,,/AwInitilizer;component/Image/Emergency.png";
- break;
- default:
- btn = "";
- btnLang = "";
- imgUrl = null;
- break;
- };
- //MessageBox.Show($"press {btn}");
- //ShowDialog($"PRESS {btn.ToUpper()}", "Button press test", "", imgUrl, cancelAble: false);
- ShowDialog(
- string.Format((string)Application.Current.FindResource("BtnPressPressHint"), btnLang),
- (string)Application.Current.FindResource("BtnPressHintTitle"),
- "", imgUrl, cancelAble: false); ;
- //retry 20 times, 20*0.5 = 10 seconds
- int testCnt;
- for (testCnt = 0; testCnt < 20; testCnt++)
- {
- await Task.Delay(500);
- pressStatus = await GetButtonStatus();
- if (pressStatus == null)
- {
- InfoLog += "Get Butoon state failed\n";
- Logger.Print("Get Butoon state failed");
- return false;
- }
- //if any button is pressed
- if (pressStatus.Button1 != 0 ||
- pressStatus.Button2 != 0 ||
- pressStatus.EmergencyButton != 0)
- {
- bool success = false;
- if (pressStatus.Button1 == (testType == 0 ? 1 : 0) &&
- pressStatus.Button2 == (testType == 1 ? 1 : 0) &&
- pressStatus.EmergencyButton == (testType == 2 ? 1 : 0))
- {
- //Status correct
- success = true;
- }
- else
- {
- //Status error
- isError = true;
- success = false;
- break;
- }
- if (success)
- {
- //wait release
- for (var releaseCnt = 0; releaseCnt < 20; releaseCnt++)
- {
- await Task.Delay(500);
- status = await GetButtonStatus();
- if (status == null)
- {
- InfoLog += "Get Butoon state failed\n";
- Logger.Print("Get Butoon state failed");
- return false;
- }
- if (status.Button1 == 0 &&
- status.Button2 == 0 &&
- status.EmergencyButton == 0)
- {
- break;
- }
- }
- break;
- }
- }
- }
- DismisDialog();
- InfoLog += $"Press check stage {btn} result\n";
- InfoLog += $"Button1:{pressStatus.Button1},Button2:{pressStatus.Button2},EmgerncyButton:{pressStatus.EmergencyButton}\n";
- LogPair.Add($"{btn}PressTest", string.Format("{0}{1}{2}", status.Button1, status.Button2, status.EmergencyButton));
- if (testCnt >= 20)
- {
- isError = true;
- Logger.Print($"{btn} press TIMEOUT", isError: true);
- break;
- }
- else if (isError)
- {
- Logger.Print($"{btn} press state ERROR", isError: true);
- Logger.Print($"Button1 {GetBtnStatusString(pressStatus.Button1)}, Button2 {GetBtnStatusString(pressStatus.Button2)}, EmergencyButton {GetBtnStatusString(pressStatus.EmergencyButton)}", isError: true);
- break;
- }
- else
- {
- Logger.Print($"{btn} press state passed");
- }
- }
- if(isError)
- return false;
- return true;
- }
- internal async Task<ButtonStatus> GetButtonStatus(bool isConnectTest = false)
- {
- try
- {
- using (WebClientTimeout webClient = new WebClientTimeout())
- {
- using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_button_action.php"))
- // 使用 StreamReader 讀取 stream 內的字元
- using (StreamReader reader = new StreamReader(stream))
- {
- // 將 StreamReader 所讀到的字元轉為 string
- string request = reader.ReadToEnd();
- InfoLog += $"Get respone : {request}\n";
- var values = JsonConvert.DeserializeObject<ButtonStatus>(request);
- return values;
- }
- }
- }
- catch (Exception e)
- {
- if (!isConnectTest)
- {
- InfoLog += "Get Button Status Failed\n";
- InfoLog += e.Message;
- InfoLog += "\n";
- Logger.Print("Get Button Status Failed", isError: true);
- Logger.Print(e.Message + "", isError: true);
- }
- }
- return null;
- }
- private string GetBtnStatusString(int status)
- {
- if (status == 1)
- return "Pressed";
- else if (status == 0)
- return "Unpressed";
- else
- return status.ToString();
- }
- private void ShowDialog(string msg,string title, string btnText,string imgUrl, bool cancelAble)
- {
- hintDialog = new HintDialog();
- hintDialog.Owner = Application.Current.MainWindow;
- hintDialog.Message = msg;
- hintDialog.Title = title;
- hintDialog.IsCancelAble = cancelAble;
- hintDialog.BtnText = btnText;
- hintDialog.ImgPath = imgUrl;
- if (cancelAble)
- {
- hintDialog.ShowDialog();
- }
- else
- {
- hintDialog.Show();
- }
- }
- private void DismisDialog()
- {
- if (hintDialog == null)
- return;
- hintDialog.Close();
- }
- }
- }
|