Skip to content

Commit 3ccc5fd

Browse files
authored
Merge pull request #121 from Zenmo/resourcefully-inputs
Include all input fields in resourcefully export
2 parents 5718748 + c74fd1b commit 3ccc5fd

File tree

4 files changed

+141
-34
lines changed

4 files changed

+141
-34
lines changed

webtool/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite --host 0.0.0.0",
8+
"typecheck": "tsc --build --noEmit",
89
"build": "tsc -b && vite build",
910
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1011
"preview": "vite preview"

webtool/frontend/src/components/resourcefully/dialog.tsx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
import {Button, Dialog, Flex, TextField, Text } from "@radix-ui/themes"
2-
import {FormEventHandler, FunctionComponent} from "react"
2+
import {EventHandler, FunctionComponent, SyntheticEvent} from "react"
33
import {Pilot} from "local4local"
44
import Animation = AnyLogicCloudClient.Animation
55
import {ResourcefullyExport, ExportMetadata} from "local4local"
66
import {createDeeplink} from "../deeplink.ts"
77

8+
function createExportObject(submitEvent: ReactSubmitEvent, pilot: Pilot): ResourcefullyExport {
9+
const form = new FormData(submitEvent.currentTarget)
10+
const exportMetadata = new ExportMetadata(
11+
form.get("scenarioDescription") as string,
12+
form.get("personName") as string,
13+
form.get("organizationName") as string,
14+
form.get("email") as string,
15+
);
16+
17+
return ResourcefullyExport.create(pilot, exportMetadata, createDeeplink(pilot))
18+
}
19+
20+
type ReactSubmitEvent = SyntheticEvent<HTMLFormElement, SubmitEvent>
21+
type SubmitEventHandeler = EventHandler<ReactSubmitEvent>
22+
823
export const ResourcefullyDialog: FunctionComponent<{
924
anyLogicAnimation?: Animation,
1025
pilot: Pilot,
1126
}> = ({pilot}) => {
12-
const onSubmit: FormEventHandler<HTMLFormElement> = (submitEvent) => {
27+
const onSubmit: SubmitEventHandeler = (submitEvent) => {
1328
submitEvent.preventDefault()
14-
const form = new FormData(submitEvent.target as HTMLFormElement)
15-
const exportMetadata = new ExportMetadata(
16-
form.get("scenarioDescription") as string,
17-
form.get("personName") as string,
18-
form.get("organizationName") as string,
19-
form.get("email") as string,
20-
);
21-
22-
const resourceFullyExport = ResourcefullyExport.create(pilot, exportMetadata, createDeeplink(pilot))
29+
const resourceFullyExport = createExportObject(submitEvent, pilot)
2330

24-
// const action = form.get("action") as string
25-
26-
const x = window.open() as Window
27-
x.document.open();
28-
x.document.write('<html><body><pre>' + resourceFullyExport.toJson() + '</pre></body></html>');
29-
x.document.close();
31+
const action = (submitEvent.nativeEvent.submitter as HTMLButtonElement).value
32+
if (action === "preview") {
33+
const x = window.open() as Window
34+
x.document.open();
35+
x.document.write('<html><body><pre>' + resourceFullyExport.toJson() + '</pre></body></html>');
36+
x.document.close();
37+
} else {
38+
// TODO submit to backend
39+
}
3040
}
3141

3242
return (

webtool/gradle/common/src/commonMain/kotlin/Pilot.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.serialization.json.Json
55
import kotlinx.serialization.encodeToString
66
import kotlin.js.ExperimentalJsExport
77
import kotlin.js.JsExport
8+
import kotlin.math.roundToInt
89

910
@JsExport
1011
@Serializable
@@ -130,7 +131,12 @@ data class HouseholdGroup(
130131
val hasHomeBattery_r: Double,
131132
/**Jaarlijks gemiddeld basisverbruik zonder warmtepomp, elektrische voertuigen en zonnepanelen */
132133
val annualBaseConsumptionAvg_kWh: Double,
133-
): AssetType
134+
): AssetType {
135+
fun hasPV_n() = (hasPV_r * households_n).roundToInt()
136+
fun hasHeatPump_n() = (hasHeatPump_r * households_n).roundToInt()
137+
fun hasChargePoint_n() = (hasChargePoint_r * households_n).roundToInt()
138+
fun hasHomeBattery_n() = (hasHomeBattery_r * households_n).roundToInt()
139+
}
134140

135141
@JsExport
136142
@Serializable

webtool/gradle/common/src/commonMain/kotlin/resourcefully/Export.kt

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ data class ResourcefullyExport(
4141
) {
4242
companion object {
4343
@JsStatic
44-
fun create(pilot: Pilot, metadata: ExportMetadata, scenarioUrl: String) = ResourcefullyExport(
45-
scenarioDescription = metadata.scenarioDescription,
46-
personName = metadata.personName,
47-
organizationName = metadata.organizationName,
48-
email = metadata.email,
49-
scenarioUrl = scenarioUrl,
50-
)
44+
fun create(pilot: Pilot, metadata: ExportMetadata, scenarioUrl: String) = with(pilot) {
45+
ResourcefullyExport(
46+
scenarioDescription = metadata.scenarioDescription,
47+
personName = metadata.personName,
48+
organizationName = metadata.organizationName,
49+
email = metadata.email,
50+
scenarioUrl = scenarioUrl,
51+
supplierCost = supplierCost,
52+
householdGroups = householdGroups.map { HouseholdGroup.create(it) },
53+
companies = companies.map { Company.create(it) },
54+
windFarms = windFarms.map { WindFarm.create(it) },
55+
solarFarms = solarFarms.map { SolarFarm.create(it) },
56+
biogasGenerators = biogasGenerators.map { BiogasGenerator.create(it) },
57+
batteries = batteries.map { Battery.create(it) },
58+
)
59+
}
5160
}
5261

5362
fun toJson(): String =
@@ -84,15 +93,38 @@ data class HouseholdGroup(
8493
//val annualElectricityProduction_kWh: Double,
8594
//val annualElectricityDelivery_kWh: Double,
8695
//val annualElectricityFeedIn_kWh: Double,
87-
)
96+
) {
97+
companion object {
98+
fun create(householdGroup: nu.local4local.common.HouseholdGroup) = with(householdGroup) {
99+
HouseholdGroup(
100+
description = type,
101+
households_n = households_n,
102+
hasPV_n = hasPV_n(),
103+
hasHeatPump_n = hasHeatPump_n(),
104+
hasChargePoint_n = hasChargePoint_n(),
105+
annualBaseConsumptionAvg_kWh = annualBaseConsumptionAvg_kWh,
106+
)
107+
}
108+
}
109+
}
88110

89111
@JsExport
90112
@Serializable
91113
data class Company(
92114
val name: String,
93115
val annualElectricityConsumption_kWh: Double,
94116
val pvInstalled_kWp: Double,
95-
)
117+
) {
118+
companion object {
119+
fun create(company: nu.local4local.common.Company) = with(company) {
120+
Company(
121+
name = name,
122+
annualElectricityConsumption_kWh = annualElectricityConsumption_kWh,
123+
pvInstalled_kWp = pvInstalled_kWp,
124+
)
125+
}
126+
}
127+
}
96128

97129
@JsExport
98130
@Serializable
@@ -103,8 +135,22 @@ data class WindFarm(
103135
val sdeBasisenergieprijs_eurpkWh: Double,
104136

105137
/** Simulation result */
106-
val annualElectricityProduction_kWh: Double,
107-
)
138+
val annualElectricityProduction_kWh: Double?,
139+
) {
140+
companion object {
141+
fun create(windFarm: nu.local4local.common.WindFarm) = with(windFarm) {
142+
with(cost) {
143+
WindFarm(
144+
nominalPower_kW = nominalPower_kW,
145+
LCOE_eurpkWH = LCOE_eurpkWH!!,
146+
sdeAanvraagbedrag_eurpkWh = sdeAanvraagbedrag_eurpkWh!!,
147+
sdeBasisenergieprijs_eurpkWh = sdeBasisenergieprijs_eurpkWh!!,
148+
annualElectricityProduction_kWh = null,
149+
)
150+
}
151+
}
152+
}
153+
}
108154

109155
@JsExport
110156
@Serializable
@@ -115,8 +161,23 @@ data class SolarFarm(
115161
val sdeAanvraagbedrag_eurpkWh: Double,
116162
val sdeBasisenergieprijs_eurpkWh: Double,
117163
/** Simulation result */
118-
val annualElectricityProduction_kWh: Double,
119-
)
164+
val annualElectricityProduction_kWh: Double?,
165+
) {
166+
companion object {
167+
fun create(solarFarm: nu.local4local.common.SolarFarm) = with(solarFarm) {
168+
with(cost) {
169+
SolarFarm(
170+
peakPower_kW = nominalPower_kW,
171+
orientation = orientation,
172+
LCOE_eurpkWH = LCOE_eurpkWH!!,
173+
sdeAanvraagbedrag_eurpkWh = sdeAanvraagbedrag_eurpkWh!!,
174+
sdeBasisenergieprijs_eurpkWh = sdeBasisenergieprijs_eurpkWh!!,
175+
annualElectricityProduction_kWh = null,
176+
)
177+
}
178+
}
179+
}
180+
}
120181

121182
@JsExport
122183
@Serializable
@@ -126,8 +187,22 @@ data class BiogasGenerator(
126187
val sdeAanvraagbedrag_eurpkWh: Double,
127188
val sdeBasisenergieprijs_eurpkWh: Double,
128189
/** Simulation result */
129-
val annualElectricityProduction_kWh: Double,
130-
)
190+
val annualElectricityProduction_kWh: Double?,
191+
) {
192+
companion object {
193+
fun create(biogasGenerator: nu.local4local.common.BiogasGenerator) = with(biogasGenerator) {
194+
with(biogasGenerator.cost) {
195+
BiogasGenerator(
196+
power_kW = power_kW,
197+
LCOE_eurpkWH = LCOE_eurpkWH!!,
198+
sdeAanvraagbedrag_eurpkWh = sdeAanvraagbedrag_eurpkWh!!,
199+
sdeBasisenergieprijs_eurpkWh = sdeBasisenergieprijs_eurpkWh!!,
200+
annualElectricityProduction_kWh = null,
201+
)
202+
}
203+
}
204+
}
205+
}
131206

132207
@JsExport
133208
@Serializable
@@ -138,7 +213,22 @@ data class Battery(
138213
val interest_r: Double,
139214
val depreciationPeriod_y: Double,
140215
val OPEX_eurpy: Double,
141-
)
216+
) {
217+
companion object {
218+
fun create(battery: nu.local4local.common.Battery) = with(battery) {
219+
with (cost) {
220+
Battery(
221+
capacity_kWh = capacity_kWh,
222+
peakPower_kW = peakPower_kW,
223+
CAPEX_eur = CAPEX_eur!!,
224+
interest_r = interest_r!!,
225+
depreciationPeriod_y = depreciationPeriod_y!!,
226+
OPEX_eurpy = OPEX_eurpy!!,
227+
)
228+
}
229+
}
230+
}
231+
}
142232

143233
@JsExport
144234
@Serializable

0 commit comments

Comments
 (0)