Beispiel #1
0
 private void GetServer(string server)
 {
     if (_piServers.Contains(server))
     {
         _piServer = _piServers[server];
     }
     else
     {
         throw new KeyNotFoundException("Specified PI System does not exist");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Initialize a PI Server Connnection object.
        /// You should call the Connect method before access data with the PIServer property
        /// </summary>
        /// <param name="server">Name of the PI System (AF Server) to connect to</param>
        public PiConnectionMgr(string server)
        {
            // if the server does not exist in the local KST we throw an exception.  This is a choice made, you could decide to connect anyway.
            // to do so you'll need to look at the PIServers.DirectoryOptions Property: https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/P_OSIsoft_AF_PI_PIServers_DirectoryOptions.htm
            // ex:
            //PIServers.DirectoryOptions=PISystems.AFDirectoryOptions.AutoAdd;

            if (_piServers.Contains(server))
            {
                _piServer = _piServers[server];
            }
            else
            {
                throw new KeyNotFoundException("Specified PI System does not exist");
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Initialize a PI Server Connnection object.
        ///     You should call the Connect method before access data with the PIServer property
        /// </summary>
        /// <param name="server">Name of the PI System (AF Server) to connect to</param>
        public PIConnection(string server)
        {
            if (string.IsNullOrEmpty(server))
            {
                server = _piServers.DefaultPIServer.Name;
                _logger.InfoFormat("The server name was not found in the configuration, will use default server: {0}", server);
            }


            if (_piServers.Contains(server))
            {
                _piServer = _piServers[server];
            }
            else
            {
                throw new KeyNotFoundException("Specified PI System does not exist");
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            Console.WriteLine(new string('~', 45));
            if (GlobalConfig.Debug)
            {
                Console.WriteLine($"Main thread: {Thread.CurrentThread.ManagedThreadId}");
            }
            if (GlobalConfig.Debug)
            {
                Console.WriteLine($"Args length: {args.Length}");
            }

            var commandsList = new List <string>()
            {
                "snap",
                "sign,a",
                "sign,s",
                "sign,sa",
                "sign,as",
                "sign,t",
                "arclist",
                "interp",
                "plot",
                "summaries",
                "update",
                "annotate",
                "delete"
            };
            var      pointsList      = new PIPointList();
            var      command         = String.Empty;
            var      startTime       = String.Empty;
            var      endTime         = String.Empty;
            var      serverName      = String.Empty;
            var      addlparam1      = string.Empty;
            var      tagMasks        = new string[] { };
            var      times           = new string[] { };
            var      summaryDuration = new AFTimeSpan(TimeSpan.FromMinutes(10));
            var      st = new AFTime();
            var      et = new AFTime();
            PIServer myServer;

            try
            {
                var AppplicationArgs = new ParseArgs(args);
                if (!AppplicationArgs.CheckHelpVersionOrEmpty())
                {
                    return;
                }
                if (!AppplicationArgs.CheckCommandExists(commandsList, out command))
                {
                    return;
                }
                if (!AppplicationArgs.GetTagNames(out tagMasks))
                {
                    return;
                }
                if (!AppplicationArgs.GetAddlParams(command, ref times, ref startTime, ref endTime, ref addlparam1, ref serverName))
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                ParseArgs.PrintHelp(ex.Message);
                return;
            }

            #region Connect Server, Verify Times and Points
            if (!String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                if (!AFTime.TryParse(startTime, out st))
                {
                    ParseArgs.PrintHelp($"Invalid start time {startTime}");
                    return;
                }
                if (!AFTime.TryParse(endTime, out et))
                {
                    ParseArgs.PrintHelp($"Invalid end time {endTime}");
                    return;
                }
                if (st == et) //same time or min case (from initialization)
                {
                    ParseArgs.PrintHelp("Incorrect or same time interval specified");
                    return;
                }
            }

            try
            {
                PIServers myServers = new PIServers();
                if (string.IsNullOrEmpty(serverName))
                {
                    if (GlobalConfig.Debug)
                    {
                        Console.WriteLine("Attempting connection to default server ...");
                    }
                    myServer = myServers.DefaultPIServer;
                }
                else if (myServers.Contains(serverName))
                {
                    if (GlobalConfig.Debug)
                    {
                        Console.WriteLine($"Attempting connection to {serverName} server ...");
                    }
                    myServer = myServers[serverName];
                }
                else
                {
                    ParseArgs.PrintHelp($"Server {serverName} not found in KST");
                    return;
                }
                if (myServer != null)
                {
                    myServer.ConnectionInfo.Preference = AFConnectionPreference.Any;
                    myServer.Connect();
                    Console.WriteLine($"Connected to {myServer.Name} as {myServer.CurrentUserIdentityString}");
                }
            }
            catch (Exception ex)
            {
                ParseArgs.PrintHelp("Server Connection error: " + ex.Message);
                return;
            }

            try
            {
                //foreach (var n in tagMasks)
                //{
                //    if (PIPoint.TryFindPIPoint(myServer, n, out PIPoint p))
                //    {
                //        if (!pointsList.Contains(p)) pointsList.Add(p);
                //        else Console.WriteLine($"Duplicate point {p.Name}");
                //    }
                //    else
                //    {
                //        Console.WriteLine($"Point {n} not found");
                //    }
                //}
                pointsList.AddRange(PIPoint.FindPIPoints(myServer, new List <string>(tagMasks), null));
                if (pointsList.Count == 0)
                {
                    ParseArgs.PrintHelp("No valid PI Points");
                    myServer.Disconnect();
                    return;
                }
            }
            catch (Exception ex)
            {
                ParseArgs.PrintHelp("Tagmask error " + ex.Message);
                return;
            }
            #endregion

            //Handle KeyPress event from the user
            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                if (GlobalConfig.Debug)
                {
                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                }
                Console.WriteLine();
                Console.WriteLine("Program termination received from user ...");
                if (command == "sign,s" || command == "sign,as" || command == "sign,sa" || command == "sign,a" || command == "sign,t")
                {
                    GlobalConfig.CancelSignups = true;
                    Thread.Sleep(Convert.ToInt32(GlobalConfig.PipeCheckFreq * 1.2));
                }
                else
                {
                    if (myServer != null)
                    {
                        myServer.Disconnect();
                    }
                    Console.WriteLine(new string('~', 45));
                }
            };

            var Exec = new ExecuteCommand();
            if (GlobalConfig.Debug)
            {
                Console.WriteLine($"Command executing: {command}");
            }
            Exec.Execute(command, pointsList, st, et, summaryDuration, times, addlparam1, myServer);
            bool isexec = true;

            if (myServer != null)
            {
                myServer.Disconnect();
                if (GlobalConfig.Debug)
                {
                    Console.WriteLine($"Disconnecting from {myServer.Name}");
                }
            }
            if (!isexec)
            {
                Console.WriteLine(new string('~', 45));
            }
        }