-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
75 lines (56 loc) · 2.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
var WmiClient = require('wmi-client');
var nircmd = require('nircmd');
var path = require('path');
// Will be added whenever wmic support works properly.
//module.exports.options = {
// timeout: 0, // Seconds for brightness level to change, can be used to get smooth change.
// monitor: 0 // Used to specify index for monitor to get/set level on.
//};
module.exports.get = function(cb) {
if (process.platform !== 'win32') {
throw new Error('Only Windows systems are supported.');
}
var wmi = new WmiClient({
host: 'localhost',
namespace: '\\\\root\\WMI'
});
wmi.query('SELECT CurrentBrightness,InstanceName FROM WmiMonitorBrightness', function (err, result) {
if (err) {
cb(err);
return;
}
if (result.length == 0) {
cb(new Error('Unable to find any monitors to read brightness levels from.'));
}
var brightnessValue = result[0].CurrentBrightness;
cb(null, brightnessValue / 100);
});
};
module.exports.set = function(val, cb) {
if (process.platform !== 'win32') {
throw new Error('Only Windows systems are supported.');
}
if (typeof val !== 'number' || isNaN(val)) {
throw new TypeError('Expected a number.');
}
nircmd(['setbrightness', Math.round(val * 100)], function (err) {
if (err) {
cb(err);
return;
}
cb();
});
// Would like to use wmic directly to set brightness, but execution of the method returns errors for different syntax.
// Need some help figuring out the proper way to CALL WmiSetBrightness using wmic.
// Want this working to remove dependency on NirCmd. "wmic" is installed with Windows.
// wmic /namespace:\\root\wmi CLASS WmiMonitorBrightnessMethods CALL WmiSetBrightness Timeout=0 Brightness=100
// Description = Invalid method Parameter(s)
// wmic /namespace:\\root\wmi CLASS WmiMonitorBrightnessMethods CALL WmiSetBrightness(0,100)
// Description = Out of present range.
// wmic /namespace:\\root\wmi CLASS WmiMonitorBrightnessMethods CALL WmiSetBrightness 0 25
// Invalid format.
// Hint: <paramlist> = <param> [, <paramlist>].
// Execution with powershell.exe from node.js have such a high performance penatly, it works to execute WmiSetBrightness
// from PowerShell, yet wmic is prefered and need a solution that works.
};