'use client';
import {
useState,
useEffect,
PropsWithChildren,
ReactNode,
SetStateAction,
Dispatch,
ChangeEventHandler,
ForwardedRef,
forwardRef,
useRef,
} from 'react';
import { useMediaQuery } from 'react-responsive';
import { updateComputationResultEWS } from '@/redux/computationsEWSSlice';
import { useAppSelector, useAppDispatch } from '@/redux/hooks';
import type Polygon from '@arcgis/core/geometry/Polygon';
import Warning from '@/app/components/warning';
import { initializeCalculationsMenuHandlers } from './potenzialkarte';
import { calculateGrid } from './gridcomputer';
const InputSection = (props: { children: ReactNode }) => {
return
{props.children}
;
};
type Properties = {
isLoading: Dispatch>;
};
type Resource = { layerId: number; layerName: string; feature: any };
const css =
'peer block min-h-[auto] w-full border px-3 py-1 leading-normal outline-none transition-all duration-200 ease-linear motion-reduce:transition-none';
export default forwardRef(function CalculationsMenu(
{ isLoading }: PropsWithChildren,
outerRef: ForwardedRef | undefined
) {
const isTablet = useMediaQuery({ maxWidth: 1024 });
const [polygon, setPolygon] = useState(null);
const [gridSpacing, setGridSpacing] = useState(10);
const [boreDepth, setBoreDepth] = useState(100);
const [BS_HZ, setBS_HZ] = useState(-1);
const [BS_KL, setBS_KL] = useState(-1);
const [P_KL, setP_KL] = useState(-1);
const [P_HZ, setP_HZ] = useState(-1);
const [points, setPoints] = useState([]);
const [heating, setHeating] = useState(35);
const [opened, setOpened] = useState(true);
const innerRef = useRef(null);
const cadastralData = useAppSelector((store) => store.cadastre.value);
const resources: Resource[] = useAppSelector((store) => store.resourcesEWS.value);
const dispatch = useAppDispatch();
// run python script with values from layers
const handlePythonCalculation = () => {
if (cadastralData && resources && points.length <= 300 && polygon) {
isLoading(true);
let pointsText = JSON.stringify(points);
const BT = parseFloat(
resources.find((result) => result.layerId === 0)?.feature?.attributes['Classify.Pixel Value']
);
const GT = parseFloat(
resources.find((result) => result.layerId === 1)?.feature?.attributes['Classify.Pixel Value']
);
const WLF = parseFloat(
resources.find((result) => result.layerId === 2)?.feature?.attributes['Classify.Pixel Value']
);
const BS_HZ_Norm = parseInt(
resources.find((result) => result.layerId === 7)?.feature?.attributes['Classify.Pixel Value']
);
const BS_KL_Norm = parseInt(
resources.find((result) => result.layerId === 8)?.feature?.attributes['Classify.Pixel Value']
);
let url = '/api';
const data = {
BT,
GT,
WLF,
BS_HZ_Norm,
BS_KL_Norm,
BS_HZ: BS_HZ === -1 ? 0 : BS_HZ,
BS_KL: BS_KL === -1 ? 0 : BS_KL,
P_HZ: P_HZ === -1 ? 0 : P_HZ,
P_KL: P_KL === -1 ? 0 : P_KL,
boreDepth,
points: pointsText,
heating,
};
if (
Object.values(data).every((x) => typeof x !== 'undefined' && x !== null) &&
!isNaN(BT) &&
!isNaN(GT) &&
!isNaN(WLF) &&
!isNaN(BS_HZ_Norm) &&
!isNaN(BS_KL_Norm)
) {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((res) => res.json())
.then((data) => {
// user defined input
const calculationMode = data[0];
const P_HZ_user = parseFloat(data[1]);
const P_KL_user = -parseFloat(data[2]);
const E_HZ_user = parseFloat(data[3]) / 1000;
const E_KL_user = -(parseFloat(data[4]) / 1000);
const cover = parseInt(data[5]);
const Pel_heatpump_user = parseFloat(data[6]);
const Pel_chiller_user = -parseFloat(data[7]);
const Eel_heatpump_user = parseFloat(data[8]) / 1000;
const Eel_chiller_user = -parseFloat(data[9]) / 1000;
const COP = parseFloat(data[15]);
const EER = parseFloat(data[16]);
const SCOP = parseFloat(data[17]);
const SEER = parseFloat(data[18]);
const Efactor_user = parseFloat(data[19]);
const imagehash = 'data:image/png;base64,' + data[20];
const imagehashSondenfeld = 'data:image/png;base64,' + data[21];
const GTcalc = parseFloat(data[22]);
const heizleistung = P_HZ_user + Pel_heatpump_user;
const heizarbeit = E_HZ_user + Eel_heatpump_user;
const kuehlleistung = P_KL_user - Pel_chiller_user;
const kuehlarbeit = E_KL_user - Eel_chiller_user;
// automatically balanced
const balanced = parseInt(data[23]);
const P_HZ_bal = parseFloat(data[24]);
const P_KL_bal = -parseFloat(data[25]);
const E_HZ_bal = parseFloat(data[26]) / 1000;
const E_KL_bal = -parseFloat(data[27]) / 1000;
const cover_bal = parseInt(data[28]);
const Pel_heatpump_bal = parseFloat(data[29]);
const Pel_chiller_bal = -parseFloat(data[30]);
const Eel_heatpump_bal = parseFloat(data[31]) / 1000;
const Eel_chiller_bal = -parseFloat(data[32]) / 1000;
const meanBoreholeSpacing = parseFloat(data[36]);
const cover_rise = parseFloat(data[37]);
const COP_bal = parseFloat(data[38]);
const EER_bal = parseFloat(data[39]);
const SCOP_bal = parseFloat(data[40]);
const SEER_bal = parseFloat(data[41]);
const Efactor_bal = parseFloat(data[42]);
const imagehashBal = 'data:image/png;base64,' + data[43];
const BS_HZ_bal = parseFloat(data[44]);
const BS_KL_bal = parseFloat(data[45]);
const T_radiator = parseInt(data[46]);
const heizleistungBal = P_HZ_bal + Pel_heatpump_bal;
const heizarbeitBal = Eel_heatpump_bal + E_HZ_bal;
const kuehlleistungBal = P_KL_bal - Pel_chiller_bal;
const kuehlarbeitBal = E_KL_bal - Eel_chiller_bal;
dispatch(
updateComputationResultEWS({
calculationMode,
points: points.length,
meanBoreholeSpacing,
boreDepth,
P_HZ,
P_KL,
BS_HZ,
BS_KL,
BS_HZ_Norm,
BS_KL_Norm,
P_HZ_user,
P_KL_user,
Pel_heatpump_user,
Pel_chiller_user,
E_HZ_user,
E_KL_user,
Efactor_user,
Eel_heatpump_user,
Eel_chiller_user,
cover,
imagehash,
imagehashSondenfeld,
balanced,
P_HZ_bal,
P_KL_bal,
Pel_heatpump_bal,
Pel_chiller_bal,
Eel_heatpump_bal,
Efactor_bal,
E_HZ_bal,
cover_bal,
imagehashBal,
heizleistung,
heizarbeit,
kuehlleistung,
kuehlarbeit,
heizleistungBal,
heizarbeitBal,
kuehlleistungBal,
GTcalc,
COP,
SCOP,
EER,
SEER,
BS_HZ_bal,
BS_KL_bal,
COP_bal,
EER_bal,
E_KL_bal,
SEER_bal,
SCOP_bal,
Eel_chiller_bal,
kuehlarbeitBal,
cover_rise,
T_radiator,
})
);
isLoading(false);
})
.catch((err) => {
dispatch(
updateComputationResultEWS({
error: JSON.stringify(err),
})
);
});
} else {
dispatch(
updateComputationResultEWS({
error: 'Aufgrund ungültiger Daten ist für dieses Grundstück keine Berechnung möglich.',
})
);
isLoading(false);
}
}
};
useEffect(() => {
// initialize callback functions
initializeCalculationsMenuHandlers(setPoints, setPolygon);
// cleanup
return () => {
// set polygon to null
setPolygon(null);
};
}, []);
// reset state
useEffect(() => {
setGridSpacing(10);
}, [polygon]);
const handleGridSpacing: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLInputElement).value);
if (value < 5) {
value = 5;
} else if (value > 15) {
value = 15;
}
setGridSpacing(value);
calculateGrid(polygon, value, setPoints);
};
const handleDepth: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLInputElement).value);
if (value > 250) {
value = 250;
} else if (value < 80) {
value = 80;
}
setBoreDepth(value);
};
const handleBS_HZ: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLInputElement).value);
if (value > 4379 || value < 0) {
value = -1;
}
if (value !== -1) {
setBS_HZ(value);
}
};
const handleBS_KL: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLInputElement).value);
if (event.target && event.target !== null) {
if (value > 4379 || value < 0) {
value = -1;
}
if (value !== -1) {
setBS_KL(value);
}
}
};
const handleP_HZ: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLInputElement).value);
if (value < 0) {
value = -1;
}
if (value !== -1) {
setP_HZ(value);
}
};
const handleP_KL: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLInputElement).value);
if (value < 0) {
value = -1;
}
if (value !== -1) {
setP_KL(value);
}
};
const handleKeyDown = (event: any) => {
event.preventDefault();
};
const handleHeating: ChangeEventHandler = (event) => {
let value = parseInt((event.target as HTMLSelectElement).value);
setHeating(value);
};
const handleClick = () => {
setOpened(!opened);
if (innerRef && innerRef.current) {
innerRef.current.classList.toggle('hidden');
}
};
return (
Berechnungsmenü {opened ? '-' : '+'}
);
});