added printer detection
This commit is contained in:
parent
72ca3a1d70
commit
a766eb2eaf
@ -46,7 +46,7 @@
|
||||
<ul>
|
||||
<li><strong>Node.js</strong> is a trademark of Joyent Inc.</li>
|
||||
<li><strong>Linux</strong> is a registered trademark of Linus Torvalds</li>
|
||||
<li><strong>Apple, macOS, OS X</strong> are registered trademarks of Apple Inc.</li>
|
||||
<li><strong>Apple, macOS, OS X, CUPS</strong> are registered trademarks of Apple Inc.</li>
|
||||
<li><strong>Windows</strong> is a registered trademark of Microsoft Corporation</li>
|
||||
<li><strong>Intel</strong> is a trademark of Intel Corporation</li>
|
||||
<li><strong>AMD</strong> is a trademark of Advanced Micro Devices Inc.</li>
|
||||
|
||||
@ -60,7 +60,7 @@ function fsSize(callback) {
|
||||
const size = parseInt(((_linux || _freebsd || _openbsd || _netbsd) ? line[2] : line[1])) * 1024;
|
||||
const used = parseInt(((_linux || _freebsd || _openbsd || _netbsd) ? line[3] : line[2])) * 1024;
|
||||
const available = parseInt(((_linux || _freebsd || _openbsd || _netbsd) ? line[4] : line[3])) * 1024;
|
||||
const use = parseFloat((100.0 * (used / (used + available)).toFixed(2)).toFixed(2));
|
||||
const use = parseFloat((100.0 * (used / (used + available))).toFixed(2));
|
||||
const mount = line[line.length - 1];
|
||||
if (!data.find(el => (el.fs === fs && el.type === fstype))) {
|
||||
data.push({
|
||||
|
||||
14
lib/index.d.ts
vendored
14
lib/index.d.ts
vendored
@ -705,6 +705,18 @@ export namespace Systeminformation {
|
||||
RTC: string;
|
||||
}
|
||||
|
||||
interface PrinterData {
|
||||
id: number;
|
||||
name: string;
|
||||
model: string;
|
||||
uri: string;
|
||||
uuid: string;
|
||||
local: boolean;
|
||||
status: string;
|
||||
default: boolean;
|
||||
shared: boolean;
|
||||
}
|
||||
|
||||
// 10. "Get All at once" - functions
|
||||
|
||||
interface StaticData {
|
||||
@ -783,6 +795,8 @@ export function dockerAll(cb?: (data: any) => any): Promise<any>;
|
||||
|
||||
export function vboxInfo(cb?: (data: Systeminformation.VboxInfoData[]) => any): Promise<Systeminformation.VboxInfoData[]>;
|
||||
|
||||
export function printer(cb?: (data: Systeminformation.PrinterData[]) => any): Promise<Systeminformation.PrinterData[]>;
|
||||
|
||||
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>;
|
||||
|
||||
@ -37,6 +37,7 @@ const users = require('./users');
|
||||
const internet = require('./internet');
|
||||
const docker = require('./docker');
|
||||
const vbox = require('./virtualbox');
|
||||
const printer = require('./printer');
|
||||
|
||||
let _platform = process.platform;
|
||||
const _windows = (_platform === 'win32');
|
||||
@ -453,6 +454,8 @@ exports.dockerAll = docker.dockerAll;
|
||||
|
||||
exports.vboxInfo = vbox.vboxInfo;
|
||||
|
||||
exports.printer = printer.printer;
|
||||
|
||||
exports.getStaticData = getStaticData;
|
||||
exports.getDynamicData = getDynamicData;
|
||||
exports.getAllData = getAllData;
|
||||
|
||||
193
lib/printer.js
Normal file
193
lib/printer.js
Normal file
@ -0,0 +1,193 @@
|
||||
'use strict';
|
||||
// @ts-check
|
||||
// ==================================================================================
|
||||
// printers.js
|
||||
// ----------------------------------------------------------------------------------
|
||||
// Description: System Information - library
|
||||
// for Node.js
|
||||
// Copyright: (c) 2014 - 2021
|
||||
// Author: Sebastian Hildebrandt
|
||||
// ----------------------------------------------------------------------------------
|
||||
// License: MIT
|
||||
// ==================================================================================
|
||||
// 15. printers
|
||||
// ----------------------------------------------------------------------------------
|
||||
|
||||
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 NOT_SUPPORTED = 'not supported';
|
||||
|
||||
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.local = util.getValue(lines, 'Location', ' ').toLowerCase().startsWith('local');
|
||||
result.status = util.getValue(lines, 'State', ' ');
|
||||
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.local = util.getValue(lines, 'Location', ':', true).toLowerCase().startsWith('local');
|
||||
result.status = lines.length > 0 && lines[0] ? (lines[0].indexOf(' idle') > 0 ? 'idle' : (lines[0].indexOf(' printing') > 0 ? 'printing' : 'unknown')): null
|
||||
result.default = null;
|
||||
result.shared = util.getValue(lines, 'Shared', ' ').toLowerCase().startsWith('yes');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseDarwinPrinters(printerObject, id) {
|
||||
const result = {};
|
||||
const uriParts = printerObject.uri.split('/');
|
||||
result.id = id;
|
||||
result.name = printerObject._name
|
||||
result.model = uriParts.length ? uriParts[uriParts.length - 1] : '';
|
||||
result.uri = printerObject.uri;
|
||||
result.uuid = null
|
||||
result.local = printerObject.printserver === 'local';
|
||||
result.status = printerObject.status;
|
||||
result.default = printerObject.default === 'yes';
|
||||
result.shared = printerObject.shared === 'yes';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseWindowsPrinters(lines) {
|
||||
return lines;
|
||||
}
|
||||
|
||||
function printer(callback) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
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]);
|
||||
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], i);
|
||||
result.push(printers);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
} else {
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (_darwin) {
|
||||
let cmd = 'system_profiler SPPrintersDataType -json'
|
||||
exec(cmd, function (error, stdout) {
|
||||
// printers.conf
|
||||
if (!error) {
|
||||
try {
|
||||
const outObj = JSON.parse(stdout.toString());
|
||||
if (outObj.SPPrintersDataType && outObj.SPPrintersDataType.length) {
|
||||
for (let i = 0; i < outObj.SPPrintersDataType.length; i++) {
|
||||
const printer = parseDarwinPrinters(outObj.SPPrintersDataType[i], i);
|
||||
result.push(printer);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
util.noop()
|
||||
}
|
||||
}
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
}
|
||||
if (_windows) {
|
||||
let cmd = '???'
|
||||
exec(cmd, function (error, stdout) {
|
||||
// printers.conf
|
||||
if (!error) {
|
||||
const outObj = stdout.toString();
|
||||
if (outObj.SPPrintersDataType && outObj.SPPrintersDataType.length) {
|
||||
|
||||
for (let i = 1; i < outObj.SPPrintersDataType.length; i++) {
|
||||
result.push(parseWindowsPrinters(outObj.SPPrintersDataType[i]))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
}
|
||||
if (_sunos) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) {
|
||||
callback(NOT_SUPPORTED);
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
exports.printer = printer;
|
||||
Loading…
x
Reference in New Issue
Block a user