123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Net.Http;
- using System.Net.Http.Json;
- using Websocket.Client;
- using Ookii.Dialogs.Wpf;
- using System.Text.Json;
- namespace pmtest_client
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private System.Timers.Timer autoRefreshTimer;
- private HttpClient httpClient;
- private WebsocketClient? wsClient;
- public MainWindow()
- {
- InitializeComponent();
- autoRefreshTimer = new System.Timers.Timer();
- autoRefreshTimer.Interval = 1000;
- autoRefreshTimer.Elapsed += AutoRefreshTimer_Elapsed;
- autoRefreshTimer.Enabled = false;
- httpClient = new HttpClient();
- }
- private void PrintLog(Brush color, String text)
- {
- Dispatcher.Invoke(() =>
- {
- Run t = new Run("[" + DateTime.Now.ToString() + "] ");
- t.Foreground = Brushes.Navy;
- Run tb = new Run(text);
- tb.Foreground = color;
- logPara.Inlines.Add(t);
- logPara.Inlines.Add(tb);
- logPara.Inlines.Add(new LineBreak());
- rtbLogBox.ScrollToEnd();
- });
- }
- private void AutoRefreshTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
- {
- DoStatusRefresh();
- }
- private void btnSetAutoRefresh_Click(object sender, RoutedEventArgs e)
- {
- autoRefreshTimer?.Start();
- }
- private void btnManualRefresh_Click(object sender, RoutedEventArgs e)
- {
- DoStatusRefresh();
- }
- private void btnStopAutoRefresh_Click(object sender, RoutedEventArgs e)
- {
- autoRefreshTimer?.Stop();
- }
- private void DoStatusRefresh()
- {
- Dispatcher.InvokeAsync(async () =>
- {
- try
- {
- httpClient.DefaultRequestHeaders.Remove("AuthKey");
- httpClient.DefaultRequestHeaders.Add("AuthKey", tbAccessKey.Text);
- HttpResponseMessage response = await httpClient.GetAsync(String.Format("http://{0}/", tbURLRoot.Text));
- string responseBody = await response.Content.ReadAsStringAsync();
- PrintLog(Brushes.LightBlue, "Status refreshed.");
- Dispatcher.Invoke(() =>
- {
- tbStatusDisplay.Text = responseBody;
- });
- }
- catch (Exception e)
- {
- PrintLog(Brushes.Crimson, String.Format("Failed do status refresh: {0}", e.Message));
- }
- });
- }
- private void btnConnectCmdOut_Click(object sender, RoutedEventArgs e)
- {
- if (wsClient == null)
- {
- Dispatcher.InvokeAsync(async () =>
- {
- try
- {
- var uri = new Uri(String.Format("ws://{0}/ws/get-cmd-output.satori?auth-key={1}", tbURLRoot.Text, tbAccessKey.Text));
- wsClient = new WebsocketClient(uri);
- wsClient.IsReconnectionEnabled = false;
- wsClient.MessageReceived.Subscribe(msg =>
- {
- Dispatcher.Invoke(() =>
- {
- tbCmdOut.AppendText(msg.Text);
- });
- });
- wsClient.DisconnectionHappened.Subscribe(msg =>
- {
- PrintLog(Brushes.RoyalBlue, "CmdOutput websocket Disconnected.");
- wsClient.Dispose();
- wsClient = null;
- });
- PrintLog(Brushes.RoyalBlue, "CmdOuput websocket connecting...");
- await wsClient.Start();
- PrintLog(Brushes.RoyalBlue, "CmdOuput websocket connected.");
- }
- catch (Exception err)
- {
- PrintLog(Brushes.Crimson, string.Format("Websocket error: {0}", err.Message));
- }
- });
- }
- }
- private void btnDisconnectCmdOut_Click(object sender, RoutedEventArgs e)
- {
- if (wsClient != null)
- {
- PrintLog(Brushes.RoyalBlue, "Disconnect CmdOutput websocket connection...");
- Dispatcher.InvokeAsync(async () =>
- {
- await wsClient.Stop(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "stop by user.");
- });
- }
- }
- private bool checkApiResponse(Dictionary<string, string> result)
- {
- if (result.ContainsKey("status"))
- {
- if (result["status"] == "200")
- {
- return true;
- }
- else
- {
- if (result.ContainsKey("errMsg"))
- {
- PrintLog(Brushes.Orange, string.Format("API Error: {0}", result["errMsg"]));
- PrintLog(Brushes.PaleVioletRed, serializeResponseJson(result));
- return false;
- }
- else
- {
- PrintLog(Brushes.Orange, "API Error: Unknown Error");
- PrintLog(Brushes.PaleVioletRed, serializeResponseJson(result));
- return false;
- }
- }
- }
- else
- {
- PrintLog(Brushes.Orange, "Invalid response: 'status' field not found.");
- PrintLog(Brushes.PaleVioletRed, serializeResponseJson(result));
- return false;
- }
- }
- private string serializeResponseJson(Dictionary<string, string> result)
- {
- var options = new JsonSerializerOptions { WriteIndented = true };
- string jsonString = JsonSerializer.Serialize(result, options);
- return jsonString;
- }
- private void btnCreateTaskDo_Click(object sender, RoutedEventArgs e)
- {
- JsonDef_Req_CreateTask req = new JsonDef_Req_CreateTask();
- req.TaskName = tbCreateTaskName.Text;
- req.ExecutablePath = tbCreateTaskExec.Text;
- req.WorkDir = tbCreateTaskWDir.Text;
- req.Args = CommandLineSplit.SplitArgs(tbCreateTaskArgs.Text);
- int etmo = 0;
- if (!int.TryParse(tbCreateTaskETmO.Text, out etmo))
- {
- etmo = 0;
- }
- req.ExecTimeout = etmo;
- Dispatcher.InvokeAsync(async () =>
- {
- try
- {
- httpClient.DefaultRequestHeaders.Remove("AuthKey");
- httpClient.DefaultRequestHeaders.Add("AuthKey", tbAccessKey.Text);
- HttpResponseMessage response = await httpClient.PostAsJsonAsync<JsonDef_Req_CreateTask>(
- String.Format("http://{0}/api/new-task.satori", tbURLRoot.Text),
- req
- );
- Dictionary<string, string> result = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
- if (result != null)
- {
- if (checkApiResponse(result))
- {
- if (result.ContainsKey("cpid") && result.ContainsKey("name"))
- {
- PrintLog(Brushes.SeaGreen, string.Format("Task created: CPID={0}, Name={1}", result["cpid"], result["name"]));
- btnCreateTaskGenNameUUID_Click(null, null);
- }
- else
- {
- PrintLog(Brushes.Orange, "Invalid response: missing some fields.");
- PrintLog(Brushes.PaleVioletRed, serializeResponseJson(result));
- }
- }
- else
- {
- return;
- }
- }
- else
- {
- PrintLog(Brushes.Orange, "Invalid response: null");
- }
- }
- catch (Exception e)
- {
- PrintLog(Brushes.Crimson, String.Format("Failed create task: {0}", e.Message));
- }
- });
- }
- private void btnCreateTaskGenBrowseWorkDir_Click(object sender, RoutedEventArgs e)
- {
- VistaFolderBrowserDialog fbd = new VistaFolderBrowserDialog();
- if (fbd.ShowDialog() == true)
- {
- tbCreateTaskWDir.Text = fbd.SelectedPath;
- }
- }
- private void btnCreateTaskGenBrowseExec_Click(object sender, RoutedEventArgs e)
- {
- VistaOpenFileDialog ofd = new VistaOpenFileDialog();
- ofd.DefaultExt = ".exe";
- ofd.Filter = "exe files|*.exe|All files|*";
- if (ofd.ShowDialog() == true)
- {
- tbCreateTaskExec.Text = ofd.FileName;
- }
- }
- private void btnCreateTaskGenNameUUID_Click(object sender, RoutedEventArgs e)
- {
- Guid guid = Guid.NewGuid();
- tbCreateTaskName.Text = String.Format("task-{0}", guid.ToString());
- }
- private void btnCreateDaemonBrowseExec_Click(object sender, RoutedEventArgs e)
- {
- VistaOpenFileDialog ofd = new VistaOpenFileDialog();
- ofd.DefaultExt = ".exe";
- ofd.Filter = "exe files|*.exe|All files|*";
- if (ofd.ShowDialog() == true)
- {
- tbCreateDaemonExec.Text = ofd.FileName;
- }
- }
- private void btnCreateDaemonBrowseWorkDir_Click(object sender, RoutedEventArgs e)
- {
- VistaFolderBrowserDialog fbd = new VistaFolderBrowserDialog();
- if (fbd.ShowDialog() == true)
- {
- tbCreateDaemonWDir.Text = fbd.SelectedPath;
- }
- }
- private void btnCreateDaemonDo_Click(object sender, RoutedEventArgs e)
- {
- JsonDef_Req_CreateDaemon req = new JsonDef_Req_CreateDaemon();
- req.DaemonName = tbCreateDaemonName.Text;
- req.ExecutablePath = tbCreateDaemonExec.Text;
- req.WorkDir = tbCreateDaemonWDir.Text;
- req.Args = CommandLineSplit.SplitArgs(tbCreateDaemonArgs.Text);
- req.EnableAfterCreate = cbCreateDaemonStartAfterC.IsChecked?.Value;
- Dispatcher.InvokeAsync(async () =>
- {
- try
- {
- httpClient.DefaultRequestHeaders.Remove("AuthKey");
- httpClient.DefaultRequestHeaders.Add("AuthKey", tbAccessKey.Text);
- HttpResponseMessage response = await httpClient.PostAsJsonAsync<JsonDef_Req_CreateTask>(
- String.Format("http://{0}/api/new-task.satori", tbURLRoot.Text),
- req
- );
- Dictionary<string, string> result = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
- if (result != null)
- {
- if (checkApiResponse(result))
- {
- if (result.ContainsKey("cpid") && result.ContainsKey("name"))
- {
- PrintLog(Brushes.SeaGreen, string.Format("Task created: CPID={0}, Name={1}", result["cpid"], result["name"]));
- btnCreateTaskGenNameUUID_Click(null, null);
- }
- else
- {
- PrintLog(Brushes.Orange, "Invalid response: missing some fields.");
- PrintLog(Brushes.PaleVioletRed, serializeResponseJson(result));
- }
- }
- else
- {
- return;
- }
- }
- else
- {
- PrintLog(Brushes.Orange, "Invalid response: null");
- }
- }
- catch (Exception e)
- {
- PrintLog(Brushes.Crimson, String.Format("Failed create task: {0}", e.Message));
- }
- });
- }
- private void btnSetDaemonEnable_Click(object sender, RoutedEventArgs e)
- {
- }
- private void btnSetDaemonDisable_Click(object sender, RoutedEventArgs e)
- {
- }
- }
- }
|