Exemplo n.º 1
0
        private static bool UpdateCache(string esiSourceDirectoryPath, uint manufacturer, uint productCode, uint revision)
        {
            // check if source ESI files have been loaded
            if (!EsiUtilities.SourceEtherCatInfoSet.Any())
            {
                EsiUtilities.LoadEsiSource(esiSourceDirectoryPath);
            }

            // try to find requested device info
            (var sourceInfo, var sourceDevice, _) = EsiUtilities.TryFindDevice(EsiUtilities.SourceEtherCatInfoSet, manufacturer, productCode, revision);

            if (sourceDevice == null)
            {
                return(false);
            }

            lock (_lock)
            {
                // find matching EtherCATInfo in cache
                var cacheInfo = EsiUtilities.CacheEtherCatInfoSet.FirstOrDefault(current =>
                {
                    var vendorId = (uint)EsiUtilities.ParseHexDecString(current.Vendor.Id);
                    return(vendorId == manufacturer);
                });

                // extend cache file
                if (cacheInfo != null)
                {
                    // add new groups
                    var cacheGroupSet = cacheInfo.Descriptions.Groups.ToList();

                    foreach (var sourceGroup in sourceInfo.Descriptions.Groups)
                    {
                        if (!cacheGroupSet.Any(current => current.Type == sourceGroup.Type))
                        {
                            cacheGroupSet.Add(sourceGroup);
                        }
                    }

                    cacheInfo.Descriptions.Groups = cacheGroupSet.ToArray();

                    // add found device
                    cacheInfo.Descriptions.Devices = cacheInfo.Descriptions.Devices.ToList().Concat(new[] { sourceDevice }).ToArray();
                }
                // create new cache file
                else
                {
                    cacheInfo = sourceInfo;
                    cacheInfo.Descriptions.Devices = new EtherCATInfoDescriptionsDevice[] { sourceDevice };

                    EsiUtilities.CacheEtherCatInfoSet.Add(cacheInfo);
                }

                // save new/updated EtherCATInfo to disk
                EsiUtilities.SaveEsi(cacheInfo, Path.Combine(_cacheDirectoryPath, $"{cacheInfo.Vendor.Id}.xml"));
            }

            // return
            return(true);
        }
Exemplo n.º 2
0
        public static (EtherCATInfoDescriptionsDevice device, EtherCATInfoDescriptionsGroup group) FindEsi(string esiSourceDirectoryPath, uint manufacturer, uint productCode, uint revision)
        {
            EtherCATInfoDescriptionsDevice device;
            EtherCATInfoDescriptionsGroup  group;

            // try to find ESI in cache
            (_, device, group) = EsiUtilities.TryFindDevice(EsiUtilities.CacheEtherCatInfoSet, manufacturer, productCode, revision);

            if (device == null)
            {
                // update cache
                EsiUtilities.UpdateCache(esiSourceDirectoryPath, manufacturer, productCode, revision);

                // try to find ESI in cache again
                (_, device, group) = EsiUtilities.TryFindDevice(EsiUtilities.CacheEtherCatInfoSet, manufacturer, productCode, revision);

                // it finally failed
                if (device == null)
                {
                    throw new Exception($"Could not find ESI information of manufacturer '0x{manufacturer:X}' for slave with product code '0x{productCode:X}' and revision '0x{revision:X}'.");
                }
            }

            return(device, group);
        }