update setLteFrequency method
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl.Implementations / util.cs
CommitLineData
a8e0f728
S
1using Microsoft.Win32;
2using Newtonsoft.Json.Linq;
491f6e3b 3using System;
a3157ac3 4using System.Linq;
a554ec14
S
5using System.Net;
6using System.Net.NetworkInformation;
7using System.Net.Sockets;
491f6e3b
S
8using System.Security.Cryptography;
9using System.Text;
3a99b353
S
10using System.Threading;
11using System.Windows;
491f6e3b
S
12using System.Windows.Media;
13using System.Xml;
14
a5a62e8d
S
15namespace SpeedportHybridControl.Implementations
16{
17 public static class util
18 {
19 public static byte[] HexToByte(string hex)
20 {
21 return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
22 }
23
24 /**
491f6e3b
S
25 * get sha256
26 *
27 * @param string password
28 * @return string
29 */
a5a62e8d
S
30 public static string sha256(this string password)
31 {
32 SHA256Managed crypt = new SHA256Managed();
33 string hash = BitConverter.ToString(crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password)));
34 crypt = null;
35 return hash.Replace("-", "").ToLower();
36 }
37
38 /**
491f6e3b
S
39 * get pbkdf2
40 *
41 * @param string password
42 * @param string salt
43 * @param int iterations
44 * @param int length
45 * @return string
46 */
a5a62e8d
S
47 public static string pbkdf2(this string password, string salt, int iterations = 1000, int length = 16)
48 {
49 Rfc2898DeriveBytes hash = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), iterations);
50 string derivedk = BitConverter.ToString(hash.GetBytes(length));
51 hash = null;
52 return derivedk.Replace("-", "").ToLower(); ;
53 }
54
55 /**
491f6e3b
S
56 * get specific value from JToken
57 *
58 * @param JToken jArray
59 * @param string varid
60 * @return string
61 */
a5a62e8d
S
62 public static string getVar(this JToken jArray, string varid)
63 {
64 foreach (JToken jToken in jArray)
65 {
66 JToken jVarid = jToken["varid"];
67 if (jVarid.ToString().Equals(varid))
68 {
69 jVarid = null;
70 jArray = null;
71 varid = null;
72 return jToken["varvalue"].ToString();
73 }
74
75 jVarid = null;
76 }
77
78 jArray = null;
79 varid = null;
80
81 return string.Empty;
82 }
83
84 /**
491f6e3b
S
85 * check if string is empty or null
86 *
87 * @param string value
88 * @return bool
89 */
a5a62e8d
S
90 public static bool IsNullOrEmpty(this string value)
91 {
92 return string.IsNullOrEmpty(value);
93 }
491f6e3b 94
a5a62e8d 95 /**
491f6e3b
S
96 * convert string to int
97 *
98 * @param string value
99 * @return int
100 */
a5a62e8d
S
101 public static int ToInt(this string value)
102 {
103 int b = 0;
104 int.TryParse(value, out b);
491f6e3b 105
a5a62e8d
S
106 return b;
107 }
491f6e3b 108
a5a62e8d 109 /**
491f6e3b
S
110 * calculate the background color for rsrp
111 * see http://www.lte-anbieter.info/technik/rsrp.php
112 *
113 * @param int rsrp
114 * @return Brush
115 */
a5a62e8d
S
116 public static Brush getRSRPColor(int rsrp)
117 {
118 if (rsrp >= -65)
119 {
120 return Brushes.DarkGreen;
121 }
122 else if (rsrp <= -66 && rsrp >= -83)
123 {
124 return Brushes.Green;
125 }
126 else if (rsrp <= -84 && rsrp >= -95)
127 {
128
129 return Brushes.Yellow;
130 }
131 else if (rsrp <= -96 && rsrp >= -105)
132 {
133 return Brushes.Orange;
134 }
135 else if (rsrp <= -106 && rsrp >= -125)
136 {
137 return Brushes.Red;
138 }
d12d28e4 139 else if (rsrp <= -126)
a5a62e8d
S
140 {
141 return Brushes.DarkRed;
142 }
143
144 return Brushes.Transparent;
145 }
146
147 /**
491f6e3b
S
148 * calculate the background color for rsrq
149 * see http://www.lte-anbieter.info/technik/rsrq.php
150 *
151 * @param int rsrp
152 * @return Brush
153 */
a5a62e8d
S
154 public static Brush getRSRQColor(int rsrq)
155 {
156 if (rsrq >= -3)
157 {
158 return Brushes.DarkGreen;
159 }
160 else if (rsrq <= -4 && rsrq >= -6)
161 {
162 return Brushes.Green;
163 }
164 else if (rsrq <= -7 && rsrq >= -8)
165 {
166 return Brushes.Yellow;
167 }
168 else if (rsrq <= -9 && rsrq >= -11)
169 {
170 return Brushes.Orange;
171 }
172 else if (rsrq <= -12 && rsrq >= -15)
173 {
174 return Brushes.Red;
175 }
176 else if (rsrq <= -16 && rsrq >= -20)
177 {
178 return Brushes.DarkRed;
179 }
180
181 return Brushes.Transparent;
182 }
183
184 /**
491f6e3b
S
185 * check for update
186 */
a5a62e8d
S
187 public static bool checkUpdate(string currentVersion)
188 {
189 try
190 {
191 XmlDocument xmlDocument = new XmlDocument();
192 xmlDocument.Load("https://stricted.net/version.xml");
193
194 string version = xmlDocument.DocumentElement["version"].InnerText;
195 if (currentVersion.Equals(version).Equals(false))
196 {
197 return true;
198 }
199 }
200 catch (Exception ex)
201 {
202 Console.WriteLine(ex.Message);
203 }
204
205 return false;
206 }
207
208 public static bool checkInstalled(string c_name)
209 {
210 string displayName = string.Empty;
211
212 string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
213 RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
214 if (key != null)
215 {
216 foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
217 {
218 displayName = subkey.GetValue("DisplayName") as string;
219
220 if (string.IsNullOrWhiteSpace(displayName).Equals(false) && displayName.Equals(c_name))
221 {
222 return true;
223 }
224 }
225 key.Close();
226 }
227
228 registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
229 key = Registry.LocalMachine.OpenSubKey(registryKey);
230 if (key != null)
231 {
232 foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
233 {
234 displayName = subkey.GetValue("DisplayName") as string;
235
236 if (string.IsNullOrWhiteSpace(displayName).Equals(false) && displayName.Equals(c_name))
237 {
238 return true;
239 }
240 }
241 key.Close();
242 }
243
244 return false;
245 }
a554ec14
S
246
247 public static bool checkLteModul ()
248 {
249 Ping ping = new Ping();
3a99b353 250 try
a554ec14 251 {
54802b99 252 PingReply reply = ping.Send("172.10.10.1", 2);
3a99b353
S
253
254 if (reply.Status == IPStatus.Success)
255 {
256 return true;
257 }
54802b99 258#if DEBUG
ff591fe6
S
259 else
260 {
261 LogManager.WriteToLog("unable to reach LTE Modul");
262 }
54802b99 263#endif
ff591fe6
S
264 }
265 catch (PingException) {
54802b99 266#if DEBUG
ff591fe6 267 LogManager.WriteToLog("unable to reach LTE Modul");
54802b99 268#endif
a554ec14
S
269 }
270
271 return false;
272 }
273
e8057c7c 274 public static void sendCommandToLteModul(string Command)
a554ec14 275 {
e8057c7c 276 if (checkLteModul().Equals(true) && Command.IsNullOrEmpty().Equals(false))
a554ec14 277 {
3a99b353
S
278 try
279 {
280 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
281 IPAddress serverAddr = IPAddress.Parse("172.10.10.1");
282 IPEndPoint endPoint = new IPEndPoint(serverAddr, 1280);
283 byte[] cmd = Encoding.ASCII.GetBytes(Command);
284 sock.SendTo(cmd, endPoint);
285 sock.Close();
286 }
287 catch (Exception)
288 {
54802b99 289 new Thread(() => { MessageBox.Show("couldn't send Command to LTE Modul", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
3a99b353
S
290 LogManager.WriteToLog("couldn't send Command to LTE Modul");
291 }
a554ec14
S
292 }
293 }
e8057c7c 294
3f1e484e 295 public static void setLteFrequency (LTEBand band)
e8057c7c
S
296 {
297 /**
3f1e484e 298 * possible lte frequency band commands:
e8057c7c
S
299 *
300 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80000,, # 800
301 * AT^SYSCFGEX="03",3FFFFFFF,3,1,4,, # 1800
302 * AT^SYSCFGEX="03",3FFFFFFF,3,1,40,, # 2600
303 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80044,, # 800 | 1800 | 2600
304 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80004,, # 800 | 1800
305 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80040,, # 800 | 2600
306 * AT^SYSCFGEX="03",3FFFFFFF,3,1,44,, # 1800 | 2600
307 */
3f1e484e 308 string Command = string.Concat("AT^SYSCFGEX=\"03\",3FFFFFFF,3,1,", (int)band, ",,");
e8057c7c
S
309
310 sendCommandToLteModul(Command);
311 }
a5a62e8d 312 }
491f6e3b 313}