private static async Task CreateStreamBasedOnPIPointAsync(PIPoint piPoint, IEnumerable <string> pointAttributes, IQiMetadataService metadata) { var otherAttributes = pointAttributes.Where(s => s != PICommonPointAttributes.Descriptor) .ToDictionary(s => s, s => piPoint.GetAttribute(s).ToString()); var id = GetStreamId(piPoint); var dataType = PIQiTypes.GetDataType(piPoint.PointType); await metadata.CreateOrUpdateStreamAsync(new QiStream() { Id = id, Name = piPoint.Name, TypeId = PIQiTypes.GetQiTypeId(dataType), Description = piPoint.GetAttribute(PICommonPointAttributes.Descriptor).ToString() }); //write stream metadata from PIPoint attributes await metadata.UpdateStreamMetadataAsync(id, otherAttributes); }
private static async Task WriteDataToOcsAsync(PIPoint piPoint, List <AFValue> afValues, IQiDataService data) { var streamId = GetStreamId(piPoint); switch (PIQiTypes.GetDataType(piPoint.PointType)) { case StreamDataType.Integer: await WriteDataForIntegerStreamAsync(data, afValues, streamId); break; case StreamDataType.Float: await WriteDataForFloatStreamAsync(data, afValues, streamId); break; case StreamDataType.String: await WriteDataForStringStreamAsync(data, afValues, streamId); break; case StreamDataType.Blob: await WriteDataForBlobStreamAsync(data, afValues, streamId); break; case StreamDataType.Time: await WriteDataForTimeStreamAsync(data, afValues, streamId); break; default: throw new ArgumentOutOfRangeException(); } Console.WriteLine( $"Writing data for point: {piPoint.Name} to stream {streamId} ({afValues.Count} values written.)"); }
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(); }); }