pooliot-client
Version:
96 lines (80 loc) • 1.86 kB
JavaScript
import { readFileSync } from 'fs';
import envfile from 'envfile';
const getHardwareInfo = () => {
let cpuInfo;
try {
cpuInfo = readFileSync('/proc/cpuinfo', { encoding: 'utf8' });
} catch (e) {
// if this fails, this is probably not a pi
return null;
}
const model = cpuInfo
.split('\n')
.map(line => line.replace(/\t/g, ''))
.filter(line => line.length > 0)
.map(line => line.split(':'))
.map(pair => pair.map(entry => entry.trim()))
.filter(pair => pair[0] === 'Hardware');
if (!model || model.length === 0) {
return null;
}
return model[0][1];
};
const getOsInfo = () => {
const snakeCaseToCamelCase = (s: string) =>
s.toLowerCase().replace(/(_\w)/g, m => m[1].toUpperCase());
try {
const parsed = envfile.parseFileSync('/etc/os-release');
const osInfo = {};
Object.keys(parsed).forEach(key => {
osInfo[snakeCaseToCamelCase(key)] = parsed[key];
});
return osInfo;
} catch (err) {
console.warn(err);
return {
id: 'unknown',
};
}
};
const PI_MODEL_NO = ['BCM2708', 'BCM2709', 'BCM2710'];
const isPi = model => PI_MODEL_NO.includes(model);
const rpiVersion = () => {
try {
// eslint-disable-next-line
return require('raspi-ver');
} catch (err) {
return null;
}
};
const detect = () => {
const model = getHardwareInfo();
const osInfo = getOsInfo();
if (isPi(model)) {
return {
id: 'rpi',
name: 'Raspberry PI',
user: 'pi',
info: rpiVersion(),
os: osInfo,
};
}
if (model && model.startsWith('Rockchip')) {
return {
id: 'tkb',
name: 'Tinker Board',
user: 'linaro',
os: osInfo,
};
}
return {
id: 'unknown',
name: 'Unknown',
os: osInfo,
};
};
let cache;
export default () => {
if (!cache) cache = detect();
return cache;
};