update some stuff
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl.Implementations / util.cs
CommitLineData
491f6e3b
S
1using Newtonsoft.Json.Linq;
2using System;
a3157ac3 3using System.Linq;
491f6e3b
S
4using System.Security.Cryptography;
5using System.Text;
6using System.Threading;
7using System.Windows;
8using System.Windows.Media;
9using System.Xml;
10
11namespace SpeedportHybridControl.Implementations {
12 public static class util {
a3157ac3
S
13 public static byte[] HexToByte (string hex) {
14 return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
15 }
16
491f6e3b
S
17 /**
18 * get sha256
19 *
20 * @param string password
21 * @return string
22 */
23 public static string sha256 (this string password) {
24 SHA256Managed crypt = new SHA256Managed();
25 string hash = BitConverter.ToString(crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password)));
26 crypt = null;
27 return hash.Replace("-", "").ToLower();
28 }
29
30 /**
31 * get pbkdf2
32 *
33 * @param string password
34 * @param string salt
35 * @param int iterations
36 * @param int length
37 * @return string
38 */
39 public static string pbkdf2 (this string password, string salt, int iterations = 1000, int length = 16) {
40 Rfc2898DeriveBytes hash = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), iterations);
41 string derivedk = BitConverter.ToString(hash.GetBytes(length));
42 hash = null;
43 return derivedk.Replace("-", "").ToLower(); ;
44 }
45
46 /**
47 * get specific value from JToken
48 *
49 * @param JToken jArray
50 * @param string varid
51 * @return string
52 */
53 public static string getVar (this JToken jArray, string varid) {
54 foreach (JToken jToken in jArray) {
55 JToken jVarid = jToken["varid"];
56 if (jVarid.ToString().Equals(varid)) {
57 jVarid = null;
58 jArray = null;
59 varid = null;
60 return jToken["varvalue"].ToString();
61 }
62
63 jVarid = null;
64 }
65
66 jArray = null;
67 varid = null;
68
69 return string.Empty;
70 }
71
72 /**
73 * check if string is empty or null
74 *
75 * @param string value
76 * @return bool
77 */
78 public static bool IsNullOrEmpty (this string value) {
79 return string.IsNullOrEmpty(value);
80 }
81
82 /**
83 * convert string to int
84 *
85 * @param string value
86 * @return int
87 */
88 public static int ToInt (this string value) {
89 int b = 0;
90 int.TryParse(value, out b);
91
92 return b;
93 }
94
95 /**
96 * calculate the background color for rsrp
97 * see http://www.lte-anbieter.info/technik/rsrp.php
98 *
99 * @param int rsrp
100 * @return Brush
101 */
102 public static Brush getRSRPColor (int rsrp) {
103 if (rsrp >= -65) {
104 return Brushes.DarkGreen;
105 }
106 else if (rsrp <= -66 && rsrp >= -83) {
107 return Brushes.Green;
108 }
109 else if (rsrp <= -84 && rsrp >= -95) {
110
111 return Brushes.Yellow;
112 }
113 else if (rsrp <= -96 && rsrp >= -105) {
114 return Brushes.Orange;
115 }
116 else if (rsrp <= -106 && rsrp >= -125) {
117 return Brushes.Red;
118 }
119 else if (rsrp >= -126) {
120 return Brushes.DarkRed;
121 }
122
123 return Brushes.Transparent;
124 }
125
126 /**
127 * calculate the background color for rsrq
128 * see http://www.lte-anbieter.info/technik/rsrq.php
129 *
130 * @param int rsrp
131 * @return Brush
132 */
133 public static Brush getRSRQColor (int rsrq) {
134 if (rsrq >= -3) {
135 return Brushes.DarkGreen;
136 }
137 else if (rsrq <= -4 && rsrq >= -6) {
138 return Brushes.Green;
139 }
140 else if (rsrq <= -7 && rsrq >= -8) {
141 return Brushes.Yellow;
142 }
143 else if (rsrq <= -9 && rsrq >= -11) {
144 return Brushes.Orange;
145 }
146 else if (rsrq <= -12 && rsrq >= -15) {
147 return Brushes.Red;
148 }
149 else if (rsrq <= -16 && rsrq >= -20) {
150 return Brushes.DarkRed;
151 }
152
153 return Brushes.Transparent;
154 }
155
156 /**
157 * check for update
158 */
159 /*
160 public static bool checkUpdate () {
161 try {
162 XmlDocument xmlDocument = new XmlDocument();
163 xmlDocument.Load("https://stricted.net/version.xml");
164
165 string version = xmlDocument.DocumentElement["version"].InnerText;
166 if (MainWindow.VERSION.Equals(version).Equals(false)) {
167 return true;
168 }
169 }
170 catch (Exception ex) {
171 Console.WriteLine(ex.Message);
172 }
173
174 return false;
175 }
176 */
177
178 /**
179 * process login stuff
180 */
181 /*
182 public static void login () {
183 try {
184 Application.Current.Dispatcher.BeginInvoke(new Action(() => {
185 MainWindow MainWindow = Application.Current.MainWindow as MainWindow;
186
187 // LoginPage
188 LoginPage LoginPage = MainWindow.loginPage as LoginPage;
189 LoginPage.button1.Content = "Logout";
190 LoginPage.PasswordCheckBox.Visibility = Visibility.Hidden;
191 LoginPage.tbip.Visibility = Visibility.Hidden;
192 LoginPage.diTextBlock.Visibility = Visibility.Hidden;
193 LoginPage.tbpw.Visibility = Visibility.Hidden;
194 LoginPage.PasswordBox.Visibility = Visibility.Hidden;
195 LoginPage.cbSave.Visibility = Visibility.Hidden;
196
197 // MainWindow
198 MainWindow.btnLogin.Content = "Logout";
199
200 MainWindow.btnOverview.IsEnabled = true;
201 MainWindow.btnDsl.IsEnabled = true;
202 MainWindow.btnLteInfo.IsEnabled = true;
203 MainWindow.btnSyslog.IsEnabled = true;
204 MainWindow.btnTR181.IsEnabled = true;
205 MainWindow.btnPhone.IsEnabled = true;
206 MainWindow.btnLan.IsEnabled = true;
207 MainWindow.btnControls.IsEnabled = true;
208 }));
209 }
210 catch (Exception ex) {
211 Console.WriteLine(ex.Message);
212 }
213 }
214 */
215 /**
216 * process logout stuff
217 */
218 /*
219 public static void logout () {
220 try {
221 Application.Current.Dispatcher.BeginInvoke(new Action(() => {
222 MainWindow MainWindow = Application.Current.MainWindow as MainWindow;
223
224 // LteInfoPage
225 LteInfoPage lteInfo = MainWindow.lteInfoPage as LteInfoPage;
226 if (Object.Equals(lteInfo._ltepopup, null).Equals(false)) {
227 lteInfo._ltepopup.StopTimer();
228 lteInfo._ltepopup.Hide();
229 lteInfo._ltepopup = null;
230 }
231 lteInfo.StopTimer();
232
233 // DslPage
234 DslPage dslInfo = MainWindow.dslPage as DslPage;
235 dslInfo.StopTimer();
236
237 // LoginPage
238 LoginPage LoginPage = MainWindow.loginPage as LoginPage;
239 LoginPage.PasswordBox.Visibility = Visibility.Visible;
240 LoginPage.tbip.Visibility = Visibility.Visible;
241 LoginPage.diTextBlock.Visibility = Visibility.Visible;
242 LoginPage.tbpw.Visibility = Visibility.Visible;
243 LoginPage.PasswordCheckBox.Visibility = Visibility.Visible;
244 LoginPage.button1.Content = "Login";
245 LoginPage.cbSave.Visibility = Visibility.Visible;
246 LoginPage.PasswordBox.Focus();
247
248 // MainWindow
249 MainWindow.btnLogin.Content = "Login";
250 MainWindow.frame.Content = MainWindow.loginPage;
251 MainWindow.changeColor(MainWindow.btnLogin);
252
253 MainWindow.btnOverview.IsEnabled = false;
254 MainWindow.btnDsl.IsEnabled = false;
255 MainWindow.btnLteInfo.IsEnabled = false;
256 MainWindow.btnSyslog.IsEnabled = false;
257 MainWindow.btnTR181.IsEnabled = false;
258 MainWindow.btnPhone.IsEnabled = false;
259 MainWindow.btnLan.IsEnabled = false;
260 MainWindow.btnControls.IsEnabled = false;
261 }));
262 }
263 catch (Exception ex) {
264 Console.WriteLine(ex.Message);
265 }
266 }
267 */
268 }
269}