homebridge-ecowitt-weather-sensors
Version:
Homebridge support for your Fine Offset weather station (Ecowitt, Ambient, Froggit, GoGen, and more)
137 lines • 6.49 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LightningSensor = void 0;
const date_fns_1 = require("date-fns");
const MotionSensor_1 = require("./MotionSensor");
const utils = __importStar(require("./../Utils"));
//------------------------------------------------------------------------------
class LightningSensor extends MotionSensor_1.MotionSensor {
constructor(platform, accessory, id, name) {
super(platform, accessory, id, name);
this.platform = platform;
this.accessory = accessory;
this.id = id;
this.name = name;
this.setName(name);
this.setStatusActive(false);
}
//---------------------------------------------------------------------------
updateLightningEvent(events, threshold, time) {
var _a, _b;
if (!Number.isFinite(events)) {
this.platform.log.warn(`Cannot update ${this.name}, strike events ${events} is NaN`);
this.updateStatusActive(false);
return;
}
const eventsStr = `${events.toFixed(0)} strikes`;
const staticNames = utils.truthy((_b = (_a = this.platform.config) === null || _a === void 0 ? void 0 : _a.additional) === null || _b === void 0 ? void 0 : _b.staticNames);
this.updateStatusActive(true);
this.updateName(staticNames ? this.name : `${this.name} ${eventsStr}`);
this.updateValue(eventsStr);
this.updateTime(time);
if (!Number.isFinite(threshold)) {
if (typeof threshold === 'undefined') {
this.platform.log.debug(`Cannot update ${this.name} threshold detection, threshold is not set`);
}
else {
this.platform.log.warn(`Cannot update ${this.name} threshold detection, threshold ${threshold} is NaN. `
+ 'Verify plugin configuration');
}
this.updateMotionDetected(false);
return;
}
this.updateMotionDetected(events >= threshold);
}
//---------------------------------------------------------------------------
updateLightningDistance(distancekm, threshold, time) {
var _a, _b, _c, _d;
if (!Number.isFinite(distancekm)) {
this.platform.log.warn(`Cannot update ${this.name}, strike distance ${distancekm} is NaN`);
this.updateStatusActive(false);
return;
}
let distanceStr;
let thresholdkm;
switch ((_b = (_a = this.platform.config) === null || _a === void 0 ? void 0 : _a.units) === null || _b === void 0 ? void 0 : _b.distance) {
case 'km':
thresholdkm = threshold;
distanceStr = `${distancekm.toFixed(1)} km`;
break;
default:
case 'mi':
thresholdkm = threshold * 1.6093;
distanceStr = `${utils.toMile(distancekm).toFixed(1)} mi`;
break;
}
const staticNames = utils.truthy((_d = (_c = this.platform.config) === null || _c === void 0 ? void 0 : _c.additional) === null || _d === void 0 ? void 0 : _d.staticNames);
this.updateStatusActive(true);
this.updateName(staticNames ? this.name : `${this.name} ${distanceStr}`);
this.updateValue(distanceStr);
this.updateTime(time);
if (!Number.isFinite(threshold)) {
if (typeof threshold === 'undefined') {
this.platform.log.debug(`Cannot update ${this.name} threshold detection, threshold is not set`);
}
else {
this.platform.log.warn(`Cannot update ${this.name} threshold detection, threshold ${threshold} is NaN. `
+ 'Verify plugin configuration');
}
this.updateMotionDetected(false);
return;
}
this.updateMotionDetected(distancekm <= thresholdkm);
}
//---------------------------------------------------------------------------
updateLightningTime(timems, threshold, time) {
var _a, _b;
if (!Number.isFinite(timems)) {
this.platform.log.warn(`Cannot update ${this.name}, strike time ${timems} is NaN`);
this.updateStatusActive(false);
return;
}
const thresholdms = threshold * 1000;
const timeStr = (0, date_fns_1.intlFormatDistance)(new Date().getTime() - timems, new Date().getTime());
const staticNames = utils.truthy((_b = (_a = this.platform.config) === null || _a === void 0 ? void 0 : _a.additional) === null || _b === void 0 ? void 0 : _b.staticNames);
this.updateStatusActive(true);
this.updateName(staticNames ? this.name : `${this.name} ${timeStr}`);
this.updateValue(timeStr);
this.updateTime(time);
if (!Number.isFinite(threshold)) {
if (typeof threshold === 'undefined') {
this.platform.log.debug(`Cannot update ${this.name} threshold detection, threshold is not set`);
}
else {
this.platform.log.warn(`Cannot update ${this.name} threshold detection, threshold ${threshold} is NaN. `
+ 'Verify plugin configuration');
}
this.updateMotionDetected(false);
return;
}
this.updateMotionDetected(timems <= thresholdms);
}
}
exports.LightningSensor = LightningSensor;
//# sourceMappingURL=LightningSensor.js.map