diff --git a/README.md b/README.md index 4e140ee..086f802 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 `'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', 'K'); +// => 315.15 +``` + ## License MIT license. beltex's libsmc is [licensed separately](./deps/libsmc/LICENSE). diff --git a/index.js b/index.js index d9095fe..7775c8a 100644 --- a/index.js +++ b/index.js @@ -34,3 +34,16 @@ exports.getFan = function(number) { exports.getNumberOfFans = function() { return binding.GetFans(); }; + +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; + + return binding.GetTemp(tempKey, selection); +}; diff --git a/src/binding.cc b/src/binding.cc index 68da34a..2d076ac 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -88,6 +88,37 @@ namespace libsmc { info.GetReturnValue().Set(information); } + // Retrieves the temperature for the specified sensor. + 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); @@ -98,6 +129,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..b462112 100644 --- a/test/disk.js +++ b/test/disk.js @@ -55,3 +55,40 @@ test('getFan()', function(t) { t.ok(typeof info.name === 'string'); t.ok(typeof info.speed === 'number'); }); + +test('getTemp()', function(t) { + t.plan(10); + + // Get the first CPU proximity value and just test that + var info = smc.getTemp('TC0P'); + + 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'); + + t.ok(typeof info === 'number'); + t.ok(info > 0); + + info = smc.getTemp('TC0P', 'K'); + + t.ok(typeof info === 'number'); + t.ok(info > 200); + + t.throws(function(){ + smc.getTemp('TC0P', 99); + }); + + t.throws(function(){ + smc.getTemp('TC0P', {}); + }); +});