Exemplo n.º 1
0
        public async Task <ActionResult> Post([FromBody] SetOfTracks setOfTracks)
        {
            foreach (var track in setOfTracks.Tracks)
            {
                track.Id = await _repo.GetNextId();

                track.AirDistance = _geoService.AirDistance((double)track.StartLat, (double)track.StartLng, (double)track.EndLat, (double)track.EndLng);
                await _repo.Create(track);

                Console.WriteLine(track.Id + " is new Id!\n");
            }
            Console.WriteLine();
            _trackSender.Send(setOfTracks.Tracks);

            return(Ok("Tracks saved successfully!"));
        }
        public async Task DoWork(CancellationToken cancellationToken)
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

            if (!cancellationToken.IsCancellationRequested)
            {
                using (var stream = File.Open("dataSource.xlsx", FileMode.Open, FileAccess.Read))
                {
                    using (var reader = ExcelReaderFactory.CreateReader(stream))
                    {
                        do
                        {
                            while (!cancellationToken.IsCancellationRequested && reader.Read()) //Each ROW
                            {
                                _logger.LogInformation("New row!\n");

                                if (_started)
                                {
                                    Track  signal     = new Track();
                                    Type   signalType = typeof(Track);
                                    string currentRow = "";

                                    for (int column = 0; column < reader.FieldCount; column++)
                                    {
                                        if (reader.GetValue(column) != null)
                                        {
                                            currentRow += reader.GetValue(column).ToString() + ", ";

                                            PropertyInfo piInstance           = signalType.GetProperty(DeviceParameters.SignalProperties[column]);
                                            string       propertyTypeFullName = piInstance.PropertyType.FullName;

                                            if (propertyTypeFullName.Contains("System.Int32"))
                                            {
                                                int convertedValue = Int32.Parse(reader.GetValue(column).ToString());
                                                piInstance.SetValue(signal, convertedValue);
                                            }
                                            else
                                            {
                                                piInstance.SetValue(signal, reader.GetValue(column));
                                            }
                                        }
                                    }

                                    dataFromSensor.Add(signal);
                                    currentRow += "\n";
                                    _logger.LogInformation(currentRow);
                                    counter++;

                                    if (counter == _ammountOfData)
                                    {
                                        object lockObject = new object();

                                        lock (lockObject)
                                        {
                                            _timeLimit     = DeviceParameters.TimeLimit;
                                            _ammountOfData = DeviceParameters.AmmountOfData;
                                        }
                                        await Task.Delay(_timeLimit * 1000);

                                        SetOfTracks setOfSignals = new SetOfTracks();
                                        setOfSignals.Tracks.AddRange(dataFromSensor);

                                        //send data to Data_Service
                                        string result = await _httpService.PostRequest("http://data_service/data-service/tracks/array-of-tracks", setOfSignals);

                                        _logger.LogInformation(result);

                                        counter = 0;
                                        dataFromSensor.Clear();
                                    }
                                }
                                else
                                {
                                    _started = true;
                                }
                            }
                        } while (!cancellationToken.IsCancellationRequested && reader.NextResult());
                    }
                }
            }
        }