forked from noroadsleft000/gnome-network-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppController.js
More file actions
216 lines (198 loc) · 7.92 KB
/
AppController.js
File metadata and controls
216 lines (198 loc) · 7.92 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
import GLib from "gi://GLib";
import { AppView } from "./ui/AppView.js";
import { DisplayMode } from "./utils/Constants.js";
import { getNextResetTime } from "./utils/DateTimeUtils.js";
import { getTitleClickedMessageBroadcaster } from "./utils/Broadcasters.js";
const kOneMinuteInMilliSeconds = 60 * 1000;
/*
* AppControlller class responsible for running timers,
* refreshing view and pushing model updates to UI.
*/
export class AppController {
constructor(logger, appSettingsModel, deviceModel) {
this._logger = logger;
this._appSettingsModel = appSettingsModel;
this._deviceModel = deviceModel;
this._appView = new AppView(logger, appSettingsModel);
this._refreshTimeout = undefined;
this._minuteTimeout = undefined;
this._rightClickSubscribeHandle = undefined;
this._settingsSubscribeHandle = undefined;
}
init() {
// TODO: remove update() call from here and move device reset time to DeviceMonitor.
this.update();
this.resetIfRequired();
this._rightClickSubscribeHandle = this.onRightClick.bind(this);
getTitleClickedMessageBroadcaster().subscribe(this._rightClickSubscribeHandle);
this._settingsSubscribeHandle = this.onSettingChanged.bind(this);
this._appSettingsModel.subscribe(this._settingsSubscribeHandle);
this.installTimers();
}
deinit() {
getTitleClickedMessageBroadcaster().unsubscribe(this._rightClickSubscribeHandle);
this._rightClickSubscribeHandle = undefined;
this._appSettingsModel.unsubscribe(this._settingsSubscribeHandle);
this._settingsSubscribeHandle = undefined;
this._deviceModel.saveStats();
this._appSettingsModel.deinit();
this.uninstallTimers();
}
installTimers() {
const { refreshInterval } = this._appSettingsModel;
this._refreshTimeout = GLib.timeout_add(GLib.G_PRIORITY_DEFAULT, refreshInterval, this.onRefreshTimeout.bind(this));
this._minuteTimeout = GLib.timeout_add(GLib.G_PRIORITY_DEFAULT, kOneMinuteInMilliSeconds, this.onEveryMinute.bind(this));
}
uninstallTimers() {
if (this._refreshTimeout) {
GLib.source_remove(this._refreshTimeout);
this._refreshTimeout = undefined;
}
if (this._minuteTimeout) {
GLib.source_remove(this._minuteTimeout);
this._minuteTimeout = undefined;
}
}
show() {
this._appView.show();
}
hide() {
this._appView.hide();
}
_getActiveDeviceName() {
const userPreferedDevice = this._appSettingsModel.preferedDeviceName;
if (this._deviceModel.hasDevice(userPreferedDevice)) {
return userPreferedDevice;
}
return this._deviceModel.getActiveDeviceName();
}
update() {
const { displayMode, displayBytes } = this._appSettingsModel;
//this._logger.debug(`displayMode : ${displayMode}`);
this._deviceModel.update(displayBytes);
const activeDevice = this._getActiveDeviceName();
//this._logger.debug(`activeDevice: ${activeDevice}`);
let titleStr = "----";
switch (displayMode) {
case DisplayMode.TOTAL_SPEED:
{
const totalSpeedStr = this._deviceModel.getTotalSpeedText(activeDevice);
titleStr = `↕ ${totalSpeedStr}`;
break;
}
case DisplayMode.DOWNLOAD_SPEED:
{
const downloadStr = this._deviceModel.getDownloadSpeedText(activeDevice);
titleStr = `↓ ${downloadStr}`;
break;
}
case DisplayMode.UPLOAD_SPEED:
{
const uploadStr = this._deviceModel.getUploadSpeedText(activeDevice);
titleStr = `↑ ${uploadStr}`;
break;
}
case DisplayMode.BOTH_SPEED:
{
const downloadStr = this._deviceModel.getDownloadSpeedText(activeDevice);
const uploadStr = this._deviceModel.getUploadSpeedText(activeDevice);
titleStr = `↓ ${downloadStr} ↑ ${uploadStr}`;
break;
}
case DisplayMode.TOTAL_DATA:
{
const totalDataStr = this._deviceModel.getTotalDataUsageText(activeDevice);
titleStr = `Σ ${totalDataStr}`;
break;
}
}
this._appView.setTitleText(titleStr);
this._appView.update(this._deviceModel);
// Debugging
// const upload = this._deviceModel.getUploadSpeed(activeDevice);
// const download = this._deviceModel.getDownloadSpeed(activeDevice);
// const totalData = this._deviceModel.getTotalDataUsage(activeDevice);
// this._logger.debug(`upload: ${upload} download: ${download} totalData: ${totalData}`);
// const uploadStr = bytesSpeedToString(upload, displayBytes);
// const downloadStr = bytesSpeedToString(download, displayBytes);
// const totalDataStr = bytesToString(totalData);
// this._logger.debug(`deviceName: ${activeDevice} upload: ${uploadStr} download: ${downloadStr} totalData: ${totalDataStr}`);
}
resetIfRequired() {
const now = new Date();
const activeDevice = this._getActiveDeviceName();
if (!activeDevice) {
this._logger.error(`No active connection: ${activeDevice}! try reset next minute`);
return;
}
const lastResetedAt = this._appSettingsModel.getLastResetDateTime(activeDevice);
const newResetTime = getNextResetTime(lastResetedAt, this._appSettingsModel);
//this._logger.log(`oldResetTime: ${lastResetedAt}`);
//this._logger.log(`newResetTime: ${newResetTime}`);
if (now.getTime() >= newResetTime.getTime()) {
// crossed the mark, Time to reset network stats
this._deviceModel.resetAll();
}
}
// #region Event handlers
onRefreshTimeout() {
//this._logger.debug("tick");
try {
this.update();
} catch (err) {
this._logger.error(`ERROR: ${err.toString()} TRACE: ${err.stack}`);
}
return true;
}
onEveryMinute() {
//this._logger.debug("every 1 minutes");
try {
this.resetIfRequired();
this._deviceModel.saveStats();
} catch (err) {
this._logger.error(`ERROR: ${err.toString()} TRACE: ${err.stack}`);
}
return true;
}
onSettingChanged() {
this.uninstallTimers();
this.installTimers();
}
onRightClick({ button }) {
if (button === "right") {
// cycle through the modes
let { displayMode } = this._appSettingsModel;
switch (displayMode) {
default:
case DisplayMode.TOTAL_SPEED:
{
displayMode = DisplayMode.DOWNLOAD_SPEED;
break;
}
case DisplayMode.DOWNLOAD_SPEED:
{
displayMode = DisplayMode.UPLOAD_SPEED;
break;
}
case DisplayMode.UPLOAD_SPEED:
{
displayMode = DisplayMode.BOTH_SPEED;
break;
}
case DisplayMode.BOTH_SPEED:
{
displayMode = DisplayMode.TOTAL_DATA;
break;
}
case DisplayMode.TOTAL_DATA:
{
displayMode = DisplayMode.TOTAL_SPEED;
break;
}
}
this._appSettingsModel.displayMode = displayMode;
this.update();
}
}
// #endregion Event handlers
}