Skip to content

Commit

Permalink
Update RpiInfo.js
Browse files Browse the repository at this point in the history
Get fan speed for Pi 5.
  • Loading branch information
ebaauw committed Nov 11, 2023
1 parent a17a85b commit 0dc9546
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/RpiInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ const os = require('os')
* @see https://pinout.xyz
*/
class RpiInfo extends events.EventEmitter {
// static get fanRpm () { return '/sys/devices/platform/cooling_fan/hwmon/hwmon2/fan1_input' }
static get fanPwm () { return '/sys/devices/platform/cooling_fan/hwmon/hwmon2/pwm1' }
static get powerLed () { return '/sys/class/leds/PWR/brightness' }
static get usbOff () { return '/sys/bus/usb/drivers/usb/unbind' }
static get usbOn () { return '/sys/bus/usb/drivers/usb/bind' }

/** Get the state of the localhost.
* @param {boolean} noPowerLed - Don't get the state of the power LED.
* @param {boolean} noFan - Don't get the speed of the fan.
* @return {object} - The state.
*/
async getState (noPowerLed = false) {
async getState (noPowerLed = false, noFan = false) {
const now = new Date(Math.round(Date.now() / 1000) * 1000)
return {
date: now.toISOString(),
boot: (new Date(now.valueOf() - os.uptime() * 1000)).toISOString(),
powerLed: await this.getPowerLedState(noPowerLed),
fan: await this.getFanSpeed(noFan),
load: Math.round(os.loadavg()[0] * 100) / 100,
temp: RpiInfo.parseTemp(await this.vcgencmd('measure_temp')),
freq: RpiInfo.parseFreq(await this.vcgencmd('measure_clock', 'arm')),
Expand All @@ -49,6 +53,18 @@ class RpiInfo extends events.EventEmitter {
}
}

/** Get the speed of the fan.
* @param {boolean} noFan - Don't get the speed of the fan.
* @return {object} - The fan speed.
*/
async getFanSpeed (noFan) {
if (noFan) {
return null
}
this.emit('readFile', RpiInfo.fanPwm)
return parseInt(await fs.readFile(RpiInfo.fanPwm, 'utf8'))
}

/** Get the state of the power LED.
* @param {boolean} noPowerLed - Don't get the state of the power LED.
* @return {string} - The state: `'0'` for off; `'255'`for on.
Expand Down Expand Up @@ -76,6 +92,7 @@ class RpiInfo extends events.EventEmitter {
date: (new Date(state.date)).toISOString(),
boot: (new Date(RpiInfo.parseBoot(state.boot))).toISOString(),
powerLed: state.powerLed === '' ? null : parseInt(state.powerLed),
fan: state.fan === '' ? null : parseInt(state.fan),
load: RpiInfo.parseLoad(state.load),
temp: RpiInfo.parseTemp(state.temp),
freq: RpiInfo.parseFreq(state.freq),
Expand Down

0 comments on commit 0dc9546

Please sign in to comment.