Compare commits

...

3 Commits

Author SHA1 Message Date
Christian Weimann
fd69b4de1b refactor(helpers): rename distanceBetween to distanceFrom 2025-02-15 08:50:28 +01:00
Christian Weimann
d2b9359fee feat(helpers): add debounceFunction and distanceBetween 2025-02-09 08:02:41 +01:00
Christian Weimann
b5eb470609 feat(utils): add SceneMgr and helpers to exports 2025-02-09 08:01:54 +01:00
2 changed files with 55 additions and 2 deletions

51
utils/helpers.js Normal file
View File

@@ -0,0 +1,51 @@
// Load dependencies
const { TimerMgr } = require('../utils');
const tm = new TimerMgr();
function debounceFunction(id, debounceTimeout, func, params) {
console.warn('debounce', id, debounceTimeout);
timeout = time.ZonedDateTime.now().plusNanos(debounceTimeout * 1000000);
if (tm.hasTimer(id) && tm.isActive(id)) {
log.info(`Skip evaluation of function with id ${id} as function is already running. Reschedule timer`);
tm.reschedule(id, timeout);
} else {
log.info(`Create timer to execute function ${id}`);
if (tm.hasTimer(id)) {
tm.cancel(id);
}
tm.create(id, timeout, func, params);
}
}
function distanceFrom(locationA, locationB) {
const [lat1, lon1] = locationA.split(',').map(Number);
const [lat2, lon2] = locationB.split(',').map(Number);
const toRadians = (degree) => degree * (Math.PI / 180);
const radLat1 = toRadians(lat1);
const radLon1 = toRadians(lon1);
const radLat2 = toRadians(lat2);
const radLon2 = toRadians(lon2);
if (lat1 === lat2 && lon1 === lon2) return '0 m';
const dlon = radLon2 - radLon1;
const dlat = radLat2 - radLat1;
const a = Math.pow(Math.sin(dlat / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(dlon / 2), 2);
const c = 2 * Math.asin(Math.sqrt(a));
const r = 6371000; // Radius of earth in meters
return `${(c * r).toFixed()} m`;
}
module.exports = {
debounceFunction,
distanceFrom
};

View File

@@ -3,5 +3,7 @@ console.loggerName = "org.openhab.js.utils";
console.log("Loading utils");
module.exports = {
get TimerMgr() { return require('./TimerMgr.js').getTimerMgr; }
}
get TimerMgr() { return require('./TimerMgr.js').getTimerMgr; },
get SceneMgr() { return require('./SceneMgr.js').getSceneMgr; },
get helpers() { return require('./helpers.js') }
}