Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CrossManager #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/js/index/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ const TREM = {
},
},
class: {
CrossManager: null,
ReportManager: null,
FocusManager: null,
EewAreaManager: null,
Expand Down
235 changes: 129 additions & 106 deletions src/js/index/core/cross.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,148 @@
const TREM = require('../constant');

let clean = false;

TREM.variable.events.on('MapLoad', (map) => {
map.addSource('cross-geojson', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
map.addLayer({
id: 'cross',
type: 'symbol',
source: 'cross-geojson',
layout: {
'symbol-sort-key': ['get', 'no'],
'symbol-z-order': 'source',
'icon-image': [
'case',
['==', ['get', 'markerType'], 'dot'], 'dot',
[
'match',
['get', 'no'],
1, 'cross1',
2, 'cross2',
3, 'cross3',
4, 'cross4',
'cross',
],
],
'icon-size': [
'interpolate',
['linear'],
['zoom'],
5, 0.02,
10, 0.1,
],
'icon-allow-overlap': true,
'icon-ignore-placement': true,
},
});
});

function refresh_cross(show) {
const markerFeatures = [];
const eew_list = [];

if (!TREM.variable.data.eew?.length) {
if (clean) {
TREM.variable.map.getSource('cross-geojson').setData({ type: 'FeatureCollection', features: [] });
clean = false;
class CrossManager {
static instance = null;
clean = false;

constructor() {
if (CrossManager.instance) {
return CrossManager.instance;
}
return;
this.bindEvents();
CrossManager.instance = this;
}

clean = true;

for (const eew of TREM.variable.data.eew) {
if (!TREM.constant.SHOW_TREM_EEW && eew.author == 'trem') {
continue;
}
const sWaveSource = TREM.variable.map.getSource(`${eew.id}-s-wave`);
const pWaveSource = TREM.variable.map.getSource(`${eew.id}-p-wave`);
if (sWaveSource && pWaveSource) {
eew_list.push(eew);
static getInstance() {
if (!CrossManager.instance) {
new CrossManager();
}
return CrossManager.instance;
}

bindEvents() {
TREM.variable.events.on('MapLoad', (map) => {
map.addSource('cross-geojson', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
map.addLayer({
id: 'cross',
type: 'symbol',
source: 'cross-geojson',
layout: {
'symbol-sort-key': ['get', 'no'],
'symbol-z-order': 'source',
'icon-image': [
'case',
['==', ['get', 'markerType'], 'dot'], 'dot',
[
'match',
['get', 'no'],
1, 'cross1',
2, 'cross2',
3, 'cross3',
4, 'cross4',
'cross',
],
],
'icon-size': [
'interpolate',
['linear'],
['zoom'],
5, 0.02,
10, 0.1,
],
'icon-allow-overlap': true,
'icon-ignore-placement': true,
},
});
});
}

for (const eew of eew_list) {
const sWaveSource = TREM.variable.map.getSource(`${eew.id}-s-wave`);
const pWaveSource = TREM.variable.map.getSource(`${eew.id}-p-wave`);
refresh_cross(show) {
const markerFeatures = [];
const eew_list = [];

if (sWaveSource && pWaveSource) {
const existingIndex = eew_list.findIndex((item) => item.id === eew.id);
let no = existingIndex;
if (eew_list.length > 1) {
no++;
if (!TREM.variable.data.eew?.length) {
if (this.clean) {
TREM.variable.map.getSource('cross-geojson').setData({ type: 'FeatureCollection', features: [] });
this.clean = false;
}
return;
}

this.clean = true;

if (no < 5) {
if (show || eew.status == 3) {
const opacity = eew.status == 3 ? 0.5 : 1;
markerFeatures.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [eew.eq.lon, eew.eq.lat],
},
properties: {
no,
markerType: eew.eq.mag === 1 ? 'dot' : 'cross',
maxIntensity: eew.eq.max,
fillColor: TREM.constant.COLOR.INTENSITY[eew.eq.max],
strokeColor: TREM.constant.COLOR.INTENSITY_TEXT[eew.eq.max],
opacity: opacity,
},
});
for (const eew of TREM.variable.data.eew) {
if (!TREM.constant.SHOW_TREM_EEW && eew.author == 'trem') {
continue;
}
const sWaveSource = TREM.variable.map.getSource(`${eew.id}-s-wave`);
const pWaveSource = TREM.variable.map.getSource(`${eew.id}-p-wave`);
if (sWaveSource && pWaveSource) {
eew_list.push(eew);
}
}

for (const eew of eew_list) {
const sWaveSource = TREM.variable.map.getSource(`${eew.id}-s-wave`);
const pWaveSource = TREM.variable.map.getSource(`${eew.id}-p-wave`);

if (sWaveSource && pWaveSource) {
const existingIndex = eew_list.findIndex((item) => item.id === eew.id);
let no = existingIndex;
if (eew_list.length > 1) {
no++;
}

if (no < 5) {
if (show || eew.status == 3) {
const opacity = eew.status == 3 ? 0.5 : 1;
markerFeatures.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [eew.eq.lon, eew.eq.lat],
},
properties: {
no,
markerType: eew.eq.mag === 1 ? 'dot' : 'cross',
maxIntensity: eew.eq.max,
fillColor: TREM.constant.COLOR.INTENSITY[eew.eq.max],
strokeColor: TREM.constant.COLOR.INTENSITY_TEXT[eew.eq.max],
opacity: opacity,
},
});
}
}
}
}
}

TREM.variable.map.getSource('cross-geojson').setData({
type: 'FeatureCollection',
features: markerFeatures,
});

if (!TREM.variable.map.getLayer('dots')) {
TREM.variable.map.addLayer({
id: 'dots',
type: 'circle',
source: 'cross-geojson',
filter: ['==', ['get', 'markerType'], 'dot'],
paint: {
'circle-radius': 10,
'circle-color': ['get', 'fillColor'],
'circle-stroke-width': 4,
'circle-stroke-color': ['get', 'strokeColor'],
'circle-opacity': ['get', 'opacity'],
'circle-stroke-opacity': ['get', 'opacity'],
},
TREM.variable.map.getSource('cross-geojson').setData({
type: 'FeatureCollection',
features: markerFeatures,
});
}

TREM.variable.map.moveLayer('cross');
TREM.variable.map.moveLayer('dots');
if (!TREM.variable.map.getLayer('dots')) {
TREM.variable.map.addLayer({
id: 'dots',
type: 'circle',
source: 'cross-geojson',
filter: ['==', ['get', 'markerType'], 'dot'],
paint: {
'circle-radius': 10,
'circle-color': ['get', 'fillColor'],
'circle-stroke-width': 4,
'circle-stroke-color': ['get', 'strokeColor'],
'circle-opacity': ['get', 'opacity'],
'circle-stroke-opacity': ['get', 'opacity'],
},
});
}

TREM.variable.map.moveLayer('cross');
TREM.variable.map.moveLayer('dots');
}
}

module.exports = refresh_cross;
TREM.class.CrossManager = CrossManager;

const crossManager = CrossManager.getInstance();
module.exports = crossManager;
4 changes: 2 additions & 2 deletions src/js/index/core/eew.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const TREM = require('../constant');
const EEWCalculator = require('../utils/eewCalculator');
const now = require('../utils/ntp');
const { formatTime } = require('../utils/utils');
const refresh_cross = require('./cross');
const cross = require('./cross');
const { ipcRenderer } = require('electron');

const calculator = new EEWCalculator(require('../../../resource/data/time.json'));
Expand Down Expand Up @@ -109,7 +109,7 @@ TREM.variable.events.on('EewAlert', (ans) => {
TREM.variable.events.on('EewUpdate', (ans) => {
eew_cache[ans.data.id] = ans.data;
show_eew(false);
refresh_cross(false);
cross.refresh_cross(false);
});
TREM.variable.events.on('EewEnd', (ans) => {
removeEewLayersAndSources(ans.data.id);
Expand Down
4 changes: 2 additions & 2 deletions src/js/index/core/loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const TREM = require('../constant');

const { formatTime } = require('../utils/utils');
const now = require('../utils/ntp');
const refresh_cross = require('./cross');
const cross = require('./cross');
const refresh_box = require('./box');
const fetchData = require('../../core/utils/fetch');

Expand Down Expand Up @@ -40,7 +40,7 @@ setInterval(() => {
TREM.variable.events.on('MapLoad', () => {
setInterval(() => {
flash = !flash;
refresh_cross(flash);
cross.refresh_cross(flash);
refresh_box(flash);
}, 500);
});
Expand Down