1 | const os = require('os-utils'); |
||
0 ignored issues
–
show
Backwards Compatibility
introduced
by
![]() |
|||
2 | |||
3 | /* private shared variables for smoothing state information */ |
||
4 | var blockDeltaMS = 10; //reporting threshold in ms |
||
5 | var interval = 500; // Check intervacl |
||
6 | var simpleMovingAverage = 0; |
||
7 | var smaSamples = 10.0; |
||
8 | var deltaMSprior = 0; |
||
9 | |||
10 | var baroReadings = []; |
||
11 | |||
12 | class HostDiagnostics{ |
||
0 ignored issues
–
show
|
|||
13 | constructor(name, deps){ |
||
14 | deps.logger.debug('This is where HostDiagnostics plugin code would execute in the node process.'); |
||
15 | |||
16 | this.deps = deps; |
||
17 | this.bus = deps.globalEventLoop; |
||
18 | this.currCpuUsage=0; |
||
19 | this._handleBaro = this._handleBaro.bind(this); |
||
20 | this._emitPressureTestData = this._emitPressureTestData.bind(this); |
||
21 | this._emitHostDiagnostics = this._emitHostDiagnostics.bind(this); |
||
22 | this._emitHostDiagnosticsLoopDelay = this._emitHostDiagnosticsLoopDelay.bind(this); |
||
23 | |||
24 | } |
||
25 | |||
26 | _emitHostDiagnostics(){ |
||
27 | os.cpuUsage((v) => { |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
28 | this.currCpuUsage = v; |
||
29 | //TODO: Refactor mcu.status to be a global telemetry message so that we can |
||
30 | //aggegate telemetry form any source in to a single message stream. |
||
31 | this.deps.globalEventLoop.emit('mcu.status',{cpu:v}); |
||
32 | }); |
||
33 | } |
||
34 | |||
35 | _emitHostDiagnosticsLoopDelay(){ |
||
36 | var last = process.hrtime(); // replace Date.now() |
||
37 | setImmediate(() => { |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
38 | var delta = process.hrtime(last); // seconds,nanoseconds |
||
39 | var deltaMS = delta[0]*1000+delta[1]/1000000; |
||
40 | var deltaNS = delta[0]*1000000000+delta[1]; |
||
41 | simpleMovingAverage += deltaMS/smaSamples - ((deltaMSprior)/smaSamples); |
||
42 | deltaMSprior = deltaMS; |
||
43 | if (simpleMovingAverage > blockDeltaMS){ |
||
44 | this.deps.globalEventLoop.emit("plugin.host-diagnostics.loopDelay", simpleMovingAverage); |
||
45 | this.deps.cockpit.emit("plugin.host-diagnostics.loopDelay", simpleMovingAverage); |
||
46 | } |
||
47 | }); |
||
48 | } |
||
49 | |||
50 | _calculateRateOfPressureChange(){ |
||
51 | if (baroReadings.length < 2){ |
||
52 | return {}; |
||
53 | } |
||
54 | var oneMinuteAgo = Date.now() - (60*1000); |
||
55 | var tenMinutesAgo = Date.now() - (10*60*1000); |
||
56 | var twentyMinutesAgo = Date.now() - (20*60*1000); |
||
57 | var i = baroReadings.length-1; |
||
58 | var sample = baroReadings[i]; |
||
59 | var fromBaro = 0; |
||
60 | var result = {}; |
||
61 | while((sample.timestamp>oneMinuteAgo) && (i>0)){ |
||
62 | i--; |
||
63 | fromBaro = sample; |
||
64 | sample = baroReadings[i]; |
||
65 | } |
||
66 | result.rate_1_minute = (baroReadings[baroReadings.length-1].baro_p - fromBaro.baro_p); |
||
67 | while((sample.timestamp>tenMinutesAgo) && (i>0)){ |
||
68 | i--; |
||
69 | fromBaro = sample; |
||
70 | sample = baroReadings[i]; |
||
71 | } |
||
72 | result.rate_10_minute = (baroReadings[baroReadings.length-1].baro_p - fromBaro.baro_p); |
||
73 | while((sample.timestamp>twentyMinutesAgo) && (i>0)){ |
||
74 | i--; |
||
75 | fromBaro = sample; |
||
76 | sample = baroReadings[i]; |
||
77 | } |
||
78 | result.rate_20_minute = (baroReadings[baroReadings.length-1].baro_p - fromBaro.baro_p); |
||
79 | //returnig the barometer change in the last minute |
||
80 | return result; |
||
81 | } |
||
82 | |||
83 | getPressureTestState(){ |
||
84 | if (baroReadings.length==0){ |
||
0 ignored issues
–
show
It is recommended to use
=== to compare with 0 .
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator. ![]() |
|||
85 | return {error:"No barometer data found."}; |
||
86 | } |
||
87 | var status = this._calculateRateOfPressureChange(); |
||
88 | |||
89 | status.current_pressure= baroReadings[baroReadings.length-1]; |
||
90 | status.available_sample_duration = status.current_pressure.timestamp - baroReadings[0].timestamp; |
||
91 | return status; |
||
92 | } |
||
93 | |||
94 | _registerRestAPIListeners(express){ |
||
95 | express.get('/pressure_test', (req, res)=> { |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
96 | |||
97 | res.json(this.getPressureTestState()); |
||
98 | }); |
||
99 | |||
100 | express.post('/pressure_test/reset', (req, res)=> { |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
101 | if (baroReadings.length>0){ |
||
102 | var lastReading = baroReadings[baroReadings.length-1]; |
||
103 | baroReadings.length = 0; |
||
104 | baroReadings.push(lastReading); |
||
105 | } else { |
||
106 | baroReadings.length = 0; |
||
107 | } |
||
108 | res.json("OK"); |
||
109 | }); |
||
110 | |||
111 | express.post('/blink', (req, res)=> { |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
112 | this.bus.emit('mcu.SendCommand', "wake()"); |
||
113 | res.json("OK"); |
||
114 | }); |
||
115 | } |
||
116 | |||
117 | _handleBaro(status){ |
||
118 | for (var data in status) { |
||
119 | switch (data) { |
||
120 | case 'baro_p' : |
||
121 | baroReadings.push({ |
||
122 | timestamp: Date.now(), |
||
123 | baro_p: status["baro_p"], |
||
0 ignored issues
–
show
|
|||
124 | baro_t: status['baro_t'] |
||
0 ignored issues
–
show
|
|||
125 | }) |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
126 | while (baroReadings.length > 5000){ |
||
127 | baroReadings.shift(); |
||
128 | } |
||
129 | break; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | _emitPressureTestData(){ |
||
135 | if (baroReadings.length>0){ |
||
136 | this.bus.emit('plugin.host-diagnostics.pressure-test-state',this.getPressureTestState()); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | runPressureTest(){ |
||
141 | this.bus.on('mcu.status',this._handleBaro); |
||
142 | this.emitPressureTestDataInterval = setInterval(this._emitPressureTestData,1000); |
||
143 | } |
||
144 | |||
145 | start(){ |
||
146 | this.emitHostInfoInterval = setInterval(this._emitHostDiagnostics,1000); |
||
147 | this.emitHostLoopDelayInterval = setInterval(this._emitHostDiagnosticsLoopDelay,interval); |
||
148 | this._registerRestAPIListeners(this.deps.app); |
||
149 | this.runPressureTest(); |
||
150 | } |
||
151 | |||
152 | stop(){ |
||
153 | if (this.emitHostInfoInterval){ |
||
154 | clearInterval(this.emitHostInfoInterval); |
||
155 | this.emitHostInfoInterval = null; |
||
156 | } |
||
157 | if (this._emitHostDiagnosticsLoopDelay){ |
||
158 | clearInterval(this._emitHostDiagnosticsLoopDelay); |
||
159 | this._emitHostDiagnosticsLoopDelay = null; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | module.exports = function (name, deps) { |
||
165 | return new HostDiagnostics(name, deps); |
||
166 | }; |
||
167 |