Ejemplo n.º 1
0
        public async Task <IActionResult> PostTestRoadTrafficReport([FromBody] string payLoad)
        {
            using var fiwareClient = new FIWAREClient();
            var response = await fiwareClient.SendUltraLight(HttpMethod.Post,
                                                             FIWAREUrls.UltraLightMeasurementUrl(FIWAREUrls.RoadTrafficDeviceIds.FirstOrDefault()), payLoad);

            var success = response.IsSuccessStatusCode;

            return(Ok(success));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PostTestWeatherMeasurement(FiwareWeatherReport model)
        {
            using var fiwareClient = new FIWAREClient();
            var response = await fiwareClient.SendJson(HttpMethod.Post,
                                                       FIWAREUrls.JsonMeasurementUrl(FIWAREUrls.WeatherDeviceIds.FirstOrDefault()), model);

            var success = response.IsSuccessStatusCode;

            return(Ok(success));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PostTestRoadTrafficReport()
        {
            var roadTraffic = new FiwareTrafficReport(1)
            {
                StartTime      = DateTime.UtcNow,
                City           = "Random",
                AddressNumber  = "AddrNr",
                StartLatitude  = 37.9600965,
                StartLongitude = 23.8576043
            };

            using var fiwareClient = new FIWAREClient();
            var response = await fiwareClient.SendUltraLight(HttpMethod.Post,
                                                             FIWAREUrls.UltraLightMeasurementUrl(FIWAREUrls.RoadTrafficDeviceIds.FirstOrDefault()), roadTraffic.ToUltraLightSyntax());

            var success = response.IsSuccessStatusCode;

            return(Ok(success));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PostTestWeatherMeasurement()
        {
            var weatherEvent = new FiwareWeatherReport(1)
            {
                Temperature   = 12,
                WindChill     = 0,
                Humidity      = 02,
                Pressure      = 33.33,
                Visibility    = 100,
                WindDirection = "NWD",
                WindSpeed     = 120.1,
                Precipitation = 30.5,
                ReportTime    = DateTime.UtcNow
            };

            using var fiwareClient = new FIWAREClient();
            var response = await fiwareClient.SendJson(HttpMethod.Post,
                                                       FIWAREUrls.JsonMeasurementUrl(FIWAREUrls.WeatherDeviceIds.FirstOrDefault()), weatherEvent);

            var success = response.IsSuccessStatusCode;

            return(Ok(success));
        }
        /// <summary>
        /// Async Task that performs operations
        /// Reports to SQL Database for possible errors
        /// And Progress Percentage
        /// </summary>
        /// <param name="uniqueId"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        public async Task SubmitToAgentsAsyncTask(Guid uniqueId, int delay)
        {
            // Log start of procedure
            await ReportStartProcedure(uniqueId);

            // Instance of File parser
            var fileParser = new FileParser();

            // Get predefined dataset results
            var accidentDataset = fileParser.ParseAccidentsDataset().ToList();

            // Split into chunks of 20 for easier use with weather devices
            var chunks = accidentDataset.Chunk(20);

            // Diagnostics
            var progress = 0;

            // Procedure to Perform Progress Updates
            var timer = new System.Threading.Timer(async(e) =>
            {
                await ReportProgress(accidentDataset.Count, progress);
            }, null, _zero, _syncingPeriod);

            // Adds async Tasks to list for execution
            var taskList = new List <Task>();

            // POST measurements to IoT devices
            foreach (var chunk in chunks)
            {
                // each chunk uses its own fiware client
                using var fiwareClient = new FIWAREClient();

                // index of iteration to select correct device
                var index = 0;

                foreach (var report in chunk)
                {
                    // Adds pausing due to severe headover in node.js/async tasks line up
                    // This pause allows for built-up entities to get cleared from the async queue
                    if (progress % 25000 == 0 && progress > 0)
                    {
                        const int minutes = 1;
                        // wait 1 minute every 25000
                        const int time = 1000 * 60 * minutes;
                        Debug.WriteLine($"Progress at {progress}");
                        Debug.WriteLine($"{DateTime.UtcNow:dd-MM-yyyy HH:mm:ss}: Waiting for {minutes} minute(s)");
                        taskList = new List <Task>();
                        await Task.Delay(time);
                    }

                    try
                    {
                        // POST to JSON
                        taskList.Add(fiwareClient.SendJson(HttpMethod.Post,
                                                           FIWAREUrls.JsonMeasurementUrl(
                                                               FIWAREUrls.WeatherDeviceIds.Skip(index).FirstOrDefault()),
                                                           report.FiwareWeatherReport));

                        // Delay Task for operation completion
                        taskList.Add(Task.Delay(delay));

                        // POST to UL
                        taskList.Add(fiwareClient.SendUltraLight(HttpMethod.Post,
                                                                 FIWAREUrls.UltraLightMeasurementUrl(
                                                                     FIWAREUrls.RoadTrafficDeviceIds.Skip(index).FirstOrDefault()),
                                                                 report.FiwareTrafficDataReport.ToUltraLightSyntax()));

                        // await Task execution
                        await Task.WhenAll(taskList);

                        taskList.Clear();
                        index++;
                        progress++;
                    }
                    catch (Exception ex)
                    {
                        await ReportException(ex, report.FiwareTrafficDataReport.UID);

                        continue;
                    }
                }
            }

            // Update sync operation with end values and attempt to stop timer
            await ReportEnd(timer);
        }