-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (140 loc) · 4.39 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
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
/*
* Copyright 2024 SeB ([email protected]) - S/V Haimana 264900475
*
* Plugin page:
*
* https://github.com/haimana/signalk-rpi-uptime
*
* This plugin has been inspired and based on:
* https://github.com/sbender9/signalk-raspberry-pi-temperature
* and
* https://github.com/sberl/signalk-rpi-monitor
*
* Many thanks to the authors: Scott Bender and Steve Berl
*
* ---------------------------------------------------------------------
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const _ = require('lodash')
const spawn = require('child_process').spawn
const command = 'cat /proc/uptime'
module.exports = function(app) {
var plugin = {};
let timer = null
plugin.id = "signalk-rpi-uptime"
plugin.name = "RPi Uptime"
plugin.description = "SignalK Node Server Plugin to provide Raspberry Pi Uptime"
plugin.schema = {
type: "object",
properties: {
path_seconds: {
title: "SignalK Path (seconds)",
type: "string",
default: "environment.rpi.uptime",
},
prettyenable: {
type: 'boolean',
title: 'Enable pretty report',
default: false
},
path_pretty: {
title: "SignalK Path (pretty)",
type: "string",
default: "environment.rpi.uptimepretty",
},
rate: {
title: "Sample Rate (in seconds)",
type: 'number',
default: 60
}
}
}
plugin.start = function(options) {
app.handleMessage(plugin.id, {
updates: [{
meta: [
{
path: options.path_seconds,
value: {
units: "s"
}
}
]
}]
});
function printUptime() {
var seconds = spawn('sh', ['-c', command ])
seconds.stdout.on('data', (data) => {
app.debug(`Output of "${command}": ${data}`)
var uptimeseconds = data.toString().split('.')[0];
app.debug(`Processed string: ${uptimeseconds}`)
app.debug(`Pretty enabled: ${options.prettyenable}`)
if (options.prettyenable) {
app.handleMessage(plugin.id, {
updates: [
{
values: [
{
path: options.path_seconds,
value: parseInt(uptimeseconds)
},
{
path: options.path_pretty,
value: secondsToDhms(uptimeseconds)
}
]
}
]
})
} else {
app.handleMessage(plugin.id, {
updates: [
{
values: [
{
path: options.path_seconds,
value: parseInt(uptimeseconds)
}
]
}
]
})
}
})
seconds.on('error', (error) => { console.error(error.toString()) })
seconds.stderr.on('data', function (data) { console.error(data.toString()) })
}
timer=setInterval(printUptime, options.rate * 1000)
}
plugin.stop = function() {
if ( timer ) {
clearInterval(timer)
timer = null
}
}
return plugin
}
//additional functions
function secondsToDhms(seconds) {
seconds = Number(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}