private void Open(object param) { try { if (_model != null) { var cancel = AskSaveCancel(); if (cancel) { return; } } var filePath = _dialogService.ChooseFileToOpen(_fileExtension, _fileFilter); if (filePath == null) { return; } _openFile = filePath; XmlSerializer serializer = new XmlSerializer(typeof(KNX)); using (var reader = new StreamReader(_openFile)) { _model = (KNX)serializer.Deserialize(reader); } _manufacturerData = _model.ManufacturerData.First(); _hardware = _manufacturerData.Hardware.First(); _product = _hardware.Products.First(); _catalogSection = _manufacturerData.Catalog.First(); _catalogItem = _catalogSection.CatalogItem.First(); _hardware2Program = _hardware.Hardware2Programs.First(); _applicationProgram = _manufacturerData.ApplicationPrograms.First(); _appProgRef = _hardware2Program.ApplicationProgramRef.First(); _codeSegment = _applicationProgram.Static.Code.RelativeSegment.First(); var parameterList = _applicationProgram.Static.Parameters.OfType <Parameter_t>(); foreach (var item in parameterList) { item.AllTypes = ParameterTypes; item.Type = ParameterTypes.First(t => t.Id == item.ParameterType); Parameters.Add(item); } RaiseChanged(); } catch (Exception ex) { ClearData(); _dialogService.ShowMessage(ex.ToString()); } }
private void ClearData() { _model = null; _openFile = null; _manufacturerData = null; _hardware = null; _product = null; _catalogItem = null; _catalogSection = null; _applicationProgram = null; _hardware2Program = null; _appProgRef = null; _codeSegment = null; Parameters.Clear(); RaiseChanged(); }
private void Export(object param) { try { if (_model == null) { return; } var cancel = AskSaveCancel(); if (cancel) { return; } // Remove all data secure related attributes if (_model.ManufacturerData.First().ApplicationPrograms.First().IsSecureEnabled == false) { _model.ManufacturerData.First().ApplicationPrograms.First().MaxSecurityIndividualAddressEntries = 0; _model.ManufacturerData.First().ApplicationPrograms.First().MaxSecurityGroupKeyTableEntries = 0; } var files = new string[] { _openFile }; var outputFile = _dialogService.ChooseSaveFile(".knxprod", "KNXProd|*.knxprod"); if (outputFile == null) { return; } string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); File.Copy("knx_master.xml", Path.Combine(tempDirectory, "knx_master.xml")); string mfid = _model.ManufacturerData.First().RefId; Directory.CreateDirectory(Path.Combine(tempDirectory, mfid)); KNX document = new KNX(); ManufacturerData_TManufacturer manufacturerData = new ManufacturerData_TManufacturer(); document.CreatedBy = _model.CreatedBy; document.ToolVersion = _model.ToolVersion; document.ManufacturerData.Add(manufacturerData); document.ManufacturerData.First().RefId = _model.ManufacturerData.First().RefId; manufacturerData.Catalog.Add(_model.ManufacturerData.First().Catalog.First()); XmlSerializer serializer = new XmlSerializer(typeof(KNX)); using (var xmlWriter = new StreamWriter(Path.Combine(tempDirectory, mfid, "Catalog.xml"), false, Encoding.UTF8)) { serializer.Serialize(xmlWriter, document); } manufacturerData.Catalog.Clear(); manufacturerData.ApplicationPrograms.Add(_model.ManufacturerData.First().ApplicationPrograms.First()); string applicationProgramFilename = _model.ManufacturerData.First().ApplicationPrograms.First().Id + ".xml"; using (var xmlWriter = new StreamWriter(Path.Combine(tempDirectory, mfid, applicationProgramFilename), false, Encoding.UTF8)) { serializer.Serialize(xmlWriter, document); } manufacturerData.ApplicationPrograms.Clear(); manufacturerData.Hardware.Add(_model.ManufacturerData.First().Hardware.First()); using (var xmlWriter = new StreamWriter(Path.Combine(tempDirectory, mfid, "Hardware.xml"), false, Encoding.UTF8)) { serializer.Serialize(xmlWriter, document); } // Sign ApplicationProgram XML file and patch RefIds in ApplicationProgram XML file IDictionary <string, string> applProgIdMappings = new Dictionary <string, string>(); IDictionary <string, string> applProgHashes = new Dictionary <string, string>(); IDictionary <string, string> mapBaggageIdToFileIntegrity = new Dictionary <string, string>(50); FileInfo applProgFileInfo = new FileInfo(Path.Combine(tempDirectory, mfid, applicationProgramFilename)); FileInfo hwFileInfo = new FileInfo(Path.Combine(tempDirectory, mfid, "Hardware.xml")); FileInfo catalogFileInfo = new FileInfo(Path.Combine(tempDirectory, mfid, "Catalog.xml")); ApplicationProgramHasher aph = new ApplicationProgramHasher(applProgFileInfo, mapBaggageIdToFileIntegrity, true); aph.HashFile(); string oldApplProgId = aph.OldApplProgId; string newApplProgId = aph.NewApplProgId; string genHashString = aph.GeneratedHashString; /* * Console.WriteLine("OldApplProgId: " + oldApplProgId); * Console.WriteLine("NewApplProgId: " + newApplProgId); * Console.WriteLine("GeneratedHashString: " + genHashString); * Console.WriteLine("XML filename: " + applicationProgramFilename); */ applProgIdMappings.Add(oldApplProgId, newApplProgId); if (!applProgHashes.ContainsKey(newApplProgId)) { applProgHashes.Add(newApplProgId, genHashString); } // Sign Hardware.xml and patch RefIds in Hardware.xml HardwareSigner hws = new HardwareSigner(hwFileInfo, applProgIdMappings, applProgHashes, true); hws.SignFile(); IDictionary <string, string> hardware2ProgramIdMapping = hws.OldNewIdMappings; // Patch RefIds in Catalog.xml CatalogIdPatcher cip = new CatalogIdPatcher(catalogFileInfo, hardware2ProgramIdMapping); cip.Patch(); // Signing directory XmlSigning.SignDirectory(Path.Combine(tempDirectory, mfid)); // Create ZIP file (aka knxprod) archive ZipFile.CreateFromDirectory(tempDirectory, outputFile); // Remove temporary working directory recursively Directory.Delete(tempDirectory, true); _dialogService.ShowMessage(Ressources.ExportSuccess); } catch (Exception ex) { _dialogService.ShowMessage(ex.ToString()); } }
private void CreateNew(object param) { try { if (_model != null) { var cancel = AskSaveCancel(); if (cancel) { return; } } _model = new KNX(); string lang = Thread.CurrentThread.CurrentCulture.Name; _manufacturerData = new ManufacturerData_TManufacturer(); _applicationProgram = new ApplicationProgram_T(); _hardware = new Hardware_T(); _catalogSection = new CatalogSection_T(); _product = new Hardware_TProductsProduct(); _hardware2Program = new Hardware2Program_T(); _appProgRef = new ApplicationProgramRef_T(); _catalogItem = new CatalogSection_TCatalogItem(); _model.ManufacturerData.Add(_manufacturerData); _manufacturerData.Catalog.Add(_catalogSection); _manufacturerData.ApplicationPrograms.Add(_applicationProgram); _manufacturerData.Hardware.Add(_hardware); _hardware.Products.Add(_product); _hardware.Hardware2Programs.Add(_hardware2Program); _hardware2Program.ApplicationProgramRef.Add(_appProgRef); _catalogSection.CatalogItem.Add(_catalogItem); _model.CreatedBy = _toolName; _model.ToolVersion = _toolVersion; ApplicationNumber = 0; ApplicationVersion = 0; _applicationProgram.ProgramType = ApplicationProgramType_T.ApplicationProgram; _applicationProgram.MaskVersion = "MV-57B0"; _applicationProgram.LoadProcedureStyle = LoadProcedureStyle_T.MergedProcedure; _applicationProgram.PeiType = 0; _applicationProgram.DefaultLanguage = lang; _applicationProgram.DynamicTableManagement = false; _applicationProgram.Linkable = false; _applicationProgram.MinEtsVersion = "5.0"; _applicationProgram.ReplacesVersions = null; _applicationProgram.IsSecureEnabled = false; _applicationProgram.MaxSecurityIndividualAddressEntries = 32; _applicationProgram.MaxSecurityGroupKeyTableEntries = 50; var appStatic = new ApplicationProgramStatic_T(); _applicationProgram.Static = appStatic; var code = new ApplicationProgramStatic_TCode(); appStatic.Code = code; _codeSegment = new ApplicationProgramStatic_TCodeRelativeSegment(); code.RelativeSegment.Add(_codeSegment); _codeSegment.Name = "Parameters"; _codeSegment.Offset = 0; _codeSegment.LoadStateMachine = 4; _codeSegment.Size = 0; appStatic.Parameters = new ApplicationProgramStatic_TParameters(); appStatic.AddressTable = new ApplicationProgramStatic_TAddressTable(); appStatic.AddressTable.MaxEntries = ushort.MaxValue; appStatic.AssociationTable = new ApplicationProgramStatic_TAssociationTable(); appStatic.AssociationTable.MaxEntries = ushort.MaxValue; appStatic.ComObjectTable = new ApplicationProgramStatic_TComObjectTable(); appStatic.Options = new ApplicationProgramStatic_TOptions(); HardwareSerial = "0"; HardwareVersion = 0; _hardware.HasIndividualAddress = true; _hardware.HasApplicationProgram = true; _hardware.IsIPEnabled = true; _product.IsRailMounted = false; _product.DefaultLanguage = lang; OrderNumber = "0"; _hardware2Program.MediumTypes.Add("MT-5"); _hardware2Program.MediumTypes.Add("MT-2"); _catalogSection.Name = Ressources.Devices; _catalogSection.Number = "1"; _catalogSection.DefaultLanguage = lang; _catalogItem.Name = _product.Text; _catalogItem.Number = 1; _catalogItem.ProductRefId = _product.Id; _catalogItem.Hardware2ProgramRefId = _hardware2Program.Id; _catalogItem.DefaultLanguage = lang; RaiseChanged(); } catch (Exception ex) { _dialogService.ShowMessage(ex.ToString()); ClearData(); } }
private void Open(object param) { try { if (_model != null) { var cancel = AskSaveCancel(); if (cancel) { return; } } var filePath = _dialogService.ChooseFileToOpen(_fileExtension, _fileFilter); if (filePath == null) { return; } _openFile = filePath; XmlSerializer serializer = new XmlSerializer(typeof(KNX)); using (var reader = new StreamReader(_openFile)) { _model = (KNX)serializer.Deserialize(reader); } if (File.Exists("knx_master.xml")) { using (var reader = new StreamReader("knx_master.xml")) { var masterData = (KNX)serializer.Deserialize(reader); _model.MasterData = masterData.MasterData; } } _manufacturerData = _model.ManufacturerData.First(); _hardware = _manufacturerData.Hardware.First(); _product = _hardware.Products.First(); _catalogSection = _manufacturerData.Catalog.First(); _catalogItem = _catalogSection.CatalogItem.First(); _hardware2Program = _hardware.Hardware2Programs.First(); _applicationProgram = _manufacturerData.ApplicationPrograms.First(); _appProgRef = _hardware2Program.ApplicationProgramRef.First(); _codeSegment = _applicationProgram.Static.Code.RelativeSegment.First(); Parameters.Clear(); foreach (var item in _applicationProgram.Static.Parameters.Parameter) { item.AllTypes = ParameterTypes; item.Type = ParameterTypes.First(t => t.Id == item.ParameterType); item.AllBlocks = ParameterBlock; item.Block = ParameterBlock.FirstOrDefault(t => t.ParameterRefRef.FirstOrDefault(o => _applicationProgram.Static.ParameterRefs.FirstOrDefault(p => o.RefId == p.Id)?.RefId == item.Id) != null); if (item.Block == null) { if (ParameterBlock.Count == 0) { CreateDefaultParameterBlock(); } item.Block = ParameterBlock.First(); } Parameters.Add(item); } RaiseChanged(); } catch (Exception ex) { ClearData(); _dialogService.ShowMessage(ex.ToString()); } }