From 94630018f7aae0de4585b01d4e8ed1ee155ce03d Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 9 May 2024 15:36:40 +0200 Subject: [PATCH] Completing MEB workflow --- workflows/meb/module/module.js | 78 ++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/workflows/meb/module/module.js b/workflows/meb/module/module.js index 4d588c8..7d5e71d 100644 --- a/workflows/meb/module/module.js +++ b/workflows/meb/module/module.js @@ -1,5 +1,6 @@ -const { dialog, BrowserWindow } = require("electron"); -const fs = require('fs'); +const { dialog, BrowserWindow } = require('electron') +const fs = require('fs') +const { Blob } = require('buffer') const dotenv = require('dotenv') const session_start = 'https://nextgenerations-cities.encs.concordia.ca/api/v1.4/session/start' //const insel_meb = 'https://nextgenerations-cities.encs.concordia.ca/api/v1.4/workflow/insel-monthly-energy-balance' @@ -25,19 +26,19 @@ exports.run = async (application, param, tmp_path) => { const session_id = response.headers.get('session_id') let token = response.headers.get('token') console.log(`token: ${token}`) - request = new FormData() - let formData = new FormData(); + let formData = new FormData() formData.append('height_field', param['height_field']) formData.append('year_of_construction_field', param['year_of_construction_field']) formData.append('function_field', param['function_field']) formData.append('function_to_hub', param['function_to_hub']) formData.append('construction_handler', param['construction_handler']) formData.append('usage_handler', param['usage_handler']) - formData.append('geometry_file', fs.createReadStream(param['geometry_file'])); + var filename = param['geometry_file'].replace(/^.*[\\/]/, '') + formData.append('geometry_file', new Blob([fs.readFileSync(param['geometry_file'])]), filename) + response = await fetch(insel_meb, { method: 'POST', headers: { - 'Content-type': 'multipart/form-data', 'session-id': session_id, 'token': token, 'application-uuid': application_uuid, @@ -45,31 +46,64 @@ exports.run = async (application, param, tmp_path) => { }, body: formData }, 600000) - console.log(`response: ${response.status}`) + var error = '' if (response.status == 200) { results = await response.json() + console.log(results); + var meb_header = "BUILDING NAME" + meb_header = `${meb_header};YEARLY COOLING DEMAND;YEARLY HEATING DEMAND;MONTHLY COOLING DEMAND(JAN);MONTHLY COOLING DEMAND(FEB);MONTHLY COOLING DEMAND(MAR);MONTHLY COOLING DEMAND(APR);MONTHLY COOLING DEMAND(MAY);MONTHLY COOLING DEMAND(JUN);MONTHLY COOLING DEMAND(JUL);MONTHLY COOLING DEMAND(AUG);MONTHLY COOLING DEMAND(SEPT);MONTHLY COOLING DEMAND(OCT);MONTHLY COOLING DEMAND(NOV);MONTHLY COOLING DEMAND(DEC);MONTHLY HEATING DEMAND(JAN);MONTHLY HEATING DEMAND(FEB);MONTHLY HEATING DEMAND(MAR);MONTHLY HEATING DEMAND(APR);MONTHLY HEATING DEMAND(MAY);MONTHLY HEATING DEMAND(JUN);MONTHLY HEATING DEMAND(JUL);MONTHLY HEATING DEMAND(AUG);MONTHLY HEATING DEMAND(SEPT);MONTHLY HEATING DEMAND(OCT);MONTHLY HEATING DEMAND(NOV);MONTHLY HEATING DEMAND(DEC)` + meb_header = `${meb_header};YEARLY COOLING PEAK LOAD;YEARLY HEATING PEAK LOAD;MONTHLY COOLING PEAK LOAD (JAN);MONTHLY COOLING PEAK LOAD (FEB);MONTHLY COOLING PEAK LOAD (MAR);MONTHLY COOLING PEAK LOAD (APR);MONTHLY COOLING PEAK LOAD (MAY);MONTHLY COOLING PEAK LOAD (JUN);MONTHLY COOLING PEAK LOAD (JUL);MONTHLY COOLING PEAK LOAD (AUG);MONTHLY COOLING PEAK LOAD (SEPT);MONTHLY COOLING PEAK LOAD (OCT);MONTHLY COOLING PEAK LOAD (NOV);MONTHLY COOLING PEAK LOAD (DEC);MONTHLY HEATING PEAK LOAD (JAN);MONTHLY HEATING PEAK LOAD (FEB);MONTHLY HEATING PEAK LOAD (MAR);MONTHLY HEATING PEAK LOAD (APR);MONTHLY HEATING PEAK LOAD (MAY);MONTHLY HEATING PEAK LOAD (JUN);MONTHLY HEATING PEAK LOAD (JUL);MONTHLY HEATING PEAK LOAD (AUG);MONTHLY HEATING PEAK LOAD (SEPT);MONTHLY HEATING PEAK LOAD (OCT);MONTHLY HEATING PEAK LOAD (NOV);MONTHLY HEATING PEAK LOAD (DEC)` + meb_header = `${meb_header};YEARLY LIGHTING PEAK LOAD;MONTHLY LIGHTING PEAK LOAD(JAN);MONTHLY LIGHTING PEAK LOAD(FEB);MONTHLY LIGHTING PEAK LOAD(MAR);MONTHLY LIGHTING PEAK LOAD(APR);MONTHLY LIGHTING PEAK LOAD(MAY);MONTHLY LIGHTING PEAK LOAD(JUN);MONTHLY LIGHTING PEAK LOAD(JUL);MONTHLY LIGHTING PEAK LOAD(AUG);MONTHLY LIGHTING PEAK LOAD(SEPT);MONTHLY LIGHTING PEAK LOAD(OCT);MONTHLY LIGHTING PEAK LOAD(NOV);MONTHLY LIGHTING PEAK LOAD(DEC)` + meb_header = `${meb_header};YEARLY APPLIANCES PEAK LOAD;MONTHLY APPLIANCES PEAK LOAD(JAN);MONTHLY APPLIANCES PEAK LOAD(FEB);MONTHLY APPLIANCES PEAK LOAD(MAR);MONTHLY APPLIANCES PEAK LOAD(APR);MONTHLY APPLIANCES PEAK LOAD(MAY);MONTHLY APPLIANCES PEAK LOAD(JUN);MONTHLY APPLIANCES PEAK LOAD(JUL);MONTHLY APPLIANCES PEAK LOAD(AUG);MONTHLY APPLIANCES PEAK LOAD(SEPT);MONTHLY APPLIANCES PEAK LOAD(OCT);MONTHLY APPLIANCES PEAK LOAD(NOV);MONTHLY APPLIANCES PEAK LOAD(DEC)` + meb_header = `${meb_header};YEARLY LIGHTING ELECTRICAL DEMAND;MONTHLY LIGHTING ELECTRICAL DEMAND(JAN);MONTHLY LIGHTING ELECTRICAL DEMAND(FEB);MONTHLY LIGHTING ELECTRICAL DEMAND(MAR);MONTHLY LIGHTING ELECTRICAL DEMAND(APR);MONTHLY LIGHTING ELECTRICAL DEMAND(MAY);MONTHLY LIGHTING ELECTRICAL DEMAND(JUN);MONTHLY LIGHTING ELECTRICAL DEMAND(JUL);MONTHLY LIGHTING ELECTRICAL DEMAND(AUG);MONTHLY LIGHTING ELECTRICAL DEMAND(SEPT);MONTHLY LIGHTING ELECTRICAL DEMAND(OCT);MONTHLY LIGHTING ELECTRICAL DEMAND(NOV);MONTHLY LIGHTING ELECTRICAL DEMAND(DEC)` + meb_header = `${meb_header};YEARLY APPLIANCES ELECTRICAL DEMAND;MONTHLY APPLIANCES ELECTRICAL DEMAND(JAN);MONTHLY APPLIANCES ELECTRICAL DEMAND(FEB);MONTHLY APPLIANCES ELECTRICAL DEMAND(MAR);MONTHLY APPLIANCES ELECTRICAL DEMAND(APR);MONTHLY APPLIANCES ELECTRICAL DEMAND(MAY);MONTHLY APPLIANCES ELECTRICAL DEMAND(JUN);MONTHLY APPLIANCES ELECTRICAL DEMAND(JUL);MONTHLY APPLIANCES ELECTRICAL DEMAND(AUG);MONTHLY APPLIANCES ELECTRICAL DEMAND(SEPT);MONTHLY APPLIANCES ELECTRICAL DEMAND(OCT);MONTHLY APPLIANCES ELECTRICAL DEMAND(NOV);MONTHLY APPLIANCES ELECTRICAL DEMAND(DEC)` + meb_header = `${meb_header};YEARLY DOMESTIC HOT WATER HEAT DEMAND;MONTHLY DOMESTIC HOT WATER HEAT DEMAND(JAN);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(FEB);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(MAR);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(APR);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(MAY);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(JUN);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(JUL);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(AUG);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(SEPT);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(OCT);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(NOV);MONTHLY DOMESTIC HOT WATER HEAT DEMAND(DEC)` + + let meb_lines = [] + for (var building in results.results) { + console.log(building) + let meb = `${building}` + let values = results.results[building] + values.yearly_cooling_demand.forEach(element => meb = `${meb};${element}`) + values.yearly_heating_demand.forEach(element => meb = `${meb};${element}`) + values.monthly_cooling_demand.forEach(element => meb = `${meb};${element}`) + values.monthly_heating_demand.forEach(element => meb = `${meb};${element}`) + values.yearly_cooling_peak_load.forEach(element => meb = `${meb};${element}`) + values.yearly_heating_peak_load.forEach(element => meb = `${meb};${element}`) + values.monthly_cooling_peak_load.forEach(element => meb = `${meb};${element}`) + values.monthly_heating_peak_load.forEach(element => meb = `${meb};${element}`) + values.yearly_lighting_peak_load.forEach(element => meb = `${meb};${element}`) + values.monthly_lighting_peak_load.forEach(element => meb = `${meb};${element}`) + values.yearly_appliances_peak_load.forEach(element => meb = `${meb};${element}`) + values.monthly_appliances_peak_load.forEach(element => meb = `${meb};${element}`) + values.yearly_lighting_electrical_demand.forEach(element => meb = `${meb};${element}`) + values.monthly_lighting_electrical_demand.forEach(element => meb = `${meb};${element}`) + values.yearly_appliances_electrical_demand.forEach(element => meb = `${meb};${element}`) + values.monthly_appliances_electrical_demand.forEach(element => meb = `${meb};${element}`) + values.yearly_domestic_hot_water_heat_demand.forEach(element => meb = `${meb};${element}`) + values.monthly_domestic_hot_water_heat_demand.forEach(element => meb = `${meb};${element}`) + console.log(values) + meb_lines.push(meb) + } require("fs").writeFile( `${tmp_path}meb.csv`, `${meb_header}\n${meb_lines.join('\n')}`, function (err) { console.log(err ? err : 'meb saved') } ) - require("fs").writeFile( - `${tmp_path}cost.csv`, - `${costs_header}\n${cost_lines.join('\n')}`, - function (err) { console.log(err ? err : 'cost saved') } - ) - require("fs").writeFile( - `${tmp_path}operational_co2.csv`, - `${operational_co2_header}\n${operational_co2_lines.join('\n')}`, - function (err) { console.log(err ? err : 'operational_co2 saved') } - ) - BrowserWindow.fromId(1).webContents.send('task-completed', 'meb
costs
operational_co2') + BrowserWindow.fromId(1).webContents.send('task-completed', 'meb') + return } - else { - console.log('unautorized insel meb') - results = await response.json() - console.log(results) + else if (response.status == 400) { + console.log('invalid request') + error = '

Insel Meb failed


Most likely there are an issue with either your geometry file, your parameters or both' } + else if (response.status == 500) { + console.log('API Error') + error = '

Insel Meb failed


There are an error in the API side.' + } + BrowserWindow.fromId(1).webContents.send('task-completed', error) + results = await response.json() + console.log(results) } else { console.log(response)