Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila Kisko committed Jul 13, 2022
1 parent f8da9e7 commit 5efe9fd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# What's this?

LoxTemplater ((**Lox**one**Templater**) is a tool to help you reusing complex logic for rooms in Loxone Config.
LoxTemplater (**Lox**one**Templater**) is a tool to help you reusing complex logic for rooms in Loxone Config.

# How?

Expand Down Expand Up @@ -33,8 +33,8 @@ Ensure that devices and their outputs are

![suffix 1](docs/templateparams.png)

- The Green Memory Flags (Input References) will send data to Actuators, or inputs of Function Blocks
- The Blue Memory Flags receive data from Sensors or outputs of Function Blocks
- The green Memory Flags (Input References) will send data to Actuators, or inputs of Function Blocks
- The blue Memory Flags receive data from Sensors or outputs of Function Blocks

### Naming

Expand Down
4 changes: 3 additions & 1 deletion src/LoxTemplater/GenerateOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public void Validate()
}
else
{
if (!Directory.Exists(Path.GetDirectoryName(OutputPath))) throw new DirectoryNotFoundException("Invalid output path: " + OutputPath);
var outdir = Path.GetDirectoryName(OutputPath);

if (!String.IsNullOrEmpty(outdir) && !Directory.Exists(outdir)) throw new DirectoryNotFoundException("Invalid output path: " + OutputPath);
}
}
}
28 changes: 23 additions & 5 deletions src/LoxTemplater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ static void RunGenerateVerb(GenerateOpts opts)
opts.Validate();

var project = LoxProjectRef.Load(opts.ProjectPath);
AssertValidRooms(project, opts.Rooms);

var collector = new IOCollector(project);
var allIO = collector.GroupIOsByPlace();
AssertUniqueIONames(allIO);

var template = FindPageByTitle(project, opts.TemplateName) ?? throw new InvalidOperationException($"Cannot find template page '{opts.TemplateName}'");

CheckInvalidPlaces(project, template);
AssertTemplateHasNoRooms(project, template);

foreach (var roomName in opts.Rooms)
{
var place = project.PlacesById.Values.First(p => OrdinalIgnoreCase.Equals(p.Title, roomName)) ?? throw new InvalidOperationException($"Invalid room {roomName}");
var place = project.PlacesById.Values.FirstOrDefault(p => OrdinalIgnoreCase.Equals(p.Title, roomName)) ?? throw new InvalidOperationException($"Invalid room {roomName}");

GeneratePage(opts, template, project, allIO, place);
}
Expand All @@ -76,6 +77,8 @@ static void RunGenerateVerb(GenerateOpts opts)
});

project.Document.Save(xw);

Log($"Saved output to {opts.OutputPath}");
}
}

Expand Down Expand Up @@ -287,10 +290,9 @@ void AddTodo()
return project.Pages.FirstOrDefault(p => OrdinalIgnoreCase.Equals(p.Title, title));
}

static void CheckInvalidPlaces(LoxProjectRef project, LoxPage page)
static void AssertTemplateHasNoRooms(LoxProjectRef project, LoxPage page)
{
var naPlace = project.PlacesById.Values.First(p => p.First).Id;
var metas = project.SerDes.Select<LoxMeta>(page).Where(m => (m.PlaceId ?? naPlace) != naPlace);
var metas = project.SerDes.Select<LoxMeta>(page).Where(m => (m.PlaceId ?? project.EmptyPlace.Id) != project.EmptyPlace.Id);

foreach (var invalid in metas)
{
Expand All @@ -299,6 +301,22 @@ static void CheckInvalidPlaces(LoxProjectRef project, LoxPage page)
}
}

static void AssertValidRooms(LoxProjectRef project, IEnumerable<string> rooms)
{
var invalids = rooms.Where(r => project.PlacesById.Values.FirstOrDefault(p => OrdinalIgnoreCase.Equals(p.Title, r)) == null).ToList();

if (invalids.Count > 0)
{
foreach (var invalid in invalids)
{
Log($"Room '{invalid}' was not found in the project");
}

throw new InvalidOperationException("Invalid arguments");
}
}


static XElement ClonePage(LoxPage page)
{
ArgumentNullException.ThrowIfNull(page);
Expand Down

0 comments on commit 5efe9fd

Please sign in to comment.