Commit 7eb4277a authored by Christian Paul's avatar Christian Paul 🦊

Support for timezones with geo-tz

parent e08103d3
This diff is collapsed.
......@@ -8,6 +8,7 @@
"thing",
"openstreetmap"
],
"repository": "gitlab:webthings/webthing-osm",
"homepage": "https://gitlab.com/webthings/webthing-osm",
"bugs": "https://gitlab.com/webthings/webthing-osm/issues",
"author": "Christian Paul",
......@@ -20,6 +21,8 @@
"main": "src/webthing.js",
"dependencies": {
"express": "^4.16.4",
"geo-tz": "^5.0.4",
"luxon": "^1.15.0",
"opening_hours": "^3.5.0"
},
"devDependencies": {
......
......@@ -40,5 +40,16 @@ async function getElementById(type, id) {
return element;
}
function transformElement(nominatimElement) {
return {
id: nominatimElement.osm_id,
lat: nominatimElement.lat,
lon: nominatimElement.lon,
opening_hours: nominatimElement.extratags.opening_hours,
nominatimElement,
};
}
module.exports.askNominatim = askNominatim;
module.exports.getElementById = getElementById;
module.exports.transformElement = transformElement;
......@@ -8,7 +8,13 @@ const cache = {
};
function loadFromFile() {
const data = JSON.parse(fs.readFileSync('./data/cache.json', 'utf8'));
let data = {};
try {
data = JSON.parse(fs.readFileSync('./data/cache.json', 'utf8'));
} catch (error) {
console.warn('Could not read element cache. Ignorning cache.');
console.warn(error);
}
cache.node = data.node || [];
cache.way = data.way || [];
cache.relation = data.relation || [];
......@@ -19,7 +25,7 @@ function saveToFile() {
}
function getFromCache(type, id) {
return cache[type].find((element) => element.osm_id === id);
return cache[type].find((element) => element.id === id);
}
function saveToCache(type, element) {
......
'use strict';
const express = require('express');
const { DateTime } = require('luxon');
const opening_hours = require('opening_hours');
const router = express.Router();
/**
* Create a date object which has the local time of a given timezone.
* @return {Date} date
*/
function fakeTimezoneForDate(date, timezone) {
if (!date) {
date = new Date();
}
return new Date(date.getTime() + getOffsetToLocal(timezone) * 60000);
}
function getOffsetToLocal(timezone) {
const localDate = DateTime.local();
const remoteDate = localDate.setZone(timezone);
return remoteDate.offset - localDate.offset
}
function requestOpeningHours(element) {
try {
const oh = new opening_hours((element.extratags || {}).opening_hours, element);
const oh = new opening_hours(element.opening_hours, element.nominatimElement);
const date = fakeTimezoneForDate(new Date(), element.timezone);
console.log(date);
return {
open: oh.getState(),
unknown: oh.getUnknown(),
comment: oh.getComment() || '',
open: oh.getState(date),
unknown: oh.getUnknown(date),
comment: oh.getComment(date) || '',
};
} catch(error) {
console.error(error);
return {
open: false,
unknown: true,
......@@ -21,8 +43,7 @@ function requestOpeningHours(element) {
}
router.get('/', async function (req, res) {
//const name = (req.osmElement.tags || {}).name || 'POI';
const name = 'POI';
const name = (req.osmElement.nominatimElement.address || [])[0] || 'POI';
res.json({
name: `${name}: Opening Hours`,
type: 'thing',
......
......@@ -2,7 +2,8 @@
const express = require('express');
const router = express.Router();
const { getElementById } = require('../apis/nominatim.js');
const { getElementById, transformElement } = require('../apis/nominatim.js');
const { applyTimezone } = require('../helper/timezone.js');
const { loadFromFile, getFromCache, saveToCache } = require('./cache.js');
loadFromFile();
......@@ -11,7 +12,8 @@ async function getOsmElement(type, id) {
let element = getFromCache(type, id);
if (!element) {
console.log(`Asking server for ${type}(${id})`);
element = await getElementById(type, id);
element = transformElement(await getElementById(type, id));
applyTimezone(element);
if (element) saveToCache(type, element);
}
return element;
......
'use strict';
const geoTz = require('geo-tz');
function applyTimezone(element) {
element.timezone = geoTz(element.lat, element.lon)[0];
}
module.exports.applyTimezone = applyTimezone;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment