@growbox/sensors
Version:
An abstraction layer for a selection of raspberry-compatible sensors
51 lines (44 loc) • 1.13 kB
JavaScript
/**
* @author nstCactus
* @date 05/12/2017 22:45
*/
/**
* Data object used to represent a value read from a sensor
*/
class SensorValue {
/**
* An enumeration (sort of) of allowed value for the type property
*/
static get TypeEnum() {
return {
TEMPERATURE: 'temperature',
LIGHT: 'light',
SOIL_MOISTURE: 'soil_moisture',
HUMIDITY: 'humidity',
};
}
/**
* Constructs a new instance
* @param {string} type The type of the value. Must be one of TypeEnum "constants".
* @param {number} value The numeric value
*/
constructor(type, value) {
const enumValues = Object.values(SensorValue.TypeEnum);
if (!enumValues.includes(type)) {
throw new TypeError(`The value of the type parameter must be one of ${enumValues.join(', ')}. "${type}" given`);
}
Object.defineProperties(this, {
type: {
writable: false,
enumerable: true,
value: type,
},
value: {
writable: false,
enumerable: true,
value: value,
},
});
}
}
module.exports = SensorValue;