You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

183 lines
4.1 KiB

import { useEffect, useReducer } from "react";
import "../config/globals";
const assets = {
floodZones: require("../assets/pontos_alagamento_peq.png"),
riverLevel: [
require("../assets/rio_baixo.png"),
require("../assets/rio_normal.png"),
require("../assets/rio_alto.png"),
require("../assets/rio_transbordando.png"),
],
rainLevel: [
require("../assets/sem_chuva.png"),
require("../assets/chuva_peq.png"),
require("../assets/chuva_peq.png"),
require("../assets/chuva_forte.png"),
require("../assets/chuva_muito_forte.png"),
require("../assets/chuva_pancadas.png"),
],
pluviometer: require("../assets/diario_pluviometrico.png"),
};
// NOTE: For debug pourposes
var offset = 0.001;
var displacement = 0;
var ID = 0;
var fetched_data = {
pluv: new Set(),
flood: new Set(),
rain: new Set(),
river: new Set(),
};
function is_valid(id, category) {
if (fetched_data[category].has(id)) {
return false;
} else {
fetched_data[category].add(id);
}
return true;
}
function partsePluviometer(row) {
if (!is_valid(row["Id"], "pluv")) {
return null;
}
displacement += offset;
return {
ID: ++ID,
title: "Pluviometro",
coordinate: {
latitude: row["Latitude"] + displacement,
longitude: row["Longitude"],
},
image: assets.pluviometer,
description: row["Pluviometer"],
};
}
function parseFloodZones(row) {
if (!is_valid(row["Id"], "flood")) {
return null;
}
displacement += offset;
return {
ID: ++ID,
title: row["Passable"] == 0 ? "Transponível" : "Intransponível",
coordinate: {
latitude: row["Latitude"],
longitude: row["Longitude"] + displacement,
},
image: assets.floodZones,
description: row["Description"],
};
}
function parseRiverLevel(row) {
if (!is_valid(row["Id"], "river")) {
return null;
}
displacement += offset;
const riverLevel = ["Baixo", "Rio normal", "Alto", "Transfordando"];
const riverIdx = row["RiverIdx"];
return {
ID: ++ID,
title: "Nível do rio",
coordinate: {
latitude: row["Latitude"],
longitude: row["Longitude"] + displacement,
},
image: assets.riverLevel[riverIdx],
description: riverLevel[riverIdx],
};
}
function parseRainLevel(row) {
if (!is_valid(row["Id"], "rain")) {
return null;
}
displacement += offset;
const rainLevel = [
"Sem chuva",
"Chuva fraca",
"Chuva moderada",
"Chuva forte",
"Chuva muito forte",
"Pancada de chuva",
];
const rainIdx = row["RainIdx"];
return {
ID: ++ID,
title: "Nível da chuva",
coordinate: {
latitude: row["Latitude"],
longitude: row["Longitude"] + displacement,
},
image: assets.rainLevel[rainIdx],
description: rainLevel[rainIdx],
};
}
function parseResult(db_result, parseRow) {
var warnings = [];
for (let i = 0; i < db_result.rows.length; ++i) {
var row = db_result.rows.item(i);
const data = parseRow(row);
if (data !== null) {
warnings.push(data);
}
}
return warnings;
}
function genericSelect(queriesToParsersMapper, dispatch, isFocused) {
useEffect(() => {
console.log("requesting data");
queriesToParsersMapper.forEach(([query, parser]) => {
global.userDataBase.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => {
dispatch({ increment: parseResult(results, parser) });
});
});
});
}, [isFocused]);
}
const initialState = { markers: new Set() };
function reducer(state = initialState, action) {
action.increment.map((val) => {
state.markers.add(val);
});
return {
markers: state.markers,
};
}
function useMarkers(isFocused) {
const [state, dispatch] = useReducer(reducer, initialState);
const queriesToParsersMapper = [
["SELECT * FROM FloodZones;", parseFloodZones],
["SELECT * FROM Pluviometer;", partsePluviometer],
["SELECT * FROM RiverLevel;", parseRiverLevel],
["SELECT * FROM RainLevel;", parseRainLevel],
];
genericSelect(queriesToParsersMapper, dispatch, isFocused);
return state;
}
export default useMarkers;