pooliot-client
Version:
143 lines (124 loc) • 2.73 kB
JavaScript
import { readFileSync } from 'fs';
import envfile from 'envfile';
const getModelInfo = () => {
try {
return readFileSync('/proc/device-tree/model', { encoding: 'utf-8' });
} catch (err) {
return '';
}
};
const getHardwareInfo = () => {
let cpuInfo;
try {
cpuInfo = readFileSync('/proc/cpuinfo', { encoding: 'utf-8' });
} catch (e) {
return {};
}
const data = new Map(
cpuInfo
.split('\n')
.map(line => line.replace(/\t/g, ''))
.filter(line => line.length)
.map((line: string) => {
const splitted = line.split(':');
return [
splitted[0].trim(),
splitted
.slice(1)
.join(':')
.trim(),
];
}),
);
return {
model: data.get('Hardware'),
serial: data.get('Serial'),
revision: data.get('Revision'),
};
};
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 isPi = model => model.startsWith('Raspberry Pi');
const rpiVersion = () => {
try {
// eslint-disable-next-line
return require('raspi-ver');
} catch (err) {
return null;
}
};
const detect = () => {
const hardwareInfo = getHardwareInfo();
const osInfo = getOsInfo();
const model = getModelInfo();
let user;
if (osInfo.id === 'osmc') {
user = 'osmc';
}
if (isPi(model)) {
return {
id: 'rpi',
name: 'Raspberry PI',
user: user || 'pi',
info: rpiVersion(),
os: osInfo,
hardware: hardwareInfo,
touchscreen: 'FT5406 memory based driver',
sound: {
type: 'alsamixer',
jack: [3, 1],
hdmi: [3, 2],
},
};
}
if (hardwareInfo.model && hardwareInfo.model.startsWith('Rockchip')) {
return {
id: 'tkb',
name: 'Tinker Board',
user: 'linaro',
os: osInfo,
hardware: hardwareInfo,
touchscreen: 'fts_ts',
sound: {
type: 'pulseaudio',
jack: [2],
hdmi: [3],
},
};
}
if (hardwareInfo.model === 'Vero4K') {
return {
id: 'vero4k',
name: 'Vero4K',
user,
os: osInfo,
hardware: hardwareInfo,
};
}
return {
id: 'unknown',
name: 'Unknown',
os: osInfo,
hardware: hardwareInfo,
};
};
let cache;
export default () => {
if (!cache) cache = detect();
return cache;
};