From 37d67686e1a1c3b0882afb71700113b1dfd26b3d Mon Sep 17 00:00:00 2001 From: GabrielTrettel Date: Tue, 23 Nov 2021 12:53:18 -0300 Subject: [PATCH] Implementing socket refresh every 10min --- src/app/hooks/useFiltering.js | 12 ++++++++++ src/app/hooks/useSocketMarkers.js | 37 +++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/app/hooks/useFiltering.js b/src/app/hooks/useFiltering.js index b988599..29a473c 100644 --- a/src/app/hooks/useFiltering.js +++ b/src/app/hooks/useFiltering.js @@ -11,6 +11,8 @@ function useFiltering(location) { const filters = [ { name: "floodZones", + url: endpoint + + `type=FLOODZONES_FORM&time=${finalDate}/${initialDate}&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50`, socketUrl: new WebSocket( endpoint + `type=FLOODZONES_FORM&time=${finalDate}/${initialDate}&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50` @@ -18,6 +20,8 @@ function useFiltering(location) { }, { name: "rain", + url: endpoint + + `type=RAIN_FORM&time=${finalDate}/${initialDate}&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50`, socketUrl: new WebSocket( endpoint + `type=RAIN_FORM&time=${finalDate}/${initialDate}&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50` @@ -25,6 +29,8 @@ function useFiltering(location) { }, { name: "riverFlood", + url: endpoint + + `type=RIVERFLOOD_FORM&time=${finalDate}/${initialDate}&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50`, socketUrl: new WebSocket( endpoint + `type=RIVERFLOOD_FORM&time=${finalDate}/${initialDate}&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50` @@ -32,6 +38,8 @@ function useFiltering(location) { }, { name: "pluviometer", + url: endpoint + + `type=PLUVIOMETER_REGISTRATION&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50`, socketUrl: new WebSocket( endpoint + `type=PLUVIOMETER_REGISTRATION&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=50` @@ -39,6 +47,8 @@ function useFiltering(location) { }, { name: "susceptibilityAreas", + url: endpoint + + `type=FLOODZONES_OFFICIAL&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=20`, socketUrl: new WebSocket( endpoint + `type=FLOODZONES_OFFICIAL&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=20` @@ -46,6 +56,8 @@ function useFiltering(location) { }, { name: "automaticPluviometer", + url: endpoint + + `type=PLUVIOMETERS_OFFICIAL&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=20`, socketUrl: new WebSocket( endpoint + `type=PLUVIOMETERS_OFFICIAL&lat=${location.lat}&lon=${location.long}&buffer=50000&limit=20` diff --git a/src/app/hooks/useSocketMarkers.js b/src/app/hooks/useSocketMarkers.js index 3b1587a..884fd38 100644 --- a/src/app/hooks/useSocketMarkers.js +++ b/src/app/hooks/useSocketMarkers.js @@ -115,15 +115,15 @@ function buildPolygonsObject(response, name) { function buildMarkerObject(response, name) { const r = JSON.parse(response); - // console.log(r); + // console.log(r); const resposta = r.formsanswersgeom; const formsanswersgeom = JSON.parse(resposta).coordinates; var situation = null; if (r.array_to_json) { - situation = r.array_to_json.find((field)=> field.fieldname == "situation"); + situation = r.array_to_json.find((field) => field.fieldname == "situation"); } -// console.log(situation.fieldsanswersvalue); + // console.log(situation.fieldsanswersvalue); return { ID: r.formsanswersid, @@ -158,16 +158,35 @@ function verifyResponse(response, name) { function getFormsAnswers(socketObject, dispatch) { const [socketResponse, setSocketResponse] = useState(); - socketObject.socketUrl.onerror = (e) => { - console.log(e.message); - }; + const openListeners = () => { + console.log(`rodando forms answ ${socketObject.name}`); + socketObject.socketUrl.onerror = (e) => { + console.log(e.message); + }; - socketObject.socketUrl.onmessage = ({ data }) => { - setSocketResponse(data); + socketObject.socketUrl.onmessage = ({ data }) => { + setSocketResponse(data); + }; }; + openListeners() + + + useEffect(() => { + const timer = setInterval(() => { + console.log( + "===== Runing interval " + socketObject.name + " ========" + ); + socketObject.socketUrl.close(); + socketObject.socketUrl = new WebSocket(socketObject.url); + openListeners(); + }, 600000); + + return () => clearTimeout(timer); + }, []); + useEffect(() => { - dispatch({ increment: verifyResponse(socketResponse, socketObject.name) }); + dispatch({ increment: verifyResponse(socketResponse, socketObject.name)}); }, [socketResponse]); }