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.
 
 
 

81 lines
2.3 KiB

import { useState } from "react";
import getFieldsAnswers from "../api/RequestFieldsAnswers/getFieldsAnswers";
import webSocketClient from "../api/Websockets/webSocketClient";
async function getPluvStation_data(id) {
const result = await getFieldsAnswers.fieldsAnswers(null, id);
if (result.data) {
return result;
}
return null;
}
function assemblePluvStationObject(pluvData){
var date = null;
var time = null;
var address = null;
var institutionType = null;
var institutionName = null;
pluvData.array_to_json.forEach(function (field) {
if(field.fieldname == "eventaddress"){
address = field.fieldsanswersvalue;
}
else if (field.fieldname == "eventdate") {
date = field.fieldsanswersvalue;
}
else if (field.fieldname == "eventtime") {
time = field.fieldsanswersvalue;
}
else if(field.fieldname== "institutename"){
institutionName = field.fieldsanswersvalue;
}
else if(field.fieldname== "institutetype"){
institutionType = field.institutionType;
}
});
return{
regiterDate: date + " | " + time,
address: address,
institutionType: institutionType,
institutionName:institutionName,
coordinates:{lat: pluvData.formsanswerslatitude, long:pluvData.formsanswerslongitude}
}
}
async function getPluviometerStation(userId, setPluviometerStation) {
const endpoint =
webSocketClient + `type=PLUVIOMETER_REGISTRATION&user=${userId}`;
const socketObject = new WebSocket(endpoint);
socketObject.onmessage = async ({ data }) => {
const dataObject = JSON.parse(data);
if (dataObject?.success) {
pluvStation_id =
dataObject.responseData.array_to_json[0].formsanswersid;
pluvStation_data = await getPluvStation_data(
pluvStation_id
);
const pluvObject = assemblePluvStationObject(pluvStation_data.data.responseData.array_to_json[0])
setPluviometerStation(pluvObject);
} else {
setPluviometerStation(false);
}
};
socketObject.onerror = (e) => {
console.log(e.message);
setPluviometerStation(null);
};
socketObject.close();
socketObject.onclose = () => {
console.log("FECHOU");
};
}
export default getPluviometerStation;