/var/www/qr4y.com/server/delivery/services
Edit: /var/www/qr4y.com/server/delivery/services/world.service.js (2281B)
const logger = require('../logger')({ service: 'WorldService' })
const PointInPolygon = require('point-in-polygon')
const { isSet, isArray } = require('../api/utils/validator.util')
const fs = require('node:fs/promises');
module.exports = class WorldService {
constructor({ db, config }) {
this.db = db
this.config = config
this.world = VARS.world
this.countries = VARS.countries
logger.child({ service: 'WorldService', method: 'Constructor' }).info('Success')
}
async Run() {
// загрузить world
try {
const data = await fs.readFile(this.config['api']['world_dir']+'world.json', { encoding: 'utf8' });
this.world = JSON.parse(data)
// console.log(data);
} catch (err) {
console.log(err);
}
this.countries = await this.db.countryCode.findAll()
logger
.child({ service: 'WorldService', method: 'Run' })
.info(`World loaded: ${this.world?.length || 0}.`)
console.log(`World loaded. Areas: ${this.world?.length || 0}. Countries: ${this.countries?.length || 0}.`);
this.GetCountryByGPS({ latitude: 48.1, longitude: 38.1 }) // Ukraine test
this.GetCountryByGPS({ latitude: 37.98, longitude: 23.78 }) // Greece test
}
async Stop() {
this.world = []
logger.child({ service: 'WorldService', method: 'Stop' }).info('Success')
}
async GetCountryByGPS({ latitude, longitude }) {
for (let country of this.world) {
for (let region of country['geo_shape']['geometry']['coordinates']) {
if (region.length == 1) {
region = region[0]
}
let res = PointInPolygon([longitude, latitude], region)
// if (country['name'] == 'Greece'||country['name'] == 'Ukraine') {
// console.log(country['name'] + ': ' + region.length+': '+res)
// }
if (res) {
console.log(country['name'])
logger
.child({ service: 'WorldService', method: 'GetCountryByGPS' })
.info(`la: ${latitude}, lo: ${longitude}. name: ${country['name']}`)
for (let c of this.countries) {
if (c.name == country['name']) {
console.log(c.id)
logger
.child({ service: 'WorldService', method: 'GetCountryByGPS' })
.info(`id: ${c.id}`)
return c;
}
}
}
}
// let coordinates = country['geo_shape']['geometry']['coordinates'][0]
}
return null
}
}