1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace AwInitilizer.Procedure
- {
- public class VersionLogProcedure : ProcedureBase
- {
- public VersionLogProcedure() : base()
- {
- Name = "Version Logger";
- Content = "Report version back to MES";
- }
- internal override async Task<bool> Run()
- {
- var versionPair = await GetVersion();
- if (versionPair != null)
- {
- foreach (var infoPair in versionPair)
- {
- LogPair.Add(infoPair.Key, infoPair.Value);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- internal async Task<Dictionary<string, string>> GetVersion()
- {
- 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();
- 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;
- }
- }
- }
- catch (Exception e)
- {
- Logger.Print("Get Version Failed", isError: true);
- Logger.Print(e.Message + "", isError: true);
- InfoLog += "Get Version Failed\n";
- InfoLog += e.Message;
- InfoLog += "\n";
- return null;
- }
- }
- }
- }
|