private static void readData(String serverName, Func <String> tagNameSupplier, int[] daysToReadList) { PIServer server = new PIServers() .Where(s => s.Name.Contains(serverName)) .First(); server.Connect(); foreach (int daysToRead in daysToReadList) { Console.Write("[{0} {1}d] ", serverName, daysToRead); Stopwatch roundTripStopwatch = Stopwatch.StartNew(); PIPoint tag = PIPoint.FindPIPoint(server, tagNameSupplier.Invoke()); roundTripStopwatch.Stop(); AFTimeRange timeRange = new AFTimeRange(new AFTime(readStart), new AFTime(readStart.Add(new TimeSpan(daysToRead, 0, 0, 0)))); try { Stopwatch readStopwatch = Stopwatch.StartNew(); AFValues values = tag.RecordedValues(timeRange, AFBoundaryType.Outside, "", true, 0); readStopwatch.Stop(); Console.WriteLine("Read {0:n0} samples in {1:0.000}s (1m samples in {2:0.000}s) EstimatedRoundTripTime: {3}ms", values.Count, readStopwatch.ElapsedMilliseconds / 1000.0, ((double)readStopwatch.ElapsedMilliseconds) / values.Count * 1000.0, roundTripStopwatch.ElapsedMilliseconds); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } } server.Disconnect(); }
private static void Main(string[] args) { PIServer server = new PIServers().DefaultPIServer; server.Connect(); List <PIPoint> points = PIPoint.FindPIPoints(server, "testSinusoid_*").ToList(); if (points.Count == 0) { server.CreatePIPoints(Enumerable.Range(1, 6000).Select(x => "testSinusoid_" + x), new Dictionary <String, Object>() { { "compressing", 0 } }); points = PIPoint.FindPIPoints(server, "testSinusoid_*").ToList(); } Console.WriteLine("Found {0} points", points.Count); TimeSpan chunkSize = new TimeSpan(5, 0, 0); for (DateTime start = new DateTime(2017, 1, 1); start < new DateTime(2018, 1, 1); start = start.Add(chunkSize)) { Console.WriteLine("Writing chunk starting at: " + start); List <AFValue> values = getSinusoidData(start, start.Add(chunkSize), new TimeSpan(0, 0, 15)); Parallel.ForEach(points, point => { point.UpdateValues(values, AFUpdateOption.Replace); }); } server.Disconnect(); }
// GET: Home public ActionResult Index() { PIServer piServer = new PIServers().DefaultPIServer; piServer.Connect(); IEnumerable<PIPoint> piPointList = PIPoint.FindPIPoints(piServer, "*"); PIPointList pointList = new PIPointList(); pointList.AddRange(piPointList); AFListResults<PIPoint, AFValue> results = pointList.EndOfStream(); IEnumerable<PIPointData> pointListData = results.Select(m => new PIPointData(m.PIPoint.Name, m.Value, m.Timestamp.LocalTime, m.PIPoint.ID)); return View(pointListData); }
//GET api/tsops/Point?server={serverName}&point={pointName} public List <string> GetPointConfig([FromUri] string server, [FromUri] string point) { PIServer srv; if (server != null) { try { srv = new PIServers()[server]; srv.Connect(); } catch { List <string> error = new List <string>(); error.Add("Error: Could not connect to PI Data Archive " + server); //Cannot connect to PI Data Archive return(error); } } else { List <string> error = new List <string>(); error.Add("Error: PI Data Archive name is null"); //Server is null return(error); } PIPoint requestedPoint; try { requestedPoint = PIPoint.FindPIPoint(srv, point); //Finds desired PI Point } catch { List <string> error = new List <string>(); error.Add("Error: PI Point " + point + " not found"); //PI Point not found return(error); } IDictionary <string, object> attributes = requestedPoint.GetAttributes(); //Gets all PI Point attributes List <string> attributeList = new List <string>(); attributeList.Add("Server : " + requestedPoint.Server.Name); //Adds the server name to the Result list foreach (KeyValuePair <string, object> pair in attributes) { attributeList.Add(pair.Key + ":" + pair.Value); //Converts Idictionary to List } return(attributeList); }
// GET api/tsops/Snapshot?server={serverName}&point={pointName>} public IEnumerable <string> GetSnapshot([FromUri] string server, [FromUri] string point) { PIServer srv; if (server != null) { try { srv = new PIServers()[server]; srv.Connect(); } catch { List <string> error = new List <string>(); error.Add("Error: Could not connect to PI Data Archive " + server); //Cannot connect to PI Data Archive return(error); } } else { List <string> error = new List <string>(); error.Add("Error: PI Data Archive name is null"); //Server is null return(error); } //Finds PI Point and gets current value try { PIPoint requestedPoint = PIPoint.FindPIPoint(srv, point); //Finds desired PI Point AFValue val = requestedPoint.CurrentValue(); //Gets current value IEnumerable <string> result = new string[] { "Server: " + val.PIPoint.Server, "Point Name : " + val.PIPoint.Name, "Current Value: " + val.Value, "Timestamp: " + val.Timestamp }; return(result); } catch { List <string> error = new List <string>(); error.Add("Error: Could not get current value for " + point); return(error); } }
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); } }
private static async Task MainAsync() { string accountId = ConfigurationManager.AppSettings["accountId"]; string namespaceId = ConfigurationManager.AppSettings["namespaceId"]; string address = ConfigurationManager.AppSettings["address"]; string resource = ConfigurationManager.AppSettings["resource"]; string clientId = ConfigurationManager.AppSettings["clientId"]; string clientSecret = ConfigurationManager.AppSettings["clientSecret"]; string piServerName = ConfigurationManager.AppSettings["PIDataArchive"]; var qiService = new QiService(new Uri(address), new QiSecurityHandler(resource, accountId, clientId, clientSecret)); var metadataService = qiService.GetMetadataService(accountId, namespaceId); var dataService = qiService.GetDataService(accountId, namespaceId); var piServer = new PIServers()[piServerName]; piServer.Connect(); PIPointQuery nameFilter = new PIPointQuery { AttributeName = PICommonPointAttributes.Tag, AttributeValue = _options.TagMask, Operator = OSIsoft.AF.Search.AFSearchOperator.Equal }; IEnumerable <string> attributesToRetrieve = new[] { PICommonPointAttributes.Descriptor, PICommonPointAttributes.EngineeringUnits, PICommonPointAttributes.PointSource }; var piPoints = (await PIPoint.FindPIPointsAsync(piServer, new[] { nameFilter }, attributesToRetrieve)).ToList(); if (!piPoints.Any()) { Console.WriteLine($"No points found matching the tagMask query!"); return; } Console.WriteLine($"Found {piPoints.Count} points matching mask: {_options.TagMask}"); //create types await PIQiTypes.CreateOrUpdateTypesInOcsAsync(metadataService); //delete existing streams if requested if (_options.Mode == CommandLineOptions.DataWriteModes.clearExistingData) { Parallel.ForEach(piPoints, piPoint => DeleteStreamBasedOnPIPointAsync(piPoint, metadataService).Wait()); } Parallel.ForEach(piPoints, piPoint => CreateStreamBasedOnPIPointAsync(piPoint, attributesToRetrieve, metadataService).Wait()); Console.WriteLine($"Created or updated {piPoints.Count()} streams."); //for each PIPoint, get the data of interest and write it to OCS Parallel.ForEach(piPoints, piPoint => { //Indices must be unique in OCS so we get rid of duplicate values for a given timestamp var timeRange = new AFTimeRange(_options.StartTime, _options.EndTime); var afValues = piPoint.RecordedValues(timeRange, AFBoundaryType.Inside, null, true) .GroupBy(value => value.Timestamp) .Select(valuesAtTimestamp => valuesAtTimestamp.Last()) //last event for particular timestamp .Where(val => val.IsGood) //System Digital States (e.g. Shutdown, IO Timeout, etc...) are ignored .ToList(); WriteDataToOcsAsync(piPoint, afValues, dataService).Wait(); }); }
// POST api/TSOps/Snapshot?server={serverName}&point={pointName} public IEnumerable <string> UpdateSnapshot([FromUri] string server, [FromUri] string point, [FromBody] JObject data) { PIServer srv; if (server != null) { try { srv = new PIServers()[server]; srv.Connect(); } catch { List <string> error = new List <string>(); error.Add("Error: Could not connect to PI Data Archive " + server); //Cannot connect to PI Data Archive return(error); } } else { List <string> error = new List <string>(); error.Add("Error: PI Data Archive name is null"); //Server is null return(error); } AFValue newVal = new AFValue(); if (data != null) { newVal.Value = data["Value"].ToString(); AFTime time = new AFTime(data["Timestamp"].ToString()); newVal.Timestamp = time; } else { List <string> error = new List <string>(); error.Add("Error: request body is null"); //Request body is null return(error); } PIPoint requestedPoint; //Finds PI Point try { requestedPoint = PIPoint.FindPIPoint(srv, point); } catch { List <string> error = new List <string>(); error.Add("Error: PI Point " + point + " not found"); //PI Point not found return(error); } //Gets current value and updates snapshot try { AFValue currVal = requestedPoint.CurrentValue(); requestedPoint.UpdateValue(newVal, OSIsoft.AF.Data.AFUpdateOption.Insert); IEnumerable <string> result = new string[] { "Server: " + requestedPoint.Server.Name, "Point Name : " + requestedPoint.Name, "Previous Value: " + currVal.Value, "Previous Timestamp: " + currVal.Timestamp, "New Value: " + newVal.Value, "new Timestamp: " + newVal.Timestamp }; return(result); } catch { List <string> error = new List <string>(); error.Add("Error: Could not update value for " + point); return(error); } }
// POST api/TSOps/Point?server={server} public HttpResponseMessage CreatePIPoint([FromUri] string server, [FromBody] JObject data) { PIServer srv; if (server != null) { try { srv = new PIServers()[server]; srv.Connect(); } catch { return(new HttpResponseMessage(HttpStatusCode.NotFound)); //Cannot connect to PI Data Archive } } else { return(new HttpResponseMessage(HttpStatusCode.NotFound)); //Server is null } //New PI Point attribute collection IDictionary <string, object> attributes = new Dictionary <string, object>(); //Classic PI Point attribute collection IDictionary <string, object> ptClassAttributes = srv.PointClasses["classic"].GetAttributes(); if (data != null) { foreach (KeyValuePair <string, JToken> pair in data) { //always create classic PI Points if (pair.Key.ToString() == "PointClass") { attributes.Add(pair.Key.ToString(), "Classic"); } else { if (ptClassAttributes.ContainsKey(pair.Key.ToString())) //Confirms the attribute name exists { attributes.Add(pair.Key, pair.Value.ToString()); } } } } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); //request body is null } try { PIPoint newPoint = srv.CreatePIPoint(data["Name"].ToString(), attributes); //Create PI Point return(new HttpResponseMessage(HttpStatusCode.Created)); } catch (PIException e) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); //tag could not be created - Most likely attributes are not set correctly } catch { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); //Could not create PI Point } }
/// <summary> /// Skips a test based on the passed condition. /// </summary> public GenericFactAttribute(TestCondition feature, bool error) : base(AFTests.KeySetting, AFTests.KeySettingTypeCode) { string afVersion = AFGlobalSettings.SDKVersion; string piHomeDir = string.Empty; // Return if the Skip property has been changed in the base constructor if (!string.IsNullOrEmpty(Skip)) { return; } string piHome64 = null; string piHome32 = null; DataLinkUtils.GetPIHOME(ref piHome64, 1); DataLinkUtils.GetPIHOME(ref piHome32, 0); switch (feature) { case TestCondition.AFCLIENTCURRENTPATCH: Version sdkVersion = new Version(afVersion); if (sdkVersion < _afClientCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_afClientCurrentVersionString}! Please consider upgrading! You are currently on {sdkVersion}"; } break; case TestCondition.AFPATCH2107: sdkVersion = new Version(afVersion); if (sdkVersion < new Version("2.10.7")) { Skip = $@"Warning! You do not have the critical patch: PI AF 2018 SP3 Patch 1 (2.10.7)! Please consider upgrading to avoid data loss! You are currently on {sdkVersion}"; } break; case TestCondition.AFSERVERCURRENTPATCH: Version serverVersion = new Version(AFFixture.GetPISystemFromConfig().ServerVersion); if (serverVersion < _afServerCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_afServerCurrentVersionString}! Please consider upgrading! You are currently on {serverVersion}"; } break; case TestCondition.ANALYSISCURRENTPATCH: Version analysisVer = new Version(AFFixture.GetPISystemFromConfig().AnalysisRulePlugIns["PerformanceEquation"].Version); if (analysisVer < _analysisCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_analysisCurrentVersionString}! Please consider upgrading! You are currently on {analysisVer}"; } break; case TestCondition.DATALINKCURRENTPATCH: string afDataDLLPath = @"Excel\OSIsoft.PIDataLink.AFData.dll"; DataLinkUtils.GetPIHOME(ref piHomeDir); Version dataLinkVersion = new Version(FileVersionInfo.GetVersionInfo(Path.Combine(piHomeDir, afDataDLLPath)).FileVersion); if (dataLinkVersion < _dataLinkCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_dataLinkCurrentVersionString}! Please consider upgrading! You are currently on {dataLinkVersion}"; } break; case TestCondition.NOTIFICATIONSCURRENTPATCH: PISystem system = AFFixture.GetPISystemFromConfig(); var configstore = new PINotificationsConfigurationStore(system); PINotificationsWCFClientManager wcfClient = new PINotificationsWCFClientManager(configstore); var serviceStatus = wcfClient.GetServiceStatus(); wcfClient.Dispose(); Version notificationsVersion = new Version(serviceStatus.Version); if (notificationsVersion < _notificationsCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_notificationsCurrentVersionString}! Please consider upgrading! You are currently on {notificationsVersion}"; } break; case TestCondition.PIDACURRENTPATCH: PIServer serv = new PIServers()[Settings.PIDataArchive]; serv.Connect(); Version pidaVersion = new Version(serv.ServerVersion); if (pidaVersion < _dataArchiveCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_dataArchiveCurrentVersionString}! Please consider upgrading! You are currently on {pidaVersion}"; } break; case TestCondition.PISQLOLEDBCURRENTPATCH: string sqlpath64OLEDB = Path.Combine(piHome64, @"SQL\SQL Client\OLEDB\PISQLOLEDB64.dll"); string sqlpath32OLEDB = Path.Combine(piHome32, @"SQL\SQL Client\OLEDB\PISQLOLEDB.dll"); if (File.Exists(sqlpath64OLEDB)) { Version piSqlVersion = new Version(FileVersionInfo.GetVersionInfo(sqlpath64OLEDB).FileVersion); if (piSqlVersion < _sqlClientOLEDBCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_sqlClientOLEDBCurrentVersionString}! Please consider upgrading! You are currently on {piSqlVersion}"; } } if (File.Exists(sqlpath32OLEDB)) { Version piSqlVersion = new Version(FileVersionInfo.GetVersionInfo(sqlpath32OLEDB).FileVersion); if (piSqlVersion < _sqlClientOLEDBCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_sqlClientOLEDBCurrentVersionString}! Please consider upgrading! You are currently on {piSqlVersion}"; } } break; case TestCondition.PISQLODBCCURRENTPATCH: string sqlpath64ODBC = Path.Combine(piHome64, @"SQL\SQL Client\ODBC\PISQLODBCB64.dll"); string sqlpath32ODBC = Path.Combine(piHome32, @"SQL\SQL Client\ODBC\PISQLODBC.dll"); if (File.Exists(sqlpath64ODBC)) { Version piSqlVersion = new Version(FileVersionInfo.GetVersionInfo(sqlpath64ODBC).FileVersion); if (piSqlVersion < _sqlClientODBCCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_sqlClientODBCCurrentVersionString}! Please consider upgrading! You are currently on {piSqlVersion}"; } } if (File.Exists(sqlpath32ODBC)) { Version piSqlVersion = new Version(FileVersionInfo.GetVersionInfo(sqlpath32ODBC).FileVersion); if (piSqlVersion < _sqlClientODBCCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_sqlClientODBCCurrentVersionString}! Please consider upgrading! You are currently on {piSqlVersion}"; } } break; case TestCondition.PIWEBAPICURRENTPATCH: var url = $"https://{Settings.PIWebAPI}:{443}/piwebapi/system"; WebClient client = new WebClient { UseDefaultCredentials = true }; AFElement elem = new AFElement(); bool disableWrites = false; bool anonymousAuth = false; JObject data = new JObject(); try { PIWebAPIFixture.GetWebApiClient(ref client, ref elem, ref disableWrites, ref anonymousAuth); data = JObject.Parse(client.DownloadString(url)); } catch { throw new InvalidOperationException($"Could not retrieve PI Web API version from server {Settings.PIWebAPI}!"); } finally { client.Dispose(); } var productVersion = (string)data["ProductVersion"]; Version piWebAPIVersion = new Version(productVersion); if (piWebAPIVersion < _webAPICurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_webAPICurrentVersionString}! Please consider upgrading! You are currently on {piWebAPIVersion}"; } break; case TestCondition.RTQPCURRENTPATCH: string connectionString = $"Provider=PISQLClient.1;Data Source={Settings.AFDatabase};Location={Settings.AFServer};Integrated Security=SSPI;OLE DB Services=-2"; using (var connection = new OleDbConnection(connectionString)) { connection.Open(); try { using (var command = new OleDbCommand("SELECT Version FROM System.Diagnostics.Version WHERE Item='Query Processor'", connection)) { string tempVersion = (string)command.ExecuteScalar(); Version rtqpVersion = new Version(tempVersion); if (rtqpVersion < _rtqpCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_rtqpCurrentVersionString}! Please consider upgrading! You are currently on {rtqpVersion}"; } } } catch (Exception) { Skip = $@"Warning! You do not have the latest update: {_rtqpCurrentVersionString}! Please consider upgrading!"; } } break; case TestCondition.PIVISIONCURRENTPATCH: string path = Settings.PIVisionServer; if (path.EndsWith("/#/", StringComparison.OrdinalIgnoreCase)) { path = path.Substring(0, path.Length - 3); } string visionUrl = $"{path}/Utility/permissions/read"; var visionProductVersion = string.Empty; using (var visionClient = new WebClient { UseDefaultCredentials = true }) { visionClient.Headers.Add("X-Requested-With", "XMLHttpRequest"); visionProductVersion = visionClient.DownloadString(visionUrl); } Version piVisionVersion = new Version(); if (!string.IsNullOrWhiteSpace(visionProductVersion)) { piVisionVersion = new Version(visionProductVersion); } else { throw new InvalidOperationException($"Could not retrieve PI Vision version from server {Settings.PIVisionServer}!"); } if (piVisionVersion < _visionCurrentVersion) { Skip = $@"Warning! You do not have the latest update: {_visionCurrentVersionString}! Please consider upgrading! You are currently on {piVisionVersion}"; } break; } if (error && !string.IsNullOrEmpty(Skip)) { throw new Exception(Skip); } }