@iotile/iotile-cloud
Version:
A typescript library for interfacing with the IOTile Cloud API
131 lines • 4.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MAX_INT32 = 2147483647;
var Mdo = /** @class */ (function () {
function Mdo(data) {
if (data === void 0) { data = {}; }
this.m = data.multiplication_factor || data.m || 1;
this.d = data.division_factor || data.d || 1;
this.o = data.offset || data.o || 0;
if (data.mdo_label) {
this.label = data.mdo_label;
}
}
Mdo.prototype.equal = function (other) {
if (this.m !== other.m || this.d !== other.d || this.o !== other.o || this.label !== other.label) {
return false;
}
return true;
};
Mdo.prototype.addToObject = function (obj, longNames) {
if (longNames) {
obj['offset'] = this.o;
obj['multiplication_factor'] = this.m;
obj['division_factor'] = this.d;
obj['mdo_label'] = this.label;
}
else {
obj['m'] = this.m;
obj['d'] = this.d;
obj['o'] = this.o;
}
};
Mdo.prototype.toJson = function (longNames) {
var obj = {};
this.addToObject(obj, longNames);
return obj;
};
/*
private _round(value, decimals) {
// See http://www.jacklmoore.com/notes/rounding-in-javascript/
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
*/
Mdo.prototype._retr_dec = function (num) {
// let numberFixedDecimal = num.toFixed(10);
var numberString = num.toString();
var decimalLength = (numberString.split(".")[1] || []).length;
if (decimalLength > 8) {
// Limit number of decimals to 8
decimalLength = 8;
}
return decimalLength;
};
Mdo.prototype.computeValue = function (value) {
var result = value;
if (this.m) {
result = result * this.m;
}
if (this.d) {
result = result / this.d;
}
if (this.o) {
result += this.o;
}
return result;
};
Mdo.prototype.setFromMdo = function (src) {
if (src.m) {
this.m = src.m;
}
if (src.d) {
this.d = src.d;
}
if (src.o) {
this.o = src.o;
}
if (src.label) {
this.label = src.label;
}
};
Mdo.prototype.setFromFactor = function (factor, invert) {
var multiplication_factor = 1;
var newFactor;
if (factor < MAX_INT32) {
// console.debug('factor is ' + factor);
var decimals = this._retr_dec(factor);
// console.debug('Decimals is ' + decimals);
multiplication_factor = Math.pow(10, decimals);
// console.debug('multiplication_factor is ' + multiplication_factor);
newFactor = Math.round(factor * multiplication_factor);
while (newFactor >= MAX_INT32) {
// console.debug('factor is too large: ' + newFactor);
newFactor = Math.round(newFactor / 10);
multiplication_factor = Math.round(multiplication_factor / 10);
}
}
else {
newFactor = MAX_INT32;
}
this.o = 0.0;
if (invert) {
this.d = newFactor;
this.m = multiplication_factor;
}
else {
this.m = newFactor;
this.d = multiplication_factor;
}
};
Mdo.prototype.eq = function () {
var eqStr = 'V * ' + this.m + '/' + this.d;
if (this.o) {
eqStr += ' + ' + this.o;
}
return eqStr;
};
Mdo.prototype.getPatchPayload = function () {
var payload = {
multiplication_factor: this.m,
division_factor: this.d,
offset: this.o
};
if (this.label) {
payload['mdo_label'] = this.label;
}
return payload;
};
return Mdo;
}());
exports.Mdo = Mdo;
//# sourceMappingURL=mdo.js.map