-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·271 lines (211 loc) · 7.88 KB
/
main.js
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
268
269
270
271
const { app, BrowserWindow, Menu, Tray, ipcMain } = require('electron')
const url = require('url')
const path = require('path')
var win = null;
const RefreshRouterStatusEveryMiliseconds = 10000;
const RouterGateway = '192.168.8.1';
const ShowAppWindow = false;
var IsConnectedToInternet = false;
//event from index.html script for checking network
ipcMain.on('online-status-changed', (event, status) => {
if (status == "online") {
IsConnectedToInternet = true;
}
else {
IsConnectedToInternet = false;
}
console.log("IsConnected: " + IsConnectedToInternet);
})
function ShowNotification(Title, Description, ImagePath = '') {
const notifier = require('node-notifier')
const path = require('path');
notifier.notify({
title: Title,
message: Description,
icon: path.join('', ImagePath), // Absolute path (doesn't work on balloons)
sound: true, // Only Notification Center or Windows Toasters
wait: true // Wait with callback, until user action is taken against notification
}, function (err, response) {
// Response is response from notification
});
notifier.on('click', function (notifierObject, options) {
console.log("You clicked on the notification")
});
notifier.on('timeout', function (notifierObject, options) {
console.log("Notification timed out!")
});
}
function GetSignalStrength(RouterResponse) {
var MaximumSiganl = RouterResponse.maxsignal[0];
var CurrentSiganl = RouterResponse.SignalIcon[0];
return "Signal: (" + CurrentSiganl + "/" + MaximumSiganl + ")";
}
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
, show: ShowAppWindow // start minimized (in tray) if false
})
win.setSkipTaskbar(true);
// load the index.html of the app.
win.loadFile(path.join(__dirname, 'Views/index.html'))
win.on('close', function (event) {
// event.preventDefault();
// win.hide();
})
win.on('minimize', function (event) {
event.preventDefault()
win.hide()
})
let AppTray = null;
const iconPath = path.join(__dirname, 'Assets/Images/BatteryIcons')
AppTray = new Tray(path.join(iconPath, 'UnknownBattery.png'));
var contextMenu = Menu.buildFromTemplate([
{
label: 'Quit',
accelerator: 'Command+Q',
selector: 'terminate:',
}
]);
AppTray.setContextMenu(contextMenu);
AppTray.setToolTip('This is huwai router utility for monitoring battery, signal, and other info.');
ShowNotification("Unofficial Huawei Router Utility started", "Utility started and will be on system tray/top menu", "");
let router = require('dialog-router-api').create({
gateway: RouterGateway
// The IP address of your router, can be found by doing
// ipconfig on windows or netstat -r on linux (right under 'Gateway')
});
var LowBatteryNotificationShowed = false;
const intervalObj = setInterval((iconPath) => {
try {
UpdateRouterStatus();
} catch (error) {
console.error(error);
}
}, RefreshRouterStatusEveryMiliseconds, iconPath)
function UpdateRouterStatus() {
if (IsConnectedToInternet) {
try {
router.getToken(function (error, token) {
if (!error) {
router.getStatus(token, function (error, response) {
var SgianlStrengthString = GetSignalStrength(response);
//if charging
if (response.BatteryStatus[0] == '1') {
console.log('Battery is charging');
AppTray.setImage(path.join(iconPath, 'ChargingBattery.png'));
if (response.BatteryPercent[0]) {
AppTray.setToolTip('Router is charging (' + response.BatteryPercent[0] + '%)\n' + SgianlStrengthString);
AppTray.setTitle(response.BatteryPercent[0] + '%');
}
else {
AppTray.setToolTip('Unknown battery level or device has no battery\n' + SgianlStrengthString);
AppTray.setImage(path.join(iconPath, 'UnknownBattery.png'));
}
}
//not charging
else {
//known battery level
if (response.BatteryPercent[0]) {
var BatteryLevelNumber = parseInt(response.BatteryPercent[0]);
let batteryIcon = 'UnknownBattery.png';
if (BatteryLevelNumber >= 75) {
batteryIcon = 'FullBattery.png';
}
else if (BatteryLevelNumber >= 50) {
batteryIcon = 'AboveMediumBattery.png';
}
else if (BatteryLevelNumber >= 25) {
batteryIcon = 'MediumBattery.png';
}
else {
batteryIcon = 'LowBattery.png';
LowBatteryNotificationShowed = true;
}
if (BatteryLevelNumber > 25) {
LowBatteryNotificationShowed = false;
}
AppTray.setImage(path.join(iconPath, batteryIcon));
AppTray.setToolTip('Battery level: (' + response.BatteryPercent[0] + '%)\n' + SgianlStrengthString);
AppTray.setTitle(response.BatteryPercent[0] + '%');
}
//Unknown battery level
else {
AppTray.setToolTip('Router is with unknown battery level or device has no battery');
let batteryIcon = 'UnknownBattery.png';
AppTray.setImage(path.join(iconPath, batteryIcon));
}
}
});
}
else {
AppTray.setToolTip('Error in connecting to Huawei router.');
let batteryIcon = 'UnknownBattery.png';
AppTray.setImage(path.join(iconPath, batteryIcon));
}
});
}
catch (error) {
console.error(error);
AppTray.setToolTip('Error in connecting to Huawei router.');
let batteryIcon = 'UnknownBattery.png';
AppTray.setImage(path.join(iconPath, batteryIcon));
}
}
else {
AppTray.setToolTip('please check your internet connection');
AppTray.setTitle('Not Connected');
let batteryIcon = 'UnknownBattery.png';
AppTray.setImage(path.join(iconPath, batteryIcon));
}
}
}
process.on('uncaughtException', function (err) {
console.error((new Date).toUTCString() + ' uncaughtException:', err.message)
console.error(err.stack)
ShowNotification("An error has occured", "Please check your connection and make sure that you have a supported router.", "");
process.exit(1)
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow);
//add to startup
app.setLoginItemSettings({
openAtLogin: true,
});
var opsys = process.platform;
if (opsys == "darwin") {
opsys = "MacOS";
}
else if (opsys == "win32" || opsys == "win64") {
opsys = "Windows";
} else if (opsys == "linux") {
opsys = "Linux";
}
console.log(opsys)
//Hide app from the dock
if (opsys == "MacOS") {
app.dock.hide();
}
// Quit when all windows are closed.
// app.on('window-all-closed', () => {
// // On macOS it is common for applications and their menu bar
// // to stay active until the user quits explicitly with Cmd + Q
// if (process.platform !== 'darwin') {
// app.quit()
// }
// })
// app.on('activate', () => {
// // On macOS it's common to re-create a window in the app when the
// // dock icon is clicked and there are no other windows open.
// if (BrowserWindow.getAllWindows().length === 0) {
// createWindow()
// }
// })
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.