public DataTable LocationExtendedAttributesTable(string tableName, string locationIdentifier)
        {
            Log.DebugFormat("Create LocationExtendedAttributesTable {0}, {1}", tableName, locationIdentifier);
            DataTable locationExtendedAttributesTable = new DataTable(tableName);

            LocationDataServiceResponse locData    = _Common.GetLocationData(locationIdentifier);
            List <ExtendedAttribute>    attributes = locData.ExtendedAttributes;

            foreach (ExtendedAttribute attribute in attributes)
            {
                try
                {
                    locationExtendedAttributesTable.Columns.Add(attribute.Name, typeof(object));
                }
                catch (Exception exp)
                {
                    Log.Error(string.Format("Error creating column in table = {0} with name = {1}", tableName, attribute.Name), exp);
                }
            }

            DataRow dataRow = locationExtendedAttributesTable.NewRow();

            foreach (ExtendedAttribute attribute in attributes)
            {
                if (locationExtendedAttributesTable.Columns.Contains(attribute.Name))
                {
                    dataRow[attribute.Name] = attribute.Value;
                }
            }

            locationExtendedAttributesTable.Rows.Add(dataRow);

            return(locationExtendedAttributesTable);
        }
Ejemplo n.º 2
0
 private static bool IsSpatialLocationDefined(LocationDataServiceResponse location)
 {
     return // ReSharper disable once CompareOfFloatsByEqualityOperator
            (location.Latitude != 0
            // ReSharper disable once CompareOfFloatsByEqualityOperator
             && location.Longitude != 0);
 }
Ejemplo n.º 3
0
        private void DeleteExistingAttachment(LocationDataServiceResponse locationData, Attachment existingAttachment)
        {
            if (!Context.DeleteExistingAttachments)
            {
                return;
            }

            var match = AttachmentUrlRegex.Match(existingAttachment.Url);

            if (!match.Success)
            {
                throw new ExpectedException($"Can't decode attachment ID from '{existingAttachment.Url}'");
            }

            var attachmentId = match.Groups["attachmentId"].Value;

            ++DeletedAttachments;

            LogAction($"Deleting existing attachment '{existingAttachment.FileName}' (uploaded {existingAttachment.DateUploaded:O} from '{locationData.Identifier}' ...");

            if (Context.DryRun)
            {
                return;
            }

            SiteVisit.Delete(new DeleteAttachmentById {
                Id = attachmentId
            });
        }
Ejemplo n.º 4
0
        public void InsertObservation(string assignedOffering, LocationDataServiceResponse location, LocationDescription locationDescription, TimeSeriesDataServiceResponse timeSeries, TimeSeriesDescription timeSeriesDescription)
        {
            var xmlTemplatePath = IsSpatialLocationDefined(location)
                ? @"XmlTemplates\InsertObservation.xml"
                : @"XmlTemplates\InsertObservationWithNoGeoSpatial.xml";

            const string pointTokenSeparator = ";";
            const string pointBlockSeparator = "@";

            var observablePropertyFieldName = SanitizeIdentifier($" {timeSeries.Parameter}_{timeSeries.Label}".Replace(" ", "_")); // TODO: Figure this mapping out

            var substitutions = CreateSubstitutions(timeSeries)
                                .Concat(new Dictionary <string, string>
            {
                { "{__offeringUri__}", assignedOffering },
                { "{__featureOfInterestName__}", location.LocationName },
                { "{__featureOfInterestLatitude__}", $"{location.Latitude}" },
                { "{__featureOfInterestLongitude__}", $"{location.Longitude}" },
                { "{__observablePropertyUnit__}", SanitizeUnitSymbol(timeSeries.Unit) },
                { "{__observablePropertyFieldName__}", observablePropertyFieldName },
                { "{__resultTime__}", $"{FixedResultTime:O}" },
                { "{__pointTokenSeparator__}", pointTokenSeparator },
                { "{__pointBlockSeparator__}", pointBlockSeparator },
            })
                                .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            var procedureUniqueId = substitutions[ProcedureUniqueIdKey];

            var existingSensor = FindExistingSensor(procedureUniqueId);

            for (var insertedPoints = 0; insertedPoints < timeSeries.Points.Count;)
            {
                var points = timeSeries.Points
                             .Skip(insertedPoints)
                             .Take(MaximumPointsPerObservation)
                             .ToList();

                substitutions["{__phenomenonStartTime__}"] = $"{points.First().Timestamp.DateTimeOffset:O}";
                substitutions["{__phenomenonEndTime__}"]   = $"{points.Last().Timestamp.DateTimeOffset:O}";
                substitutions["{__pointCount__}"]          = $"{points.Count}";
                substitutions["{__pointValues__}"]         = string.Join(pointBlockSeparator, points.Select(p => $"{p.Timestamp.DateTimeOffset:O}{pointTokenSeparator}{p.Value.Display}"));

                existingSensor.PhenomenonTime = existingSensor.PhenomenonTime
                                                .Concat(new[] { points.Last().Timestamp.DateTimeOffset })
                                                .OrderBy(x => x)
                                                .ToList();

                insertedPoints += points.Count;

                var xml = TransformXmlTemplate(xmlTemplatePath, substitutions);

                Log.Info($"Posting {points.Count} data points to '{procedureUniqueId}' ...");
                PostPox(xml);
            }
        }
        public DataTable LocationDataTable(string tableName, string locationIdentifier)
        {
            Log.DebugFormat("Create LocationDataTable {0}, {1}", tableName, locationIdentifier);
            DataTable locationTable = new DataTable(tableName);

            locationTable.Columns.Add("UniqueId", typeof(Guid));
            locationTable.Columns.Add("LocationIdentifier", typeof(string));
            locationTable.Columns.Add("LocationName", typeof(string));
            locationTable.Columns.Add("UtcOffset", typeof(TimeSpan));
            locationTable.Columns.Add("UtcOffsetString", typeof(string));
            locationTable.Columns.Add("Description", typeof(string));
            locationTable.Columns.Add("Latitude", typeof(double));
            locationTable.Columns.Add("Longitude", typeof(double));
            locationTable.Columns.Add("Elevation", typeof(double));
            locationTable.Columns.Add("ElevationUnit", typeof(string));
            locationTable.Columns.Add("ElevationUnitSymbol", typeof(string));
            locationTable.Columns.Add("LocationType", typeof(string));
            locationTable.Columns.Add("IsExternal", typeof(bool));
            locationTable.Columns.Add("Tags", typeof(object));
            locationTable.Columns.Add("LocationIdentifierAndNameInformation", typeof(string));
            locationTable.Columns.Add("LocationExtraInformation", typeof(string));
            DataRow dataRow = locationTable.NewRow();

            LocationDescription         locDescription = _Common.GetLocationDescriptionByIdentifier(locationIdentifier);
            LocationDataServiceResponse locData        = _Common.GetLocationData(locationIdentifier);

            dataRow["UniqueId"]            = locDescription.UniqueId;
            dataRow["LocationIdentifier"]  = locationIdentifier;
            dataRow["LocationName"]        = locDescription.Name;
            dataRow["UtcOffset"]           = TimeSpan.FromHours(locData.UtcOffset);
            dataRow["UtcOffsetString"]     = _Common.GetOffsetString(locData.UtcOffset);
            dataRow["Description"]         = locData.Description;
            dataRow["Latitude"]            = locData.Latitude;
            dataRow["Longitude"]           = locData.Longitude;
            dataRow["Elevation"]           = locData.Elevation;
            dataRow["ElevationUnit"]       = locData.ElevationUnits;
            dataRow["ElevationUnitSymbol"] = _Common.GetUnitSymbol(locData.ElevationUnits);
            dataRow["LocationType"]        = locData.LocationType;
            dataRow["IsExternal"]          = locDescription.IsExternalLocation;
            dataRow["Tags"] = locDescription.Tags;
            dataRow["LocationIdentifierAndNameInformation"] = Resources.LocationIdentifier + ": " + locationIdentifier +
                                                              ", " + Resources.LocationName + ": " + locDescription.Name;
            dataRow["LocationExtraInformation"] = Resources.UtcOffset + ": " + _Common.GetOffsetString(locData.UtcOffset) +
                                                  ", " + Resources.Latitude + ": " + locData.Latitude +
                                                  ", " + Resources.Longitude + ": " + locData.Longitude +
                                                  ", " + Resources.Elevation + ": " + locData.Elevation.ToString() +
                                                  ((string.IsNullOrEmpty(locData.Elevation.ToString()) ? "" : " " + locData.ElevationUnits));

            locationTable.Rows.Add(dataRow);

            return(locationTable);
        }
        public static void AddReportSpecificTables(DataSet dataSet)
        {
            try
            {
                Common common = (Common)dataSet.Tables["RunReportRequest"].Rows[0]["CommonLibrary"];

                DataTable settingsTable = dataSet.Tables["ReportSettings"];
                settingsTable.Columns.Add("ReportTitle", typeof(string));
                settingsTable.Rows[0]["ReportTitle"] = Resources.BenchmarkHistory;

                DataTable table = new DataTable("BenchmarkHistoryDataTable");
                dataSet.Tables.Add(table);

                RunFileReportRequest runReportRequest = (RunFileReportRequest)dataSet.Tables["RunReportRequest"].Rows[0]["RunReportRequest"];
                IPublishGateway      publish          = runReportRequest.Publish;
                string locationIdentifier             = (string)dataSet.Tables["LocationInput"].Rows[0]["Identifier"];

                string dllName = (string)dataSet.Tables["RunReportRequest"].Rows[0]["DllName"];
                Log.DebugFormat("{0} - Document GenerateScript input location = {1}", dllName, locationIdentifier);

                ///////////////////////////////////////////////////////////////
                DateTimeOffsetInterval reportPeriod = (DateTimeOffsetInterval)dataSet.Tables["ReportPeriods"].Rows[0]["NoGroupBy"];

                int    formatPrecision = common.GetParameterInt("FormatPrecision", 3);
                string formatType      = common.GetParameterString("FormatType", "Fixed");
                bool   formatFixed     = (formatType == "Fixed");

                int groupSizeLimit = 6;

                DataTable benchmarks = dataSet.Tables.Add("Benchmarks");

                benchmarks.Columns.Add("Name", typeof(string));
                benchmarks.Columns.Add("Elevation", typeof(string));
                benchmarks.Columns.Add("DateConstructed", typeof(string));
                benchmarks.Columns.Add("DateRemoved", typeof(string));
                benchmarks.Columns.Add("Status", typeof(string));
                benchmarks.Columns.Add("Description", typeof(string));

                DataTable groupBy = dataSet.Tables.Add("GroupBy");
                groupBy.Columns.Add("GroupNumber", typeof(int));
                for (int i = 0; i < groupSizeLimit; i++)
                {
                    groupBy.Columns.Add("Name" + (i + 1).ToString(), typeof(string));
                }

                DataTable benchmarkHistory = dataSet.Tables.Add("BenchmarkHistory");

                benchmarkHistory.Columns.Add("GroupNumber", typeof(int));
                benchmarkHistory.Columns.Add("DateTime", typeof(string));
                benchmarkHistory.Columns.Add("Party", typeof(string));
                benchmarkHistory.Columns.Add("Comments", typeof(string));
                for (int i = 0; i < groupSizeLimit; i++)
                {
                    benchmarkHistory.Columns.Add("Elevation" + (i + 1).ToString(), typeof(string));
                    benchmarkHistory.Columns.Add("Correction" + (i + 1).ToString(), typeof(string));
                }

                dataSet.Relations.Add("GroupByBenchmarkHistory", groupBy.Columns["GroupNumber"], benchmarkHistory.Columns["GroupNumber"]);

                LocationDataServiceResponse locData = common.GetLocationData(locationIdentifier);

                List <ReferencePoint> refPoints = new List <ReferencePoint>();
                foreach (ReferencePoint refPoint in locData.ReferencePoints)
                {
                    List <ReferencePointPeriod> refPointPeriods = refPoint.ReferencePointPeriods;

                    if (refPointPeriods.Count == 0)
                    {
                        continue;
                    }

                    refPointPeriods.Sort((x, y) => x.ValidFrom.CompareTo(y.ValidFrom));
                    DateTimeOffset validFrom = refPointPeriods[0].ValidFrom;

                    if (ReportSpecificFunctions.TimeRangeOverlaps(validFrom, refPoint.DecommissionedDate, reportPeriod))
                    {
                        refPoints.Add(refPoint);
                    }
                }

                if (refPoints.Count == 0)
                {
                    return;
                }

                Dictionary <Guid, List <ReferencePointPeriod> > refPointHistories = new Dictionary <Guid, List <ReferencePointPeriod> >();
                Dictionary <Guid, int> refPointNumbers = new Dictionary <Guid, int>();

                for (int i = 0; i < refPoints.Count; i++)
                {
                    ReferencePoint refPoint = refPoints[i];

                    refPointNumbers.Add(refPoint.UniqueId, i);

                    DataRow row = benchmarks.NewRow();

                    row["Name"]        = refPoint.Name;
                    row["Description"] = refPoint.Description;

                    if (refPoint.DecommissionedDate.HasValue)
                    {
                        row["DateRemoved"] = refPoint.DecommissionedDate.Value.ToString("yyyy-MM-dd");
                    }

                    string status = (refPoint.DecommissionedDate.HasValue) ? Resources.InactiveLowerCase : Resources.ActiveLowerCase;
                    status += (refPoint.PrimarySinceDate.HasValue) ? " " + Resources.PrimaryLowerCase : "";

                    row["Status"] = status;

                    DateTimeOffset?primarySince = refPoint.PrimarySinceDate;

                    List <ReferencePointPeriod> refPointPeriods = refPoint.ReferencePointPeriods;
                    refPointPeriods.Sort((x, y) => x.ValidFrom.CompareTo(y.ValidFrom));

                    DateTimeOffset validFrom = refPointPeriods[0].ValidFrom;
                    row["DateConstructed"] = validFrom.ToString("yyyy-MM-dd");

                    double elevation = refPointPeriods[refPointPeriods.Count - 1].Elevation;
                    row["Elevation"] = Common.FormatDoubleValue(elevation, formatFixed, formatPrecision, "");

                    refPointPeriods.Reverse();
                    refPointHistories[refPoint.UniqueId] = refPointPeriods;

                    benchmarks.Rows.Add(row);
                }

                int numberOfGroups = (int)Math.Ceiling((double)benchmarks.Rows.Count / groupSizeLimit);

                FieldVisitDataByLocationServiceRequest fdRequest = new FieldVisitDataByLocationServiceRequest();
                fdRequest.LocationIdentifier = locationIdentifier;
                fdRequest.Activities         = new List <ActivityType> {
                    ActivityType.LevelSurvey
                };
                FieldVisitDataByLocationServiceResponse fdResponse = publish.Get(fdRequest);
                List <FieldVisit> fieldVisitData = fdResponse.FieldVisitData;

                try
                {
                    fieldVisitData.Sort((x, y) => y.StartTime.Value.CompareTo(x.StartTime.Value));
                }
                catch { }


                for (int i = 0; i < numberOfGroups; i++)
                {
                    DataRow groupByRow = dataSet.Tables["GroupBy"].NewRow();
                    groupByRow["GroupNumber"] = i;

                    for (int j = 0; j < groupSizeLimit; j++)
                    {
                        int index = (i * groupSizeLimit) + j;
                        if (index < refPoints.Count)
                        {
                            ReferencePoint refPoint = refPoints[index];
                            groupByRow["Name" + (j + 1).ToString()] = refPoint.Name;
                        }
                    }

                    dataSet.Tables["GroupBy"].Rows.Add(groupByRow);
                }

                foreach (FieldVisit fieldVisit in fieldVisitData)
                {
                    if (!ReportSpecificFunctions.TimeRangeOverlaps(fieldVisit.StartTime.Value, fieldVisit.StartTime.Value, reportPeriod))
                    {
                        continue;
                    }

                    string dateTime = fieldVisit.StartTime.Value.ToString("yyyy-MM-dd HH:mm");
                    string comments = fieldVisit.LevelSurveyActivity.Comments;
                    string party    = fieldVisit.LevelSurveyActivity.Party;

                    int fieldVisitFirstRow = dataSet.Tables["BenchmarkHistory"].Rows.Count;

                    for (int i = 0; i < numberOfGroups; i++)
                    {
                        DataRow row = dataSet.Tables["BenchmarkHistory"].NewRow();
                        row["GroupNumber"] = i;
                        row["DateTime"]    = dateTime;
                        row["Party"]       = party;
                        row["Comments"]    = comments;
                        dataSet.Tables["BenchmarkHistory"].Rows.Add(row);
                    }

                    List <LevelSurveyMeasurement> levelMeasurements = fieldVisit.LevelSurveyActivity.LevelMeasurements;

                    foreach (LevelSurveyMeasurement lsm in levelMeasurements)
                    {
                        if (!lsm.MeasuredElevation.Numeric.HasValue)
                        {
                            continue;
                        }

                        Guid id = lsm.ReferencePointUniqueId;

                        if (!refPointNumbers.ContainsKey(id))
                        {
                            continue;
                        }

                        int number         = refPointNumbers[id];
                        int theGroupNumber = (int)Math.Floor((double)number / groupSizeLimit);
                        int thePosition    = (int)(number % groupSizeLimit);

                        double level      = lsm.MeasuredElevation.Numeric.Value;
                        double correction = ReportSpecificFunctions.calculateDifference(lsm, refPointHistories[id]);

                        DataRow row = dataSet.Tables["BenchmarkHistory"].Rows[fieldVisitFirstRow + theGroupNumber];

                        row["Elevation" + (1 + thePosition).ToString()]  = Common.FormatDoubleValue(level, formatFixed, formatPrecision, "");
                        row["Correction" + (1 + thePosition).ToString()] = Common.FormatDoubleValue(correction, formatFixed, formatPrecision, "");
                    }
                }

                List <DataRow> emptyRows = new List <DataRow>();
                for (int i = 0; i < dataSet.Tables["BenchmarkHistory"].Rows.Count; i++)
                {
                    DataRow row = dataSet.Tables["BenchmarkHistory"].Rows[i];

                    int emptyCount = 0;
                    for (int j = 0; j < groupSizeLimit; j++)
                    {
                        bool empty = string.IsNullOrEmpty(row["Elevation" + (j + 1).ToString()].ToString());
                        if (empty)
                        {
                            emptyCount++;
                        }
                    }
                    if (emptyCount == groupSizeLimit)
                    {
                        emptyRows.Add(row);
                    }
                }

                for (int i = 0; i < emptyRows.Count; i++)
                {
                    dataSet.Tables["BenchmarkHistory"].Rows.Remove(emptyRows[i]);
                }
            }
            catch (Exception exp)
            {
                Log.Error("Error creating report specific data tables ", exp);
                throw exp;
            }
        }
Ejemplo n.º 7
0
 public static DateTimeOffset?Parse(LocationDataServiceResponse location, string timeText)
 {
     return(Parse(timeText, () => TimeSpan.FromHours(location.UtcOffset)));
 }