Skip to content

Commit 763d1d9

Browse files
committed
Remove "asset" abstraction
1 parent aeb1452 commit 763d1d9

File tree

8 files changed

+46
-66
lines changed

8 files changed

+46
-66
lines changed

webtool/frontend/src/components/assets/battery.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const BatteriesDisplayEdit: FunctionComponent<{
9595
<BatteryForm
9696
key={"Battery_" + i}
9797
save={(asset: Battery) => {
98-
onChange(pilot.replaceAsset(asset, i))
98+
onChange(pilot.replaceBattery(asset, i))
9999
setSelected(null);
100100
}}
101101
hide={() => {
@@ -108,7 +108,7 @@ export const BatteriesDisplayEdit: FunctionComponent<{
108108
key={"Battery_" + i}
109109
battery={it}
110110
onEdit={() => { setSelected(it)}}
111-
toDelete={() => onChange(pilot.remove(it))}
111+
toDelete={() => onChange(pilot.removeBattery(it))}
112112
/>
113113
)
114114
)}

webtool/frontend/src/components/assets/biogas-generator.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const BiogasGeneratorsDisplayEdit: FunctionComponent<{
8585
<BiogasGeneratorForm
8686
key={"BiogasGenerator_" + i}
8787
save={(asset: BiogasGenerator) => {
88-
onChange(pilot.replaceAsset(asset, i))
88+
onChange(pilot.replaceBiogasGenerator(asset, i))
8989
setSelected(null);
9090
}}
9191
hide={() => {
@@ -98,7 +98,7 @@ export const BiogasGeneratorsDisplayEdit: FunctionComponent<{
9898
key={"BiogasGenerator_" + i}
9999
biogasGenerator={it}
100100
onEdit={() => { setSelected(it)}}
101-
toDelete={() => onChange(pilot.remove(it))}
101+
toDelete={() => onChange(pilot.removeBiogasGenerator(it))}
102102
/>
103103
)
104104
)}

webtool/frontend/src/components/assets/wind-farm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const WindFarmsDisplayEdit: FunctionComponent<{
8686
<WindFarmForm
8787
key={"WindFarm_" + i}
8888
save={(asset: WindFarm) => {
89-
onChange(pilot.replaceAsset(asset, i))
89+
onChange(pilot.replaceWindFarm(asset, i))
9090
setSelected(null);
9191
}}
9292
hide={() => {
@@ -99,7 +99,7 @@ export const WindFarmsDisplayEdit: FunctionComponent<{
9999
key={"WindFarm_" + i}
100100
windFarm={it}
101101
onEdit={() => { setSelected(it)}}
102-
toDelete={() => onChange(pilot.remove(it))}
102+
toDelete={() => onChange(pilot.removeWindFarm(it))}
103103
/>
104104
)
105105
)}

webtool/frontend/src/components/configure.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ export const Configure: FunctionComponent<{
5858
<HeatStoragesDisplayEdit pilot={pilot} onChange={onChange}/>
5959

6060
{showAddHouseholdGroup &&
61-
<HouseholdForm save={(asset: HouseholdGroup) => onChange(pilot.create(asset))} hide={() => setShowAddHouseholdGroup(false)}/>}
61+
<HouseholdForm save={(asset: HouseholdGroup) => onChange(pilot.addHouseHoldGroup(asset))} hide={() => setShowAddHouseholdGroup(false)}/>}
6262
{showAddCompany &&
6363
<CompanyForm save={(company: Company) => onChange(pilot.addCompany(company))} hide={() => setShowAddCompany(false)}/>}
6464
{showAddSolarFarm &&
65-
<SolarFarmForm save={(asset: SolarFarm) => onChange(pilot.create(asset))} hide={() => setShowAddSolarFarm(false)}/>}
65+
<SolarFarmForm save={(asset: SolarFarm) => onChange(pilot.addSolarFarm(asset))} hide={() => setShowAddSolarFarm(false)}/>}
6666
{showAddWindFarm &&
67-
<WindFarmForm save={(asset: WindFarm) => onChange(pilot.create(asset))} hide={() => setShowAddWindFarm(false)}/>}
67+
<WindFarmForm save={(asset: WindFarm) => onChange(pilot.addWindFarm(asset))} hide={() => setShowAddWindFarm(false)}/>}
6868
{showAddBiogasGenerator &&
69-
<BiogasGeneratorForm save={(asset) => onChange(pilot.create(asset))} hide={() => setShowAddBiogasGenerator(false)}/>}
69+
<BiogasGeneratorForm save={(asset) => onChange(pilot.addBiogasGenerator(asset))} hide={() => setShowAddBiogasGenerator(false)}/>}
7070
{showAddBattery &&
71-
<BatteryForm save={(asset: Battery) => onChange(pilot.create(asset))} hide={() => setShowAddBattery(false)}/>}
71+
<BatteryForm save={(asset: Battery) => onChange(pilot.addBattery(asset))} hide={() => setShowAddBattery(false)}/>}
7272
{showAddHeatStorage &&
73-
<HeatStorageForm save={(asset: HeatStorage) => onChange(pilot.create(asset))} hide={() => setShowAddHeatStorage(false)}/>}
73+
<HeatStorageForm save={(asset: HeatStorage) => onChange(pilot.addHeatStorage(asset))} hide={() => setShowAddHeatStorage(false)}/>}
7474

7575
{showAddDropdown &&
7676
<Flex gap=".5rem" justify="end">

webtool/frontend/src/components/heat-storage/heat-storage-display-edit.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const HeatStoragesDisplayEdit: FunctionComponent<{
1616
<HeatStorageForm
1717
key={"HeatStorage_" + i}
1818
save={(asset: HeatStorage) => {
19-
onChange(pilot.replaceAsset(asset, i))
19+
onChange(pilot.replaceHeatStorage(asset, i))
2020
setSelected(null);
2121
}}
2222
hide={() => {
@@ -29,7 +29,7 @@ export const HeatStoragesDisplayEdit: FunctionComponent<{
2929
key={"HeatStorage_" + i}
3030
heatStorage={it}
3131
onEdit={() => { setSelected(it)}}
32-
toDelete={() => onChange(pilot.remove(it))}
32+
toDelete={() => onChange(pilot.removeHeatStorage(it))}
3333
/>
3434
)
3535
)}

webtool/frontend/src/components/household/households-display-edit.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const HouseholdsDisplayEdit: FunctionComponent<{
1515
<HouseholdForm
1616
key={"householdGroup_" + i}
1717
save={(asset: HouseholdGroup) => {
18-
onChange(pilot.replaceAsset(asset, i))
18+
onChange(pilot.replaceHouseholdGroup(asset, i))
1919
setSelectedHouseholdGroup(null);
2020
}}
2121
hide={() => {
@@ -28,7 +28,7 @@ export const HouseholdsDisplayEdit: FunctionComponent<{
2828
key={"householdGroup_" + i}
2929
householdGroup={it}
3030
onEdit={() => { setSelectedHouseholdGroup(it)}}
31-
toDelete={() => onChange(pilot.remove(it))}
31+
toDelete={() => onChange(pilot.removeHouseholdGroup(it))}
3232
/>
3333
)
3434
)}

webtool/frontend/src/components/solarfarm/solarfarms-display-edit.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const SolarFarmsDisplayEdit: FunctionComponent<{
1616
<SolarFarmForm
1717
key={"SolarFarm_" + i}
1818
save={(asset: SolarFarm) => {
19-
onChange(pilot.replaceAsset(asset, i))
19+
onChange(pilot.replaceSolarFarm(asset, i))
2020
setSelected(null);
2121
}}
2222
hide={() => {
@@ -29,7 +29,7 @@ export const SolarFarmsDisplayEdit: FunctionComponent<{
2929
key={"SolarFarm_" + i}
3030
solarFarm={it}
3131
onEdit={() => { setSelected(it)}}
32-
toDelete={() => onChange(pilot.remove(it))}
32+
toDelete={() => onChange(pilot.removeSolarFarm(it))}
3333
/>
3434
)
3535
)}

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

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package nu.local4local.common
33
import kotlinx.serialization.Serializable
44
import kotlinx.serialization.json.Json
55
import kotlinx.serialization.encodeToString
6-
import kotlin.js.ExperimentalJsExport
76
import kotlin.js.JsExport
87
import kotlin.math.roundToInt
98

@@ -24,38 +23,29 @@ data class Pilot(
2423
val bufferPrice_eurpkWh
2524
get() = supplierCost.bufferPrice_eurpkWh
2625

27-
// Create
28-
fun create(asset: AssetType): Pilot = when (asset) {
29-
is HouseholdGroup -> copy(householdGroups = this.householdGroups + asset)
30-
is SolarFarm -> copy(solarFarms = this.solarFarms + asset)
31-
is WindFarm -> copy(windFarms = this.windFarms + asset)
32-
is Battery -> copy(batteries = this.batteries + asset)
33-
is HeatStorage -> copy(heatStorages = this.heatStorages + asset)
34-
is BiogasGenerator -> copy(biogasGenerators = this.biogasGenerators + asset)
35-
else -> throw Exception("Unknown type")
36-
}
37-
38-
// Delete
39-
fun remove(asset: AssetType): Pilot = when (asset) {
40-
is HouseholdGroup -> copy(householdGroups = this.householdGroups - asset)
41-
is SolarFarm -> copy(solarFarms = this.solarFarms - asset)
42-
is WindFarm -> copy(windFarms = this.windFarms - asset)
43-
is Battery -> copy(batteries = this.batteries - asset)
44-
is HeatStorage -> copy(heatStorages = this.heatStorages - asset)
45-
is BiogasGenerator -> copy(biogasGenerators = this.biogasGenerators - asset)
46-
else -> throw Exception("Unknown type")
47-
}
26+
fun addHouseHoldGroup(householdGroup: HouseholdGroup) = copy(householdGroups = householdGroups + householdGroup)
27+
fun addCompany(company: Company) = copy(companies = companies + company)
28+
fun addSolarFarm(solarFarm: SolarFarm) = copy(solarFarms = solarFarms + solarFarm)
29+
fun addWindFarm(windFarm: WindFarm) = copy(windFarms = windFarms + windFarm)
30+
fun addBattery(battery: Battery) = copy(batteries = batteries + battery)
31+
fun addHeatStorage(heatStorage: HeatStorage) = copy(heatStorages = heatStorages + heatStorage)
32+
fun addBiogasGenerator(biogasGenerator: BiogasGenerator) = copy(biogasGenerators = biogasGenerators + biogasGenerator)
4833

49-
// Generalized replace function
50-
fun replaceAsset(newAsset: AssetType, index: Int,): Pilot = when (newAsset) {
51-
is HouseholdGroup -> copy(householdGroups = householdGroups.replaceAt(index, newAsset))
52-
is SolarFarm -> copy(solarFarms = solarFarms.replaceAt(index, newAsset))
53-
is WindFarm -> copy(windFarms = windFarms.replaceAt(index, newAsset))
54-
is Battery -> copy(batteries = batteries.replaceAt(index, newAsset))
55-
is HeatStorage -> copy(heatStorages = heatStorages.replaceAt(index, newAsset))
56-
is BiogasGenerator -> copy(biogasGenerators = biogasGenerators.replaceAt(index, newAsset))
57-
else -> throw Exception("Unknown type")
58-
}
34+
fun removeHouseholdGroup(householdGroup: HouseholdGroup) = copy(householdGroups = this.householdGroups - householdGroup)
35+
fun removeCompany(company: Company) = copy(companies = companies - company)
36+
fun removeSolarFarm(solarFarm: SolarFarm) = copy(solarFarms = this.solarFarms - solarFarm)
37+
fun removeWindFarm(windFarm: WindFarm) = copy(windFarms = this.windFarms - windFarm)
38+
fun removeBattery(battery: Battery) = copy(batteries = this.batteries - battery)
39+
fun removeHeatStorage(heatStorage: HeatStorage) = copy(heatStorages = this.heatStorages - heatStorage)
40+
fun removeBiogasGenerator(biogasGenerator: BiogasGenerator) = copy(biogasGenerators = this.biogasGenerators - biogasGenerator)
41+
42+
fun replaceHouseholdGroup(householdGroup: HouseholdGroup, index: Int) = copy(householdGroups = householdGroups.replaceAt(index, householdGroup))
43+
fun replaceSolarFarm(solarFarm: SolarFarm, index: Int) = copy(solarFarms = solarFarms.replaceAt(index, solarFarm))
44+
fun replaceWindFarm(windFarm: WindFarm, index: Int) = copy(windFarms = windFarms.replaceAt(index, windFarm))
45+
fun replaceBattery(battery: Battery, index: Int) = copy(batteries = batteries.replaceAt(index, battery))
46+
fun replaceHeatStorage(heatStorage: HeatStorage, index: Int) = copy(heatStorages = heatStorages.replaceAt(index, heatStorage))
47+
fun replaceBiogasGenerator(biogasGenerator: BiogasGenerator, index: Int) = copy(biogasGenerators = biogasGenerators.replaceAt(index, biogasGenerator))
48+
fun replaceCompany(old: Company, new: Company) = copy(companies = companies.replace(old, new))
5949

6050
// Extension function for List replacement
6151
private fun <T> List<T>.replaceAt(index: Int, newValue: T): List<T> {
@@ -67,12 +57,6 @@ data class Pilot(
6757

6858
fun withSupplierCost(supplierCost: SupplierCost) = copy(supplierCost = supplierCost)
6959

70-
fun addCompany(company: Company) = copy(companies = companies + company)
71-
72-
fun removeCompany(company: Company) = copy(companies = companies - company)
73-
74-
fun replaceCompany(old: Company, new: Company) = copy(companies = companies.replace(old, new))
75-
7660
fun toJson(): String =
7761
Json.encodeToString(this)
7862
}
@@ -104,10 +88,6 @@ data class SupplierCost (
10488
val feedInCompensation_eurpkWh: Double = 0.00,
10589
)
10690

107-
@JsExport
108-
@Serializable
109-
sealed interface AssetType
110-
11191
@JsExport
11292
@Serializable
11393
data class AssetCost(
@@ -131,7 +111,7 @@ data class HouseholdGroup(
131111
val hasHomeBattery_r: Double,
132112
/**Jaarlijks gemiddeld basisverbruik zonder warmtepomp, elektrische voertuigen en zonnepanelen */
133113
val annualBaseConsumptionAvg_kWh: Double,
134-
): AssetType {
114+
) {
135115
fun hasPV_n() = (hasPV_r * households_n).roundToInt()
136116
fun hasHeatPump_n() = (hasHeatPump_r * households_n).roundToInt()
137117
fun hasChargePoint_n() = (hasChargePoint_r * households_n).roundToInt()
@@ -178,29 +158,29 @@ data class SolarFarm(
178158
val nominalPower_kW: Double,
179159
val orientation: PVOrientation = PVOrientation.SOUTH,
180160
val cost: AssetCost,
181-
): AssetType
161+
)
182162

183163
@JsExport
184164
@Serializable
185165
data class BiogasGenerator(
186166
val power_kW: Double,
187167
val cost: AssetCost,
188-
): AssetType
168+
)
189169

190170
@JsExport
191171
@Serializable
192172
data class WindFarm(
193173
val nominalPower_kW: Double,
194174
val cost: AssetCost,
195-
): AssetType
175+
)
196176

197177
@JsExport
198178
@Serializable
199179
data class Battery(
200180
val capacity_kWh: Double,
201181
val peakPower_kW: Double,
202182
val cost: AssetCost,
203-
): AssetType
183+
)
204184

205185
@JsExport
206186
@Serializable
@@ -210,7 +190,7 @@ data class HeatStorage(
210190
val minTemp_degC: Double,
211191
val maxTemp_degC: Double,
212192
val cost: AssetCost,
213-
): AssetType {
193+
) {
214194
fun getCapacity_kWh(): Double {
215195
val specificHeatCapacity = 4.18 // kJ/kg/K
216196

0 commit comments

Comments
 (0)