added bluetoothDevices

This commit is contained in:
Sebastian Hildebrandt
2021-01-18 14:08:55 +01:00
parent f2fb334188
commit 143b1abe86
8 changed files with 779 additions and 96 deletions
+22 -83
View File
@@ -28,16 +28,6 @@ const _openbsd = (_platform === 'openbsd');
const _netbsd = (_platform === 'netbsd');
const _sunos = (_platform === 'sunos');
const winPrinterStatus = {
1: 'Other',
2: 'Unknown',
3: 'Idle',
4: 'Printing',
5: 'Warmup',
6: 'Stopped Printing',
7: 'Offline',
}
function getLinuxAudioPci() {
let cmd = 'lspci -v 2>/dev/null'
let result = [];
@@ -66,13 +56,12 @@ function parseLinuxAudioPciMM(lines, audioPCI) {
result.id = slotId;
result.name = util.getValue(lines, 'SDevice');
result.type = util.getValue(lines, 'Class');
// result.type = util.getValue(lines, 'Class');
result.manufacturer = util.getValue(lines, 'SVendor');
result.builtIn = null;
result.default = null;
result.revision = util.getValue(lines, 'Rev');
result.driver = pciMatch && pciMatch.length === 1 && pciMatch[0].driver ? pciMatch[0].driver : '';
result.channel = null;
result.default = null;
result.channel = 'PCIe';
result.in = null;
result.out = null;
result.status = 'online';
@@ -80,40 +69,11 @@ function parseLinuxAudioPciMM(lines, audioPCI) {
return result;
}
function parseLinuxLpstatPrinter(lines, id) {
const result = {};
result.id = id
result.name = util.getValue(lines, 'Description', ':', true);
result.model = lines.length > 0 && lines[0] ? lines[0].split(' ')[0] : '';
result.uri = null;
result.uuid = null
result.status = lines.length > 0 && lines[0] ? (lines[0].indexOf(' idle') > 0 ? 'idle' : (lines[0].indexOf(' printing') > 0 ? 'printing' : 'unknown')) : null
result.local = util.getValue(lines, 'Location', ':', true).toLowerCase().startsWith('local');
result.default = null;
result.shared = util.getValue(lines, 'Shared', ' ').toLowerCase().startsWith('yes');
return result;
}
// id
// name
// type
// manufacturer
// builtIn
// default
// revision
// driver
// (onboard)
// #channels
// in
// out
// interfaceType HDMI, Display - Port, Build -in, USB, PCIe(darwin: coreaudio_device_transport)
// status
function parseDarwinChannel(str) {
let result = '';
if (str.indexOf('builtin') >= 0) { result = 'Built-In'; }
if (str.indexOf('extern') >= 0) { result = 'Audio-Jack'; }
if (str.indexOf('hdmi') >= 0) { result = 'HDMI'; }
if (str.indexOf('displayport') >= 0) { result = 'Display-Port'; }
if (str.indexOf('usb') >= 0) { result = 'USB'; }
@@ -124,14 +84,14 @@ function parseDarwinChannel(str) {
function parseDarwinAudio(audioObject, id) {
const result = {};
const channelStr = (audioObject.coreaudio_device_transport || '');
const channelStr = ((audioObject.coreaudio_device_transport || '') + ' ' + (audioObject._name || '')).toLowerCase();
result.id = id;
result.name = audioObject._name
result.manufacturer = audioObject.coreaudio_device_manufacturer;
result.default = !!(audioObject.coreaudio_default_audio_input_device || '') || !!(audioObject.coreaudio_default_audio_output_device || '');
result.revision = null;
result.driver = null;
result.default = !!(audioObject.coreaudio_default_audio_input_device || '') || !!(audioObject.coreaudio_default_audio_output_device || '');
result.channel = parseDarwinChannel(channelStr);
result.in = !!(audioObject.coreaudio_device_input || '')
result.out = !!(audioObject.coreaudio_device_output || '')
@@ -140,19 +100,21 @@ function parseDarwinAudio(audioObject, id) {
return result;
}
function parseWindowsAudio(lines, id) {
function parseWindowsAudio(lines) {
const result = {};
const status = parseInt(util.getValue(lines, 'PrinterStatus', '='), 10);
const status = util.getValue(lines, 'StatusInfo', '=');
// const description = util.getValue(lines, 'Description', '=');
result.id = id;
result.id = util.getValue(lines, 'DeviceID', '='); // PNPDeviceID??
result.name = util.getValue(lines, 'name', '=');
result.model = util.getValue(lines, 'DriverName', '=');
result.uri = null;
result.uuid = null
result.status = winPrinterStatus[status] ? winPrinterStatus[status] : null;
result.local = util.getValue(lines, 'Local', '=') === 'TRUE';
result.default = util.getValue(lines, 'Default', '=') === 'TRUE';
result.shared = util.getValue(lines, 'Shared', '=') === 'TRUE';
result.manufacturer = util.getValue(lines, 'manufacturer', '=');
result.revision = null;
result.driver = null
result.default = null
result.in = null
result.out = null
result.interfaceType = null
result.status = status
return result;
}
@@ -177,33 +139,10 @@ function audio(callback) {
}
}
}
if (result.length === 0) {
if (_linux) {
cmd = 'export LC_ALL=C; lpstat -lp 2>/dev/null; unset LC_ALL';
// lpstat
exec(cmd, function (error, stdout) {
const parts = ('\n' + stdout.toString()).split('\nprinter ');
for (let i = 1; i < parts.length; i++) {
const printers = parseLinuxLpstatPrinter(parts[i].split('\n'), i);
result.push(printers);
}
});
if (callback) {
callback(result);
}
resolve(result);
} else {
if (callback) {
callback(result);
}
resolve(result);
}
} else {
if (callback) {
callback(result);
}
resolve(result);
if (callback) {
callback(result);
}
resolve(result);
});
}
if (_darwin) {
@@ -229,7 +168,7 @@ function audio(callback) {
});
}
if (_windows) {
util.wmic('printer get /value', function (error, stdout) {
util.wmic('path Win32_SoundDevice get /value', function (error, stdout) {
if (!error) {
const parts = stdout.toString().split(/\n\s*\n/);
for (let i = 0; i < parts.length; i++) {
+205
View File
@@ -0,0 +1,205 @@
'use strict';
// @ts-check
// ==================================================================================
// audio.js
// ----------------------------------------------------------------------------------
// Description: System Information - library
// for Node.js
// Copyright: (c) 2014 - 2021
// Author: Sebastian Hildebrandt
// ----------------------------------------------------------------------------------
// License: MIT
// ==================================================================================
// 17. bluetooth
// ----------------------------------------------------------------------------------
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const util = require('./util');
// const fs = require('fs');
let _platform = process.platform;
const _linux = (_platform === 'linux');
const _darwin = (_platform === 'darwin');
const _windows = (_platform === 'win32');
const _freebsd = (_platform === 'freebsd');
const _openbsd = (_platform === 'openbsd');
const _netbsd = (_platform === 'netbsd');
const _sunos = (_platform === 'sunos');
function getLinuxAudioPci() {
let cmd = 'lspci -v 2>/dev/null'
let result = [];
try {
const parts = execSync(cmd).toString().split('\n\n');
for (let i = 0; i < parts.length; i++) {
const lines = parts[i].split('\n');
if (lines && lines.length && lines[0].toLowerCase().indexOf('audio') >= 0) {
const audio = {};
audio.slotId = lines[0].split(' ')[0];
audio.driver = util.getValue(lines, 'Kernel driver in use', ':', true) || util.getValue(lines, 'Kernel modules', ':', true)
result.push(audio);
}
}
return result;
} catch (e) {
return result;
}
}
// device("device_services")
// name-- > Key
// address-- > "device_addr"
// batteryPercent-- > "device_batteryPercent"
// manufacturer-- > "device_manufacturer"
// type(keyboard, ...)-- > ("device_majorClassOfDevice_string"), "device_minorClassOfDevice_string"
// connected
function parseLinuxAudioPciMM(lines, audioPCI) {
const result = {};
const slotId = util.getValue(lines, 'Slot');
const pciMatch = audioPCI.filter(function (item) { return item.slotId === slotId })
result.id = slotId;
result.name = util.getValue(lines, 'SDevice');
// result.type = util.getValue(lines, 'Class');
result.manufacturer = util.getValue(lines, 'SVendor');
result.revision = util.getValue(lines, 'Rev');
result.driver = pciMatch && pciMatch.length === 1 && pciMatch[0].driver ? pciMatch[0].driver : '';
result.default = null;
result.channel = 'PCIe';
result.in = null;
result.out = null;
result.status = 'online';
return result;
}
function parseDarwinBluetoothTyoe(str) {
let result = '';
if (str.indexOf('keyboard') >= 0) { result = 'Keyboard'; }
if (str.indexOf('mouse') >= 0) { result = 'Mouse'; }
if (str.indexOf('speaker') >= 0) { result = 'Speaker'; }
if (str.indexOf('headset') >= 0) { result = 'Headset'; }
// to be continued ...
return result;
}
function parseDarwinBluetoothDevices(bluetoothObject) {
const result = {};
const typeStr = ((bluetoothObject.device_minorClassOfDevice_string || bluetoothObject.device_majorClassOfDevice_string || '') + (bluetoothObject.device_name || '')).toLowerCase();
result.device = bluetoothObject.device_services || '';
result.name = bluetoothObject.device_name || '';
result.manufacturer = bluetoothObject.device_manufacturer || '';
result.address = bluetoothObject.device_addr || '';
result.batteryPercent = bluetoothObject.device_batteryPercent || null;
result.tyoe = parseDarwinBluetoothTyoe(typeStr);
result.connected = bluetoothObject.device_isconnected === 'attrib_Yes'|| false;
return result;
}
function parseWindowsAudio(lines) {
const result = {};
const status = util.getValue(lines, 'StatusInfo', '=');
// const description = util.getValue(lines, 'Description', '=');
result.id = util.getValue(lines, 'DeviceID', '='); // PNPDeviceID??
result.name = util.getValue(lines, 'name', '=');
result.manufacturer = util.getValue(lines, 'manufacturer', '=');
result.revision = null;
result.driver = null
result.default = null
result.in = null
result.out = null
result.interfaceType = null
result.status = status
return result;
}
function bluetoothDevices(callback) {
return new Promise((resolve) => {
process.nextTick(() => {
let result = [];
if (_linux || _freebsd || _openbsd || _netbsd) {
let cmd = 'lspci -vmm 2>/dev/null'
exec(cmd, function (error, stdout) {
// PCI
if (!error) {
const audioPCI = getLinuxAudioPci();
const parts = stdout.toString().split('\n\n');
for (let i = 0; i < parts.length; i++) {
const lines = parts[i].split('\n');
if (util.getValue(lines, 'class', ':', true).toLowerCase().indexOf('audio') >= 0) {
const audio = parseLinuxAudioPciMM(lines, audioPCI);
result.push(audio);
}
}
}
if (callback) {
callback(result);
}
resolve(result);
});
}
if (_darwin) {
let cmd = 'system_profiler SPBluetoothDataType -json'
exec(cmd, function (error, stdout) {
if (!error) {
try {
const outObj = JSON.parse(stdout.toString());
if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_title'] && outObj.SPBluetoothDataType[0]['device_title'].length) {
for (let i = 0; i < outObj.SPBluetoothDataType[0]['device_title'].length; i++) {
const obj = outObj.SPBluetoothDataType[0]['device_title'][i];
const objKey = Object.keys(obj);
if (objKey && objKey.length === 1) {
const innerObject = obj[objKey[0]];
innerObject.device_name = objKey[0];
const bluetoothDevice = parseDarwinBluetoothDevices(innerObject);
result.push(bluetoothDevice);
}
}
}
} catch (e) {
util.noop()
}
}
if (callback) {
callback(result);
}
resolve(result);
});
}
if (_windows) {
util.wmic('path Win32_SoundDevice get /value', function (error, stdout) {
if (!error) {
const parts = stdout.toString().split(/\n\s*\n/);
for (let i = 0; i < parts.length; i++) {
result.push(parseWindowsAudio(parts[i].split('\n'), i))
}
}
if (callback) {
callback(result);
}
resolve(result);
});
}
if (_sunos) {
resolve(null);
}
});
});
}
exports.bluetoothDevices = bluetoothDevices;
+19 -8
View File
@@ -732,15 +732,25 @@ export namespace Systeminformation {
interface AudioData {
id: number | string;
bus: number;
deviceId: number;
name: string;
type: string;
removable: boolean;
vendor: string;
manufacturer: string;
maxPower: string;
serialNumber: string;
default: boolean;
revision: string;
driver: string;
in: boolean;
out: boolean;
interfaceType: string;
status: string;
}
interface BluetoothDeviceData {
device: string;
name: string;
address: string;
batteryPercent: number;
manufacturer: string;
type: string;
connected: boolean;
}
// 10. "Get All at once" - functions
@@ -760,7 +770,6 @@ export namespace Systeminformation {
memLayout: MemLayoutData[];
diskLayout: DiskLayoutData[];
}
}
export function version(): string;
@@ -827,6 +836,8 @@ export function usb(cb?: (data: Systeminformation.UsbData[]) => any): Promise<Sy
export function audio(cb?: (data: Systeminformation.AudioData[]) => any): Promise<Systeminformation.AudioData[]>;
export function bluetoothDevices(cb?: (data: Systeminformation.BlockDevicesData[]) => any): Promise<Systeminformation.BlockDevicesData[]>;
export function getStaticData(cb?: (data: Systeminformation.StaticData) => any): Promise<Systeminformation.StaticData>;
export function getDynamicData(srv?: string, iface?: string, cb?: (data: any) => any): Promise<any>;
export function getAllData(srv?: string, iface?: string, cb?: (data: any) => any): Promise<any>;
+2
View File
@@ -40,6 +40,7 @@ const vbox = require('./virtualbox');
const printer = require('./printer');
const usb = require('./usb');
const audio = require('./audio');
const bluetooth = require('./bluetooth');
let _platform = process.platform;
const _windows = (_platform === 'win32');
@@ -493,6 +494,7 @@ exports.printer = printer.printer;
exports.usb = usb.usb;
exports.audio = audio.audio;
exports.bluetoothDevices = bluetooth.bluetoothDevices;
exports.getStaticData = getStaticData;
exports.getDynamicData = getDynamicData;