const { BrowserWindow, app } = require('electron') const fs = require('fs') const path = require('path') 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' const session_start = 'http://127.0.1:15789/v1.4/session/start' const insel_meb = 'http://127.0.0.1:15789/v1.4/workflow/insel-monthly-energy-balance' exports.run = async (application, param, tmp_path) => { console.log(`Executing ${application}`) dotenv.config({ path: path.resolve(`${app.getAppPath()}/environments/workflow_launcher/.env`) }) const application_uuid = process.env.APPLICATION const username = process.env.APPLICATION_USERNAME const password = process.env.APPLICATION_PASSWORD let response = await fetch(session_start, { method: 'PUT', headers: { 'Content-type': 'application/json', 'username': username, 'password': password, 'application-uuid': application_uuid, 'accept': 'application/json' } }) if (response.status == 200) { const session_id = response.headers.get('session_id') let token = response.headers.get('token') console.log(`token: ${token}`) 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']) 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: { 'session-id': session_id, 'token': token, 'application-uuid': application_uuid, 'accept': 'application/json' }, body: formData }, 600000) var error = '' if (response.status == 200) { results = await response.json() var meb_header = 'BUILDING NAME;TOTAL HEATING AREA;YEAR_OF_CONSTRUCTION;FUNCTION' 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] meb = `${meb};${values.total_heating_area};${values.year_of_construction};${values.function}` 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}/insel_meb.csv`, `${meb_header}\n${meb_lines.join('\n')}`, function (err) { console.log(err ? err : 'meb saved') } ) BrowserWindow.fromId(1).webContents.send('task-completed', `meb`) return } else if (response.status == 400) { console.log('invalid request') error = `

${application} failed


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

${application} failed


There is 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) console.log(JSON.stringify(response)) console.log('unautorized session start') } }