write ErrorLog if the LTE Module is not reachable
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl.Implementations / util.cs
1 using Microsoft.Win32;
2 using Newtonsoft.Json.Linq;
3 using System;
4 using System.Linq;
5 using System.Net;
6 using System.Net.NetworkInformation;
7 using System.Net.Sockets;
8 using System.Security.Cryptography;
9 using System.Text;
10 using System.Threading;
11 using System.Windows;
12 using System.Windows.Media;
13 using System.Xml;
14
15 namespace 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 /**
25 * get sha256
26 *
27 * @param string password
28 * @return string
29 */
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 /**
39 * get pbkdf2
40 *
41 * @param string password
42 * @param string salt
43 * @param int iterations
44 * @param int length
45 * @return string
46 */
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 /**
56 * get specific value from JToken
57 *
58 * @param JToken jArray
59 * @param string varid
60 * @return string
61 */
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 /**
85 * check if string is empty or null
86 *
87 * @param string value
88 * @return bool
89 */
90 public static bool IsNullOrEmpty(this string value)
91 {
92 return string.IsNullOrEmpty(value);
93 }
94
95 /**
96 * convert string to int
97 *
98 * @param string value
99 * @return int
100 */
101 public static int ToInt(this string value)
102 {
103 int b = 0;
104 int.TryParse(value, out b);
105
106 return b;
107 }
108
109 /**
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 */
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 }
139 else if (rsrp <= -126)
140 {
141 return Brushes.DarkRed;
142 }
143
144 return Brushes.Transparent;
145 }
146
147 /**
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 */
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 /**
185 * check for update
186 */
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 }
246
247 public static bool checkLteModul ()
248 {
249 Ping ping = new Ping();
250 try
251 {
252 PingReply reply = ping.Send("172.10.10.2", 2);
253
254 if (reply.Status == IPStatus.Success)
255 {
256 return true;
257 }
258 else
259 {
260 LogManager.WriteToLog("unable to reach LTE Modul");
261 }
262 }
263 catch (PingException) {
264 LogManager.WriteToLog("unable to reach LTE Modul");
265 }
266
267 return false;
268 }
269
270 public static void sendCommandToLteModul (string Command)
271 {
272 if (checkLteModul().Equals(true))
273 {
274 try
275 {
276 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
277 IPAddress serverAddr = IPAddress.Parse("172.10.10.1");
278 IPEndPoint endPoint = new IPEndPoint(serverAddr, 1280);
279 byte[] cmd = Encoding.ASCII.GetBytes(Command);
280 sock.SendTo(cmd, endPoint);
281 sock.Close();
282 }
283 catch (Exception)
284 {
285 new Thread(() => { MessageBox.Show("couldn't send Command to LTE Modul", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
286 LogManager.WriteToLog("couldn't send Command to LTE Modul");
287 }
288 }
289 }
290 }
291 }