-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeDisplayPane.cpp
More file actions
268 lines (215 loc) · 7.34 KB
/
NodeDisplayPane.cpp
File metadata and controls
268 lines (215 loc) · 7.34 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdio.h>
#include <algorithm> // max()
#include <iostream>
#include <sstream>
#include <iomanip>
#include <wx/tooltip.h>
#include "Config.h"
extern ConfigInfo config;
#include "Node.h"
#include "NodeDisplayPane.h"
#include "ColorInterpolate.h"
NodeDisplayPane::NodeDisplayPane(wxWindow *parent, const wxSize &cell_size)
: wxTextCtrl(parent, wxID_ANY, wxT(""), wxDefaultPosition, cell_size,
wxTE_MULTILINE|wxTE_READONLY|wxTE_NO_VSCROLL|wxTE_RICH2)
{
start_color.Set(start_r, start_g, start_b);
response_color.setColors(fast_color_r, fast_color_g, fast_color_b,
slow_color_r, slow_color_g, slow_color_b);
fail_color.setColors(min_fails_color_r, min_fails_color_g, min_fails_color_b,
max_fails_color_r, max_fails_color_g, max_fails_color_b);
// SetMinSize(cell_size);
SetMargins(wxPoint(2,-1));
}
void NodeDisplayPane::update()
{
Update();
}
void NodeDisplayPane::updateDisplay(const Node &node)
{
wxColour *bg_color;
// Figure out the color for the background
if (node.num_fails < 0) {
bg_color = new wxColour(start_color);
}
else if (node.num_fails > 0) {
double factor = 0.0;
if (static_cast<unsigned int>(node.num_fails) >= config.max_num_fails)
factor = 1.0;
else
factor = static_cast<double>(node.num_fails)/static_cast<double>(config.max_num_fails);
bg_color = fail_color.color(factor);
}
else {
double factor = 0.0;
if (node.last_response_time >= config.max_response_time)
factor = 1.0;
else
factor = static_cast<double>(node.last_response_time) / static_cast<double>(config.max_response_time);
bg_color = response_color.color(factor);
}
// Clear the pane
Clear();
SetBackgroundColour(*bg_color);
ClearBackground();
// Print the node name in bold
wxTextAttr ta = GetDefaultStyle();
ta.SetFontWeight(wxFONTWEIGHT_BOLD);
ta.SetBackgroundColour(*bg_color);
SetDefaultStyle(ta);
if (node.name.empty())
AppendText(node.url);
else
AppendText(node.name);
// Print the rest normally
ta.SetFontWeight(wxFONTWEIGHT_NORMAL);
ta.SetBackgroundColour(*bg_color);
SetDefaultStyle(ta);
delete bg_color;
if (node.num_fails < 0)
{
// Show nothing else initially
}
else if (node.num_fails == 0)
{
std::stringstream node_info;
if (!node.wifi_ip.empty())
node_info << " WIFI:" << node.wifi_ip;
if (!node.lan_ip.empty())
node_info << " LAN:" << node.lan_ip;
if (!node.wan_ip.empty())
node_info << " WAN:" << node.wan_ip;
node_info << std::endl;
char info[200];
sprintf(info, "Channel: %3d BW: %2.0f, SSID: %s\n",
node.channel, node.chanbw, node.ssid.c_str());
node_info << info;
sprintf(info, "Model: %s, Firmware: %s %s\n", node.model.c_str(),
node.firmware_mfg.c_str(),
node.firmware_version.c_str());
node_info << info;
std::string last_time = node.last_succesful_probe_time.Format("%X %x").ToStdString();
sprintf(info, "Last Response Time: %6.2f seconds at %s",
static_cast<double>(node.last_response_time) / 1000.0,
last_time.c_str());
node_info << info;
if (config.display_mode == NODE_DISPLAY_PANE)
{
AppendText(node_info.str());
}
else if (config.display_mode == NODE_DISPLAY_PANE_ONE_LINE_STATUS)
{
const unsigned int pane_width = GetSize().x;
const std::string curr_text = GetLineText(0).ToStdString();
wxTextAttr ta = GetDefaultStyle();
ta.SetFontWeight(wxFONTWEIGHT_BOLD);
SetDefaultStyle(ta);
Update();
const unsigned int curr_text_width = GetTextExtent(curr_text).x;
ta.SetFontWeight(wxFONTWEIGHT_NORMAL);
SetDefaultStyle(ta);
Update();
const unsigned int space_width = GetTextExtent(" ").x;
// See if we can add the channel in the space available
std::stringstream text;
text << " ch." << node.channel;
#ifdef __WINDOWS__
#define FUDGE_FACTOR 20
#else
#define FUDGE_FACTOR 7
#endif
const unsigned int new_text_width = GetTextExtent(text.str()).x;
const unsigned int full_text_width = curr_text_width + new_text_width + FUDGE_FACTOR*space_width;
// The fudge factor tries to make it work cross-platform
if (full_text_width < pane_width)
AppendText(text.str());
std::string tool_tip = std::string("Node stats for ") + node.name;
tool_tip += node_info.str();
SetToolTip(tool_tip);
}
Update();
// Right-click menu: https://stackoverflow.com/questions/14487102/wxwidgets-contextmenu-popup
}
else
{
wxDateTime now = wxDateTime::Now();
const bool no_successful_probe = node.last_succesful_probe_time == node.start_time;
const wxTimeSpan delta_time = now.Subtract(node.last_succesful_probe_time);
const unsigned long int nsecs = static_cast<unsigned int>(delta_time.GetSeconds().ToDouble());
const unsigned int num_days = nsecs / (24*60*60);
const unsigned int num_hours = (nsecs - ((24*60*60) * num_days)) / (60*60);
const unsigned int num_minutes = (nsecs - ((24*60*60) * num_days) - ((60*60) * num_hours)) / 60;
const unsigned int num_seconds = nsecs - ((24*60*60) * num_days) - ((60*60) * num_hours) - (60 * num_minutes);
const bool non_zero_delta_time = ((num_days > 0) or (num_hours > 0) or (num_minutes > 0) or (num_seconds > 0.0));
std::stringstream fail_info;
fail_info << std::endl;
if (no_successful_probe)
{
fail_info << "No successful access ";
if (non_zero_delta_time)
fail_info << "since ";
}
else
{
std::string last_time = node.last_succesful_probe_time.Format("%X %x").ToStdString();
fail_info << "Last successful access at " << last_time << ", " << std::endl;
}
if (num_days > 0)
fail_info << num_days << " days ";
if (num_hours > 0)
fail_info << num_hours << " hours ";
if (num_minutes > 0)
fail_info << num_minutes << " minutes ";
if (num_seconds > 0.0)
fail_info << num_seconds << " seconds";
if (non_zero_delta_time)
fail_info << " ago ";
if (no_successful_probe)
{
std::string start_time = node.start_time.Format("%X %x").ToStdString();
if (non_zero_delta_time)
fail_info << std::endl << "at ";
else
fail_info << std::endl << "since ";
fail_info << start_time << ", ";
}
fail_info << "(num failures: " << node.num_fails << ")";
if (config.display_mode == NODE_DISPLAY_PANE)
{
AppendText(fail_info.str());
}
else if (config.display_mode == NODE_DISPLAY_PANE_ONE_LINE_STATUS)
{
// Do not display anything extra for failing nodes in this mode
// std::stringstream ss;
// if (no_successful_probe)
// {
// std::string start_time = node.start_time.Format("%X %x").ToStdString();
// ss << " " << "No access since " << start_time << ", " << node.num_fails << " fails";
// }
// else
// {
// std::string last_time = node.last_succesful_probe_time.Format("%X %x").ToStdString();
// ss << " " << "Last access at " << last_time << ", " << node.num_fails << " fails";
// }
//
// AppendText(ss.str());
std::string tool_tip = std::string("Node failure stats for ") + node.name;
tool_tip += fail_info.str();
SetToolTip(tool_tip);
}
Update();
}
}
int NodeDisplayPane::numRows() const
{
const int char_height = GetCharHeight();
const wxSize win_size = GetSize();
return win_size.y / char_height;
}
int NodeDisplayPane::numCols() const
{
const int char_width = GetCharWidth();
const wxSize win_size = GetSize();
return win_size.x / char_width;
}