From b95b5c9b5046d63fc7c67eec643f7fd907aa8e6a Mon Sep 17 00:00:00 2001 From: Stricted Date: Thu, 1 Sep 2016 19:40:04 +0200 Subject: [PATCH] add new syslog parser add numeric textbox --- .../NumberTextBox.cs | 81 ++++++++++++++ ...edportHybridControl.Implementations.csproj | 1 + .../Data/SpeedportHybrid.cs | 102 ++++++++---------- .../Data/SpeedportHybridAPI.cs | 24 ++--- SpeedportHybridControl/page/TR181Page.xaml | 3 +- 5 files changed, 137 insertions(+), 74 deletions(-) create mode 100644 SpeedportHybridControl.Implementations/NumberTextBox.cs diff --git a/SpeedportHybridControl.Implementations/NumberTextBox.cs b/SpeedportHybridControl.Implementations/NumberTextBox.cs new file mode 100644 index 0000000..013750e --- /dev/null +++ b/SpeedportHybridControl.Implementations/NumberTextBox.cs @@ -0,0 +1,81 @@ +using System; +using System.Windows; +using System.Windows.Input; +using System.Windows.Controls; + +namespace SpeedportHybridControl.Implementations +{ + // https://social.msdn.microsoft.com/Forums/vstudio/en-US/fb0745f0-6c26-4a9e-b792-3f7e8484b243/allow-only-number-in-textbox?forum=wpf#8acf76ea-7667-4763-a837-473ea2b03fa5 + public class NumberTextBox : TextBox + { + static NumberTextBox() + { + EventManager.RegisterClassHandler( + typeof(NumberTextBox), + DataObject.PastingEvent, + (DataObjectPastingEventHandler)((sender, e) => + { + if (!IsDataValid(e.DataObject)) + { + DataObject data = new DataObject(); + data.SetText(String.Empty); + e.DataObject = data; + e.Handled = false; + } + })); + } + + protected override void OnDrop(DragEventArgs e) + { + e.Handled = !IsDataValid(e.Data); + base.OnDrop(e); + } + + protected override void OnDragOver(DragEventArgs e) + { + if (!IsDataValid(e.Data)) + { + e.Handled = true; + e.Effects = DragDropEffects.None; + } + + base.OnDragEnter(e); + } + + private static Boolean IsDataValid(IDataObject data) + { + Boolean isValid = false; + if (data != null) + { + String text = data.GetData(DataFormats.Text) as String; + if (!String.IsNullOrEmpty(text == null ? null : text.Trim())) + { + Int32 result = -1; + if (Int32.TryParse(text, out result)) + { + if (result > 0) + { + isValid = true; + } + } + } + } + + return isValid; + } + + protected override void OnKeyDown(KeyEventArgs e) + { + if (e.Key < Key.D0 || e.Key > Key.D9) + { + if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) + { + if (e.Key != Key.Back) + { + e.Handled = true; + } + } + } + } + } +} diff --git a/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj b/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj index ea9fc19..263347a 100644 --- a/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj +++ b/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj @@ -54,6 +54,7 @@ + diff --git a/SpeedportHybridControl/Data/SpeedportHybrid.cs b/SpeedportHybridControl/Data/SpeedportHybrid.cs index 44da76f..540b479 100644 --- a/SpeedportHybridControl/Data/SpeedportHybrid.cs +++ b/SpeedportHybridControl/Data/SpeedportHybrid.cs @@ -448,7 +448,7 @@ namespace SpeedportHybridControl.Data return; DslPageModel dsl = Application.Current.FindResource("DslPageModel") as DslPageModel; - + string response = SpeedportHybridAPI.getInstance().sendEnryptedRequest("data/dsl.json"); if (response.IsNullOrEmpty()) return; @@ -671,90 +671,76 @@ namespace SpeedportHybridControl.Data return; SyslogPageModel syslog = Application.Current.FindResource("SyslogPageModel") as SyslogPageModel; - - string response = SpeedportHybridAPI.getInstance().sendEnryptedRequest("data/SystemMessages.json"); + string response = SpeedportHybridAPI.getInstance().sendEnryptedRequest("data/Syslog.json", string.Concat("csrf_token=",SpeedportHybridAPI.getInstance().getToken(), "&exporttype=0")); if (response.IsNullOrEmpty()) return; - JToken jArray = JToken.Parse(response); - response = null; - + string[] lines = response.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); List syslogList = new List(); - foreach (JToken jToken in jArray) + foreach (string line in lines) { - JToken varid = jToken["varid"]; - if (varid.ToString().Equals("addmessage")) - { - var a = jToken["varvalue"]; - var stamp = a[1]["varvalue"]; - var msg = a[2]["varvalue"]; - - // login - if (msg.ToString().Contains("(G101)").Equals(true)) - continue; + // first line / last line + if ((line.Contains("Datum/Uhrzeit") && line.Contains("Meldung")) || line.IsNullOrEmpty()) + continue; - // logout - if (msg.ToString().Contains("(G102)").Equals(true)) - continue; + // login + if (line.Contains("(G101)").Equals(true)) + continue; - // session timeout - if (msg.ToString().Contains("(G103)").Equals(true)) - continue; + // logout + if (line.Contains("(G102)").Equals(true)) + continue; - // dnsv6 error - if (msg.ToString().Contains("(P008)").Equals(true)) - continue; + // session timeout + if (line.Contains("(G103)").Equals(true)) + continue; - // Funkzellen Info - if (msg.ToString().Contains("(LT004)") && isLTE.Equals(true)) - { - LteInfoModel lte = Application.Current.FindResource("LteInfoModel") as LteInfoModel; + // dnsv6 error + if (line.Contains("(P008)").Equals(true)) + continue; - string[] parts = msg.ToString().Split(','); - string frequenz = parts[2]; + string[] parts; - if (frequenz.Equals("20")) - { - frequenz = "800 MHz"; - } - else if (frequenz.Equals("3")) - { - frequenz = "1800 MHz"; - } - else if (frequenz.Equals("7")) - { - frequenz = "2600 MHz"; - } + // Funkzellen Info + if (line.Contains("(LT004)") && isLTE.Equals(true)) + { + LteInfoModel lte = Application.Current.FindResource("LteInfoModel") as LteInfoModel; - lte.frequenz = frequenz; + parts = line.Split(','); + string frequenz = parts[2]; - varid = null; - jArray = null; - a = null; - stamp = null; - msg = null; - syslogList = null; - return; + if (frequenz.Equals("20")) + { + frequenz = "800 MHz"; + } + else if (frequenz.Equals("3")) + { + frequenz = "1800 MHz"; + } + else if (frequenz.Equals("7")) + { + frequenz = "2600 MHz"; } - syslogList.Add(new SyslogList() { timestamp = stamp.ToString(), message = msg.ToString() }); + lte.frequenz = frequenz; - a = null; - stamp = null; - msg = null; + syslogList = null; + return; } - varid = null; + + parts = line.Split(new string[] { " " }, 3, StringSplitOptions.None); + syslogList.Add(new SyslogList() { timestamp = string.Concat(parts[0], " ", parts[1]), message = parts[2] }); } syslog.syslogList = syslogList; syslogList = null; - jArray = null; DateTime time = DateTime.Now; string format = "dd.MM.yyyy HH:mm:ss"; syslog.datetime = time.ToString(format); syslog = null; + } catch (Exception ex) { diff --git a/SpeedportHybridControl/Data/SpeedportHybridAPI.cs b/SpeedportHybridControl/Data/SpeedportHybridAPI.cs index 2dcfe37..afeed3a 100644 --- a/SpeedportHybridControl/Data/SpeedportHybridAPI.cs +++ b/SpeedportHybridControl/Data/SpeedportHybridAPI.cs @@ -141,19 +141,13 @@ namespace SpeedportHybridControl.Data JToken jArray = JToken.Parse(response); if (jArray.getVar("status").Equals("ok")) { - if (isLoggedin().Equals(true)) - { - logout = false; - } - else { - logout = true; - _password = ""; - _challenge = ""; - _cookie = new CookieContainer(); - _lastReboot = DateTime.MinValue; - _hash = ""; - _derivedk = ""; - } + logout = true; + _password = ""; + _challenge = ""; + _cookie = new CookieContainer(); + _lastReboot = DateTime.MinValue; + _hash = ""; + _derivedk = ""; } jArray = null; @@ -223,7 +217,7 @@ namespace SpeedportHybridControl.Data */ public bool isLoggedin() { - string response = sendRequest("data/SecureStatus.json"); + string response = sendRequest("data/heartbeat.json"); if (response.IsNullOrEmpty()) return false; @@ -232,7 +226,7 @@ namespace SpeedportHybridControl.Data { JToken jArray = JToken.Parse(response); - if (jArray.getVar("loginstate").Equals("1")/* && jArray.getVar("login").Equals("true")*/) + if (jArray.getVar("loginstate").Equals("1")) { login = true; } diff --git a/SpeedportHybridControl/page/TR181Page.xaml b/SpeedportHybridControl/page/TR181Page.xaml index 9aa2e7e..7377bbb 100644 --- a/SpeedportHybridControl/page/TR181Page.xaml +++ b/SpeedportHybridControl/page/TR181Page.xaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SpeedportHybridControl" + xmlns:Implementations="clr-namespace:SpeedportHybridControl.Implementations;assembly=SpeedportHybridControl.Implementations" mc:Ignorable="d" Width="Auto" Height="Auto" Title="TR181Page"> @@ -52,7 +53,7 @@ - +