Ejemplo n.º 1
0
        public AFDatabase InitializeAf()
        {
            //отключил олицетворение клиента, код выполняется от учетки пула
            using (WindowsIdentity.Impersonate(IntPtr.Zero))
            {
                try
                {
                    var myPiSystem = new PISystems(true).DefaultPISystem;

                    if (myPiSystem == null)
                    {
                        throw new InvalidOperationException("Default PISystem was not found.");
                    }

                    myPiSystem.Connect();

                    currentDb = myPiSystem.Databases["Database"];
                    return(currentDb);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            //
        }
        static void Main(string[] args)
        {
            logger.Info("The application has started.");

            PISystem pisystem = new PISystems().DefaultPISystem;

            pisystem.Connect();
            logger.Info($"Connecting as: {pisystem.CurrentUserIdentityString}");
            AFDatabase configuration = pisystem.Databases["Configuration"];
            AFElements preferences   = configuration.Elements["LimitCalculator"].Elements;

            logger.Info($"Will process {preferences.Count} preferences");

            List <DatabaseMonitoring> monitoredDB = new List <DatabaseMonitoring> {
            };

            Parallel.ForEach(preferences, (preference) =>
            {
                string JSON = (string)preference.Attributes["configuration"].GetValue().Value;
                logger.Info($"Configuration for preference: {JSON}");
                LimitCalculation calc = new LimitCalculation(CalculationPreference.CalculationPreferenceFromJSON(JSON), preference.Name);
                monitoredDB.Add(new DatabaseMonitoring(calc));
            });

            WaitForQuit();
        }
Ejemplo n.º 3
0
        private bool ConnectAF()
        {
            PISystem piSys      = new PISystems()[txtAFServer.Text];
            var      credential = new NetworkCredential(txtLogin.Text, txtPassword.Text);

            try {
                if (!piSys.ConnectionInfo.IsConnected)
                {
                    piSys.Connect(credential);
                }

                _afDb = piSys.Databases[txtAFDatabase.Text];
                return(true);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return(false);
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var username = Environment.GetEnvironmentVariable("PI_USER");
            var password = Environment.GetEnvironmentVariable("PI_PASSWORD");
            var domain   = Environment.GetEnvironmentVariable("PI_DOMAIN");
            var afname   = Environment.GetEnvironmentVariable("AF_NAME") ?? "empty";
            var piname   = Environment.GetEnvironmentVariable("PI_NAME") ?? "empty";

            try
            {
                Console.WriteLine("Hostname of AF Server:" + afname);
                PISystem sys = new PISystems()[afname];
                if (sys == null)
                {
                    throw new Exception("PI System is null");
                }
                var cred = new NetworkCredential(username, password, domain);
                sys.Connect(cred);

                Console.WriteLine("AF version:" + sys.ServerVersion + "\tAF name:" + sys.Name);
                Console.WriteLine("User:"******"\tIdentity:" + sys.CurrentUserIdentityString);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Can't connect to PI Server");
                Console.WriteLine(ex);
            }

            try
            {
                Console.WriteLine("Hostname of PI Data Archive:" + piname);
                PIServer srv = new PIServers()[piname];
                srv.Connect();
                Console.WriteLine("PI version:" + srv.ServerVersion + "\tPI name:" + srv.Name);
                Console.WriteLine("User:"******"\tIdentity:" + srv.CurrentUserIdentityString);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Can't connect to PI Data Archive");
                Console.WriteLine(ex);
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var piSystem = new PISystems()[piSystemName];

            if (!piSystem.ConnectionInfo.IsConnected)
            {
                if (string.IsNullOrEmpty(piSystemUser))
                {
                    piSystem.Connect();
                }
                else
                {
                    var creds = new NetworkCredential(piSystemUser, piSystemPass, piSystemDomain);
                    piSystem.Connect(creds);
                }
            }

            if (piSystem.ConnectionInfo.IsConnected)
            {
                Console.WriteLine($"Connected to PI System '{piSystem.Name}' (GUID='{piSystem.ID}')");

                if (piSystem.Databases.Contains(piSystemDatabase))
                {
                    var afDatabase = piSystem.Databases[piSystemDatabase];

                    Console.WriteLine($"Connected to AF Database '{afDatabase.Name}' (GUID='{afDatabase.ID}')");

                    // Do stuff here...
                    object sysCookie = piSystem.GetFindChangedItemsCookie(searchSandbox: false);

                    for (int i = 0; i < 100; i++)
                    {
                        Console.WriteLine("Checking for changes...");

                        List <AFChangeInfo> list = new List <AFChangeInfo>();
                        int resultsPerPage       = 1000;
                        while (true)
                        {
                            var results = piSystem.FindChangedItems(false, true, resultsPerPage, sysCookie, out sysCookie);
                            if ((results?.Count ?? 0) == 0)
                            {
                                break;
                            }
                            list.AddRange(results);
                        }

                        // Refresh objects that have been changed.
                        AFChangeInfo.Refresh(piSystem, list);

                        // Find the objects that have been changed.
                        foreach (AFChangeInfo info in list)
                        {
                            try
                            {
                                AFObject myObj = info.FindObject(piSystem, true);
                                Console.WriteLine("Found changed object: {0}", myObj.ToString());
                                Console.WriteLine("Identity: " + myObj.Identity.ToString());
                                Console.WriteLine("Action = " + info.Action);
                                Console.WriteLine("Change Time = " + info.ChangeTime);
                                Console.WriteLine("Value Updated = " + info.IsValueUpdate);
                                Console.WriteLine("Parent ID = " + info.ParentID);
                                Console.WriteLine("Parent = " + ((info.ParentID == Guid.Empty) ? "NONE" : AFElement.FindElement(piSystem, info.ParentID).GetPath()));
                                Console.WriteLine(" ");
                            }
                            catch { }
                        }

                        System.Threading.Thread.Sleep(5000);
                    }
                }
                else
                {
                    Console.WriteLine($"PI System Database '{piSystemDatabase}' does not exist.");
                }
            }
            else
            {
                Console.WriteLine($"Cannot connect to PI System '{piSystemName}'.");
            }
        }