-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpinging.js
57 lines (46 loc) · 1.15 KB
/
pinging.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
const silentNotify = require('./silent-notify.js')
const iconPaths = require('./icon-paths.js')
const ping = require('ping')
let tray = null
let lastSeenState = null
//
// config
//
const intervalDuration = 1000
const domainToPing = 'google.com'
//
// UI Interaction Code
//
const maybeNotify = function (lastResponseIsAlive) {
if (lastResponseIsAlive !== lastSeenState) {
if (lastResponseIsAlive === true) {
silentNotify('Wifi back on')
} else if (lastResponseIsAlive === false) {
silentNotify('Wifi down')
}
lastSeenState = lastResponseIsAlive
}
}
const maybeChangeIcon = function (lastResponseIsAlive) {
if (lastResponseIsAlive) {
tray.setImage(iconPaths.icon1)
} else {
tray.setImage(iconPaths.icon2)
}
}
//
// ping code
//
const onRecievePing = function (pingResponse) {
const lastResponseIsAlive = pingResponse.alive
maybeNotify(lastResponseIsAlive)
maybeChangeIcon(lastResponseIsAlive)
}
const doThePing = function () {
ping.promise.probe(domainToPing).then(onRecievePing)
}
const startToPing = function (opts) {
tray = opts.tray
setInterval(doThePing, intervalDuration)
}
module.exports = startToPing