forked from SignalK/set-system-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (104 loc) · 3.21 KB
/
index.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
module.exports = function (app) {
const logError =
app.error ||
(err => {
console.error(err)
})
const debug =
app.debug ||
(msg => {
console.log(msg)
})
var plugin = {
unsubscribes: []
}
plugin.id = 'set-system-time'
plugin.name = 'Set System Time'
plugin.description =
'Plugin that sets the system date & time from navigation.datetime delta messages'
plugin.schema = () => ({
title: 'Set System Time with sudo',
type: 'object',
properties: {
interval: {
type: 'number',
title: 'Interval between updates in seconds (0 is once upon plugin start when datetime received)',
default: 0
},
sudo: {
type: 'boolean',
title: 'Use sudo when setting the time',
default: true
},
preferNetworkTime: {
type: 'boolean',
title: 'Set system time only if no other source is available (only chrony detected)',
default: true
}
}
})
const SUDO_NOT_AVAILABLE = 'SUDO_NOT_AVAILABLE'
let count = 0
let lastMessage = ''
plugin.statusMessage = function () {
return `${lastMessage} ${count > 0 ? '- system time set ' + count + ' times' : ''}`
}
plugin.start = function (options) {
let stream = app.streambundle.getSelfStream('navigation.datetime')
if (options && options.interval > 0) {
stream = stream.debounceImmediate(options.interval * 1000)
} else {
stream = stream.take(1)
}
plugin.unsubscribes.push(
stream.onValue(function (datetime) {
var child
if (process.platform == 'win32') {
console.error("Set-system-time supports only linux-like os's")
} else {
if( ! plugin.useNetworkTime(options) ){
const useSudo = typeof options.sudo === 'undefined' || options.sudo
const setDate = `date --iso-8601 -u -s "${datetime}"`
const command = useSudo
? `if sudo -n date &> /dev/null ; then sudo ${setDate} ; else exit 3 ; fi`
: setDate
child = require('child_process').spawn('sh', ['-c', command])
child.on('exit', value => {
if (value === 0) {
count++
lastMessage = 'System time set to ' + datetime
debug(lastMessage)
} else if (value === 3) {
lastMessage =
'Passwordless sudo not available, can not set system time'
logError(lastMessage)
}
})
child.stderr.on('data', function (data) {
lastMessage = data.toString()
logError(lastMessage)
})
}
}
})
)
}
plugin.useNetworkTime = (options) => {
if ( typeof options.preferNetworkTime !== 'undefined' && options.preferNetworkTime == true ){
const chronyCmd = "chronyc sources 2> /dev/null | cut -c2 | grep -ce '-\|*'";
try {
validSources = require('child_process').execSync(chronyCmd,{timeout:500});
} catch (e) {
return false
}
if(validSources > 0 ){
return true
}
}
return false
}
plugin.stop = function () {
plugin.unsubscribes.forEach(f => f())
}
return plugin
}