@trusthab/composable-resources
Version:
migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable
79 lines (65 loc) • 1.88 kB
JavaScript
const { underscore } = require('inflected');
const _ = require('lodash');
const composer = require('../../mixin_loader');
module.exports = (App) => {
class ThermostatRunningStatusResource {
static mixins() {
return ['Kpi'];
}
getMetricId() {
return 'thermostat_running_status';
}
getMetricType() {
return 'gauge';
}
getValue() {
let { status } = this.get('obj');
// an object must match all inclusive property values in order to be given
// a datapoint
const inclusives = [
['device_type', 'thermostat'],
['location_type', 'unit']
];
let inclusivesResults = inclusives.map(([prop, val]) => {
const propVal = this.get('obj')[prop];
if (propVal === val) { return true; }
return undefined;
});
inclusivesResults = _.compact(inclusives);
if (inclusivesResults.length !== inclusives.length) {
return null;
}
// select the correct numerical status value or fallback to the
// `not_running` default
const statuses = {
unknown: -2,
off: -1,
not_running: 0,
cooling: 1,
heating: 2
};
const hasStatus = !!statuses[status];
status = hasStatus ? status : 'not_running';
const numericalStatus = statuses[status];
return numericalStatus;
}
customDimensions() {
return true;
}
getDimensions() {
const dimensions = _.pick(this.get('obj'), [
'id',
'location_id',
'parent_location_id',
'root_location_id'
]);
return {
device_id: dimensions.id,
unit_id: dimensions.location_id,
building_id: dimensions.parent_location_id,
property_id: dimensions.root_location_id
};
}
}
return composer(ThermostatRunningStatusResource, App);
};