index.js 5.8 KB
Newer Older
abergavenny's avatar
abergavenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import jwtDecode from 'jwt-decode'

import {
  CONVERSION_FACTOR_GAS,
  CONVERSION_FACTOR_HEAT_PUMP,
  CONVERSION_FACTOR_OIL,
  CONVERSION_FACTOR_PELLETS,
  EQUIVALENT_CO2,
  IMPACT_WINDOW_CONDITION,
  IMPACT_WINDOW_GLAZING,
  LAMBDA_INSULATION,
  LAMBDA_ROOF,
  LAMBDA_ROOF_THICKNESS,
  LAMBDA_STRUCTURE,
  REFERENCE_CO2_EMISSION,
  REFERENCE_ENERGY_PERFORMANCE
} from '@/data/parameters'

const status = {
  Success: 'SUCCESS',
  Warning: 'WARNING',
  Error: 'ERROR'
}

Object.freeze(status)

export const ResponseStatus = status

export const getBearerToken = () => {
  const storage = window.localStorage

  const token = storage.getItem('token')

  return `Bearer ${token}`
}

export const setToken = (token) => {
  const storage = window.localStorage

  storage.setItem('token', token)
}

export const getTokenPayload = (token) => {
  setToken(token)

  const payload = jwtDecode(token)

  return payload
}

// DEVINFO: Hier wird eine Kennung für ein Gebäude erzeugt
export function randomPrefix(length = 4) {
  const chars = 'abcdefghijkmnopqrstuxyz'
  let prefix = ''

  for (let i = 0; i < length; i++) {
    prefix += chars.charAt(Math.floor(Math.random() * chars.length))
  }

  return prefix
}

// DEVINFO: Dauer des Timeouts bis das Formular nach absenden erneut benutzt werden kann - default: 2000
export function resetAlert(alert) {
  return setTimeout(() => {
    alert.value = null
  }, 2000)
}

export function numberOrText(value, ignore = false) {
  return ignore || isNaN(value) ? value : value.toLocaleString('de-DE')
}

export function convertHeatConsumption(value, unit) {
  if (!unit) return 0

  let conversion

  if (unit === 'DISTRICT_HEATING') {
    conversion = value
  } else if (unit === 'OIL_HEATING') {
    conversion = value * CONVERSION_FACTOR_OIL
  } else if (unit === 'GAS_HEATING') {
    conversion = value * CONVERSION_FACTOR_GAS
  } else if (unit === 'HEAT_PUMP') {
    conversion = value / CONVERSION_FACTOR_HEAT_PUMP
  } else if (unit === 'HEAT_PUMP_CLEAN') {
    conversion = value / CONVERSION_FACTOR_HEAT_PUMP
  } else if (unit === 'PELLETS') {
    conversion = value * CONVERSION_FACTOR_PELLETS
  } else {
    conversion = value
  }

  return Math.round(conversion)
}

// DEVINFO Hier kann der Bewertungstext (Heizung) angepasst werden
export function convertHeaterEfficiency(value) {
  if (value < 0.75) return `Schlecht`
  else if (value < 0.80) return `Akzeptabel`
  else if (value < 0.95) return `Gut`
  else if (value <= 1) return `Sehr gut`
  else return null
}

// DEVINFO Hier kann der Bewertungstext (Fenster) angepasst werden
export function convertWindowEfficiency(value) {
  if (value < 1.5) return `Schlecht`
  else if (value >= 1.5 && value < 2.5) return `Akzeptabel`
  else if (value >= 2.5 && value < 3.5) return `Gut`
  else if (value >= 3.5) return `Sehr gut`
  else return null
}

export function calculateCO2Efficiency(type, consumption) {
  const refValue = REFERENCE_CO2_EMISSION
  let performance, potential, proportion

  const pellets = EQUIVALENT_CO2['PELLETS']
  const heating = EQUIVALENT_CO2[type]

  performance = (heating / 1000) * consumption
  potential = performance - ((pellets / 1000) * consumption)
  proportion = performance / refValue

  return [Math.round(performance), Math.round(potential), Math.round(proportion)]
}

export function calculateFacadeEfficiency(material, materialThickness, structure, structureThickness) {
  const refValue = 0.213
  let performance, potential

  const insulationLambda = LAMBDA_INSULATION[material]
  const structureLambda = LAMBDA_STRUCTURE[structure]

  const insulationThickness = materialThickness / 100
  const thickness = structureThickness / 100
  const insulationDelta = insulationThickness / insulationLambda
  const structureDelta = (thickness - insulationThickness) / structureLambda

  performance = 1 / (insulationDelta + structureDelta + refValue)
  potential = ((1 / refValue * performance) - 1) * 100

  return [performance.toPrecision(3), Math.round(potential)]
}

export function calculateHeaterEfficiency(total, working) {
  let performance

  performance = working / total

  return [performance]
}

export function calculateHeatingEfficiency(epc = null, consumption = null, buildingArea = null, demand = null, apartmentArea = null) {
  const refValue = REFERENCE_ENERGY_PERFORMANCE
  let distict, performance, potential

  if (consumption && buildingArea) {
    distict = false
    performance = consumption / buildingArea
  } else if (demand) {
    distict = true
    performance = demand / apartmentArea
  } else {
    distict = false
    performance = epc
  }

  potential = ((1 / refValue * performance) - 1) * 100

  return [distict, Math.round(performance), Math.round(potential)]
}

export function calculateRoofEfficiency(flat, material, materialThickness) {
  const refValue = 0.17
  let performance, potential

  const insulationLambda = LAMBDA_INSULATION[material]
  const roofLambda = LAMBDA_ROOF[flat ? 'FLAT' : 'NORMAL']
  const roofThickness = LAMBDA_ROOF_THICKNESS[flat ? 'FLAT' : 'NORMAL']

  const insulationThickness = materialThickness / 100
  const thickness = roofThickness / 100
  const insulationDelta = insulationThickness / insulationLambda
  const roofDelta = thickness / roofLambda

  performance = 1 / (insulationDelta + roofDelta + refValue)
  potential = ((1 / refValue * performance) - 1) * 100

  return [performance.toPrecision(3), Math.round(potential)]
}

export function calculateWindowEfficiency(avgAge, avgCondition, glazing) {
  let refValue, performance

  if (avgAge < 5) {
    refValue = 4
  } else if (avgAge < 10) {
    refValue = 3
  } else if (avgAge < 20) {
    refValue = 2
  } else if (avgAge >= 20) {
    refValue = 1
  }

  const conditionImpact = IMPACT_WINDOW_CONDITION[avgCondition]
  const glazingImpact = IMPACT_WINDOW_GLAZING[glazing]

  performance = ((refValue * 2) + conditionImpact + (glazingImpact * 2)) / 5

  return [performance]
}