Skip to content

Instantly share code, notes, and snippets.

@PsyChip
Last active June 22, 2016 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PsyChip/374b2dec0d4c6c8d1e44c9893c3348d9 to your computer and use it in GitHub Desktop.
Save PsyChip/374b2dec0d4c6c8d1e44c9893c3348d9 to your computer and use it in GitHub Desktop.
/*
Raspberry pi toolkit for node.js
depends on sync runner library (npm install sync-runner)
functions:
getThrm(): returns cpu temperature
getVcc(): returns total used voltage by soc unit
uptime(): system uptime in milliseconds
serviceStart(): starts a system daemon, call serviceStop() to do opposite
@todo gpio access routines will be implemented soon
Armagan Corlu aka PsyChip
root@psychip.net
23.03.2016
*/
var exec = require('child_process').exec;
var execsync = require("sync-runner");
var os = require('os');
var fs = require("fs");
module.exports = {
shellc: {
temp: "/sys/class/thermal/thermal_zone0/temp",
vcc: "vcgencmd measure_volts "
},
vcc: ['core', 'sdram_c', 'sdram_i', 'sdram_p'],
execute: function (command, callback) {
exec(command, function (error, stdout, stderr) {
if (typeof callback === 'function') {
callback(stdout);
}
error = null;
stdout = null;
stderr = null;
});
},
getVcc: function () {
var res = 0;
for (var i = 0; i < this.vcc.length; i++) {
res += Math.round(this.util.parseParam(execsync(this.shellc.vcc + this.vcc[i]), "volt").slice(0, -1) * 1000);
}
return res;
},
getThrm: function () {
return parseInt(fs.readFileSync(this.shellc.temp, "utf8").toString().trim()) / 1000;
},
uptime: function () {
return Math.round((os.uptime() * 1000));
},
serviceStart: function (name) {
this.execute("service " + name + " start");
},
serviceStop: function (name) {
this.execute("service " + name + " stop");
},
util: {
explode: function (str, chr, num) {
var xx = str.toString();
var pc = xx.split(chr);
xx = null;
return pc[num];
},
parseParam: function (input, key) {
if (input.indexOf("=") <= -1) {
return "";
}
input += '\n';
var lines = input.split("\n");
for (var i = 0; i < lines.length; i++) {
if (this.explode(lines[i], "=", 0).toString().trim() === key) {
input = null;
key = null;
return this.explode(lines[i], "=", 1).toString().trim();
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment