This repository has been archived by the owner on Dec 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
151 lines (127 loc) · 5.36 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Net;
using Newtonsoft.Json;
using System;
using System.Text;
using System.IO;
using NativeWifi;
using System.Collections.ObjectModel;
using System.Windows.Media;
namespace mifi_status
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private int _oldBatteryPercentage = 100;
private int _lowBatteryPercentage = 25;
private WlanClient _wlan;
public MainWindow()
{
InitializeComponent();
}
public void RefreshData()
{
string ssid0;
try
{
if (_wlan == null)
{
_wlan = new WlanClient();
}
Collection<string> connectedSsids = new Collection<string>();
foreach (var wlanInterface in _wlan.Interfaces)
{
var ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
connectedSsids.Add(new string(Encoding.ASCII.GetChars(ssid.SSID, 0, (int)ssid.SSIDLength)));
}
ssid0 = connectedSsids[0];
}
catch (Exception)
{
ssid0 = "";
}
if (ssid0 == "Albatross")
{
try
{
var request = (HttpWebRequest)WebRequest.Create("http://192.168.0.1/cgi-bin/qcmap_web_cgi");
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes("{\"module\":\"status\",\"action\":0}");
request.ContentLength = data.Length;
var stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
var response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
if (stream == null) return;
var reader = new StreamReader(stream);
var serializedJson = reader.ReadToEnd();
dynamic json = JsonConvert.DeserializeObject(serializedJson);
string[] connections = { "disable", "disconnected", "connecting", "disconnecting", "connected" };
string[] networks = { "no service", "GSM", "3G", "LTE", "TD-SCDMA", "CDMA 1x", "CDMA EVDO" };
string[] sims = { "invalid", "no SIM", "error", "ready", "PIN requested", "PIN verified", "PUK requested", "permanently locked" };
var voltage = Convert.ToInt32(json.battery.voltage);
StatusConn.Content = connections[json.wan.connectStatus];
StatusBatt.Content = json.battery.voltage + "%";
StatusNet.Content = networks[json.wan.networkType] + " (" + json.wan.signalStrength + "/4)";
StatusSim.Content = sims[json.wan.simStatus];
StatusClients.Content = json.connectedDevices.number;
StatusMessages.Content = json.message.unreadMessages;
var ds = Convert.ToDouble(json.wan.dailyStatistics);
var ts = Convert.ToDouble(json.wan.totalStatistics);
StatusUse.Content = GetFilesizeHuman(ds, 2) + " / " + GetFilesizeHuman(ts, 2);
var rxs = Convert.ToDouble(json.wan.rxSpeed);
var txs = Convert.ToDouble(json.wan.txSpeed);
StatusSpeed.Content = GetFilesizeHuman(txs, 1) + "/s / " + GetFilesizeHuman(rxs, 1) + "/s";
LowBatteryNotification(voltage);
}
catch (Exception)
{
SetAllToEmptyString();
}
}
else
{
SetAllToEmptyString();
}
}
private void SetAllToEmptyString()
{
const string empty = "–";
StatusConn.Content = empty;
StatusBatt.Content = empty;
StatusNet.Content = empty;
StatusSim.Content = empty;
StatusUse.Content = empty;
StatusSpeed.Content = empty;
StatusClients.Content = empty;
}
private static string GetFilesizeHuman(double len, int decimalPlaces = 0)
{
string[] sizes = { "B", "KB", "MB", "GB" };
var order = 0;
while (len >= 1024 && ++order < sizes.Length)
{
len = len / 1024;
}
var decimalPlacesString = "";
while (decimalPlaces > 0)
{
decimalPlacesString += "0";
decimalPlaces--;
}
// ReSharper disable FormatStringProblem
return string.Format("{0:0." + decimalPlacesString + "} {1}", len, sizes[order]);
}
private void LowBatteryNotification(int percentage)
{
if (_oldBatteryPercentage == percentage) return;
_oldBatteryPercentage = percentage;
var brush = percentage <= _lowBatteryPercentage ? new SolidColorBrush(Colors.Orange) : new SolidColorBrush(Colors.Transparent);
LabelBatt.Background = brush;
StatusBatt.Background = brush;
}
}
}