From dea0d4027d5ab912eaac45d4913c35f9e6a521c2 Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Thu, 18 Jan 2018 15:08:14 -0600 Subject: [PATCH 1/6] expose `get_tmp` (without unit support). --- index.js | 9 +++++++++ src/binding.cc | 11 +++++++++++ test/disk.js | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/index.js b/index.js index d9095fe..cf884a3 100644 --- a/index.js +++ b/index.js @@ -34,3 +34,12 @@ exports.getFan = function(number) { exports.getNumberOfFans = function() { return binding.GetFans(); }; + +exports.getTemp = function(tempKey) { + if (!tempKey) + throw new Error('Must pass a valid temperature key to read'); + + var info = binding.GetTemp(tempKey); + + return info || 0; +}; diff --git a/src/binding.cc b/src/binding.cc index 68da34a..9b049c6 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -49,6 +49,15 @@ namespace libsmc { info.GetReturnValue().Set(status); } + // Checks whether the machine is under battery power. Returns a boolean. + NAN_METHOD(GetTemp) { + // Retrieve the temperature based on key + //TODO: Let the user pick the units. + double temp = get_tmp((char *)*v8::String::Utf8Value(info[0]), CELSIUS); + + info.GetReturnValue().Set(temp); + } + // Retrieves the number of fans in a machine. Returns an integer. NAN_METHOD(GetFans) { int fans = get_num_fans(); @@ -98,6 +107,8 @@ namespace libsmc { NAN_EXPORT(exports, GetFans); NAN_EXPORT(exports, GetFanInformation); + + NAN_EXPORT(exports, GetTemp); } NODE_MODULE(libsmc, Initialize) diff --git a/test/disk.js b/test/disk.js index 7aa0847..0a70056 100644 --- a/test/disk.js +++ b/test/disk.js @@ -55,3 +55,13 @@ test('getFan()', function(t) { t.ok(typeof info.name === 'string'); t.ok(typeof info.speed === 'number'); }); + +test('getTemp()', function(t) { + t.plan(2); + + // Get the first CPU proximity value and just test that + var info = smc.getTemp('TC0P'); + + t.ok(typeof info === 'number'); + t.ok(info > 0); +}); From 0537481ddcede151204a454eb3996b579017e5b1 Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Thu, 18 Jan 2018 16:15:24 -0600 Subject: [PATCH 2/6] added unit support to getTemp. --- index.js | 26 ++++++++++++++++++++++++-- src/binding.cc | 40 +++++++++++++++++++++++++++++++--------- test/disk.js | 13 ++++++++++++- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index cf884a3..4c07e33 100644 --- a/index.js +++ b/index.js @@ -35,11 +35,33 @@ exports.getNumberOfFans = function() { return binding.GetFans(); }; -exports.getTemp = function(tempKey) { +exports.getTemp = function(tempKey, units) { if (!tempKey) throw new Error('Must pass a valid temperature key to read'); - var info = binding.GetTemp(tempKey); + var unitType = 0; + + if(units) { + // We unify this for checking + units = units.toUpperCase(); + + switch(units) + { + case 'FAHRENHEIT': + unitType = 1; + break; + + case 'KELVIN': + unitType = 2; + break; + + case 'CELSIUS': + default: + unitType = 0; + } + } + + var info = binding.GetTemp(tempKey, unitType); return info || 0; }; diff --git a/src/binding.cc b/src/binding.cc index 9b049c6..33d9990 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -49,15 +49,6 @@ namespace libsmc { info.GetReturnValue().Set(status); } - // Checks whether the machine is under battery power. Returns a boolean. - NAN_METHOD(GetTemp) { - // Retrieve the temperature based on key - //TODO: Let the user pick the units. - double temp = get_tmp((char *)*v8::String::Utf8Value(info[0]), CELSIUS); - - info.GetReturnValue().Set(temp); - } - // Retrieves the number of fans in a machine. Returns an integer. NAN_METHOD(GetFans) { int fans = get_num_fans(); @@ -97,6 +88,37 @@ namespace libsmc { info.GetReturnValue().Set(information); } + // Checks whether the machine is under battery power. Returns a boolean. + NAN_METHOD(GetTemp) { + tmp_unit_t units; + + // Read the unit type in from Javascript as an integer (for our ease of use) + Nan::Maybe maybeNumber = Nan::To(info[1]); + unsigned int unitType = maybeNumber.FromMaybe(0); + + + // Populate our units variable + switch(unitType) { + case 1: + units = FAHRENHEIT; + break; + + case 2: + units = KELVIN; + break; + + case 0: + default: + units = CELSIUS; + break; + } + + // Retrieve the temperature based on key + double temp = get_tmp((char*)*v8::String::Utf8Value(info[0]), units); + + info.GetReturnValue().Set(temp); + } + // Initialize the module by exporting the methods. void Initialize(v8::Local exports) { NAN_EXPORT(exports, Open); diff --git a/test/disk.js b/test/disk.js index 0a70056..4c6d99b 100644 --- a/test/disk.js +++ b/test/disk.js @@ -57,11 +57,22 @@ test('getFan()', function(t) { }); test('getTemp()', function(t) { - t.plan(2); + t.plan(6); // Get the first CPU proximity value and just test that var info = smc.getTemp('TC0P'); t.ok(typeof info === 'number'); t.ok(info > 0); + + // Test units + info = smc.getTemp('TC0P', 'FAHRENHEIT'); + + t.ok(typeof info === 'number'); + t.ok(info > 0); + + info = smc.getTemp('TC0P', 'KELVIN'); + + t.ok(typeof info === 'number'); + t.ok(info > 200); }); From dca92c236fb3401a24883f22ff2b87a7ef79ec87 Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Thu, 18 Jan 2018 16:20:51 -0600 Subject: [PATCH 3/6] added documentation for `getTemp` --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 4e140ee..2e71c24 100644 --- a/README.md +++ b/README.md @@ -54,5 +54,18 @@ var fan = smc.getFan(0); // => { name: 'ExhaustZ', speed: 2176 } ``` +### `smc.getTemp(sensor, units)` +Gets the temperature value related to a specific sensor. `sensor` is a valid SMC +sensor name (ex: `'TC0P'`). `units` is the string `'celsius'`, `'fahrenheit'`, or +`'kelvin'` (case insensitive). Defaults to `'celsius'`. Returns a floating point +number that is in the units specified. + +```javascript +var smc = require('libsmc'); + +var fan = smc.getTemp('TC0P', 'kelvin'); +// => 315.15 +``` + ## License MIT license. beltex's libsmc is [licensed separately](./deps/libsmc/LICENSE). From a7d22695f5e0918bcb9acf50d5afdc82a003fae8 Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Fri, 19 Jan 2018 10:26:47 -0600 Subject: [PATCH 4/6] fixed minor issues. --- README.md | 10 +++++----- index.js | 12 +++++------- src/binding.cc | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2e71c24..086f802 100644 --- a/README.md +++ b/README.md @@ -54,16 +54,16 @@ var fan = smc.getFan(0); // => { name: 'ExhaustZ', speed: 2176 } ``` -### `smc.getTemp(sensor, units)` +### `smc.getTemp(sensor[, units])` Gets the temperature value related to a specific sensor. `sensor` is a valid SMC -sensor name (ex: `'TC0P'`). `units` is the string `'celsius'`, `'fahrenheit'`, or -`'kelvin'` (case insensitive). Defaults to `'celsius'`. Returns a floating point -number that is in the units specified. +sensor name (ex: `'TC0P'`). `units` is the string `'C'`, `'F'`, or `'K'` (case +insensitive). Defaults to `'C'`. Returns a floating point number that is in the +units specified. ```javascript var smc = require('libsmc'); -var fan = smc.getTemp('TC0P', 'kelvin'); +var fan = smc.getTemp('TC0P', 'K'); // => 315.15 ``` diff --git a/index.js b/index.js index 4c07e33..af21a25 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ exports.getNumberOfFans = function() { }; exports.getTemp = function(tempKey, units) { - if (!tempKey) + if (typeof tempKey !== 'string') throw new Error('Must pass a valid temperature key to read'); var unitType = 0; @@ -47,21 +47,19 @@ exports.getTemp = function(tempKey, units) { switch(units) { - case 'FAHRENHEIT': + case 'F': unitType = 1; break; - case 'KELVIN': + case 'K': unitType = 2; break; - case 'CELSIUS': + case 'C': default: unitType = 0; } } - var info = binding.GetTemp(tempKey, unitType); - - return info || 0; + return binding.GetTemp(tempKey, unitType); }; diff --git a/src/binding.cc b/src/binding.cc index 33d9990..2d076ac 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -88,7 +88,7 @@ namespace libsmc { info.GetReturnValue().Set(information); } - // Checks whether the machine is under battery power. Returns a boolean. + // Retrieves the temperature for the specified sensor. NAN_METHOD(GetTemp) { tmp_unit_t units; From d2d44ae483a304472ccbb130a3deffe99419898b Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Sat, 20 Jan 2018 23:05:29 -0600 Subject: [PATCH 5/6] fixed unit tests, and simplified unit type code. --- index.js | 25 +++---------------------- test/disk.js | 4 ++-- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index af21a25..de6ac9f 100644 --- a/index.js +++ b/index.js @@ -39,27 +39,8 @@ exports.getTemp = function(tempKey, units) { if (typeof tempKey !== 'string') throw new Error('Must pass a valid temperature key to read'); - var unitType = 0; + var types = { 'C': 0, 'F': 1, 'K': 2 }; + var selection = types[units && units.toUpperCase()] || 0; - if(units) { - // We unify this for checking - units = units.toUpperCase(); - - switch(units) - { - case 'F': - unitType = 1; - break; - - case 'K': - unitType = 2; - break; - - case 'C': - default: - unitType = 0; - } - } - - return binding.GetTemp(tempKey, unitType); + return binding.GetTemp(tempKey, selection); }; diff --git a/test/disk.js b/test/disk.js index 4c6d99b..2f70dcb 100644 --- a/test/disk.js +++ b/test/disk.js @@ -66,12 +66,12 @@ test('getTemp()', function(t) { t.ok(info > 0); // Test units - info = smc.getTemp('TC0P', 'FAHRENHEIT'); + info = smc.getTemp('TC0P', 'F'); t.ok(typeof info === 'number'); t.ok(info > 0); - info = smc.getTemp('TC0P', 'KELVIN'); + info = smc.getTemp('TC0P', 'K'); t.ok(typeof info === 'number'); t.ok(info > 200); From 4e96a9eafe9de7ef78d392c0a99c499c059700d7 Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Sat, 20 Jan 2018 23:13:38 -0600 Subject: [PATCH 6/6] updated unit tests to test for passing in bad types. --- index.js | 3 +++ test/disk.js | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index de6ac9f..7775c8a 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,9 @@ exports.getTemp = function(tempKey, units) { if (typeof tempKey !== 'string') throw new Error('Must pass a valid temperature key to read'); + if (units && typeof units !== 'string') + throw new Error('Must pass a valid string for units'); + var types = { 'C': 0, 'F': 1, 'K': 2 }; var selection = types[units && units.toUpperCase()] || 0; diff --git a/test/disk.js b/test/disk.js index 2f70dcb..b462112 100644 --- a/test/disk.js +++ b/test/disk.js @@ -57,7 +57,7 @@ test('getFan()', function(t) { }); test('getTemp()', function(t) { - t.plan(6); + t.plan(10); // Get the first CPU proximity value and just test that var info = smc.getTemp('TC0P'); @@ -65,6 +65,14 @@ test('getTemp()', function(t) { t.ok(typeof info === 'number'); t.ok(info > 0); + t.throws(function(){ + smc.getTemp(99); + }); + + t.throws(function(){ + smc.getTemp(null); + }); + // Test units info = smc.getTemp('TC0P', 'F'); @@ -75,4 +83,12 @@ test('getTemp()', function(t) { t.ok(typeof info === 'number'); t.ok(info > 200); + + t.throws(function(){ + smc.getTemp('TC0P', 99); + }); + + t.throws(function(){ + smc.getTemp('TC0P', {}); + }); });