NtopngGlobe/model/globedata.js

220 lines
6.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
},
globe: {
arcDashLength: 0.4,
arcAltitudeAutoScale: 0.4,
arcDashGap: 0.1,
arcDashInitialGap: 0.1,
arcDashAnimateTime: 7000,
arcStroke: 0.5,
labelSize: 0,
labelDotRadius: 0.4,
labelResolution: 2
}
},
timer:{
del: 30
}
}
var geoip = require('fast-geoip');
async function addArc(src, dest, uid, src_name, dest_name){
const arcName = src_name + " -> " + dest_name;
var geoSrc = undefined;
var startLat = undefined;
var startLng = undefined;
var geoDest = undefined;
var endLat = undefined;
var endLng = undefined;
if(!data.arc.some(x => x.src === src && x.dest === dest)) {
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 txrx = [{src: src, dest: dest, src_name: src_name, dest_name: dest_name}];
var dat = {
uid: uid,
name: arcName,
startLat: startLat,
startLng: startLng,
endLat: endLat,
endLng: endLng,
color: settings.colors.arc.default,
stroke: settings.sizes.arc.default,
src: src,
dest: dest,
src_name: src_name,
dest_name: dest_name,
txrx: txrx
}
if(!data.arc.some(x => x.startLat === startLat && x.startLng === startLng && x.endLat === endLat && x.endLng === endLng)){
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;
data.arc[dataIndex].txrx.push({src: dat.src, dest: dat.dest, src_name: dat.src_name, dest_name: dat.dest_name})
}
} 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;
loc.txrx.push({src: dat.src, dest: dat.dest, src_name: dat.src_name, dest_name: dat.dest_name})
editLoc(loc);
}
}
}
}
function round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
function addLoc(name, lat, lng, uid, src, dest, src_name, dest_name){
var txrx = [{src: src, dest: dest, src_name: src_name, dest_name: dest_name}]
var dat = {
uid: uid,
name: name,
lat: lat,
lng: lng,
size: settings.sizes.loc.default,
color: settings.colors.loc.default,
txrx: txrx
};
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 + "\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(){
var ret = data;
ret.settings = settings
return ret
}
function getSettigns(){
return settings;
}
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;
}
function delUids(uids){
uids.forEach(uid => delUid(uid))
}
function delUid(uid){
console.log("delglobe")
console.log(uid)
delArc(uid);
delLoc(uid);
}
function delLoc(uid){
data.loc = data.loc.filter(loc => loc.uid !== uid)
}
function delArc(uid){
data.arc = data.arc.filter(arc => arc.uid !== uid)
}
module.exports = {
addArc, addLoc, getLocData, getArcColor, getData, getArcData, getLocColor, getDisplayData, delUids, getSettigns
}