private AFValue Convert(AFValue inVal, AFDatabase db, AFAttribute uomAttr)
        {
            AFValue result;

            if (uomAttr == null)
            {
                throw new ArgumentException("No UOM attribute " + Attribute.Name + "|UOM");
            }
            else
            {
                var targetUOMVal = uomAttr.GetValue().Value as string;
                if (string.IsNullOrEmpty(targetUOMVal))
                {
                    throw new ArgumentException("UOM attribute " + Attribute.Name + "|UOM is empty");
                }
                else
                {
                    result = inVal.Convert(db.UOMs[targetUOMVal]);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        override public void WriteNextResult()
        {
            try
            {
                values = valuesTask.Result;
            }
            catch (PITimeoutException)
            {
                fetchNextPage = true;
                exceptionCount++;
                if (exceptionCount > 10)
                {
                    throw new Exception("EXCEPTION: Too many retries: " + this.tag.Name);
                }
                return;
            }

            string nextStartTimeStamp = nextStartTime.UtcTime.ToString("yyyy/MM/dd HH:mm:ss");
            string nextEndTimeStamp   = timeRange.EndTime.UtcTime.ToString("yyyy/MM/dd HH:mm:ss");

            lock (logger)
            {
                logger.Log($"{k}, {nextStartTimeStamp} : {timeSpan} : {nextEndTimeStamp}, {values.Count}, {tag.Name}");
            }
            k++;

            for (int i = skipCount; i < values.Count; i++)
            {
                string datePath = PIRandomFunctionsUtil.DateTimeToDatePath(values[i].Timestamp, timeResolution, numYears);
                if (outputStreamWriter == null || lastDatePath != datePath)
                {
                    string outputPath = Path.Combine(outputDirectory, datePath, tag.Name);
                    Directory.CreateDirectory(new FileInfo(outputPath).Directory.FullName);
                    if (outputStreamWriter != null)
                    {
                        outputStreamWriter.Close();
                    }
                    outputStreamWriter = new StreamWriter(outputPath);
                    lastDatePath       = datePath;
                }
                AFValue value = values[i];
                value.Attribute = attribute;
                if (value.IsGood && (desiredUOM != null) && (desiredUOM != value.UOM))
                {
                    outputStreamWriter.WriteLine($"{value.Timestamp.UtcTime.ToString("o")},{value.Convert(desiredUOM).Value}");
                }
                else
                {
                    outputStreamWriter.WriteLine($"{value.Timestamp.UtcTime.ToString("o")},{value.Value}");
                }
            }

            if (fetchNextPage)
            {
                int lastIndex = values.Count - 1;
                skipCount     = 1;
                nextStartTime = values[lastIndex].Timestamp;
                for (int i = lastIndex - 1; i >= 0; --i)
                {
                    if (values[i].Timestamp == nextStartTime)
                    {
                        ++skipCount;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (!fetchNextPage)
            {
                this.Close();
                lock (closedLock) { closed = true; }
            }
        }