/// <summary> /// Updates a log window by adding text to it /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void WriteToLog(object sender, TCSMessageEventArgs args) { Dispatcher.Invoke(new Action(() => { if (ChoosenVerbosity == Verbosity.Important && args.Verbosity == Verbosity.Verbose) { return; } RichTextBox recipientTextBox = args.Recipient == Recipient.HttpServerTextBox ? richTextBox_logHTTPServer : args.Recipient == Recipient.TwinCatTextBox ? richTextBox_logTwinCAT : null; TextRange tr = new TextRange(recipientTextBox.Document.ContentEnd, recipientTextBox.Document.ContentEnd); tr.Text = Environment.NewLine + "[" + args.When.ToString("hh-mm-ss.fff") + "] "; tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Gray); TextRange tr2 = new TextRange(recipientTextBox.Document.ContentEnd, recipientTextBox.Document.ContentEnd); string filler = args.Category == LogTextCategory.Incoming ? "> " : args.Category == LogTextCategory.Outgoing ? "< " : " "; tr2.Text = filler + args.Message; var brush = args.Category == LogTextCategory.Error ? Brushes.Red : args.Category == LogTextCategory.Warning ? Brushes.Orange : args.Category == LogTextCategory.Info ? Brushes.LightGray : args.Category == LogTextCategory.Incoming ? Brushes.LightBlue : Brushes.LightGreen; tr2.ApplyPropertyValue(TextElement.ForegroundProperty, brush); })); }
/// <summary> /// Triggered when http connect button is hit /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_connectHttp_Click(object sender, RoutedEventArgs e) { int port = int.Parse(this.textBox_httpPort.Text); if (AspThread == null) { var tmpLogArgs = new TCSMessageEventArgs(); tmpLogArgs.Message = string.Format("Starting ASP.NET WebAPI to listen for incoming requests."); tmpLogArgs.Verbosity = Verbosity.Important; tmpLogArgs.Category = LogTextCategory.Info; tmpLogArgs.Recipient = Recipient.HttpServerTextBox; tmpLogArgs.When = DateTime.Now; Logging.SendMessage(tmpLogArgs); AspThread = new Thread(p => { AspHost = WebAPISession.RunAsp((int)p); AspHost.Run(); }); AspThread.Start(port); tmpLogArgs.When = DateTime.Now; string hostName = Dns.GetHostName(); tmpLogArgs.Message = "..Done! Available request routes: "; tmpLogArgs.Message += Logging.NewLineTab + "http://" + hostName + ":" + port + "/twincat/"; tmpLogArgs.Message += Logging.NewLineTab + "http://localhost:" + port + "/twincat/"; tmpLogArgs.Message += Logging.NewLineTab + "http://127.0.0.1:" + port + "/twincat/"; foreach (IPAddress ip in Dns.GetHostAddresses(hostName)) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { tmpLogArgs.Message += Logging.NewLineTab + "http://" + ip.ToString() + ":" + port + "/twincat/"; } } tmpLogArgs.Verbosity = Verbosity.Verbose; Logging.SendMessage(tmpLogArgs); } else { AspHost?.StopAsync().Wait(); AspThread?.Abort(); AspThread = null; } if (InternalServer == null || InternalServer.listener == null || !InternalServer.listener.IsListening || !InternalServer.Running) { HttpThread = new Thread(p => { InternalServer.Start((int)p); }); HttpThread.Start(port + 1); } else { Task.Run(new Action(() => { try { //will throw an error, bc it's shutting down the server it actively tries to call InternalWorkerClient.GetAsync("http://localhost:" + InternalServer.Port.ToString() + "/btn_shutdown").Wait(); } finally { HttpThread?.Abort(); } })); } }