diskLayout() optimized media type detection (HD, SSD) on Windows
This commit is contained in:
parent
bf8ac7e96b
commit
942e497025
@ -100,6 +100,7 @@ Other changes
|
||||
|
||||
| Version | Date | Comment |
|
||||
| -------------- | -------------- | -------- |
|
||||
| 3.45.3 | 2018-09-06 | `diskLayout()` optimized media type detection (HD, SSD) on Windows |
|
||||
| 3.45.2 | 2018-09-05 | updated imags shields icons |
|
||||
| 3.45.1 | 2018-09-05 | updated documentation |
|
||||
| 3.45.0 | 2018-09-04 | `diskLayout()` added smartStatus |
|
||||
|
||||
@ -841,7 +841,7 @@ function diskLayout(callback) {
|
||||
const status = util.getValue(lines, 'Status', '=').trim();
|
||||
if (size) {
|
||||
result.push({
|
||||
type: device.indexOf('SSD') > -1 ? 'SSD' : 'HD', // not really correct(!) ... maybe this one is better: MSFT_PhysicalDisk - Media Type??
|
||||
type: device.indexOf('SSD') > -1 ? 'SSD' : 'HD', // just a starting point ... better: MSFT_PhysicalDisk - Media Type ... see below
|
||||
name: util.getValue(lines, 'Caption', '='),
|
||||
vendor: util.getValue(lines, 'Manufacturer', '='),
|
||||
size: parseInt(size),
|
||||
@ -859,11 +859,45 @@ function diskLayout(callback) {
|
||||
});
|
||||
}
|
||||
});
|
||||
util.powerShell('Get-PhysicalDisk | Format-List')
|
||||
.then(data => {
|
||||
let devices = data.split(/\n\s*\n/);
|
||||
devices.forEach(function (device) {
|
||||
let lines = device.split('\r\n');
|
||||
const serialNum = util.getValue(lines, 'SerialNumber', ':').trim();
|
||||
const name = util.getValue(lines, 'FriendlyName', ':').trim();
|
||||
const size = util.getValue(lines, 'Size', ':').trim();
|
||||
let mediaType = util.getValue(lines, 'MediaType', ':').trim();
|
||||
if (mediaType === '3' || mediaType === 'HDD') { mediaType = 'HD'; }
|
||||
if (mediaType === '4') { mediaType = 'SSD'; }
|
||||
if (mediaType === '5') { mediaType = 'SCM'; }
|
||||
if (size) {
|
||||
let i = util.findObjectByKey(result, 'serialNum', serialNum);
|
||||
if (i === -1) {
|
||||
i = util.findObjectByKey(result, 'name', name);
|
||||
}
|
||||
if (i != -1) {
|
||||
result[i].type = mediaType;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
})
|
||||
.catch(() => {
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
} else {
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
} catch (e) {
|
||||
if (callback) { callback(result); }
|
||||
|
||||
38
lib/util.js
38
lib/util.js
@ -14,6 +14,7 @@
|
||||
|
||||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
let _cores = 0;
|
||||
let wmic = '';
|
||||
@ -124,6 +125,15 @@ function parseDateTime(dt) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function findObjectByKey(array, key, value) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
if (array[i][key] === value) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function getWmic() {
|
||||
if (os.type() === 'Windows_NT' && !wmic) {
|
||||
if (fs.existsSync(process.env.WINDIR + '\\system32\\wbem\\wmic.exe')) {
|
||||
@ -133,6 +143,32 @@ function getWmic() {
|
||||
return wmic;
|
||||
}
|
||||
|
||||
function powerShell(cmd) {
|
||||
return new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
|
||||
let result = '';
|
||||
const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], {
|
||||
stdio: 'pipe'
|
||||
});
|
||||
|
||||
child.stdout.on('data', function (data) {
|
||||
result = result + data.toString('utf8');
|
||||
});
|
||||
child.stderr.on('data', function (data) {
|
||||
reject(data);
|
||||
});
|
||||
child.on('close', function () {
|
||||
resolve(result);
|
||||
});
|
||||
|
||||
child.stdin.write(cmd + '\n');
|
||||
child.stdin.end();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function noop() { }
|
||||
|
||||
exports.isFunction = isFunction;
|
||||
@ -142,5 +178,7 @@ exports.cores = cores;
|
||||
exports.getValue = getValue;
|
||||
exports.decodeEscapeSequence = decodeEscapeSequence;
|
||||
exports.parseDateTime = parseDateTime;
|
||||
exports.findObjectByKey = findObjectByKey;
|
||||
exports.getWmic = getWmic;
|
||||
exports.powerShell = powerShell;
|
||||
exports.noop = noop;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user