Skip to content

Commit 89b815a

Browse files
author
Fraser Greenroyd
authored
Adapter methods: Serialisation to and from ladybug objects (#142)
2 parents a12cd35 + 350813d commit 89b815a

34 files changed

+1607
-82
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Adapter;
24+
using BH.oM.Adapter;
25+
using BH.oM.Base;
26+
using BH.oM.Data.Requests;
27+
using BH.oM.LadybugTools;
28+
using System;
29+
using System.Collections.Generic;
30+
using System.Text;
31+
32+
namespace BH.Adapter.LadybugTools
33+
{
34+
public partial class LadybugToolsAdapter : BHoMAdapter
35+
{
36+
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
37+
{
38+
if (actionConfig == null)
39+
{
40+
BH.Engine.Base.Compute.RecordError("Please provide a valid LadybugConfig ActionConfig.");
41+
return new List<IBHoMObject>();
42+
}
43+
44+
LadybugConfig config = actionConfig as LadybugConfig;
45+
if (config == null)
46+
{
47+
BH.Engine.Base.Compute.RecordError($"The type of actionConfig provided: {actionConfig.GetType().FullName} is not valid for this adapter. Please provide a valid LadybugConfig actionConfig.");
48+
return new List<IBHoMObject>();
49+
}
50+
51+
if (config.JsonFile == null)
52+
{
53+
BH.Engine.Base.Compute.RecordError("Please provide a valid JsonFile FileSettings object.");
54+
return new List<IBHoMObject>();
55+
}
56+
57+
if (!System.IO.File.Exists(config.JsonFile.GetFullFileName()))
58+
{
59+
BH.Engine.Base.Compute.RecordError($"The file at {config.JsonFile.GetFullFileName()} does not exist to pull from.");
60+
return new List<IBHoMObject>();
61+
}
62+
63+
if (request != null)
64+
{
65+
FilterRequest filterRequest = request as FilterRequest;
66+
return Read(filterRequest.Type, actionConfig: config);
67+
}
68+
else
69+
return Read(null, config);
70+
}
71+
}
72+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Adapter;
24+
using BH.Engine.LadybugTools;
25+
using BH.oM.Adapter;
26+
using BH.oM.Base;
27+
using BH.oM.Data.Requests;
28+
using BH.oM.LadybugTools;
29+
using System;
30+
using System.Collections.Generic;
31+
using System.Linq;
32+
using System.Text;
33+
34+
namespace BH.Adapter.LadybugTools
35+
{
36+
public partial class LadybugToolsAdapter : BHoMAdapter
37+
{
38+
public override List<object> Push(IEnumerable<object> objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null)
39+
{
40+
if (actionConfig == null)
41+
{
42+
BH.Engine.Base.Compute.RecordError("Please provide a valid LadybugConfig ActionConfig.");
43+
return new List<object>();
44+
}
45+
46+
LadybugConfig config = actionConfig as LadybugConfig;
47+
if (config == null)
48+
{
49+
BH.Engine.Base.Compute.RecordError("Please input a valid LadybugConfig.");
50+
return new List<object>();
51+
}
52+
53+
if (objects.Count() == 0)
54+
{
55+
BH.Engine.Base.Compute.RecordError("Please input a valid LadybugTools object.");
56+
return new List<object>();
57+
}
58+
59+
List<ILadybugTools> lbtObjects = objects.Where(x => typeof(ILadybugTools).IsAssignableFrom(x.GetType())).Cast<ILadybugTools>().ToList();
60+
61+
if (lbtObjects.Count() < objects.Count())
62+
{
63+
BH.Engine.Base.Compute.RecordWarning("The LadybugTools Toolkit adapter does not support converting non-ILadybugTools objects to json, skipping all objects that are not an ILadybugTools");
64+
}
65+
66+
CreateLadybug(lbtObjects, config);
67+
return objects.Where(x => typeof(ILadybugTools).IsAssignableFrom(x.GetType())).ToList();
68+
}
69+
}
70+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Adapter;
24+
using BH.oM.Adapter;
25+
using BH.oM.Base.Debugging;
26+
using BH.oM.LadybugTools;
27+
using System;
28+
using System.Collections.Generic;
29+
using System.IO;
30+
using System.Linq;
31+
using System.Text;
32+
33+
namespace BH.Adapter.LadybugTools
34+
{
35+
public partial class LadybugToolsAdapter : BHoMAdapter
36+
{
37+
public static void CreateLadybug(List<ILadybugTools> objects, LadybugConfig config = null)
38+
{
39+
List<string> jsonObjects = new List<string>();
40+
41+
foreach (ILadybugTools lbtObject in objects)
42+
{
43+
jsonObjects.Add(lbtObject.FromBHoM());
44+
}
45+
46+
string json = "[" + string.Join(", ", jsonObjects) + "]";
47+
File.WriteAllText(config.JsonFile.GetFullFileName(), json);
48+
}
49+
}
50+
}

LadybugTools_Adapter/CRUD/Read.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Adapter;
24+
using BH.Engine.Serialiser;
25+
using BH.oM.Adapter;
26+
using BH.oM.Base;
27+
using BH.oM.LadybugTools;
28+
using System;
29+
using System.Collections;
30+
using System.Collections.Generic;
31+
using System.Text;
32+
33+
namespace BH.Adapter.LadybugTools
34+
{
35+
public partial class LadybugToolsAdapter : BHoMAdapter
36+
{
37+
protected override IEnumerable<IBHoMObject> IRead(Type type, IList indices = null, ActionConfig actionConfig = null)
38+
{
39+
LadybugConfig config = actionConfig as LadybugConfig;
40+
return config.JsonFile.ToBHoM();
41+
}
42+
}
43+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Serialiser;
24+
using BH.oM.Base;
25+
using BH.oM.LadybugTools;
26+
using System;
27+
using System.Collections.Generic;
28+
using System.Collections.ObjectModel;
29+
using System.Linq;
30+
using System.Text;
31+
32+
namespace BH.Adapter.LadybugTools
33+
{
34+
public static partial class Convert
35+
{
36+
public static BH.oM.LadybugTools.EPW ToEPW(Dictionary<string, object> oldObject)
37+
{
38+
Location location = new Location();
39+
Dictionary<string, object> metaData = new Dictionary<string, object>();
40+
41+
try
42+
{
43+
if (oldObject["location"].GetType() == typeof(CustomObject))
44+
oldObject["location"] = (oldObject["location"] as CustomObject).CustomData;
45+
location = ToLocation(oldObject["location"] as Dictionary<string, object>);
46+
}
47+
catch (Exception ex)
48+
{
49+
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the Location of the EPW. returning a default Location.\n The error: {ex}");
50+
}
51+
52+
try
53+
{
54+
if (oldObject["metadata"].GetType() == typeof(CustomObject))
55+
oldObject["metadata"] = (oldObject["metadata"] as CustomObject).CustomData;
56+
metaData = (Dictionary<string, object>)oldObject["metadata"];
57+
}
58+
catch (Exception ex)
59+
{
60+
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the meta data of the EPW. returning an empty metadata object.\n The error: {ex}");
61+
}
62+
63+
List<BH.oM.LadybugTools.HourlyContinuousCollection> collections = new List<BH.oM.LadybugTools.HourlyContinuousCollection>();
64+
65+
if (oldObject.ContainsKey("data_collections"))
66+
{
67+
foreach (var collection in oldObject["data_collections"] as List<object>)
68+
{
69+
if (collection.GetType() == typeof(CustomObject))
70+
collections.Add(ToHourlyContinuousCollection((collection as CustomObject).CustomData));
71+
else
72+
collections.Add(ToHourlyContinuousCollection(collection as Dictionary<string, object>));
73+
}
74+
}
75+
else
76+
{
77+
BH.Engine.Base.Compute.RecordError($"Could not find any data collections for this EPW object. Returning an empty list.");
78+
}
79+
80+
return new EPW()
81+
{
82+
Location = location,
83+
DataCollections = collections,
84+
Metadata = metaData
85+
};
86+
87+
}
88+
89+
public static string FromEPW(BH.oM.LadybugTools.EPW epw)
90+
{
91+
string type = "EPW";
92+
string location = FromLocation(epw.Location).ToJson();
93+
string dataCollections = string.Join(", ", epw.DataCollections.Select(x => FromHourlyContinuousCollection(x)));
94+
string metadata = epw.Metadata.ToJson();
95+
96+
if (metadata.Length == 0)
97+
metadata = "{}";
98+
99+
string json = @"{ ""type"": """ + type + @""", ""location"": " + location + @", ""data_collections"": [ " + dataCollections + @"], ""metadata"": " + metadata + "}";
100+
101+
return json;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)