error handling

This commit is contained in:
Wolfgang Hottgenroth
2017-08-28 15:00:30 +02:00
parent 1cf6c76a7d
commit f44a7109b4
7 changed files with 132 additions and 12 deletions

View File

@ -1,6 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const utils = require("./utils");
class EspThermError extends Error {
constructor(message) {
super(message);
}
}
exports.EspThermError = EspThermError;
class EspThermData {
constructor(temperature, voltage, timeConsumed) {
this.temperature = temperature;
@ -44,7 +51,24 @@ exports.EspThermMessage = EspThermMessage;
function espThermToJson(message) {
let messageStr = "" + message;
let parts = messageStr.split(' ');
let espThermMessage = new EspThermMessage(parts[0], parseFloat(parts[1]), parseFloat(parts[2]), parseInt(parts[3]));
if (parts.length != 4) {
throw new EspThermError(`too few or too much, ${parts.length} elements in message, it was "${message}"`);
}
let clientId = parts[0];
let temperature = parseFloat(parts[1]);
log.info(`temperature: ${temperature}`);
if ((typeof (temperature) != "number") || (temperature == null) || isNaN(temperature)) {
throw new EspThermError(`temperature element should be a number, message was "${message}"`);
}
let voltage = parseFloat(parts[2]);
if ((typeof (voltage) != "number") || (voltage == null) || isNaN(voltage)) {
throw new EspThermError(`voltage element should be a number, message was "${message}"`);
}
let timeConsumed = parseInt(parts[3]);
if ((typeof (timeConsumed) != "number") || (timeConsumed == null) || isNaN(timeConsumed)) {
throw new EspThermError(`timeConsumed element should be a number, message was "${message}"`);
}
let espThermMessage = new EspThermMessage(clientId, temperature, voltage, timeConsumed);
return espThermMessage;
}
exports.espThermToJson = espThermToJson;