|
@@ -194,6 +194,16 @@ namespace CsuWebApiLib
|
|
|
return CallBase(api, param, customContents: new List<HttpContent>() { });
|
|
|
}
|
|
|
|
|
|
+ internal static Task<EvHttpClientResult> SetRestart()
|
|
|
+ {
|
|
|
+ string api = "set_restart_action.php";
|
|
|
+ //Dictionary<string, string> param = new Dictionary<string, string>(){
|
|
|
+ // {key,value},
|
|
|
+ //};
|
|
|
+
|
|
|
+ return CallBase(api, new Dictionary<string, string>(), customContents: new List<HttpContent>() { });
|
|
|
+ }
|
|
|
+
|
|
|
private static async Task<EvHttpClientResult> CallBase(
|
|
|
string api,
|
|
|
Dictionary<string, string> param,
|
|
@@ -201,6 +211,11 @@ namespace CsuWebApiLib
|
|
|
List<HttpContent> customContents = null,
|
|
|
int timeOutSeconds = 5)
|
|
|
{
|
|
|
+ //if (!await CallWithNewVersion(api, param, firmwareNames, customContents, timeOutSeconds))
|
|
|
+ //{
|
|
|
+ // CallWithOldVersion();
|
|
|
+ //}
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
var url = string.Format("{0}/{1}", ServerUrl, api);
|
|
@@ -229,19 +244,163 @@ namespace CsuWebApiLib
|
|
|
{
|
|
|
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(timeOutSeconds);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ EvHttpClientLogger.Instance.Log("post with new version");
|
|
|
+ postResult = await evClient.PostAsync(url, formContent);
|
|
|
+ EvHttpClientLogger.Instance.Log(postResult.StatusCode.ToString());
|
|
|
+
|
|
|
+ //MessageBox.Show("Rest Result:" + postResult.StatusCode);
|
|
|
+ if (postResult == null || !postResult.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ throw new Exception("Post fail");
|
|
|
+ }
|
|
|
+ result = await postResult.Content.ReadAsStringAsync();
|
|
|
+ EvHttpClientLogger.Instance.Log(result);
|
|
|
+
|
|
|
+ //MessageBox.Show("Rest Result:" + result);
|
|
|
+
|
|
|
+ if (result.Contains("File is uploaded, please wait a moment to upgrade"))
|
|
|
+ {
|
|
|
+ return new EvHttpClientResult()
|
|
|
+ {
|
|
|
+ IsSuccess = true,
|
|
|
+ Msg = result,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.Contains("Charger restarted successfully"))
|
|
|
+ {
|
|
|
+ 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 (Exception e)
|
|
|
+ {
|
|
|
+ //post fail
|
|
|
+ EvHttpClientLogger.Instance.Log(e.Message);
|
|
|
+ EvHttpClientLogger.Instance.Log(e.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ using (WebClientTimeout webClient = new WebClientTimeout())
|
|
|
+ {
|
|
|
+ EvHttpClientLogger.Instance.Log("post with old version");
|
|
|
+ NameValueCollection parameters = new NameValueCollection();
|
|
|
+ foreach (var inpam in param)
|
|
|
+ {
|
|
|
+ parameters.Add(inpam.Key, inpam.Value);
|
|
|
+ }
|
|
|
+ webClient.QueryString = parameters;
|
|
|
|
|
|
- //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[]");
|
|
|
- // }
|
|
|
- //}
|
|
|
+ using (Stream stream = webClient.OpenRead(url))
|
|
|
+ // 使用 StreamReader 讀取 stream 內的字元
|
|
|
+ using (StreamReader reader = new StreamReader(stream))
|
|
|
+ {
|
|
|
+ // 將 StreamReader 所讀到的字元轉為 string
|
|
|
+ result = await reader.ReadToEndAsync();
|
|
|
+ }
|
|
|
+ EvHttpClientLogger.Instance.Log(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 (Exception e)
|
|
|
+ {
|
|
|
+ return new EvHttpClientResult() { IsSuccess = false, Msg = e.Message }; ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static async Task<EvHttpClientResult> CallWithNewVersion(string api, Dictionary<string, string> param, List<string> firmwareNames, List<HttpContent> customContents, int timeOutSeconds)
|
|
|
+ {
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var url = string.Format("{0}/{1}", ServerUrl, api);
|
|
|
+ EvHttpClientLogger.Instance.Log(url);
|
|
|
+
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ foreach (var fileName in firmwareNames)
|
|
|
+ {
|
|
|
+ formContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)), "file[]");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (customContents != null)
|