123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using AwInitilizer.Assist;
- using AwInitilizer.Interface;
- using AwInitilizer.Model;
- using MesAdaptor;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace AwInitilizer.Procedure
- {
- public enum ProcedureStatus
- {
- IDLE,PASS,FAIL
- }
- public class ProcedureBase
- {
- public string Name { get; set; }
- public string Content { get; set; }
- public bool IsActivated { get; set; } = true;
- public ProcedureStatus Status { get; private set; }
- public string InfoLog { get; internal set; }
- public static UpdateData UpdateData { get; set; }
- public static IIogger Logger { get; set; }
- public ValueReportDatas MesLogData { get; private set; } = new ValueReportDatas();
- public List<string> ReportLog { get; private set; } = new List<string>();
- internal SerialPortocol serialPortocol { get; private set; }
- internal static string ServerIpAddress = "192.168.1.10";
- public async Task<bool> DoWork() {
- if (!IsActivated)
- return false;
- InfoLog = "";
- Logger.Print(Name + "Started");
- var result = await Run();
- Dispose();
- Status = result ? ProcedureStatus.PASS : ProcedureStatus.FAIL;
- Logger.Print(Name + "Complete");
- return result;
- }
- internal virtual async Task<bool> Run()
- {
- return true;
- }
- public void Reset()
- {
- Status = ProcedureStatus.IDLE;
- InfoLog = "";
- }
- internal async Task<bool> CheckAndCreateSocket()
- {
- TcpSerializer socketConnection = null;
- if (socketConnection == null || socketConnection.ConnectStatus == ConnectStatus.ConnectionFail ||
- socketConnection.ConnectStatus == ConnectStatus.DisConnected)
- {
- socketConnection = new Assist.TcpSerializer(ip:ServerIpAddress);
- await socketConnection.OpenAsync();
- if(socketConnection.ConnectStatus != ConnectStatus.Connected)
- {
- Logger.Print("EVSE tcp connection Failed,Check EVSE is connected", isError: true);
- return false;
- }
- Logger.Print("Connected");
- serialPortocol = new SerialPortocol(socketConnection);
- return true;
- }
- return true;
- }
- public async Task<bool> ChekCsuBootCompelete()
- {
- //await Task.Delay(TimeSpan.FromMinutes(2));
- try
- {
- using (WebClientTimeout webClient = new WebClientTimeout())
- {
- NameValueCollection parameters = new NameValueCollection();
- parameters.Add("opt", "2");
- webClient.QueryString = parameters;
- using (Stream stream = webClient.OpenRead($"https://{ServerIpAddress}/get_query_action.php"))
- // 使用 StreamReader 讀取 stream 內的字元
- using (StreamReader reader = new StreamReader(stream))
- {
- // 將 StreamReader 所讀到的字元轉為 string
- string request = reader.ReadToEnd();
- Regex rx = new Regex("(SystemStatus)\\\": ([0-9]*)");
- var matches = rx.Matches(request);
- bool isAllPassed = true;
- for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
- {
- var match = matches[matchIndex];
- if (match.Groups.Count != 3)
- {
- isAllPassed = false;
- break;
- }
- else
- {
- if (match.Groups[2].Value != "1")
- {
- isAllPassed = false;
- break;
- }
- }
- }
- return isAllPassed;
- }
- }
- }
- catch (Exception e)
- {
- return false;
- }
- }
- public async Task<bool> CheckGetQuertyAction()
- {
- //await Task.Delay(TimeSpan.FromMinutes(2));
- try
- {
- using (WebClientTimeout webClient = new WebClientTimeout())
- {
- NameValueCollection parameters = new NameValueCollection();
- parameters.Add("opt", "2");
- webClient.QueryString = parameters;
- using (Stream stream = webClient.OpenRead($"https://{ServerIpAddress}/get_query_action.php"))
- // 使用 StreamReader 讀取 stream 內的字元
- using (StreamReader reader = new StreamReader(stream))
- {
- // 將 StreamReader 所讀到的字元轉為 string
- string request = reader.ReadToEnd();
- return true;
- }
- }
- }
- catch (Exception e)
- {
- return false;
- }
- }
- private void Dispose()
- {
- if(serialPortocol!=null)
- {
- try
- {
- serialPortocol.Close();
- }catch
- {
- }
- }
- }
- }
- }
|