public async Task SaveCameras(IFormFile file)
        {
            var a      = file.OpenReadStream();
            var reader = ExcelReaderFactory.CreateReader(a).AsDataSet(new ExcelDataSetConfiguration
            {
                ConfigureDataTable = _ => new ExcelDataTableConfiguration
                {
                    UseHeaderRow = true
                }
            });

            foreach (DataTable dataTable in reader.Tables)
            {
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    var address = dataRow[0].ToString();
                    var house   = await _context.Houses.FirstOrDefaultAsync(x => x.Address == address);

                    if (house == null)
                    {
                        house = new House
                        {
                            Address = address
                        };
                        var response = await Geocoder.GeocodeAddress(house.Address);

                        house.Latitude  = response.Data.Items[0].Coordinates[0];
                        house.Longitude = response.Data.Items[0].Coordinates[1];
                        await _context.Houses.AddAsync(house);

                        await _context.SaveChangesAsync();

                        var entrancesList = new List <Models.Entrance>();
                        for (var i = 0; i < response.Data.Items[0].Entrances.Length; i++)
                        {
                            entrancesList.Add(new Models.Entrance
                            {
                                HouseId   = house.Id,
                                Number    = i + 1,
                                Latitude  = response.Data.Items[0].Entrances[i].Coordinates[0],
                                Longitude = response.Data.Items[0].Entrances[i].Coordinates[1]
                            });
                        }
                        await _context.Entrances.AddRangeAsync(entrancesList);

                        await _context.SaveChangesAsync();
                    }

                    var enter = await _context.Entrances.FirstOrDefaultAsync(x => x.HouseId == house.Id && x.Number == int.Parse(dataRow[1].ToString()));

                    if (enter != null)
                    {
                        enter.CameraNumber = int.Parse(dataRow[2].ToString());
                        await _context.SaveChangesAsync();
                    }
                }
            }
        }
예제 #2
0
        private void MainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (IsGeocoding)
                {
                    Log.Warn("An existing geocoding session was found, will try again in {0} minutes.",
                             AppConfig.Interval);
                }
                else
                {
                    Log.Info("Started geocoding session using {0} threads with {1} min / {2} max seconds of jitter.",
                             AppConfig.Parallelism, AppConfig.JitterMinSleep, AppConfig.JitterMaxSleep);
                    IsGeocoding = true;

                    GeocoderQueue = DataAccessLayer.GetOutstanding();

                    GeocoderQueue.AsParallel().WithDegreeOfParallelism(AppConfig.Parallelism).ForAll(job =>
                    {
                        // add a element of jitter to the parrallelism to prevent
                        // a thundering herd of requests.
                        var jitter = Random.Next(AppConfig.JitterMinSleep, AppConfig.JitterMaxSleep);

                        Log.Debug("Sleeping for {0} seconds, afterwards will retrieve {1}.", jitter, job.Address);
                        System.Threading.Thread.Sleep(jitter * 1000);

                        var response = Geocoder.GeocodeAddress(job.Address);

                        if (response.Equals(null))
                        {
                            Log.Error("SERVICE_ERROR: {0} {1} response was null.", job.Address, job.CompanyName);
                            GeocoderStats.ServiceError++;
                        }
                        else
                        {
                            switch (response.status.ToUpperInvariant())
                            {
                            case "OK":
                                GeocoderStats.Ok++;
                                Log.Info("OK: {0} ({1})", job.CompanyName, job.Address);
                                DataAccessLayer.SaveResponse(job, response);
                                break;

                            case "ZERO_RESULTS":
                                Log.Info("ZERO_RESULTS: {0} ({1})", job.CompanyName, job.Address);
                                GeocoderStats.ZeroResults++;
                                break;

                            case "OVER_QUERY_LIMIT":
                                Log.Fatal("OVER_QUERY_LIMIT: {0} ({1})", job.CompanyName, job.Address);
                                GeocoderStats.OverQueryLimit++;
                                break;

                            case "REQUEST_DENIED":
                                Log.Fatal("REQUEST_DENIED: {0} ({1})", job.CompanyName, job.Address);
                                GeocoderStats.RequestDenied++;
                                break;

                            case "INVALID_REQUEST":
                                Log.Fatal("INVALID_REQUEST: {0} ({1})", job.CompanyName, job.Address);
                                GeocoderStats.InvalidRequest++;
                                break;

                            case "UNKNOWN_ERROR":
                                Log.Error("UNKNOWN_ERROR: {0} ({1})", job.CompanyName, job.Address);
                                GeocoderStats.UnknownError++;
                                break;

                            default:
                                // This would only happen if Google add new STATUS codes to their
                                // api or a typo exists in the above switch statement.
                                Log.Fatal("SERVICE_ERROR: {0} ({1})", job.CompanyName, job.Address);
                                GeocoderStats.ServiceError++;
                                break;
                            }
                        }
                    });

                    Log.Info("Finished Geocoding Session: \n\n {0} \n {1} \n", GeocoderQueue.ToJson(), GeocoderStats);

                    ResetGeocoderStats();

                    IsGeocoding = false;
                }
            }
            catch (Exception ex)
            {
                Log.FatalException("Abort, Abort.. Something went seriously wrong: ", ex);

                IsGeocoding = false;
            }
        }