示例#1
0
        public void GetObservationsFromString_ValidData_ReturnsCorrectObservations()
        {
            // Arrange
            COReCsvExtractor   sut         = new COReCsvExtractor();
            List <Observation> expected    = getValidObservations();
            string             fileContent = getValidCOReString();

            // Act
            List <Observation> actual = sut.GetObservations(fileContent, -8);

            // Assert
            // TODO: Add Equals functions to Measurement class
            Assert.Equal(expected[0].AirPressure, actual[0].AirPressure);
            Assert.Equal(expected[0].AirTemperature, actual[0].AirTemperature);
            Assert.Equal(expected[0].BatteryVoltage, actual[0].BatteryVoltage);
            Assert.Equal(expected[0].DateTime, actual[0].DateTime);
            Assert.Equal(expected[0].LoggerTemperature, actual[0].LoggerTemperature);
            Assert.Equal(expected[0].LongWaveIn, actual[0].LongWaveIn);
            Assert.Equal(expected[0].LTARSiteAcronym, actual[0].LTARSiteAcronym);
            Assert.Equal(expected[0].PAR, actual[0].PAR);
            Assert.Equal(expected[0].Precipitation, actual[0].Precipitation);
            Assert.Equal(expected[0].RecordType, actual[0].RecordType);
            Assert.Equal(expected[0].RelativeHumidity, actual[0].RelativeHumidity);
            Assert.Equal(expected[0].ShortWaveIn, actual[0].ShortWaveIn);
            Assert.Equal(expected[0].StationID, actual[0].StationID);
            Assert.Equal(expected[0].WindDirection, actual[0].WindDirection);
            Assert.Equal(expected[0].WindSpeed, actual[0].WindSpeed);
        }
示例#2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            //log.LogInformation("C# HTTP trigger function processed a request.");
            //
            //string name = req.Query["name"];
            //
            //string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            //dynamic data = JsonConvert.DeserializeObject(requestBody);
            //name = name ?? data?.name;
            //
            //return name != null
            //    ? (ActionResult)new OkObjectResult($"Hello, {name}")
            //    : new BadRequestObjectResult("Please pass a name on the query string or in the request body");

            log.LogInformation($"Webhook was triggered!");

            int utcOffset = -8;

            string  jsonContent = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(jsonContent);

            //log.Info("data: " + data.ToString());

            if (data == null |
                data?.measurements[0]?.physicalQuantities[0] == null |
                data?.recentFilePath == null |
                data?.recentFileContent == null)
            {
                log.LogError("No data");

                return(new BadRequestObjectResult("Data are null"));
            }

            // Convert Measurements to CORe.Observation
            //MeasurementJsonExtractor extractor = new MeasurementJsonExtractor();

            List <MeasurementV2> measurements =
                JsonConvert.DeserializeObject <List <MeasurementV2> >(
                    data.measurements.ToString());

            LtarDataPortalCOReTransformer transformer = new LtarDataPortalCOReTransformer();
            COReCsvStringWriter           loader      = new COReCsvStringWriter();

            //List<Measurement> measurements = extractor.ToMeasurements(data.measurements.ToString());
            List <Observation> observations = transformer.ToCOReObservations("CAF", "000", 'L', utcOffset, measurements);

            log.LogInformation("count: " + observations.Count);
            // Check if we're writing a new file
            string filename = loader.GetFilenamePstDateTime(observations[0]);
            //string filename = "cafMET001L_01_20170900_00.csv";
            string oldFileYYYYMM = data.recentFilePath.ToString().Substring(data.recentFilePath.ToString().Length - 15, 6);
            string newFileYYYYMM = filename.Substring(filename.Length - 15, 6);

            log.LogInformation("old: " + oldFileYYYYMM);
            log.LogInformation("new: " + newFileYYYYMM);

            // If files match then we need to append the data
            if (oldFileYYYYMM == newFileYYYYMM)
            {
                log.LogInformation("Files match, appending old data");
                COReCsvExtractor e = new COReCsvExtractor();
                //log.Info("Recent content: " + data.recentFileContent.ToString());
                List <Observation> oldObs = e.GetObservations(data.recentFileContent.ToString(), utcOffset);
                if (oldObs.Count > 0)
                {
                    oldObs.AddRange(observations);
                    observations = oldObs;
                }
            }

            // Now write the data to string and return
            string fileContent = loader.GetContentString(observations);
            //string fileContent = "foo";

            var returnObj = new { filename = filename, fileContent = fileContent };

            string result = JsonConvert.SerializeObject(returnObj);

            return(new JsonResult(result));
        }