2024-05-24 02:49:16 -04:00
const { BrowserWindow , app } = require ( 'electron' )
2024-05-09 09:36:40 -04:00
const fs = require ( 'fs' )
2024-05-24 02:25:41 -04:00
const path = require ( 'path' )
2024-05-09 09:36:40 -04:00
const { Blob } = require ( 'buffer' )
2024-04-25 00:54:32 -04:00
const dotenv = require ( 'dotenv' )
2024-10-22 15:40:01 -04:00
const session _start = 'https://ngci.encs.concordia.ca/api/v1.4/session/start'
const insel _meb = 'https://ngci.encs.concordia.ca/api/v1.4/workflow/insel-monthly-energy-balance'
2024-04-25 00:54:32 -04:00
2024-05-07 01:32:22 -04:00
exports . run = async ( application , param , tmp _path ) => {
2024-04-25 00:54:32 -04:00
console . log ( ` Executing ${ application } ` )
2024-05-24 02:49:16 -04:00
dotenv . config ( { path : path . resolve ( ` ${ app . getAppPath ( ) } /environments/workflow_launcher/.env ` ) } )
2024-05-24 02:25:41 -04:00
const application _uuid = process . env . APPLICATION
const username = process . env . APPLICATION _USERNAME
const password = process . env . APPLICATION _PASSWORD
2024-04-25 00:54:32 -04:00
let response = await fetch ( session _start , {
method : 'PUT' ,
headers : {
'Content-type' : 'application/json' ,
'username' : username ,
'password' : password ,
'application-uuid' : application _uuid ,
'accept' : 'application/json'
}
2024-05-09 10:26:05 -04:00
} )
2024-04-25 00:54:32 -04:00
if ( response . status == 200 ) {
const session _id = response . headers . get ( 'session_id' )
let token = response . headers . get ( 'token' )
console . log ( ` token: ${ token } ` )
2024-05-09 09:36:40 -04:00
let formData = new FormData ( )
2024-05-07 01:32:22 -04:00
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' ] )
2024-05-09 09:36:40 -04:00
var filename = param [ 'geometry_file' ] . replace ( /^.*[\\/]/ , '' )
formData . append ( 'geometry_file' , new Blob ( [ fs . readFileSync ( param [ 'geometry_file' ] ) ] ) , filename )
2024-05-07 01:32:22 -04:00
response = await fetch ( insel _meb , {
2024-04-25 00:54:32 -04:00
method : 'POST' ,
headers : {
'session-id' : session _id ,
'token' : token ,
'application-uuid' : application _uuid ,
'accept' : 'application/json'
} ,
2024-05-07 01:32:22 -04:00
body : formData
2024-04-25 00:54:32 -04:00
} , 600000 )
2024-05-09 09:36:40 -04:00
var error = ''
2024-04-25 00:54:32 -04:00
if ( response . status == 200 ) {
results = await response . json ( )
2024-05-24 02:25:41 -04:00
var meb _header = 'BUILDING NAME;TOTAL HEATING AREA;YEAR_OF_CONSTRUCTION;FUNCTION'
2024-05-09 09:36:40 -04:00
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 ]
2024-05-16 23:42:19 -04:00
meb = ` ${ meb } ; ${ values . total _heating _area } ; ${ values . year _of _construction } ; ${ values . function } `
2024-05-09 09:36:40 -04:00
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 )
}
2024-05-24 02:25:41 -04:00
require ( 'fs' ) . writeFile (
2024-05-16 23:42:19 -04:00
` ${ tmp _path } /insel_meb.csv ` ,
2024-04-25 00:54:32 -04:00
` ${ meb _header } \n ${ meb _lines . join ( '\n' ) } ` ,
function ( err ) { console . log ( err ? err : 'meb saved' ) }
)
2024-05-16 23:42:19 -04:00
BrowserWindow . fromId ( 1 ) . webContents . send ( 'task-completed' , ` <a href=" ${ tmp _path } /insel_meb.csv">meb</a> ` )
2024-05-09 09:36:40 -04:00
return
2024-04-25 00:54:32 -04:00
}
2024-05-09 09:36:40 -04:00
else if ( response . status == 400 ) {
console . log ( 'invalid request' )
2024-05-14 10:32:18 -04:00
error = ` <h3> ${ application } failed</h3><br />Most likely, there is an issue with either your geometry file, your parameters or both `
2024-05-09 09:36:40 -04:00
}
else if ( response . status == 500 ) {
console . log ( 'API Error' )
2024-05-14 10:32:18 -04:00
error = ` <h3> ${ application } failed</h3><br />There is an error in the API side. `
2024-04-25 00:54:32 -04:00
}
2024-05-09 09:36:40 -04:00
BrowserWindow . fromId ( 1 ) . webContents . send ( 'task-completed' , error )
results = await response . json ( )
console . log ( results )
2024-04-25 00:54:32 -04:00
}
else {
console . log ( response )
console . log ( JSON . stringify ( response ) )
console . log ( 'unautorized session start' )
}
}