node-flipr
Version:
Unofficial module for Flipr devices
183 lines (182 loc) • 8.06 kB
JavaScript
"use strict";
/**
* Measure specific module.
* @module Flipr
* @author Nicolas Nunge <me@nikkow.eu>
* @version 1.0.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
var temperature_unit_enum_1 = require("../enums/temperature-unit.enum");
var disinfectanttype_enum_1 = require("../enums/disinfectanttype.enum");
var deviation_enum_1 = require("../enums/deviation.enum");
/** FliprMeasure is used to handle metrics returned by Flipr */
var FliprMeasure = /** @class */ (function () {
// TODO: Replace with proper interface
/**
* Creates a new instance of FliprMeasure
*/
function FliprMeasure(rawMeasureObject) {
this.measureDate = null;
this.batteryLevel = null;
this.uvIndex = null;
this.cloudCoverage = null;
this.temperature = null;
this.disinfectantType = null;
this.disinfectantDeviation = null;
this.disinfectantDeviationSector = deviation_enum_1.Deviation.UNKNOWN;
this.phValue = null;
this.phDeviation = null;
this.phDeviationSector = deviation_enum_1.Deviation.UNKNOWN;
if (rawMeasureObject.hasOwnProperty('DateTime')) {
this.measureDate = new Date(rawMeasureObject.DateTime);
}
if (rawMeasureObject.hasOwnProperty('Battery') && rawMeasureObject.Battery.hasOwnProperty('Deviation')) {
this.batteryLevel = rawMeasureObject.Battery.Deviation;
}
if (rawMeasureObject.hasOwnProperty('CloudCoverage')) {
this.cloudCoverage = rawMeasureObject.CloudCoverage;
}
if (rawMeasureObject.hasOwnProperty('UvIndex')) {
this.uvIndex = rawMeasureObject.UvIndex;
}
if (rawMeasureObject.hasOwnProperty('Temperature')) {
this.temperature = rawMeasureObject.Temperature;
}
if (rawMeasureObject.hasOwnProperty('Desinfectant')) {
if (rawMeasureObject.Desinfectant.hasOwnProperty('Label')) {
switch (rawMeasureObject.Desinfectant.Label) {
case "Chlore":
this.disinfectantType = disinfectanttype_enum_1.DisinfectantType.CHLORINE;
break;
case "Brome":
this.disinfectantType = disinfectanttype_enum_1.DisinfectantType.BROMINE;
break;
case "Sel":
this.disinfectantType = disinfectanttype_enum_1.DisinfectantType.SALT;
break;
}
}
if (rawMeasureObject.Desinfectant.hasOwnProperty('Deviation')) {
this.disinfectantDeviation = rawMeasureObject.Desinfectant.Deviation;
}
if (rawMeasureObject.Desinfectant.hasOwnProperty('DeviationSector')) {
this.disinfectantDeviationSector = this.stringToDeviation(rawMeasureObject.Desinfectant.DeviationSector);
}
}
if (rawMeasureObject.hasOwnProperty('PH')) {
if (rawMeasureObject.PH.hasOwnProperty('Value')) {
this.phValue = rawMeasureObject.PH.Value;
}
if (rawMeasureObject.PH.hasOwnProperty('Deviation')) {
this.phDeviation = rawMeasureObject.PH.Deviation;
}
if (rawMeasureObject.PH.hasOwnProperty('DeviationSector')) {
this.phDeviationSector = this.stringToDeviation(rawMeasureObject.PH.DeviationSector);
}
}
}
/**
* Returns the date when these metrics were captured by Flipr.
* @return {Date|null} a native Date object of the metric capture date.
*/
FliprMeasure.prototype.getDate = function () {
return this.measureDate;
};
/**
* Returns the UV index at the moment the metrics were captured.
* @return {number|null}
*/
FliprMeasure.prototype.getUVIndex = function () {
return this.uvIndex;
};
/**
* Returns the cloud coverage at the moment the metrics were captured.
* @param {boolean} expectRawValue
* @return {number|null} When the "expectRawValue" parameter is set to true, the method will return the cloud coverage as returned by Flipr: a float 0 <= x <= 1. Otherwhise, the result will be returned as a percentage 0 <= x <= 100.
*/
FliprMeasure.prototype.getCloudCoverage = function (expectRawValue) {
if (expectRawValue) {
return this.cloudCoverage;
}
return this.cloudCoverage !== null ? (this.cloudCoverage * 100) : null;
};
/**
* Returns the device's battery level at the moment the metrics were captured.
* @param {boolean} expectRawValue
* @return {number|null} When the "expectRawValue" parameter is set to true, the method will return the battery level as returned by Flipr: a float 0 <= x <= 1. Otherwise, the result will be returned as a percentage 0 <= x <= 100.
*/
FliprMeasure.prototype.getBatteryLevel = function (expectRawValue) {
if (expectRawValue) {
return this.batteryLevel;
}
return this.batteryLevel !== null ? (this.batteryLevel * 100) : null;
};
/**
* Returns the water's temperature in various units.
* @param {TemperatureUnit} unit Specifies the unit the result should be returned. See TemperatureUnit enum for possible values.
* @return {number|null}
*/
FliprMeasure.prototype.getTemperature = function (unit) {
if (unit === void 0) { unit = temperature_unit_enum_1.TemperatureUnit.CELCIUS; }
if (this.temperature === null) {
return null;
}
switch (unit) {
case temperature_unit_enum_1.TemperatureUnit.FAHRENHEIT:
return this.temperature * (9 / 5) + 32;
case temperature_unit_enum_1.TemperatureUnit.KELVIN:
return this.temperature + 273.15;
case temperature_unit_enum_1.TemperatureUnit.CELCIUS:
default:
return this.temperature;
}
};
/**
* Returns the type of disinfectant currently in use.
* @return {DisinfectantType|null}
*/
FliprMeasure.prototype.getDisinfectantType = function () {
return this.disinfectantType;
};
/**
* Returns the current disinfectant deviation/variation.
* @param {boolean} expectRawValue When true, the result will be the raw deviation (as a number, -1 <= x <= 1). Otherwise, the deviation sector (see Deviation enum)
* @return {number|null}
*/
FliprMeasure.prototype.getDisinfectantDeviation = function (expectRawValue) {
return expectRawValue ? this.disinfectantDeviation : this.disinfectantDeviationSector;
};
/**
* Returns the raw PH value.
* @return {number|null}
*/
FliprMeasure.prototype.getPHValue = function () {
return this.phValue;
};
/**
* Returns the current PH deviation/variation.
* @param {boolean} expectRawValue When true, the result will be the raw deviation (as a number, -1 <= x <= 1). Otherwise, the deviation sector (see Deviation enum)
* @return {number|null}
*/
FliprMeasure.prototype.getPHDeviation = function (expectRawValue) {
return expectRawValue ? this.phDeviation : this.phDeviationSector;
};
/**
* Converts API values to Deviation
* @private
* @param {string} input
* @return {Deviation}
*/
FliprMeasure.prototype.stringToDeviation = function (input) {
switch (input.toUpperCase()) {
case "TOOHIGH": return deviation_enum_1.Deviation.TOO_HIGH;
case "MEDIUMHIGH": return deviation_enum_1.Deviation.MEDIUM_HIGH;
case "MEDIUM": return deviation_enum_1.Deviation.MEDIUM;
case "MEDIUMLOW": return deviation_enum_1.Deviation.MEDIUM_LOW;
case "TOOLOW": return deviation_enum_1.Deviation.TOO_LOW;
default: return deviation_enum_1.Deviation.UNKNOWN;
}
};
return FliprMeasure;
}());
exports.FliprMeasure = FliprMeasure;