UNPKG

telldus2graphite

Version:

Fetches sensor values from Telldus Live and forwards it to Graphite

62 lines (61 loc) 2.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // @ts-ignore const graphite_promise_1 = require("graphite-promise"); const metric_1 = __importDefault(require("./metric")); const validator_1 = require("./validator"); module.exports = function (config) { const metric = (0, metric_1.default)(config.format); const client = new graphite_promise_1.GraphiteClient(config); function logMetric(metric, timestamp) { return client.write(metric, timestamp); } function _logSensorInfo(sensorInfo) { return new Promise(function (resolve, reject) { if (!sensorInfo) { return reject(new Error('sensorInfo must not be empty')); } if (!sensorInfo.name) { return reject(new Error('sensorInfo must have a name')); } if (!(0, validator_1.isTimestamp)(sensorInfo.lastUpdated)) { return reject(new Error('lastUpdated is not a timestamp')); } // @ts-ignore const promises = sensorInfo.data.map(data => { if (!data.name) { return reject(new Error('data must have a name')); } if (!(0, validator_1.isNumber)(data.value)) { return reject(new Error('data must have a numeric value')); } const m = metric.create(sensorInfo, data); const ts = (sensorInfo.lastUpdated * 1000) + sensorInfo.timezoneoffset || 0; return logMetric(m, ts); }); return Promise.all(promises).then((metric) => { return resolve(metric); }, (error) => { reject(error); }); }); } /** * Log all sensor infos * @param sensorInfos list of sensor infos */ function logAll(sensorInfos) { return Promise.all(sensorInfos.filter(s => s.name).map(_logSensorInfo)); } function end() { client.end(); } return { logAll: logAll, end: end, _logSensorInfo: _logSensorInfo }; };