Example #1
0
        public void Run()
        {
            PISystems piSystems = new PISystems();
            PISystem  piSystem  = piSystems["<AFSERVER>"];

            AFDatabase afDatabase = piSystem.Databases["NuGreen"];

            const int pageSize   = 1000;
            int       startIndex = 0;
            int       totalCount;

            do
            {
                // Find a collection of elements instantiated from the Boiler tempplate.
                // Only the Elements' header information (Name, Description, Template, Type, etc.)
                // are loaded from the AF Server by this call.
                AFNamedCollection <AFElement> elements = AFElement.FindElements(
                    database: afDatabase,
                    searchRoot: null,
                    query: "Boiler",
                    field: AFSearchField.Template,
                    searchFullHierarchy: true,
                    sortField: AFSortField.Name,
                    sortOrder: AFSortOrder.Ascending,
                    startIndex: startIndex,
                    maxCount: pageSize,
                    totalCount: out totalCount);
                if (elements == null)
                {
                    break;
                }

                // This call goes to the AF Server to fully load the found elements in one call,
                // so further AF Server calls can be avoided.
                AFElement.LoadElements(elements);

                // Now we can use the elements without having to make any additional server calls.
                // In the example below, accessing the Attributes collection would have
                // caused an additional call per element if we had not called LoadElements previously.
                Console.WriteLine("Found {0} Elements.", elements.Count);
                foreach (AFElement item in elements)
                {
                    Console.WriteLine("  Element {0} has {1} Attributes", item.Name, item.Attributes.Count);
                }

                startIndex += pageSize;
            } while (startIndex < totalCount);
        }
Example #2
0
        public void RunCalculations(IEnumerable <string> elementNames, DateTime startTime, DateTime endTime,
                                    TimeStampsGenerator.TimeStampsInterval interval, string afServerName, string afDatabaseName
                                    , int afThreadCount, int analysesThreadCount, int dataWriterDelay, string outputFile, bool enableWrite)
        {
            var statistics = new Statistics();
            var dateWriter = new DataWriter();
            var times      = TimeStampsGenerator.Get(interval, startTime, endTime);
            var aftimes    = times.Select(t => new AFTime(t)).ToList();
            ConcurrentQueue <List <AFValue> > dataWritingQueue = null;

            #region AFConnection

            // connects to the the PI system
            _logger.InfoFormat("Connecting to AF server:{0} and database: {1}...", afServerName, afDatabaseName);
            var        afServers = new PISystems();
            var        afServer  = afServers[afServerName];
            AFDatabase database;
            if (!afServer.ConnectionInfo.IsConnected)
            {
                afServer.Connect();
                _logger.Info("AF server connected");
            }

            database = afServer.Databases[afDatabaseName];

            #endregion AFConnection

            statistics.Run();

            if (enableWrite)
            {
                dateWriter.Run(dataWriterDelay, outputFile);
            }

            var elements = new List <AFElement>();
            _logger.Info("Loading elements");
            foreach (var elementName in elementNames)
            {
                var element = database.Elements[elementName];

                if (element != null)
                {
                    elements.Add(element);
                }
                else
                {
                    _logger.WarnFormat("Passed element name \"{0}\" did not return an Element object. AF Database {1}", elementName, database.Name);
                }
            }

            AFElement.LoadElements(elements);

            _logger.InfoFormat("{0} Elements Loaded...", elements.Count);
            _logger.InfoFormat("Starting calculation from {0} to {1}. Means {2} evaluations per analysis to execute.", startTime, endTime, aftimes.Count);
            _logger.InfoFormat("Configuration:{0} threads for elements and {1} threads for Analyses", afThreadCount, analysesThreadCount);

            // for each element we carry out the calculations
            if (enableWrite)
            {
                dataWritingQueue = DataWriter.DataQueue;
            }
            Parallel.ForEach(elements, new ParallelOptions()
            {
                MaxDegreeOfParallelism = afThreadCount
            }, (element) => RunAnalysesParallel(element, aftimes, analysesThreadCount, dataWritingQueue));
        }