Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions LadybugTools_Adapter/AdapterActions/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@
*/

using BH.Engine.Adapter;
using BH.Engine.Base;
using BH.Engine.LadybugTools;
using BH.Engine.Python;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.oM.Adapter.Commands;
using BH.oM.Base;
using BH.oM.Data.Requests;
using BH.oM.LadybugTools;
using BH.oM.Python;
using BH.Engine.Python;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using BH.Engine.Base;
using System.Drawing;
using BH.Engine.Serialiser;
using System.Reflection;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BH.Adapter.LadybugTools
{
Expand Down Expand Up @@ -85,6 +86,33 @@ private List<object> RunCommand(IExecuteCommand command, ActionConfig actionConf
BH.Engine.Base.Compute.RecordError($"The command {command.GetType().FullName} is not valid for the LadybugTools Adapter. Please use a LadybugCommand, or use the correct adapter for the input command.");
return new List<object>();
}

private (string, bool) ExecutePython(List<string> args, string json)
{
string result = "";
bool success;

if (m_httpClient != null)
{
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args, json);
task.Wait();
(result, success) = task.Result;
}
else
{
//if the server was not running or some other error happened, try running the python directly.
string script = Path.Combine(Engine.Python.Query.DirectoryCode(), m_environment.Name, "src", m_environment.Name.ToLower() , "bhom", "run_wrapped.py");
string tempFileName = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(tempFileName, json);
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)} -in \"{tempFileName}\"";
result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
System.IO.File.Delete(tempFileName);
}

success = !result.Contains("Traceback (most recent call last):");

return (result, success);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using BH.Engine.Adapter;
using BH.Engine.Base;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.LadybugTools;
Expand Down Expand Up @@ -75,48 +76,43 @@ private List<object> RunCommand(CompareEPWKeyPlotCommand command, ActionConfig a
string epwFile = System.IO.Path.GetFullPath(command.EPWFile.GetFullFileName()).Replace('\\', '/');
List<string> epwFileList = command.EPWCompareFiles.Select(e => e.GetFullFileName().Replace('\\', '/')).ToList();

Dictionary<string, object> dict = new Dictionary<string, object>()
{
{ "epw_list", epwFileList },
{ "data_type_key", command.EPWKey.ToText() },
{ "line", command.PlotTimeseries },
{ "save_path", command.OutputLocation.Replace('\\', '/') }
};

string json = dict.ToJson();

// run the process
List<string> args = new List<string>
{
"--command", "plot/epw_comparison",
"-e", epwFile,
"-dtk", command.EPWKey.ToText(),
"-p", command.OutputLocation.Replace('\\', '/'),
"-el" //append compare epw file list here
"-e", epwFile
};
args.AddRange(epwFileList);

if (command.PlotTimeseries)
args.Add("-l");
(string result, bool success) = ExecutePython(args, json);
m_executeSuccess = success;

string result = "";
bool success;

if (m_httpClient != null)
if (!success)
{
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
task.Wait();
(result, success) = task.Result;
BH.Engine.Base.Compute.RecordError($"A python error occurred while running the command `{command.GetType().Name}`. Python output:\n{result}");
return new List<object>();
}
else
{
//if the server was not running or some other error happened, try running the python directly.
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";

result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true).Split('\n').Last();
}
result = result.Split('\n').Last();

try
{
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(result);
PlotInformation info = Convert.ToPlotInformation(obj, new NoData()); //this plot type doesn't have collection metadata yet...
m_executeSuccess = true;
PlotInformation info = (PlotInformation)BH.Engine.Serialiser.Convert.FromJson(result);
return new List<object>() { info };
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError(ex, $"An error occurred when deserialising the output from the script.\n Python output: {result}");
m_executeSuccess = false;
return new List<object>();
}
}
Expand Down
42 changes: 25 additions & 17 deletions LadybugTools_Adapter/AdapterActions/Execute/DiurnalPlotCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using BH.Engine.Adapter;
using BH.Engine.Base;
using BH.Engine.LadybugTools;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.LadybugTools;
Expand Down Expand Up @@ -73,37 +74,44 @@ private List<object> RunCommand(DiurnalPlotCommand command, ActionConfig actionC

//string returnFile = Path.GetTempFileName();

Dictionary<string, object> dict = new Dictionary<string, object>()
{
{ "data_type_key", command.EPWKey.ToText() },
{ "colour", command.Colour.ToHexCode() },
{ "title", command.Title },
{ "period", command.Period.ToString().ToLower() },
{ "save_path", command.OutputLocation.Replace('\\', '/') }
};

string json = dict.ToJson();

// run the process
List<string> args = new List<string>() { "--command", "plot/diurnal", "-e", epwFile.Replace('\\', '/'), "-dtk", command.EPWKey.ToText(), "--colour", command.Colour.ToHexCode(), "-t", command.Title, "-ap", command.Period.ToString().ToLower(), "-p", command.OutputLocation.Replace('\\', '/') };
List<string> args = new List<string>()
{
"--command", "plot/epw_diurnal",
"-e", epwFile.Replace('\\', '/')
};

string result = "";
bool success;
(string result, bool success) = ExecutePython(args, json);
m_executeSuccess = success;

if (m_httpClient != null)
if (!success)
{
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
task.Wait();
(result, success) = task.Result;
BH.Engine.Base.Compute.RecordError($"A python error occurred while running the command `{command.GetType().Name}`. Python output:\n{result}");
return new List<object>();
}
else
{
//if the server was not running or some other error happened, try running the python directly.
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";

result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true).Split('\n').Last();
}
result = result.Split('\n').Last();

try
{
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(result);
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());
m_executeSuccess = true;
PlotInformation info = (PlotInformation)BH.Engine.Serialiser.Convert.FromJson(result);
return new List<object>() { info };
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError(ex, $"An error occurred when deserialising the output from the script.\n Python output: {result}");
m_executeSuccess = false;
return new List<object>();
}
}
Expand Down
29 changes: 11 additions & 18 deletions LadybugTools_Adapter/AdapterActions/Execute/EPWToCSVCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

using BH.Engine.Adapter;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.oM.LadybugTools;
using System;
Expand Down Expand Up @@ -59,28 +60,20 @@ private List<object> RunCommand(EPWToCSVCommand command, ActionConfig actionConf
return null;
}

List<string> args = new List<string>() { "--command", "epw_to_csv", "-e", command.EPWFile.GetFullFileName().Replace('\\', '/'), "-a", command.IncludeAdditionalCalculated.ToString() };
Dictionary<string, object> dict = new Dictionary<string, object>()
{
{ "include_additional", command.IncludeAdditionalCalculated }
};

string result = "";
bool success = true;
string json = dict.ToJson();

if (m_httpClient != null)
{
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
task.Wait();
(result, success) = task.Result; //in this case, result is the text of the csv file.
}
else
List<string> args = new List<string>()
{
//if the server was not running or some other error happened, try running the python directly.
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";

result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
}
"--command", "epw_to_csv",
"-e", command.EPWFile.GetFullFileName().Replace('\\', '/'),
};

//as the file output is hard to verify by itself, check that no errors got output to stderr log
success &= !result.Contains("Traceback (most recent call last):");
(string result, bool success) = ExecutePython(args, json);

if (!success)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,42 @@ private List<object> RunCommand(FacadeCondensationRiskCommand command, ActionCon
else
commandArg = "plot/facade_condensation_risk_chart";

Dictionary<string, object> dict = new Dictionary<string, object>()
{
{ "thresholds", thresholds },
{ "save_path", command.OutputLocation.Replace('\\', '/') }
};

string json = dict.ToJson();

//construct args: insert thresholds as a range as concatenating them into a space delimited string causes the numbers to be wrapped in quotes which breaks the python argument parser
List<string> args = new List<string>() { "-command", commandArg, "-e", epwFile.Replace('\\', '/'), "-t", "-p", command.OutputLocation.Replace('\\', '/') };
args.InsertRange(args.IndexOf("-t") + 1, thresholds.Select(x => x.ToString()));
List<string> args = new List<string>()
{
"-command", commandArg,
"-e", epwFile.Replace('\\', '/')
};

// run the process
string result = "";
bool success;
(string result, bool success) = ExecutePython(args, json);
m_executeSuccess = success;

if (m_httpClient != null)
if (!success)
{
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
task.Wait();
(result, success) = task.Result;
BH.Engine.Base.Compute.RecordError($"A python error occurred while running the command `{command.GetType().Name}`. Python output:\n{result}");
return new List<object>();
}
else
{
//if the server was not running or some other error happened, try running the python directly.
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";

result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true).Split('\n').Last();
}
result = result.Split('\n').Last();

try
{
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(result);
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());
m_executeSuccess = true;
PlotInformation info = (PlotInformation)BH.Engine.Serialiser.Convert.FromJson(result);
return new List<object>() { info };
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError(ex, $"An error occurred when deserialising the output from the script.\n Python output: {result}");
m_executeSuccess = false;
return new List<object>();
}
}
Expand Down
28 changes: 10 additions & 18 deletions LadybugTools_Adapter/AdapterActions/Execute/GEMToHBJSONCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

using BH.Engine.Adapter;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.oM.LadybugTools;
using System;
Expand Down Expand Up @@ -58,28 +59,19 @@ public List<object> RunCommand(GEMToHBJSONCommand command, ActionConfig actionCo
return null;
}

List<string> args = new List<string>() { "--command", "gem_to_hbjson", "-g", command.GEMFile.GetFullFileName().Replace('\\', '/') };
Dictionary<string, object> dict = new Dictionary<string, object>()
{
{ "gem_file", command.GEMFile.GetFullFileName().Replace('\\', '/') }
};

string result = "";
bool success = true;
string json = dict.ToJson();

if (m_httpClient != null)
{
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
task.Wait();
(result, success) = task.Result; //in this case, result is the text of the csv file.
}
else
List<string> args = new List<string>()
{
//if the server was not running or some other error happened, try running the python directly.
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";

result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
}
"--command", "gem_to_hbjson"
};

//as the file output is hard to verify by itself, check that no errors got output to stderr log
success &= !result.Contains("Traceback (most recent call last):");
(string result, bool success) = ExecutePython(args, json);

if (!success)
{
Expand Down
Loading