Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
33 changes: 33 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int> maybeNumber = Nan::To<unsigned int>(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<v8::Object> exports) {
NAN_EXPORT(exports, Open);
Expand All @@ -98,6 +129,8 @@ namespace libsmc {

NAN_EXPORT(exports, GetFans);
NAN_EXPORT(exports, GetFanInformation);

NAN_EXPORT(exports, GetTemp);
}

NODE_MODULE(libsmc, Initialize)
Expand Down
37 changes: 37 additions & 0 deletions test/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,40 @@ test('getFan()', function(t) {
t.ok(typeof info.name === 'string');
t.ok(typeof info.speed === 'number');
});

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test for type checking? (passing non-strings, etc)

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', {});
});
});