/// <summary> /// Creates an instance of the PIFixture class. /// </summary> public PIFixture() { var dataArchiveName = Settings.PIDataArchive; var servers = PIServers.GetPIServers(); PIServer = servers[dataArchiveName]; if (PIServer != null) { PIServer.Connect(); } else { throw new InvalidOperationException( $"The specific PI Data Archive [{dataArchiveName}] does not exist or is not configured."); } }
public PIConnection() { database = new PISystems().DefaultPISystem.Databases.DefaultDatabase; server = PIServers.GetPIServers().DefaultPIServer; server.Connect(); }
public PISetup() { piServer = PIServers.GetPIServers().DefaultPIServer; }
public void OMFTest() { // Use ticks as a unique id mask to avoid naming collisions with other PI Points var idTicks = $"{DateTime.UtcNow.Ticks}"; string typeId = $"TankMeasurement{idTicks}"; string containerId = $"Tank1Measurements{idTicks}"; PISystem omfAFServer = null; AFDatabase omfDatabase = null; PIServer omfPIServer = null; List <PIPoint> omfPIPoints = null; AFElementTemplate omfTypeAsTemplate = null; var jsonType = @"[ { ""id"": """ + typeId + @""", ""version"": ""1.0.0.0"", ""type"": ""object"", ""classification"": ""dynamic"", ""properties"": { ""Time"": { ""format"": ""date-time"", ""type"": ""string"", ""isindex"": true }, ""Pressure"": { ""type"": ""number"", ""name"": ""Tank Pressure"" }, ""Temperature"": { ""type"": ""number"", ""name"": ""Tank Temperature"" } } } ]"; var jsonContainer = @"[{ ""id"": """ + containerId + @""", ""typeid"": """ + typeId + @""", ""typeVersion"": ""1.0.0.0"", ""indexes"": [""Pressure""] }]"; var jsonData = @"[{ ""containerid"": """ + containerId + @""", ""values"": [{ ""Time"": ""2017-01-11T22:24:23.430Z"", ""Pressure"": 11.5, ""Temperature"": 101 }] }]"; var coll = new NameValueCollection { { "messagetype", "type" }, { "messageformat", "json" }, { "omfversion", "1.1" }, { "action", "create" }, }; try { // Create the type object var omfUrl = $"{Fixture.HomePageUrl}/omf"; Fixture.Client.Headers.Add(coll); Output.WriteLine($"Create OMF type through Web API using Url [{omfUrl}] and JSON [{jsonType}]."); var createTypeResponse = JObject.Parse(Fixture.Client.UploadString(omfUrl, "POST", jsonType)); Assert.True(createTypeResponse["OperationId"] != null, $"OperationId not returned in OMF response."); // Create a container for the created type Fixture.Client.Headers["messagetype"] = "container"; Output.WriteLine($"Create OMF container through Web API using Url [{omfUrl}] and JSON [{jsonContainer}]."); var createContainerResponse = JObject.Parse(Fixture.Client.UploadString(omfUrl, "POST", jsonContainer)); Assert.True(createContainerResponse["OperationId"] != null, "OperationId not returned in OMF response."); // Add data to the container Fixture.Client.Headers["messagetype"] = "data"; Output.WriteLine($"Add data to the OMF container through Web API using Url [{omfUrl}] and JSON [{jsonData}]."); var createDataResponse = JObject.Parse(Fixture.Client.UploadString(omfUrl, "POST", jsonData)); Assert.True(createDataResponse["OperationId"] != null, "OperationId not returned in OMF response."); // Verify objects were created correctly in AF and PI var instanceConfigUrl = $"{Fixture.HomePageUrl}/system/instanceconfiguration"; var config = JObject.Parse(Fixture.Client.DownloadString(instanceConfigUrl)); Output.WriteLine($"Verify OMF objects were create in AF and PI through Web API using Url [{instanceConfigUrl}]."); omfAFServer = new PISystems()[(string)config["OmfAssetServerName"]]; omfDatabase = omfAFServer.Databases[(string)config["OmfAssetDatabaseName"]]; omfPIServer = PIServers.GetPIServers()[(string)config["OmfDataArchiveName"]]; if (omfPIServer.ConnectionInfo == null) { omfPIServer.Connect(); } var typeInAF = omfDatabase.ElementTemplates[typeId]; Assert.True(typeInAF != null, $"The OMF Type [{typeId}] should exist as an ElementTemplate in [{omfDatabase}] on [{omfAFServer}]."); var pointFound = PIPoint.TryFindPIPoint(omfPIServer, $"{containerId}.Pressure", out var piPointPressure); Assert.True(pointFound, $"PI Point [{containerId}.Pressure] not found in [{omfPIServer.Name}]."); Assert.True(piPointPressure.CurrentValue().ValueAsSingle() == 11.5, $"Value for PI Point [{containerId}.Pressure] incorrect. Expected: [11.5], Actual: [{piPointPressure.CurrentValue().ValueAsSingle()}]."); pointFound = PIPoint.TryFindPIPoint(omfPIServer, $"{containerId}.Temperature", out var piPointTemperature); Assert.True(pointFound, $"PI Point [{containerId}.Temperature] not found in [{omfPIServer.Name}]."); Assert.True(piPointTemperature.CurrentValue().ValueAsInt32() == 101, $"Value for PI Point [{containerId}.Temperature] incorrect. Expected: [101], Actual: [{piPointTemperature.CurrentValue().ValueAsInt32()}]."); // Delete the data Output.WriteLine("Delete OMF data."); Fixture.Client.Headers["action"] = "delete"; var deleteDataResponse = JObject.Parse(Fixture.Client.UploadString(omfUrl, "POST", jsonData)); Assert.True(deleteDataResponse["OperationId"] != null, "OperationId not returned in OMF response."); // Delete the container Fixture.Client.Headers["messagetype"] = "container"; var deleteContainerResponse = JObject.Parse(Fixture.Client.UploadString(omfUrl, "POST", jsonContainer)); Assert.True(deleteContainerResponse["OperationId"] != null, "OperationId not returned in OMF response."); // Delete the type Fixture.Client.Headers["messagetype"] = "type"; var deleteTypeResponse = JObject.Parse(Fixture.Client.UploadString(omfUrl, "POST", jsonType)); Assert.True(deleteTypeResponse["OperationId"] != null, "OperationId not returned in OMF response."); // Verify objects were deleted. Remove objects that were not deleted before Assert to ensure cleanup omfPIPoints = PIPoint.FindPIPoints(omfPIServer, $"{containerId}*", null, null).ToList(); omfDatabase.ElementTemplates.Refresh(); omfTypeAsTemplate = omfDatabase.ElementTemplates[typeId]; Assert.True(omfPIPoints.Count == 0, "PIPoints were not deleted by the DELETE CONTAINER request to OMF."); Assert.True(omfTypeAsTemplate == null, $"The OMF Type [{typeId}] was not deleted by the DELETE TYPE request to OMF."); } catch (WebException ex) { var httpResponse = (HttpWebResponse)ex.Response; var statusCode = string.Empty; if (httpResponse.StatusCode != HttpStatusCode.OK && httpResponse.StatusCode != HttpStatusCode.Accepted && httpResponse.StatusCode != HttpStatusCode.NoContent) { statusCode = $"{httpResponse.StatusCode}: {httpResponse.StatusDescription}"; } // For troubleshooting error response codes returned by PI Web API using (var reader = new StreamReader(ex.Response.GetResponseStream())) { var response = JObject.Parse(reader.ReadToEnd())["Messages"]?[0]["Events"]?[0]["Message"]?.ToString(); if (!string.IsNullOrEmpty(response)) { Assert.True(false, $"Error returned from OMF: {response}. {statusCode}."); } else { throw; } } } finally { // Clean up any existing OMF objects if not deleted by OMF if (omfPIServer != null) { if (omfPIServer.ConnectionInfo == null) { omfPIServer.Connect(); } omfPIPoints = PIPoint.FindPIPoints(omfPIServer, $"{containerId}*", null, null).ToList(); bool pointsDeleted = omfPIPoints.Count == 0; if (!pointsDeleted) { omfPIServer.DeletePIPoints(omfPIPoints.Select(p => p.Name).ToList()); } } if (omfAFServer != null && omfDatabase != null) { omfTypeAsTemplate = omfDatabase.ElementTemplates[typeId]; bool typeDeleted = omfTypeAsTemplate == null; if (!typeDeleted) { AFElementTemplate.DeleteElementTemplates(omfAFServer, new List <Guid> { omfTypeAsTemplate.ID }); } } } }