Robert 1 жил өмнө
parent
commit
e2fa7b43e1

+ 456 - 0
AwInitilizer/Assist/EvApi.cs

@@ -0,0 +1,456 @@
+using AwInitilizer.Model;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace AwInitilizer.Assist
+{
+    public class EvApiResult<T>
+    {
+        public T Result { get; set; }
+        public string Response { get; set; }
+    }
+
+    public class EvApiWifiResult
+    {
+        public int Rssi { get; set; }
+        public int ErrorCode { get; set; }
+    }
+
+    public static class EvApi
+    {
+        internal static async Task<EvApiResult<bool>> ChekCsuBootCompelete()
+        {
+            var getResult = await EvHttpClient.GetQueryActionOpt2String();
+            if (!getResult.IsSuccess ||
+                string.IsNullOrEmpty(getResult.Msg))
+            {
+                return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
+            }
+
+            Regex rx = new Regex("(SystemStatus)\\\": ([0-9]*)");
+            var matches = rx.Matches(getResult.Msg);
+            if (matches.Count == 0)
+            {
+                return new EvApiResult<bool>()
+                {
+                    Result = false,
+                    Response = getResult.Msg
+                };
+            }
+            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 new EvApiResult<bool>()
+            {
+                Result = isAllPassed,
+                Response = getResult.Msg,
+            };
+        }
+
+        internal static async Task<EvApiResult<bool>> CheckGetQueryAction()
+        {
+            var getResult = await EvHttpClient.GetQueryActionOpt2String();
+            if (!getResult.IsSuccess ||
+                string.IsNullOrEmpty(getResult.Msg))
+            {
+                return new EvApiResult<bool>() { Response = getResult.Msg, Result = false };
+            }
+
+            return new EvApiResult<bool>()
+            {
+                Result = !string.IsNullOrEmpty(getResult.Msg),
+                Response = getResult.Msg
+            };
+        }
+
+        internal static async Task<EvApiResult<ButtonStatus>> GetButtonStatus()
+        {
+            var getResult = await EvHttpClient.GetButtonStatusString();
+            var result = getResult.Msg;
+
+            if (!getResult.IsSuccess)
+            {
+                return new EvApiResult<ButtonStatus>()
+                {
+                    Result = null,
+                    Response = getResult.Msg
+                };
+            }
+
+            if (!result.Contains("Button1") ||
+                !result.Contains("Button2") ||
+                !result.Contains("EmergencyButton"))
+            {
+                return new EvApiResult<ButtonStatus>()
+                {
+                    Result = null,
+                    Response = result
+                };
+            }
+            var values = JsonConvert.DeserializeObject<ButtonStatus>(result);
+            return new EvApiResult<ButtonStatus>()
+            {
+                Result = values,
+                Response = result,
+            };
+        }
+
+        internal static async Task<EvApiResult<Dictionary<string, string>>> GetVersion()
+        {
+            var getResult = await EvHttpClient.GetQueryActionOpt1String();
+            var toReturn = new Dictionary<string, string>();
+
+            string result = getResult.Msg;
+            if (!getResult.IsSuccess)
+            {
+                return new EvApiResult<Dictionary<string, string>>()
+                {
+                    Result = toReturn,
+                    Response = getResult.Msg,
+                };
+            }
+
+            var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
+
+            foreach (var pair in values)
+            {
+                if (pair.Value is string v)
+                {
+                    toReturn.Add(pair.Key, v);
+                }
+                else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
+                {
+                    try
+                    {
+                        var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
+                        for (int index = 0; index < versionList.Count; index++)
+                        {
+                            toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
+                        }
+                    }
+                    catch
+                    {
+
+                    }
+                }
+            }
+
+            return new EvApiResult<Dictionary<string, string>>()
+            {
+                Result = toReturn,
+                Response = result
+            };
+        }
+
+        internal static async Task<EvApiResult<string>> GetTelcomModemImei()
+        {
+            var getResult = await EvHttpClient.GetQueryActionOpt3String();
+            string result = getResult.Msg;
+            if (!getResult.IsSuccess)
+            {
+                return new EvApiResult<string>()
+                {
+                    Result = string.Empty,
+                    Response = getResult.Msg,
+                };
+            }
+
+            Regex rx = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\"");
+            var matches = rx.Matches(result);
+            string imeiString = string.Empty;
+
+            if (matches.Count != 0)
+            {
+                var match = matches[0];
+                if (match.Groups.Count != 3)
+                {
+                    imeiString = string.Empty;
+                }
+                else
+                {
+                    if (match.Groups[2].Value is string imei)
+                    {
+                        imeiString = imei;
+                    }
+                    else
+                    {
+                        imeiString = "";
+                    }
+                }
+            }
+            else
+            {
+                imeiString = "";
+            }
+
+            return new EvApiResult<string>()
+            {
+                Result = imeiString,
+                Response = result
+            };
+        }
+
+        internal static async Task<EvApiResult<EvApiWifiResult>> GetWifiRssi()
+        {
+            var getResult = await EvHttpClient.GetQueryActionOpt3String();
+            string result = getResult.Msg;
+            if (!getResult.IsSuccess)
+            {
+                return new EvApiResult<EvApiWifiResult>()
+                {
+                    Result = new EvApiWifiResult()
+                    {
+                        Rssi = 0,
+                        ErrorCode = 0
+                    },
+                    Response = getResult.Msg,
+                };
+            }
+
+            Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
+            var matches_mode = rx_mode.Matches(result);
+            int rssi = 0;
+            int errorCode = -1;
+
+            if (matches_mode.Count != 0)
+            {
+                var match = matches_mode[0];
+                if (match.Groups.Count != 3)
+                {
+                    errorCode = 0;
+                }
+                else
+                {
+                    if (int.TryParse(match.Groups[2].Value, out var wifiMode))
+                    {
+                        if (wifiMode != 1)
+                        {
+                            errorCode = 1;
+                        }
+                    }
+                }
+            }
+            else
+            {
+                errorCode = 2;
+            }
+
+            if (errorCode != -1)
+            {
+                return new EvApiResult<EvApiWifiResult>()
+                {
+                    Result = new EvApiWifiResult()
+                    {
+                        Rssi = rssi,
+                        ErrorCode = errorCode
+                    },
+                    Response = result
+                };
+            }
+
+            Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
+            var matches = rx.Matches(result);
+
+            if (matches.Count != 0)
+            {
+                var match = matches[0];
+                if (match.Groups.Count != 3)
+                {
+                    errorCode = 3;
+                }
+                else
+                {
+                    if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
+                    {
+                        rssi = rssiSignal;
+                    }
+                    else
+                    {
+                        errorCode = 4;
+                    }
+                }
+            }
+            else
+            {
+                errorCode = 5;
+            }
+
+            return new EvApiResult<EvApiWifiResult>()
+            {
+                Result = new EvApiWifiResult()
+                {
+                    Rssi = rssi,
+                    ErrorCode = errorCode
+                },
+                Response = result
+            };
+        }
+
+        internal static async Task<EvApiResult<Dictionary<int, string>>> GetConnectorStatus()
+        {
+            var getResult = await EvHttpClient.GetQueryActionOpt2String();
+            Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
+
+            string result = getResult.Msg;
+            if (!getResult.IsSuccess)
+            {
+                return new EvApiResult<Dictionary<int, string>>()
+                {
+                    Result = connectorStatusPair,
+                    Response = getResult.Msg
+                };
+            }
+
+            Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
+            var matches = rx.Matches(result);
+            for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
+            {
+                var match = matches[matchIndex];
+                if (match.Groups.Count != 3)
+                {
+                    //LogWriter.Log($"Connector {matchIndex} status string mismatched");
+                    return new EvApiResult<Dictionary<int, string>>()
+                    {
+                        Result = null,
+                        Response = result
+                    };
+                    //InfoLog += $"Connector {matchIndex} status string mismatched\n";
+                    //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
+                }
+                else
+                {
+                    connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
+                }
+            }
+
+            return new EvApiResult<Dictionary<int, string>>()
+            {
+                Result = connectorStatusPair,
+                Response = result,
+            };
+        }
+
+        internal static async Task<EvApiResult<bool>> FactorySet()
+        {
+            var getResult = await EvHttpClient.GetFactorySetResultString();
+            var result = getResult.Msg;
+            if (!getResult.IsSuccess)
+            {
+                return new EvApiResult<bool>()
+                {
+                    Result = false,
+                    Response = getResult.Msg
+                };
+            }
+
+            Regex rx = new Regex("(result)\":\"([a-zA-Z]*)\"");
+            var matches = rx.Matches(result);
+            if (matches.Count > 0 &&
+                matches[0].Success &&
+                matches[0].Groups.Count == 3 &&
+                matches[0].Groups[2].Value.ToLower() == "success")
+            {
+                return new EvApiResult<bool>()
+                {
+                    Result = true,
+                    Response = result
+                };
+            }
+            return new EvApiResult<bool>()
+            {
+                Result = false,
+                Response = result
+            };
+        }
+
+        internal static async Task<EvApiResult<bool>> SignalUpdateFirmware()
+        {
+            var result = await EvHttpClient.GetSignalUpdateFirmwareResultString();
+            if (!result.IsSuccess)
+            {
+                return new EvApiResult<bool>()
+                {
+                    Result = false,
+                    Response = result.Msg
+                };
+            }
+
+            return new EvApiResult<bool>()
+            {
+                Result = !string.IsNullOrEmpty(result.Msg),
+                Response = result.Msg,
+            };
+        }
+
+        /// <summary>
+        /// Not Tested
+        /// </summary>
+        /// <param name="fileName"></param>
+        /// <returns></returns>
+        [Obsolete]
+        internal static async Task<EvApiResult<bool>> Uploadfirmware(string fileName)
+        {
+            var result = await EvHttpClient.GetUploadfirmwareResultString(new List<string> { fileName });
+            if (!result.IsSuccess)
+            {
+                return new EvApiResult<bool>()
+                {
+                    Result = false,
+                    Response = result.Msg
+                };
+            }
+
+            return new EvApiResult<bool>()
+            {
+                Result = !string.IsNullOrEmpty(result.Msg),
+                Response = result.Msg,
+            };
+        }
+
+        /// <summary>
+        /// Not tested
+        /// </summary>
+        /// <param name="fileNames"></param>
+        /// <returns></returns>
+        [Obsolete]
+        internal static async Task<EvApiResult<bool>> Uploadfirmware(List<string> fileNames)
+        {
+            var result = await EvHttpClient.GetUploadfirmwareResultString(fileNames);
+            if (!result.IsSuccess)
+            {
+                return new EvApiResult<bool>()
+                {
+                    Result = false,
+                    Response = result.Msg
+                };
+            }
+
+            return new EvApiResult<bool>()
+            {
+                Result = !string.IsNullOrEmpty(result.Msg),
+                Response = result.Msg,
+            };
+        }
+    }
+}

+ 273 - 0
AwInitilizer/Assist/EvHttpClient.cs

@@ -0,0 +1,273 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace AwInitilizer.Assist
+{
+    public class EvHttpClientResult
+    {
+        public bool IsSuccess { get; set; }
+        public string Msg { get; set; }
+    }
+
+    public class EvAuthMsg
+    {
+        public string result { get; set; }
+        public string message { get; set; }
+    }
+
+    public static class EvHttpClient
+    {
+        private static string account = "admin";
+        private static string pass = "1231231238";
+        internal static string ServerIpAddress = "192.168.1.10";
+        internal static string ServerUrl = "https://192.168.1.10";
+        //updated EV
+        //internal static string ServerIpAddress = "192.168.80.197";
+        //internal static string ServerUrl = "https://192.168.80.197";
+        //original EV
+        //internal static string ServerIpAddress = "192.168.80.199";
+        //internal static string ServerUrl = "https://192.168.80.199";
+
+        internal static Task<EvHttpClientResult> GetQueryActionOpt1String()
+        {
+            string api = "get_query_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>() {
+                {"opt","1"}
+            };
+            return CallBase(api, param);
+        }
+
+        internal static Task<EvHttpClientResult> GetQueryActionOpt2String()
+        {
+            string api = "get_query_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>() {
+                {"opt","2"}
+            };
+            return CallBase(api, param);
+        }
+
+        internal static Task<EvHttpClientResult> GetQueryActionOpt3String()
+        {
+            string api = "get_query_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>() {
+                {"opt","3"}
+            };
+            return CallBase(api, param);
+        }
+
+        internal static Task<EvHttpClientResult> GetButtonStatusString()
+        {
+            string api = "get_button_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>()
+            {
+            };
+            return CallBase(api, param);
+        }
+
+        internal static Task<EvHttpClientResult> GetFactorySetResultString()
+        {
+            string api = "set_system_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>();
+            param.Add("SystemId", "");
+            param.Add("SystemDateTime", "");
+            param.Add("PhaseLossPolicy", "");
+            param.Add("FactoryConfiguration", "1");
+            param.Add("AuthorisationMode", "");
+            param.Add("isAPP", "");
+            param.Add("isQRCode", "");
+            param.Add("isRFID", "");
+            param.Add("QRCodeMadeMode", "");
+            param.Add("QRCodeContent", "");
+            param.Add("Intensity", "");
+            param.Add("RfidCardNumEndian", "");
+            param.Add("PsuAcInputType", "");
+            return CallBase(api, param);
+        }
+
+        internal static Task<EvHttpClientResult> GetUploadfirmwareResultString(List<string> fileNames)
+        {
+            string api = "upgrade_iso_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>(){
+                {"fw_tag","iso"}
+            };
+            return CallBase(api, param, firmwareNames: fileNames);
+        }
+
+        internal static Task<EvHttpClientResult> GetSignalUpdateFirmwareResultString()
+        {
+            string api = "upgrade_iso_action.php";
+            Dictionary<string, string> param = new Dictionary<string, string>(){
+                {"fw_tag","iso"},
+            };
+            var dummyFileCotent = new ByteArrayContent(new byte[0]);
+            dummyFileCotent.Headers.ContentDisposition
+              = new ContentDispositionHeaderValue("attachment")
+              {
+                  FileName = "temp.txt",
+                  Size = 0,
+                  CreationDate = DateTime.Now,
+                  ModificationDate = DateTime.Now,
+                  ReadDate = DateTime.Now,
+                  Name = "files[]"
+              };
+
+            return CallBase(api, param, customContents: new List<HttpContent>() { dummyFileCotent });
+        }
+
+        private static async Task<EvHttpClientResult> CallBase(
+            string api,
+            Dictionary<string, string> param,
+            List<string> firmwareNames = null,
+            List<HttpContent> customContents = null)
+        {
+            try
+            {
+                var url = string.Format("{0}/{1}", ServerUrl, api);
+                Dictionary<string, string> pams = new Dictionary<string, string>
+                {
+                    { "account", account },
+                    { "password", pass }
+                };
+                foreach (var pam in param)
+                {
+                    pams.Add(pam.Key, pam.Value);
+                }
+
+
+                var formContent = new MultipartFormDataContent();
+                foreach (var pam in pams)
+                {
+                    formContent.Add(new StringContent(pam.Value), pam.Key);
+                }
+
+                if (firmwareNames != null)
+                {
+                    if (firmwareNames.Count == 1)
+                    {
+                        var fileName = firmwareNames[0];
+                        formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file");
+                    }
+                    else
+                    {
+                        foreach (var fileName in firmwareNames)
+                        {
+                            formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file[]");
+                        }
+                    }
+                }
+
+                if (customContents != null)
+                {
+                    foreach (var content in customContents)
+                    {
+                        formContent.Add(content);
+                    }
+                }
+
+                HttpResponseMessage postResult;
+                string result = null;
+
+
+                var handler = new HttpClientHandler();
+                handler.ClientCertificateOptions = ClientCertificateOption.Manual;
+                handler.ServerCertificateCustomValidationCallback =
+                    (httpRequestMessage, cert, cetChain, policyErrors) =>
+                    {
+                        return true;
+                    };
+
+                using (HttpClient evClient = new HttpClient(handler))
+                {
+                    evClient.Timeout = TimeSpan.FromSeconds(5);
+
+                    try
+                    {
+                        postResult = await evClient.PostAsync(url, formContent);
+                        if (postResult == null || !postResult.IsSuccessStatusCode)
+                        {
+                            throw new Exception("Post fail");
+                        }
+                        result = await postResult.Content.ReadAsStringAsync();
+
+                        if (result.Contains("File is uploaded, please wait a moment to upgrade"))
+                        {
+                            return new EvHttpClientResult()
+                            {
+                                IsSuccess = true,
+                                Msg = result,
+                            };
+                        }
+
+                        var check = JsonConvert.DeserializeObject<EvAuthMsg>(result);
+                        if (check != null &&
+                            check.result != null &&
+                            check.result.ToLower() == "fail")
+                        {
+                            return new EvHttpClientResult()
+                            {
+                                IsSuccess = false,
+                                Msg = result,
+                            };
+                        }
+
+                        return new EvHttpClientResult()
+                        {
+                            IsSuccess = true,
+                            Msg = result,
+                        };
+                    }
+                    catch
+                    {
+                        //post fail
+                    }
+                }
+
+                using (WebClientTimeout webClient = new WebClientTimeout())
+                {
+                    NameValueCollection parameters = new NameValueCollection();
+                    foreach (var inpam in param)
+                    {
+                        parameters.Add(inpam.Key, inpam.Value);
+                    }
+                    webClient.QueryString = parameters;
+
+                    using (Stream stream = webClient.OpenRead(url))
+                    // 使用 StreamReader 讀取 stream 內的字元
+                    using (StreamReader reader = new StreamReader(stream))
+                    {
+                        // 將 StreamReader 所讀到的字元轉為 string
+                        result = await reader.ReadToEndAsync();
+                    }
+
+                    var check = JsonConvert.DeserializeObject<EvAuthMsg>(result);
+                    if (check != null &&
+                        check.result != null &&
+                        check.result.ToLower() == "fail")
+                    {
+                        return new EvHttpClientResult()
+                        {
+                            IsSuccess = false,
+                            Msg = result,
+                        };
+                    }
+                }
+                return new EvHttpClientResult() { IsSuccess = true, Msg = result };
+
+            }
+            catch (Exception e)
+            {
+                return new EvHttpClientResult() { IsSuccess = false, Msg = e.Message }; ;
+            }
+        }
+    }
+}

+ 2 - 0
AwInitilizer/Initilizer.csproj

@@ -89,6 +89,8 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="Assist\EvApi.cs" />
+    <Compile Include="Assist\EvHttpClient.cs" />
     <Compile Include="Converter\MesErrorCodeMaper.cs" />
     <Compile Include="Converter\PressStatusConverter.cs" />
     <Compile Include="KeyinListener.cs" />

+ 3 - 15
AwInitilizer/Procedure/ButtonStatusCheckPorcedure.cs

@@ -299,21 +299,9 @@ namespace AwInitilizer.Procedure.ButtonStatusCheck
         {
             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";
-                        LogWriter.Log($"Get respone : {request}", isDebugLog: true);
-                        var values = JsonConvert.DeserializeObject<ButtonStatus>(request);
-
-                        return values;
-                    }
-                }
+                var result = await EvApi.GetButtonStatus();
+                LogWriter.Log($"Get respone : {result.Response}", isDebugLog: true);
+                return result.Result;
             }
             catch (Exception e)
             {

+ 1 - 1
AwInitilizer/Procedure/FirmwareBundleUploadProcedure.cs

@@ -63,7 +63,7 @@ namespace AwInitilizer.Procedure.FirmwareBundleUpload
             for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
             {
                 await Task.Delay(TimeSpan.FromSeconds(15));
-                response = await CheckGetQuertyAction();
+                response = await CheckGetQueryAction();
                 if (response)
                     break;
             }

+ 3 - 44
AwInitilizer/Procedure/FirmwareCheckVersionProcedure.cs

@@ -146,50 +146,9 @@ namespace AwInitilizer.Procedure.FirmwareCheckVersion
         {
             try
             {
-                using (WebClientTimeout webClient = new WebClientTimeout())
-                {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("opt", "1");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string request = reader.ReadToEnd();
-                        LogWriter.Log($"get version response:{request}", isDebugLog: true);
-                        //InfoLog += $"get version response:{request}\n";
-
-                        var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
-
-                        var toReturn = new Dictionary<string, string>();
-                        foreach (var pair in values)
-                        {
-                            if (pair.Value is string v)
-                            {
-                                toReturn.Add(pair.Key, v);
-                            }
-                            else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
-                            {
-                                try
-                                {
-                                    var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
-                                    for (int index = 0; index < versionList.Count; index++)
-                                    {
-                                        toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
-                                    }
-                                }
-                                catch
-                                {
-
-                                }
-                            }
-                        }
-
-                        return toReturn;
-                    }
-                }
+                var result = await EvApi.GetVersion();
+                LogWriter.Log($"get version response:{result.Response}", isDebugLog: true);
+                return result.Result;
             }
             catch (Exception e)
             {

+ 11 - 27
AwInitilizer/Procedure/FirmwareFtpUploadProcedure.cs

@@ -1,4 +1,5 @@
-using FluentFTP;
+using AwInitilizer.Assist;
+using FluentFTP;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -46,7 +47,7 @@ namespace AwInitilizer.Procedure.FirmwareBundleUpload
             for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
             {
                 await Task.Delay(TimeSpan.FromSeconds(15));
-                response = await CheckGetQuertyAction();
+                response = await CheckGetQueryAction();
                 if (response)
                     break;
             }
@@ -127,35 +128,18 @@ namespace AwInitilizer.Procedure.FirmwareBundleUpload
 
         private async Task<bool> SignalUpdate()
         {
-            var updateList = UpdateData.FirmwareUpdateModels;
-
-            var restClient = new RestSharp.RestClient($"https://{ServerIpAddress}");
-            restClient.ConfigureWebRequest((r) => { r.KeepAlive = true; });
-
-            var request = new RestSharp.RestRequest("upgrade_iso_action.php", RestSharp.Method.POST);
-            request.AlwaysMultipartFormData = true;
-            request.AddHeader("Content-Type", "multipart/form-data");
-
-            request.AddParameter("fw_tag", "iso");
-            //request.AddFile("files[]", new byte[0] , "dummy");
-
-            RestSharp.IRestResponse response = await restClient.ExecuteAsync(request);
-
-            if (response.IsSuccessful)
+            try
             {
-                return true;
+                var result = await EvApi.SignalUpdateFirmware();
+                LogWriter.Log(result.Response);
+                return result.Result;
             }
-            else
+            catch (Exception e)
             {
-                if (response.ErrorException != null)
+                LogWriter.Log(e.Message, isError: true, isDebugLog: true);
+                if (e.InnerException != null)
                 {
-                    LogWriter.Log(response.ErrorException.Message, isError: true, isDebugLog: true);
-                    if (response.ErrorException.InnerException != null)
-                    {
-                        LogWriter.Log(response.ErrorException.InnerException.Message, isError: true, isDebugLog: true);
-                    }
-
-                    LogWriter.Log(response.StatusCode.ToString(), isError: true, isDebugLog: true);
+                    LogWriter.Log(e.InnerException.Message, isError: true, isDebugLog: true);
                 }
                 return false;
             }

+ 32 - 65
AwInitilizer/Procedure/FirmwareUpdateProcedure.cs

@@ -133,48 +133,9 @@ namespace AwInitilizer.Procedure
         {
             try
             {
-                using (WebClientTimeout webClient = new WebClientTimeout())
-                {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("opt", "1");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string request = reader.ReadToEnd();
-                        InfoLog += $"get version response:{request}\n";
-                        var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
-
-                        var toReturn = new Dictionary<string, string>();
-                        foreach(var pair in values)
-                        {
-                            if (pair.Value is string v)
-                            {
-                                toReturn.Add(pair.Key, v);
-                            }
-                            else if(pair.Value is Newtonsoft.Json.Linq.JArray a)
-                            {
-                                try
-                                {
-                                    var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
-                                    for (int index = 0; index < versionList.Count; index++)
-                                    {
-                                        toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
-                                    }
-                                }
-                                catch
-                                {
-
-                                }
-                            }
-                        }
-
-                        return toReturn;
-                    }
-                }
+                var result = await EvApi.GetVersion();
+                InfoLog += $"get version response:{result.Response}\n";
+                return result.Result;
             }
             catch(Exception e)
             {
@@ -196,6 +157,12 @@ namespace AwInitilizer.Procedure
         {
             try
             {
+                var result = await EvApi.Uploadfirmware(fileName);
+                InfoLog += $"get firmware update response {result.Response}\n";
+                if (result.Response != null && result.Response.Contains("file is uploaded"))
+                    return true;
+                return false;
+
                 //using (var stream = File.Open(fileName, FileMode.Open))
                 //{
                 //    UploadFileAsync(
@@ -213,30 +180,30 @@ namespace AwInitilizer.Procedure
                 //}
 
                 //return true;
-                using (var stream = File.Open(fileName, FileMode.Open))
-                {
-                    var response = await UploadFiles(
-                    $"https://{ServerIpAddress}/upgrade_iso_action.php",
-                    new List<UploadFile>() {
-                        new UploadFile()
-                        {
-                            Name="file",
-                            Filename= Path.GetFileName(fileName),
-                            Stream = stream
-                        }
-                    },
-                    new NameValueCollection() {
-                        {"fw_tag","iso" }
-                    }
-                    );
+                //using (var stream = File.Open(fileName, FileMode.Open))
+                //{
+                //    var response = await UploadFiles(
+                //    $"https://{ServerIpAddress}/upgrade_iso_action.php",
+                //    new List<UploadFile>() {
+                //        new UploadFile()
+                //        {
+                //            Name="file",
+                //            Filename= Path.GetFileName(fileName),
+                //            Stream = stream
+                //        }
+                //    },
+                //    new NameValueCollection() {
+                //        {"fw_tag","iso" }
+                //    }
+                //    );
 
-                    var responseStr = Encoding.ASCII.GetString(response).ToLower();
-                    InfoLog += $"get firmware update response {responseStr}\n";
-                    if (responseStr.Contains("file is uploaded"))
-                        return true;
-                    return false;
-                }
-                return true;
+                //    var responseStr = Encoding.ASCII.GetString(response).ToLower();
+                //    InfoLog += $"get firmware update response {responseStr}\n";
+                //    if (responseStr.Contains("file is uploaded"))
+                //        return true;
+                //    return false;
+                //}
+                //return true;
                 //using (WebClient webClient = new WebClient())
                 //{
                 //    NameValueCollection parameters = new NameValueCollection();

+ 29 - 25
AwInitilizer/Procedure/FirmwareUploadProcedure.cs

@@ -1,4 +1,5 @@
-using AwInitilizer.Model;
+using AwInitilizer.Assist;
+using AwInitilizer.Model;
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
@@ -44,6 +45,9 @@ namespace AwInitilizer.Procedure
         {
             try
             {
+                var result = await EvApi.Uploadfirmware(fileName);
+                InfoLog += $"get firmware update response {result.Response}\n";
+                return result.Result;
                 //using (var stream = File.Open(fileName, FileMode.Open))
                 //{
                 //    UploadFileAsync(
@@ -61,30 +65,30 @@ namespace AwInitilizer.Procedure
                 //}
 
                 //return true;
-                using (var stream = File.Open(fileName, FileMode.Open))
-                {
-                    var response = await UploadFiles(
-                    $"https://{ServerIpAddress}/upgrade_iso_action.php",
-                    new List<UploadFile>() {
-                        new UploadFile()
-                        {
-                            Name="file",
-                            Filename= Path.GetFileName(fileName),
-                            Stream = stream
-                        }
-                    },
-                    new NameValueCollection() {
-                        {"fw_tag","iso" }
-                    }
-                    );
-
-                    var responseStr = Encoding.ASCII.GetString(response).ToLower();
-                    InfoLog += $"get firmware update response {responseStr}\n";
-                    if (responseStr.Contains("file is uploaded"))
-                        return true;
-                    return false;
-                }
-                return true;
+                //using (var stream = File.Open(fileName, FileMode.Open))
+                //{
+                //    var response = await UploadFiles(
+                //    $"https://{ServerIpAddress}/upgrade_iso_action.php",
+                //    new List<UploadFile>() {
+                //        new UploadFile()
+                //        {
+                //            Name="file",
+                //            Filename= Path.GetFileName(fileName),
+                //            Stream = stream
+                //        }
+                //    },
+                //    new NameValueCollection() {
+                //        {"fw_tag","iso" }
+                //    }
+                //    );
+
+                //    var responseStr = Encoding.ASCII.GetString(response).ToLower();
+                //    InfoLog += $"get firmware update response {responseStr}\n";
+                //    if (responseStr.Contains("file is uploaded"))
+                //        return true;
+                //    return false;
+                //}
+                //return true;
                 //using (WebClient webClient = new WebClient())
                 //{
                 //    NameValueCollection parameters = new NameValueCollection();

+ 5 - 68
AwInitilizer/Procedure/ProcedureBase.cs

@@ -85,77 +85,14 @@ namespace AwInitilizer.Procedure
 
         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;
-            }
+            var result = await EvApi.ChekCsuBootCompelete();
+            return result.Result;
         }
 
-        public async Task<bool> CheckGetQuertyAction()
+        public async Task<bool> CheckGetQueryAction()
         {
-            //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;
-            }
+            var result = await EvApi.CheckGetQueryAction();
+            return result.Result;
         }
 
         private void Dispose()

+ 11 - 64
AwInitilizer/Procedure/RestarttoIdelProcedure.cs

@@ -1,4 +1,5 @@
-using Newtonsoft.Json;
+using AwInitilizer.Assist;
+using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
@@ -84,35 +85,8 @@ namespace AwInitilizer.Procedure.RestarttoIdel
         {
             try
             {
-                using (WebClient webClient = new WebClient())
-                {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("SystemId", "");
-                    parameters.Add("SystemDateTime", "");
-                    parameters.Add("PhaseLossPolicy", "");
-                    parameters.Add("FactoryConfiguration", "1");
-                    parameters.Add("AuthorisationMode", "");
-                    parameters.Add("isAPP", "");
-                    parameters.Add("isQRCode", "");
-                    parameters.Add("isRFID", "");
-                    parameters.Add("QRCodeMadeMode", "");
-                    parameters.Add("QRCodeContent", "");
-                    parameters.Add("Intensity", "");
-                    parameters.Add("RfidCardNumEndian", "");
-                    parameters.Add("PsuAcInputType", "");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/set_system_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string response = reader.ReadToEnd();
-                        //InfoLog += $"factory set respons:\n{request}\n";
-                        LogWriter.Log($"factory set respons: {response}", isDebugLog: true);
-                        var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
-                    }
-                }
+                var result=  await EvApi.FactorySet();
+                LogWriter.Log($"factory set respons: {result.Response}", isDebugLog: true);
                 return true;
             }
             catch (Exception e)
@@ -154,44 +128,17 @@ namespace AwInitilizer.Procedure.RestarttoIdel
 
         private async Task<Dictionary<int, string>> GetConnectorStatus()
         {
-            Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
+            //Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
             try
             {
-                using (WebClient webClient = new WebClient())
+                var result = await EvApi.GetConnectorStatus();
+                LogWriter.Log($"get status respons:\n{result.Response}\n", isDebugLog: true);
+                if (result.Result == null)
                 {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("opt", "2");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string request = reader.ReadToEnd();
-                        LogWriter.Log($"get status respons:\n{request}\n", isDebugLog: true);
-                        //InfoLog += $"get status respons:\n{request}\n";
-                        //LogPair.Add($"EvseStatus", request);
-                        Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
-                        var matches = rx.Matches(request);
-                        for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
-                        {
-                            var match = matches[matchIndex];
-                            if (match.Groups.Count != 3)
-                            {
-                                LogWriter.Log($"Connector {matchIndex} status string mismatched");
-                                return null;
-                                //InfoLog += $"Connector {matchIndex} status string mismatched\n";
-                                //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
-                            }
-                            else
-                            {
-                                connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
-                            }
-                        }
-                    }
+                    LogWriter.Log($"Connector status string mismatched");
+                    return null;
                 }
-                return connectorStatusPair;
+                return result.Result;
             }
             catch (Exception e)
             {

+ 5 - 40
AwInitilizer/Procedure/TelcomModemImeiRecordProcedure.cs

@@ -66,48 +66,13 @@ namespace AwInitilizer.Procedure.TelcomModemImeiRecord
         {
             try
             {
-                using (WebClientTimeout webClient = new WebClientTimeout())
+                var result = await EvApi.GetTelcomModemImei();
+                LogWriter.Log($"GetResponse:{result.Response}", isDebugLog: true);
+                if (string.IsNullOrEmpty(result.Result))
                 {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("opt", "3");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string request = reader.ReadToEnd();
-                        LogWriter.Log($"GetResponse:{request}", isDebugLog: true);
-
-                        Regex rx = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\"");
-                        var matches = rx.Matches(request);
-
-                        if (matches.Count != 0)
-                        {
-                            var match = matches[0];
-                            if (match.Groups.Count != 3)
-                            {
-                                Error = ErrorType.TelcomModemImeiNotFound;
-                                return "";
-                            }
-                            else
-                            {
-                                if (match.Groups[2].Value is string imei)
-                                {
-                                    return imei;
-                                }
-                                Error = ErrorType.TelcomModemImeiNotFound;
-                                return "";
-                            }
-                        }
-                        else
-                        {
-                            Error = ErrorType.TelcomModemImeiNotFound;
-                            return "";
-                        }
-                    }
+                    Error = ErrorType.TelcomModemImeiNotFound;
                 }
+                return "";
             }
             catch (Exception e)
             {

+ 5 - 44
AwInitilizer/Procedure/VersionLogProcedure.cs

@@ -1,4 +1,5 @@
-using Newtonsoft.Json;
+using AwInitilizer.Assist;
+using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
@@ -114,49 +115,9 @@ namespace AwInitilizer.Procedure.VersionLog
         {
             try
             {
-                using (WebClient webClient = new WebClient())
-                {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("opt", "1");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string request = reader.ReadToEnd();
-                        LogWriter.Log($"get version response:{request}\n", isDebugLog: true);
-                        //InfoLog += $"get version response:{request}\n";
-                        var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
-
-                        var toReturn = new Dictionary<string, string>();
-                        foreach (var pair in values)
-                        {
-                            if (pair.Value is string v)
-                            {
-                                toReturn.Add(pair.Key, v);
-                            }
-                            else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
-                            {
-                                try
-                                {
-                                    var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
-                                    for (int index = 0; index < versionList.Count; index++)
-                                    {
-                                        toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
-                                    }
-                                }
-                                catch
-                                {
-
-                                }
-                            }
-                        }
-
-                        return toReturn;
-                    }
-                }
+                var result = await EvApi.GetVersion();
+                LogWriter.Log($"get version response:{result.Response}\n", isDebugLog: true);
+                return result.Result;
             }
             catch (Exception e)
             {

+ 18 - 69
AwInitilizer/Procedure/WifRssiCheckProcedure.cs

@@ -96,77 +96,26 @@ namespace AwInitilizer.Procedure.WifRssiCheck
         {
             try
             {
-                using (WebClientTimeout webClient = new WebClientTimeout())
+                var result = await EvApi.GetWifiRssi();
+                switch (result.Result.ErrorCode)
                 {
-                    NameValueCollection parameters = new NameValueCollection();
-                    parameters.Add("opt", "3");
-                    webClient.QueryString = parameters;
-
-                    using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
-                    // 使用 StreamReader 讀取 stream 內的字元
-                    using (StreamReader reader = new StreamReader(stream))
-                    {
-                        // 將 StreamReader 所讀到的字元轉為 string
-                        string request = reader.ReadToEnd();
-                        LogWriter.Log($"GetResponse:{request}", isDebugLog: true);
-
-                        Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
-                        var matches_mode = rx_mode.Matches(request);
-
-                        if (matches_mode.Count != 0)
-                        {
-                            var match = matches_mode[0];
-                            if (match.Groups.Count != 3)
-                            {
-                                Error = ErrorType.WifiModeDataNotFound;
-                                return 0;
-                            }
-                            else
-                            {
-                                if (int.TryParse(match.Groups[2].Value, out var wifiMode))
-                                {
-                                    if (wifiMode != 1)
-                                    {
-                                        Error = ErrorType.WifiModeNotClient;
-                                        return 0;
-                                    }
-                                }
-                            }
-                        }
-                        else
-                        {
-                            Error = ErrorType.WifiModeDataNotFound;
-                            return 0;
-                        }
-
-                        Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
-                        var matches = rx.Matches(request);
-
-                        if (matches.Count != 0)
-                        {
-                            var match = matches[0];
-                            if (match.Groups.Count != 3)
-                            {
-                                Error = ErrorType.WifiRssiDataNotFound;
-                                return 0;
-                            }
-                            else
-                            {
-                                if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
-                                {
-                                    return rssiSignal;
-                                }
-                                Error = ErrorType.WifiRssiDataNotFound;
-                                return 0;
-                            }
-                        }
-                        else
-                        {
-                            Error = ErrorType.WifiRssiDataNotFound;
-                            return 0;
-                        }
-                    }
+                    case 0:
+                    case 2:
+                        Error = ErrorType.WifiModeDataNotFound;
+                        break;
+                    case 1:
+                        Error = ErrorType.WifiModeNotClient;
+                        break;
+                    case 3:
+                    case 4:
+                    case 5:
+                        Error = ErrorType.WifiRssiDataNotFound;
+                        break;
+                    default:
+                        break;
                 }
+                LogWriter.Log($"GetResponse:{result.Response}", isDebugLog: true);
+                return result.Result.Rssi;
             }
             catch (Exception e)
             {

+ 1 - 1
AwInitilizer/Properties/AssemblyInfo.cs

@@ -34,4 +34,4 @@ using System.Runtime.InteropServices;
 // [assembly: AssemblyVersion("1.9.7.0")]
 [assembly: AssemblyVersion("1.9.7.0")]
 [assembly: AssemblyFileVersion("1.9.7.0")]
-[assembly: AssemblyInformationalVersion("d82c309")]
+[assembly: AssemblyInformationalVersion("525b4ef")]

+ 1 - 1
Initilizer/AssemblyInfo.cs

@@ -11,5 +11,5 @@
 
 [assembly: AssemblyVersion("1.9.7.0")]
 [assembly: AssemblyFileVersion("1.9.7.0")]
-[assembly: AssemblyInformationalVersion("d82c309")]
+[assembly: AssemblyInformationalVersion("525b4ef")]
 

+ 1 - 1
MesAdaptor/Properties/AssemblyInfo.cs

@@ -34,4 +34,4 @@ using System.Runtime.InteropServices;
 // [assembly: AssemblyVersion("1.9.7.0")]
 [assembly: AssemblyVersion("1.9.7.0")]
 [assembly: AssemblyFileVersion("1.9.7.0")]
-[assembly: AssemblyInformationalVersion("d82c309")]
+[assembly: AssemblyInformationalVersion("525b4ef")]