|
|
@ -1,5 +1,6 @@ |
|
|
|
import { useEffect, useReducer } from "react"; |
|
|
|
import { useState, useEffect, useReducer, useContext } from "react"; |
|
|
|
|
|
|
|
import { CurrentLocationContext } from "../context/CurrentLocationContext"; |
|
|
|
import "../config/globals"; |
|
|
|
import assets from "../config/assets"; |
|
|
|
|
|
|
@ -44,32 +45,6 @@ function is_valid(id, category) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
function partsePluviometer(row) { |
|
|
|
if (!is_valid(row["Id"], "pluv")) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
const description = row["Description"] ? "\n\n" + row["Description"] : ""; |
|
|
|
// displacement += offset;
|
|
|
|
const i = { |
|
|
|
ID: ++ID, |
|
|
|
name: "pluviometer", |
|
|
|
title: "Pluviometro", |
|
|
|
coordinate: { |
|
|
|
latitude: row["Latitude"] + displacement, |
|
|
|
longitude: row["Longitude"], |
|
|
|
}, |
|
|
|
image: custom_assets.pluviometer, |
|
|
|
date: row["Date"], |
|
|
|
description: |
|
|
|
row["Precipitation"] + "mm" + ", " + row["Date"] + description, |
|
|
|
pictures: row["Images"], |
|
|
|
address: row["Address"], |
|
|
|
}; |
|
|
|
console.log(i); |
|
|
|
return i; |
|
|
|
} |
|
|
|
|
|
|
|
function parseFloodZones(row) { |
|
|
|
if (!is_valid(row["Id"], "flood")) { |
|
|
|
return null; |
|
|
@ -96,6 +71,7 @@ function parseFloodZones(row) { |
|
|
|
} |
|
|
|
|
|
|
|
function parseRiverLevel(row) { |
|
|
|
console.log("IDDD " + row["Id"]); |
|
|
|
if (!is_valid(row["Id"], "river")) { |
|
|
|
return null; |
|
|
|
} |
|
|
@ -152,7 +128,9 @@ function parseRainLevel(row) { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
function parseResult(db_result, parseRow) { |
|
|
|
function parseResult(db_result, parseRow, cleanParse) { |
|
|
|
if (cleanParse) return parseRow(db_result); |
|
|
|
|
|
|
|
var warnings = []; |
|
|
|
|
|
|
|
for (let i = 0; i < db_result.rows.length; ++i) { |
|
|
@ -174,10 +152,10 @@ function genericSelect(queriesToParsersMapper, dispatch, isFocused) { |
|
|
|
useEffect(() => { |
|
|
|
console.log("requesting data"); |
|
|
|
if (isAvailable(global.userDataBase)) { |
|
|
|
queriesToParsersMapper.forEach(([query, parser]) => { |
|
|
|
queriesToParsersMapper.forEach(([query, parser, cleanParse]) => { |
|
|
|
global.userDataBase.transaction((tx) => { |
|
|
|
tx.executeSql(query, [], (tx, results) => { |
|
|
|
dispatch({ increment: parseResult(results, parser) }); |
|
|
|
dispatch({ increment: parseResult(results, parser, cleanParse) }); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -188,6 +166,13 @@ function genericSelect(queriesToParsersMapper, dispatch, isFocused) { |
|
|
|
const initialState = { markers: new Set() }; |
|
|
|
|
|
|
|
function reducer(state = initialState, action) { |
|
|
|
// NOTE: removing old pluviometer value to new data can be displayed
|
|
|
|
state.markers.forEach((m) => { |
|
|
|
if (m.name === "pluviometer") { |
|
|
|
state.markers.delete(m); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
action.increment.map((val) => { |
|
|
|
state.markers.add(val); |
|
|
|
}); |
|
|
@ -200,11 +185,55 @@ function reducer(state = initialState, action) { |
|
|
|
function useMarkers(isFocused) { |
|
|
|
const [state, dispatch] = useReducer(reducer, initialState); |
|
|
|
|
|
|
|
const context = useContext(CurrentLocationContext); |
|
|
|
const location = context.currentCoordinates; |
|
|
|
const addr = context.currentLocation; |
|
|
|
|
|
|
|
const parsePluviometer = (db_result) => { |
|
|
|
if (db_result.rows.length <= 0) return []; |
|
|
|
|
|
|
|
const values = { |
|
|
|
pictures: null, |
|
|
|
data: { |
|
|
|
labels: [], |
|
|
|
values: [], |
|
|
|
}, |
|
|
|
description: "", |
|
|
|
date: "", |
|
|
|
}; |
|
|
|
const start = Math.max(0, db_result.rows.length - 7); |
|
|
|
for (let i = start; i < db_result.rows.length; ++i) { |
|
|
|
var row = db_result.rows.item(i); |
|
|
|
|
|
|
|
description = row["Description"] ? "\n\n" + row["Description"] : ""; |
|
|
|
|
|
|
|
values.image = custom_assets.pluviometer; |
|
|
|
values.date = row["Date"]; |
|
|
|
values.description = |
|
|
|
row["Precipitation"] + "mm" + ", " + row["Date"] + description; |
|
|
|
values.pictures = row["Images"]; |
|
|
|
values.data.labels.push(row["Date"].slice(0, 5)); |
|
|
|
values.data.values.push(parseInt(row["Precipitation"])); |
|
|
|
} |
|
|
|
|
|
|
|
const result = { |
|
|
|
ID: ++ID, |
|
|
|
name: "pluviometer", |
|
|
|
title: "Pluviometro 1", |
|
|
|
coordinate: location, |
|
|
|
address: addr, |
|
|
|
...values, |
|
|
|
}; |
|
|
|
|
|
|
|
console.log(result); |
|
|
|
return [result]; |
|
|
|
}; |
|
|
|
|
|
|
|
const queriesToParsersMapper = [ |
|
|
|
["SELECT * FROM FloodZones;", parseFloodZones], |
|
|
|
["SELECT * FROM Pluviometer;", partsePluviometer], |
|
|
|
["SELECT * FROM RiverLevel;", parseRiverLevel], |
|
|
|
["SELECT * FROM RainLevel;", parseRainLevel], |
|
|
|
["SELECT * FROM FloodZones;", parseFloodZones, false], |
|
|
|
["SELECT * FROM RiverLevel;", parseRiverLevel, false], |
|
|
|
["SELECT * FROM RainLevel;", parseRainLevel, false], |
|
|
|
["SELECT * FROM PluviometerData;", parsePluviometer, true], |
|
|
|
]; |
|
|
|
|
|
|
|
genericSelect(queriesToParsersMapper, dispatch, isFocused); |
|
|
|