-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
95 lines (83 loc) · 1.93 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
var electron = require("electron");
var constants = require("./src/constants");
var ping = require("./src/ping");
var app = electron.app;
var Menu = electron.Menu;
var Tray = electron.Tray;
var ICONS = constants.ICONS;
// App Events
app.on("window-all-closed", app.quit);
app.on("ready", initProcess);
/*
* Init the app process
* start ping
* create tray
* listen for ping output
*/
function initProcess() {
var pingInstance = ping.start();
var tray = createTray(ICONS.red);
pingInstance.stdout.on("data", function(data) {
if (isNetworkPinged(data)) {
tray.setImage(ICONS.green);
} else {
tray.setImage(ICONS.red);
}
});
}
/**
* @function createTray
* @description creates a new tray instance out of input icon path
* @param {string} icon Path to icon
*/
function createTray(icon) {
var tray = new Tray(icon);
var trayMenu = createMenu();
tray.setToolTip("Ping " + app.getVersion());
tray.on("click", function() {
showTrayMenu(tray, trayMenu);
});
tray.on("right-click", function() {
showTrayMenu(tray, trayMenu);
});
return tray;
}
/**
* @function showTrayMenu
* @description Shows context menu of Tray
* @param {Tray} tray
* @param {Menu} menu
*/
function showTrayMenu(tray, menu) {
tray.popUpContextMenu(menu);
}
/**
* @function createMenu
* @description Returns menu built for tray
*/
function createMenu() {
return Menu.buildFromTemplate([
{
label: "Quit",
click: function() {
app.quit();
}
}
]);
}
/**
* @function isNetworkPinged
* @description parses ping stdout for network checking
* @param {string|Buffer} str Buffer or String
*/
function isNetworkPinged(str) {
/**
* stdout: <64 bytes from 172.217.22.14: icmp_seq=201 ttl=46 time=568.209 ms
* stdout: <Request timeout for icmp_seq 6
*/
// Convert Buffer to String
if (typeof str !== "string") {
str = str.toString("utf8");
}
return str.indexOf("timeout") === -1;
}