Compare commits
3 Commits
475d238ea3
...
fd69b4de1b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd69b4de1b | ||
|
|
d2b9359fee | ||
|
|
b5eb470609 |
51
utils/helpers.js
Normal file
51
utils/helpers.js
Normal 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
|
||||
};
|
||||
@@ -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') }
|
||||
}
|
||||
Reference in New Issue
Block a user