Skip to content

Commit

Permalink
fix: issue #53
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhobox committed Sep 10, 2024
1 parent d49ee63 commit e1025a6
Showing 1 changed file with 63 additions and 58 deletions.
121 changes: 63 additions & 58 deletions Kaenx.Creator/Classes/ImportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void StartZip(MainModel general, ObservableCollection<DataPointType> dpts

if(entries.Count() == 0)
{
// TODO translate
System.Windows.MessageBox.Show("Knxprod enthält keine Applikation");
return;
} else if(entries.Count() == 1)
Expand All @@ -178,7 +179,8 @@ public void StartZip(MainModel general, ObservableCollection<DataPointType> dpts
apps.Add(new KeyValuePair<string, object>(appName + " (" + appId +")", xentry));
names.Add(appName + " (" + appId +")");
}


// TODO translate
ListDialog diag = new ("Welche Applikation soll importiert werden?", "Import", (from app in apps select app.Key).ToList());
diag.ShowDialog();

Expand Down Expand Up @@ -1535,66 +1537,73 @@ private void ImportArguments(XElement xargs, Models.Module vbase)
}

private void ImportHardware(XElement xhards) {
XElement temp = xhards.Descendants(Get("Product")).FirstOrDefault();
if(temp != null)
{
string def = temp.Attribute("DefaultLanguage").Value;

if(!def.Contains("-")){
def = _langTexts.Keys.First(l => l.StartsWith(def + "-"));
XElement temp = xhards.Descendants(Get("ApplicationProgramRef")).Single(h => h.Attribute("RefId").Value == AppImportHelper);
XElement xhard = temp.Parent.Parent.Parent;

_general.Info.Name = xhard.Attribute("Name").Value;
_general.Info.SerialNumber = xhard.Attribute("SerialNumber").Value;
_general.Info.Version = int.Parse(xhard.Attribute("VersionNumber").Value);
_general.Info.HasApplicationProgram = xhard.Attribute("HasApplicationProgram")?.Value == "true" || xhard.Attribute("HasApplicationProgram")?.Value == "1";
_general.Info.HasIndividualAddress = xhard.Attribute("HasIndividualAddress")?.Value == "true" || xhard.Attribute("HasIndividualAddress")?.Value == "1";
_general.Info.BusCurrent = (int)StringToFloat(xhard.Attribute("BusCurrent")?.Value, 10);
_general.Info.IsIpEnabled = xhard.Attribute("IsIPEnabled")?.Value == "true" || xhard.Attribute("IsIPEnabled")?.Value == "1";

XElement xprods = xhard.Element(Get("Products"));
XElement xprod = null;
string toImport = "";
if(xprods.Elements().Count() > 1) {
List<KeyValuePair<string, string>> prods = [];
foreach(XElement xprodt in xprods.Elements()) {
prods.Add(new ($"{xprodt.Attribute("Text").Value} ({xprodt.Attribute("OrderNumber").Value})", xprodt.Attribute("Id").Value));
}
// TODO translate
ListDialog diag = new ("Welches Produkt soll importiert werden?", "Import", (from prod in prods select prod.Key).ToList());
diag.ShowDialog();
if(diag.DialogResult == true) {
string id = prods.First(prod => prod.Key == diag.Answer).Value;
xprod = xprods.Elements().Single(p => p.Attribute("Id").Value == id);
}

if(!_general.Languages.Any(l => l.CultureCode == def))
_general.Languages.Add(new Language(_langTexts[def], def));
}

if(xprod == null) {
xprod = xprods.Elements().ElementAt(0);
}

foreach(XElement xhard in xhards.Elements())
{
if(xhard.Descendants(Get("ApplicationProgramRef")).Count() > 1)
System.Windows.MessageBox.Show(Properties.Messages.import_multiple_applications);

XElement appProgRef = xhard.Descendants(Get("ApplicationProgramRef")).ElementAt(0);
if(appProgRef.Attribute("RefId").Value != AppImportHelper)
continue;
string def = xprod.Attribute("DefaultLanguage").Value;

_general.Info.Name = xhard.Attribute("Name").Value;
_general.Info.SerialNumber = xhard.Attribute("SerialNumber").Value;
_general.Info.Version = int.Parse(xhard.Attribute("VersionNumber").Value);
_general.Info.HasApplicationProgram = xhard.Attribute("HasApplicationProgram")?.Value == "true" || xhard.Attribute("HasApplicationProgram")?.Value == "1";
_general.Info.HasIndividualAddress = xhard.Attribute("HasIndividualAddress")?.Value == "true" || xhard.Attribute("HasIndividualAddress")?.Value == "1";
_general.Info.BusCurrent = (int)StringToFloat(xhard.Attribute("BusCurrent")?.Value, 10);
_general.Info.IsIpEnabled = xhard.Attribute("IsIPEnabled")?.Value == "true" || xhard.Attribute("IsIPEnabled")?.Value == "1";

XElement xprod = xhard.Descendants(Get("Product")).ElementAt(0);
_general.Info.OrderNumber = xprod.Attribute("OrderNumber").Value;
_general.Info.Name = xprod.Parent.Parent.Attribute("Name").Value;
_general.Info.IsRailMounted = xprod.Attribute("IsRailMounted")?.Value == "true" || xprod.Attribute("IsRailMounted")?.Value == "1";
if(!def.Contains("-")){
def = _langTexts.Keys.First(l => l.StartsWith(def + "-"));
}

if(!_general.Languages.Any(l => l.CultureCode == def))
_general.Languages.Add(new Language(_langTexts[def], def));

_general.Info.OrderNumber = xprod.Attribute("OrderNumber").Value;
_general.Info.Name = xprod.Parent.Parent.Attribute("Name").Value;
_general.Info.IsRailMounted = xprod.Attribute("IsRailMounted")?.Value == "true" || xprod.Attribute("IsRailMounted")?.Value == "1";
}

private void ImportCatalog(XElement xcat)
{
foreach (XElement xitem in xcat.Elements())
{
ParseCatalogItem(xitem, _general.Catalog[0]);
}
string orderNumber = ExportHelper.GetEncoded(_general.Info.OrderNumber);
XElement xitem = xcat.Descendants(Get("CatalogItem")).Single(c => c.Attribute("Id").Value.Contains(orderNumber));

CheckCatalogSectionLanguages(_general.Catalog[0]);
}
List<XElement> parents = [xitem];
while(xitem.Parent.Name.LocalName != "Catalog") {
xitem = xitem.Parent;
parents.Insert(0, xitem);
}

private void CheckCatalogSectionLanguages(CatalogItem parent)
{
foreach(CatalogItem item in parent.Items)
{
if(!item.IsSection) continue;
CatalogItem _current = _general.Catalog[0];
foreach(XElement x in parents) {
_current = ParseCatalogItem(x, _current);
if(!_current.IsSection) continue;

foreach(Language lang in _general.Languages)
{
if(!item.Text.Any(t => t.Language.CultureCode == lang.CultureCode))
item.Text.Add(new Translation(lang, ""));
if(!_current.Text.Any(t => t.Language.CultureCode == lang.CultureCode))
_current.Text.Add(new Translation(lang, ""));
}

CheckCatalogSectionLanguages(item);
}
}

Expand All @@ -1614,7 +1623,7 @@ private CatalogItem GetSection(IEnumerable<string> ids)
return item;
}

private void ParseCatalogItem(XElement xitem, CatalogItem parent)
private CatalogItem ParseCatalogItem(XElement xitem, CatalogItem parent)
{
//!TODO check if section exists
switch (xitem.Name.LocalName)
Expand All @@ -1638,21 +1647,15 @@ private void ParseCatalogItem(XElement xitem, CatalogItem parent)
item.IsSection = true;
item.Text = GetTranslation(xitem.Attribute("Id").Value, "Name", xitem, true);

foreach (XElement xele in xitem.Elements())
ParseCatalogItem(xele, item);
break;
return item;
}

case "CatalogItem":
{
string id = xitem.Attribute("Hardware2ProgramRefId").Value.Substring(xitem.Attribute("Hardware2ProgramRefId").Value.IndexOf("_HP-") + 3, 13);
if(!AppImportHelper.Contains(id))
break;

_general.Info.Text = GetTranslation(xitem.Attribute("Id")?.Value ?? "", "Name", xitem, true);
_general.Info.Description = GetTranslation(xitem.Attribute("Id")?.Value ?? "", "VisibleDescription", xitem, true);



CatalogItem item = new CatalogItem()
{
Parent = parent,
Expand All @@ -1663,10 +1666,12 @@ private void ParseCatalogItem(XElement xitem, CatalogItem parent)
};

parent.Items.Add(item);
break;
return item;
}
}

default:
throw new NotImplementedException("Not implemented CatalogType: " + xitem.Name.LocalName);
}
}

public void ImportDynamic(XElement xdyn, IVersionBase vbase)
Expand Down Expand Up @@ -2138,7 +2143,7 @@ public static string Unescape(string input)
input = input.Replace(".2E", ".");
return input;
}

private string GetLastSplit(string input, int offset = 0)
{
return input.Substring(input.LastIndexOf('_') + 1 + offset);
Expand Down

0 comments on commit e1025a6

Please sign in to comment.