public void LoadAmlOrMtp( MtpVisualObjectLib objectLib, IMtpDataSourceFactoryOpcUa dataSourceFactory, MtpDataSourceOpcUaPreLoadInfo preLoadInfo, MtpDataSourceSubscriber subscriber, string fn, MtpSymbolMapRecordList makeUpConfigRecs = null) { // check if (fn == null) { return; } // check if we have a mtp-file, which needs to be unzipped bool unzip = fn.ToLower().EndsWith(".mtp") || fn.ToLower().EndsWith(".zip"); // easy? if (!unzip) { using (var stream = File.OpenRead(fn)) { LoadStream(objectLib, dataSourceFactory, preLoadInfo, subscriber, stream, makeUpConfigRecs); } return; } // not easy .. using (var file = File.OpenRead(fn)) using (var zip = new ZipArchive(file, ZipArchiveMode.Read)) { // simply take the first .aml file foreach (var entry in zip.Entries) { // take the 1st *.anl file; should be only one on the root level, even for MultiFileMTP if (entry.FullName.ToLower().EndsWith(".aml")) { using (var stream = entry.Open()) { LoadStream(objectLib, dataSourceFactory, preLoadInfo, subscriber, stream, makeUpConfigRecs); } break; } } } }
public void LoadStream( MtpVisualObjectLib objectLib, IMtpDataSourceFactoryOpcUa dataSourceFactory, MtpDataSourceOpcUaPreLoadInfo preLoadInfo, MtpDataSourceSubscriber subscriber, Stream stream, MtpSymbolMapRecordList makeUpConfigRecs = null) { // check if (stream == null) { return; } // try open file var doc = CAEXDocument.LoadFromStream(stream); if (doc == null) { return; } // load dynamic Instances var refIdToDynamicInstance = MtpAmlHelper.FindAllDynamicInstances(doc.CAEXFile); // load data sources if (dataSourceFactory != null) { MtpAmlHelper.CreateDataSources(dataSourceFactory, preLoadInfo, doc.CAEXFile); } // index pictures var pl = MtpAmlHelper.FindAllMtpPictures(doc.CAEXFile); foreach (var pi in pl) { var p = MtpPicture.ParsePicture(objectLib, subscriber, refIdToDynamicInstance, pi.Item2, makeUpConfigRecs); if (p != null) { this.PictureCollection.Add(pi.Item1, p); } } }
public static void CreateDataSources( IMtpDataSourceFactoryOpcUa dataSourceFactory, MtpDataSourceOpcUaPreLoadInfo preLoadInfo, CAEXFileType aml) { // access if (dataSourceFactory == null || aml == null) { return; } // assumption: all instances are on a fixed level of a instance hierarchy .. foreach (var ih in aml.InstanceHierarchy) // e.g.: ModuleTypePackage { foreach (var ie in ih.InternalElement) // e.g. Class = ModuleTypePackage { foreach (var ie2 in ie.InternalElement) // e.g. CommunicationSet { foreach (var ie3 in ie2.InternalElement) // e.g. InstanceList { if (ie3.RefBaseSystemUnitPath.Trim() == "MTPSUCLib/CommunicationSet/SourceList") { foreach (var server in ie3.InternalElement) // now on server { // check if server valid if (server.RefBaseSystemUnitPath.Trim() != "MTPCommunicationSUCLib/ServerAssembly/OPCUAServer") { continue; } if (!server.Name.HasContent()) { continue; } // get attributes var ep = FindAttributeValueByName(server.Attribute, "Endpoint"); // mapping?? if (preLoadInfo?.EndpointMapping != null) { foreach (var epm in preLoadInfo.EndpointMapping) { if (epm?.IsValid == true && (server.Name.Trim() == epm.ForName?.Trim() || server.ID.Trim() == epm.ForId?.Trim())) { ep = epm.NewEndpoint?.Trim(); } } } // check endpoint if (!ep.HasContent()) { continue; } // make server var serv = dataSourceFactory.CreateOrUseUaServer(ep); if (serv == null) { continue; } // go into items foreach (var item in server.ExternalInterface) { // check to item // TODO (MIHO, 2020-08-06): spec/example files seem not to be in a final state // check for the final role/class names to be used if (!item.RefBaseClassPath.Trim().Contains("OPCUAItem")) { continue; } // get attributes var id = FindAttributeValueByName(item.Attribute, "Identifier"); var ns = FindAttributeValueByName(item.Attribute, "Namespace"); var ac = FindAttributeValueByName(item.Attribute, "Access"); // potential renaming? if (preLoadInfo?.IdentifierRenaming != null) { foreach (var ren in preLoadInfo.IdentifierRenaming) { id = ren.DoReplacement(id); } } if (preLoadInfo?.NamespaceRenaming != null) { foreach (var ren in preLoadInfo.NamespaceRenaming) { ns = ren.DoReplacement(ns); } } // create // ReSharper disable once UnusedVariable var it = dataSourceFactory.CreateOrUseItem(serv, id, ns, ac, item.ID); } } } } } } } }
public static void CreateDataSources(IMtpDataSourceFactoryOpcUa dataSourceFactory, CAEXFileType aml) { // access if (dataSourceFactory == null || aml == null) { return; } // assumption: all instances are on a fixed level of a instance hierarchy .. foreach (var ih in aml.InstanceHierarchy) // e.g.: ModuleTypePackage { foreach (var ie in ih.InternalElement) // e.g. Class = ModuleTypePackage { foreach (var ie2 in ie.InternalElement) // e.g. CommunicationSet { foreach (var ie3 in ie2.InternalElement) // e.g. InstanceList { if (ie3.RefBaseSystemUnitPath.Trim() == "MTPSUCLib/CommunicationSet/SourceList") { foreach (var server in ie3.InternalElement) // now on server { // check if server if (server.RefBaseSystemUnitPath.Trim() != "MTPCommunicationSUCLib/ServerAssembly/OPCUAServer") { continue; } // get attributes var ep = FindAttributeValueByName(server.Attribute, "Endpoint"); if (ep == null || ep.Trim().Length < 1) { continue; } // make server var serv = dataSourceFactory.CreateOrUseUaServer(ep); if (serv == null) { continue; } // go into items foreach (var item in server.ExternalInterface) { // check fo item // TODO (MIHO, 2020-08-06): spec/example files seem not to be in a final state // check for the final role/class names to be used if (!item.RefBaseClassPath.Trim().Contains("OPCUAItem")) { continue; } // get attributes var id = FindAttributeValueByName(item.Attribute, "Identifier"); var ns = FindAttributeValueByName(item.Attribute, "Namespace"); var ac = FindAttributeValueByName(item.Attribute, "Access"); // create // ReSharper disable once UnusedVariable var it = dataSourceFactory.CreateOrUseItem(serv, id, ns, ac, item.ID); } } } } } } } }