started audio
This commit is contained in:
parent
bf4470ed05
commit
636e3f6b50
235
lib/audio.js
Normal file
235
lib/audio.js
Normal file
@ -0,0 +1,235 @@
|
|||||||
|
'use strict';
|
||||||
|
// @ts-check
|
||||||
|
// ==================================================================================
|
||||||
|
// audio.js
|
||||||
|
// ----------------------------------------------------------------------------------
|
||||||
|
// Description: System Information - library
|
||||||
|
// for Node.js
|
||||||
|
// Copyright: (c) 2014 - 2021
|
||||||
|
// Author: Sebastian Hildebrandt
|
||||||
|
// ----------------------------------------------------------------------------------
|
||||||
|
// License: MIT
|
||||||
|
// ==================================================================================
|
||||||
|
// 16. audio
|
||||||
|
// ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
const winPrinterStatus = {
|
||||||
|
1: 'Other',
|
||||||
|
2: 'Unknown',
|
||||||
|
3: 'Idle',
|
||||||
|
4: 'Printing',
|
||||||
|
5: 'Warmup',
|
||||||
|
6: 'Stopped Printing',
|
||||||
|
7: 'Offline',
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseLinuxCupsHeader(lines) {
|
||||||
|
const result = {}
|
||||||
|
if (lines && lines.length) {
|
||||||
|
if (lines[0].indexOf(' CUPS v') > 0) {
|
||||||
|
const parts = lines[0].split(' CUPS v');
|
||||||
|
result.cupsVersion = parts[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseLinuxCupsPrinter(lines) {
|
||||||
|
const result = {};
|
||||||
|
const printerId = util.getValue(lines, 'PrinterId', ' ');
|
||||||
|
result.id = printerId ? parseInt(printerId, 10) : null;
|
||||||
|
result.name = util.getValue(lines, 'Info', ' ');
|
||||||
|
result.model = lines.length > 0 && lines[0] ? lines[0].split(' ')[0] : '';
|
||||||
|
result.uri = util.getValue(lines, 'DeviceURI', ' ');
|
||||||
|
result.uuid = util.getValue(lines, 'UUID', ' ');
|
||||||
|
result.status = util.getValue(lines, 'State', ' ');
|
||||||
|
result.local = util.getValue(lines, 'Location', ' ').toLowerCase().startsWith('local');
|
||||||
|
result.default = null;
|
||||||
|
result.shared = util.getValue(lines, 'Shared', ' ').toLowerCase().startsWith('yes');
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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('hdmi') >= 0) { result = 'HDMI'; }
|
||||||
|
if (str.indexOf('displayport') >= 0) { result = 'Display-Port'; }
|
||||||
|
if (str.indexOf('usb') >= 0) { result = 'USB'; }
|
||||||
|
if (str.indexOf('pci') >= 0) { result = 'PCIe'; }
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseDarwinAudio(audioObject, id) {
|
||||||
|
const result = {};
|
||||||
|
const channelStr = (audioObject.coreaudio_device_transport || '');
|
||||||
|
|
||||||
|
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.channel = parseDarwinChannel(channelStr);
|
||||||
|
result.in = !!(audioObject.coreaudio_device_input || '')
|
||||||
|
result.out = !!(audioObject.coreaudio_device_output || '')
|
||||||
|
result.status = 'online';
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseWindowsAudio(lines, id) {
|
||||||
|
const result = {};
|
||||||
|
const status = parseInt(util.getValue(lines, 'PrinterStatus', '='), 10);
|
||||||
|
|
||||||
|
result.id = id;
|
||||||
|
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';
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function audio(callback) {
|
||||||
|
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
process.nextTick(() => {
|
||||||
|
let result = [];
|
||||||
|
if (_linux || _freebsd || _openbsd || _netbsd) {
|
||||||
|
let cmd = 'cat /etc/cups/printers.conf 2>/dev/null'
|
||||||
|
exec(cmd, function (error, stdout) {
|
||||||
|
// printers.conf
|
||||||
|
if (!error) {
|
||||||
|
const parts = stdout.toString().split('<Printer ');
|
||||||
|
const printerHeader = parseLinuxCupsHeader(parts[0]);
|
||||||
|
for (let i = 1; i < parts.length; i++) {
|
||||||
|
const printers = parseLinuxCupsPrinter(parts[i].split('\n'));
|
||||||
|
printers.engine = 'CUPS';
|
||||||
|
printers.engineVersion = printerHeader.cupsVersion;
|
||||||
|
result.push(printers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 (_darwin) {
|
||||||
|
let cmd = 'system_profiler SPAudioDataType -json'
|
||||||
|
exec(cmd, function (error, stdout) {
|
||||||
|
if (!error) {
|
||||||
|
try {
|
||||||
|
const outObj = JSON.parse(stdout.toString());
|
||||||
|
if (outObj.SPAudioDataType && outObj.SPAudioDataType.length) {
|
||||||
|
for (let i = 0; i < outObj.SPAudioDataType.length; i++) {
|
||||||
|
const audio = parseDarwinAudio(outObj.SPAudioDataType[i], i);
|
||||||
|
result.push(audio);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
util.noop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (callback) {
|
||||||
|
callback(result);
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (_windows) {
|
||||||
|
util.wmic('printer 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.audio = audio;
|
||||||
30
lib/index.d.ts
vendored
30
lib/index.d.ts
vendored
@ -717,6 +717,32 @@ export namespace Systeminformation {
|
|||||||
shared: boolean;
|
shared: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface UsbData {
|
||||||
|
id: number | string;
|
||||||
|
bus: number;
|
||||||
|
deviceId: number;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
removable: boolean;
|
||||||
|
vendor: string;
|
||||||
|
manufacturer: string;
|
||||||
|
maxPower: string;
|
||||||
|
serialNumber: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AudioData {
|
||||||
|
id: number | string;
|
||||||
|
bus: number;
|
||||||
|
deviceId: number;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
removable: boolean;
|
||||||
|
vendor: string;
|
||||||
|
manufacturer: string;
|
||||||
|
maxPower: string;
|
||||||
|
serialNumber: string;
|
||||||
|
}
|
||||||
|
|
||||||
// 10. "Get All at once" - functions
|
// 10. "Get All at once" - functions
|
||||||
|
|
||||||
interface StaticData {
|
interface StaticData {
|
||||||
@ -797,6 +823,10 @@ export function vboxInfo(cb?: (data: Systeminformation.VboxInfoData[]) => any):
|
|||||||
|
|
||||||
export function printer(cb?: (data: Systeminformation.PrinterData[]) => any): Promise<Systeminformation.PrinterData[]>;
|
export function printer(cb?: (data: Systeminformation.PrinterData[]) => any): Promise<Systeminformation.PrinterData[]>;
|
||||||
|
|
||||||
|
export function usb(cb?: (data: Systeminformation.UsbData[]) => any): Promise<Systeminformation.UsbData[]>;
|
||||||
|
|
||||||
|
export function audio(cb?: (data: Systeminformation.AudioData[]) => any): Promise<Systeminformation.AudioData[]>;
|
||||||
|
|
||||||
export function getStaticData(cb?: (data: Systeminformation.StaticData) => any): Promise<Systeminformation.StaticData>;
|
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 getDynamicData(srv?: string, iface?: string, cb?: (data: any) => any): Promise<any>;
|
||||||
export function getAllData(srv?: string, iface?: string, cb?: (data: any) => any): Promise<any>;
|
export function getAllData(srv?: string, iface?: string, cb?: (data: any) => any): Promise<any>;
|
||||||
|
|||||||
@ -39,6 +39,7 @@ const docker = require('./docker');
|
|||||||
const vbox = require('./virtualbox');
|
const vbox = require('./virtualbox');
|
||||||
const printer = require('./printer');
|
const printer = require('./printer');
|
||||||
const usb = require('./usb');
|
const usb = require('./usb');
|
||||||
|
const audio = require('./audio');
|
||||||
|
|
||||||
let _platform = process.platform;
|
let _platform = process.platform;
|
||||||
const _windows = (_platform === 'win32');
|
const _windows = (_platform === 'win32');
|
||||||
@ -491,6 +492,8 @@ exports.printer = printer.printer;
|
|||||||
|
|
||||||
exports.usb = usb.usb;
|
exports.usb = usb.usb;
|
||||||
|
|
||||||
|
exports.audio = audio.audio;
|
||||||
|
|
||||||
exports.getStaticData = getStaticData;
|
exports.getStaticData = getStaticData;
|
||||||
exports.getDynamicData = getDynamicData;
|
exports.getDynamicData = getDynamicData;
|
||||||
exports.getAllData = getAllData;
|
exports.getAllData = getAllData;
|
||||||
|
|||||||
@ -11,12 +11,7 @@
|
|||||||
},
|
},
|
||||||
"types": "./lib/index.d.ts",
|
"types": "./lib/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"clean": "rimraf dist",
|
"test": "node ./test/test.js"
|
||||||
"test-bare": "npm run compile && mocha ./test/**/*.test.js",
|
|
||||||
"compile": "tsc",
|
|
||||||
"watch": "tsc -w",
|
|
||||||
"test": "nyc mocha --require ts-node/register --require source-map-support/register ./test/**/*.test.ts",
|
|
||||||
"coverage": "nyc report --reporter=text-lcov"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"lib/"
|
"lib/"
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
const readline = require('readline');
|
const readline = require('readline');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const { exec } = require('child_process');
|
const exec = require('child_process').exec;
|
||||||
const lib_version = require('../package.json').version;
|
const lib_version = require('../package.json').version;
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
let waiting = false;
|
let waiting = false;
|
||||||
let timer;
|
let timer;
|
||||||
@ -74,7 +75,8 @@ process.stdin.on('keypress', (key, data) => {
|
|||||||
if (!waiting) {
|
if (!waiting) {
|
||||||
waiting = true;
|
waiting = true;
|
||||||
startDots();
|
startDots();
|
||||||
exec(`node si.js '${key}'`, {timeout: 30000}, (error, stdout) => {
|
const siPath = path.join(__dirname, 'si.js');
|
||||||
|
exec(`node ${siPath} '${key}'`, {timeout: 30000}, (error, stdout) => {
|
||||||
waiting = false;
|
waiting = false;
|
||||||
stopDots();
|
stopDots();
|
||||||
clearline();
|
clearline();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user