183 lines
5.6 KiB
JavaScript
183 lines
5.6 KiB
JavaScript
var data = {
|
|
loc: [],
|
|
arc: []
|
|
}
|
|
|
|
var settings = {
|
|
location: {
|
|
home: {
|
|
lat: 48.1,
|
|
lng: 16.3
|
|
},
|
|
precision: 0
|
|
},
|
|
colors: {
|
|
loc: {
|
|
default: "green",
|
|
dualsender: "orange"
|
|
},
|
|
arc: {
|
|
default: ["green", "red"],
|
|
dualsender: ["orange", "orange"]
|
|
}
|
|
},
|
|
sizes: {
|
|
loc: {
|
|
default: 0.1
|
|
},
|
|
arc: {
|
|
default: 1.1
|
|
}
|
|
}
|
|
}
|
|
|
|
var geoip = require('fast-geoip');
|
|
|
|
async function addArc(src, dest, uid){
|
|
const arcName = src + " -> " + dest;
|
|
var geoSrc = undefined;
|
|
var startLat = undefined;
|
|
var startLng = undefined;
|
|
var geoDest = undefined;
|
|
var endLat = undefined;
|
|
var endLng = undefined;
|
|
if(data.arc.some(x => x.name === arcName)) {
|
|
console.log("is scho da")
|
|
}else{
|
|
console.log(src.includes("192.168.1.") || src.includes("127.0.0.1"))
|
|
console.log(dest.includes("192.168.1.") || dest.includes("127.0.0.1"))
|
|
if (src.includes("192.168.1.") || src.includes("127.0.0.1")) {
|
|
startLat = round(settings.location.home.lat, settings.location.precision)
|
|
startLng = round(settings.location.home.lng, settings.location.precision)
|
|
} else {
|
|
geoSrc = await geoip.lookup(src);
|
|
startLat = round(geoSrc.ll[0], settings.location.precision)
|
|
startLng = round(geoSrc.ll[1], settings.location.precision)
|
|
if (geoSrc == undefined) {
|
|
startLat = round(settings.location.home.lat, settings.location.precision)
|
|
startLng = round(settings.location.home.lng, settings.location.precision)
|
|
}
|
|
}
|
|
if (dest.includes("192.168.1.") || dest.includes("127.0.0.1")) {
|
|
endLat = round(settings.location.home.lat, settings.location.precision)
|
|
endLng = round(settings.location.home.lng, settings.location.precision)
|
|
} else {
|
|
geoDest = await geoip.lookup(dest);
|
|
endLat = round(geoDest.ll[0], settings.location.precision)
|
|
endLng = round(geoDest.ll[1], settings.location.precision)
|
|
if (geoDest == undefined) {
|
|
endLat = round(settings.location.home.lat, settings.location.precision)
|
|
endLng = round(settings.location.home.lng, settings.location.precision)
|
|
}
|
|
}
|
|
var dat = {
|
|
uid: uid,
|
|
name: arcName,
|
|
startLat: startLat,
|
|
startLng: startLng,
|
|
endLat: endLat,
|
|
endLng: endLng,
|
|
color: settings.colors.arc.default,
|
|
stroke: settings.sizes.arc.default
|
|
}
|
|
console.log(arcName)
|
|
if(data.arc.some(x => x.startLat === startLat && x.startLng === startLng && x.endLat === endLat && x.endLng === endLng)){
|
|
console.log("is scho da 2")
|
|
}else {
|
|
const newUid = addLoc(arcName, startLat, startLng, uid)
|
|
dat.uid = newUid;
|
|
if (!data.arc.some(x => x.startLat === startLat && x.startLng === startLng && x.endLat === endLat && x.endLng === endLng)) {
|
|
if (!data.arc.some(x => x.startLat === endLat && x.startLng === endLng && x.endLat === startLat && x.endLng === startLng)) {
|
|
data.arc.push(dat);
|
|
console.log(dat.uid)
|
|
} else {
|
|
const dataIndex = data.arc.findIndex(x => x.startLat === endLat && x.startLng === endLng && x.endLat === startLat && x.endLng === startLng);
|
|
data.arc[dataIndex].color = settings.colors.arc.dualsender;
|
|
}
|
|
} else {
|
|
const dataIndex = data.arc.findIndex(x => x.startLat === startLat && x.startLng === startLng && x.endLat === endLat && x.endLng === endLng);
|
|
const arcuid = data.arc[dataIndex].uid;
|
|
var loc = getLoc(arcuid)
|
|
loc.color = settings.colors.loc.dualsender;
|
|
editLoc(loc);
|
|
console.log("selbe richtiung schon da")
|
|
}
|
|
}
|
|
}
|
|
console.log("NACH IS SCHO DA")
|
|
}
|
|
|
|
function round(value, precision) {
|
|
var multiplier = Math.pow(10, precision || 0);
|
|
return Math.round(value * multiplier) / multiplier;
|
|
}
|
|
|
|
function addLoc(name, lat, lng, uid){
|
|
var dat = {
|
|
uid: uid,
|
|
name: name,
|
|
lat: lat,
|
|
lng: lng,
|
|
size: settings.sizes.loc.default,
|
|
color: settings.colors.loc.default
|
|
};
|
|
|
|
if(!data.loc.some(x => x.lat === lat && x.lng === lng)) {
|
|
data.loc.push(dat);
|
|
return dat.uid
|
|
}else{
|
|
const dataIndex = data.loc.findIndex(obj => obj.lat === lat && obj.lng === lng);
|
|
data.loc[dataIndex].name = data.loc[dataIndex].name + "<br>\n" + name;
|
|
return data.loc[dataIndex].uid
|
|
}
|
|
}
|
|
|
|
function getLocColor(){
|
|
return settings.colors.loc.default;
|
|
}
|
|
|
|
function getArcColor(){
|
|
return settings.colors.arc.default;
|
|
}
|
|
|
|
function getData(){
|
|
return data;
|
|
}
|
|
|
|
function getDisplayData(){
|
|
return {
|
|
arc: filterUid(data.arc),
|
|
loc: filterUid(data.loc)
|
|
}
|
|
}
|
|
|
|
function filterUid(tofilter){
|
|
var ret = []
|
|
tofilter.forEach(a => {
|
|
const {uid, ...newObj} = a;
|
|
ret.push(newObj)
|
|
})
|
|
return ret
|
|
}
|
|
|
|
function getLocData(){
|
|
return data.loc;
|
|
}
|
|
|
|
function getArcData(){
|
|
return data.arc;
|
|
}
|
|
|
|
function getLoc(uid){
|
|
return data.loc[data.loc.findIndex(x => x.uid === uid)];
|
|
}
|
|
|
|
function editLoc(loc){
|
|
const dataIndex = data.loc.findIndex(x => x.uid === loc.uid);
|
|
data.loc[dataIndex] = loc;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
addArc, addLoc, getLocData, getArcColor, getData, getArcData, getLocColor, getDisplayData
|
|
} |