示例#1
0
        private void RunFirstQuery()
        {
            Console.WriteLine("Sending events...");
            FirstQueryInputModel newInput;

            using var reader = new StreamReader(CSVPath);
            using var csv    = new CsvReader(reader);
            csv.Configuration.HasHeaderRecord = false;

            int sentEventsCount  = 0;
            int usentEventsCount = 0;
            int logFilterCount   = 0;
            int allCount         = 0;

            while (csv.Read())
            {
                try
                {
                    var record = csv.GetRecord <DataModel>();

                    var dropTime = record.DropoffDatetime.GetUnixTime();
                    LastDropoff = Math.Max(LastDropoff, dropTime);
                    var _30minAgo = LastDropoff - (30 * 60 * 1000);

                    if (_30minAgo > dropTime)
                    {
                        continue;
                    }

                    var pickUpLocation = new TaxiLocation
                                         (
                        (double.Parse(record.PickupLongitude), double.Parse(record.PickupLatitude)),
                        QueryRespect.RespectQuery1
                                         );

                    var dropOffLocation = new TaxiLocation
                                          (
                        new Coordinates(double.Parse(record.DropoffLongitude), double.Parse(record.DropoffLatitude)),
                        QueryRespect.RespectQuery1
                                          );

                    newInput = new FirstQueryInputModel
                    {
                        PickupTime      = record.PickupDatetime.GetUnixTime(),
                        DropoffTime     = record.DropoffDatetime.GetUnixTime(),
                        PickupTimeOrig  = record.PickupDatetime,
                        DropoffTimeOrig = record.DropoffDatetime,
                        PickCell        = pickUpLocation.CellId,
                        DropCell        = dropOffLocation.CellId,
                        EventTimestamp  = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                    };

                    var jsonModel = new
                    {
                        @event = newInput
                    };

                    SendEvent(JsonSerializer.Serialize(jsonModel), FirstQueryUri);
                    sentEventsCount++;
                }
                catch (Exception)
                {
                    usentEventsCount++;
                }
                finally
                {
                    logFilterCount++;
                    allCount++;
                    if (logFilterCount > 1000)
                    {
                        var now = DateTime.Now;
                        Console.Write($"All: {allCount}    Sent events: {sentEventsCount}      Unsent events: {usentEventsCount}, {now.ToLongTimeString()}, Took ");
                        var d = now - LastDateTime;
                        Console.ForegroundColor = (d > TimeSpan.FromSeconds(10)) ? ConsoleColor.Red : ConsoleColor.Green;
                        Console.WriteLine(d.ToString());
                        Console.ResetColor();
                        LastDateTime   = now;
                        logFilterCount = 0;
                    }
                }
            }
        }
        private void UpdateTaxiLocation(TaxiLocation value)
        {
            if (value != null && value.Latitude.HasValue && value.Longitude.HasValue && value.VehicleNumber.HasValue())
            {
                ShowAvailableVehicles(null);

                // Update Marker and Animate it to see it move on the map
                if (_taxiLocationPin != null)
                {
                    var icon = ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue
                                                ? BitmapDescriptorFactory.FromBitmap(DrawHelper.RotateImageByDegreesWithСenterCrop(Resource.Drawable.nearby_oriented_passenger, value.CompassCourse.Value))
                        : BitmapDescriptorFactory.FromBitmap(CreateTaxiBitmap());

                    AnimateMarkerOnMap(icon, _taxiLocationPin, new LatLng(value.Latitude.Value, value.Longitude.Value), value.CompassCourse, new Position
                    {
                        Latitude  = value.Latitude.Value,
                        Longitude = value.Longitude.Value
                    });

                    if (_showVehicleNumber)
                    {
                        _taxiLocationPin.ShowInfoWindow();
                    }
                }

                // Create Marker the first time
                else
                {
                    try
                    {
                        var mapOptions = new MarkerOptions()
                                         .Anchor(0.5f, ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue ? 0.5f : 1f)
                                         .SetPosition(new LatLng(value.Latitude.Value, value.Longitude.Value))
                                         .InvokeIcon(
                            ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue
                                                                ? BitmapDescriptorFactory.FromBitmap(DrawHelper.RotateImageByDegreesWithСenterCrop(Resource.Drawable.nearby_oriented_passenger, value.CompassCourse.Value))
                                : BitmapDescriptorFactory.FromBitmap(CreateTaxiBitmap()))
                                         .Visible(true);

                        if (_showVehicleNumber)
                        {
                            var inflater        = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
                            var addBottomMargin = !(ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue);
                            Map.SetInfoWindowAdapter(new CustomMarkerPopupAdapter(inflater, addBottomMargin, _resources, value.Market));
                            mapOptions.SetTitle(value.VehicleNumber);
                        }

                        _taxiLocationPin = Map.AddMarker(mapOptions);

                        if (_showVehicleNumber)
                        {
                            _taxiLocationPin.ShowInfoWindow();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex);
                    }

                    _isBookingMode = true;

                    return;
                }
            }

            // Booking is now over, so we need to clean up.
            if (value == null && _taxiLocationPin != null)
            {
                _isBookingMode           = false;
                _taxiLocationPin.Visible = false;
                _taxiLocationPin.Remove();
                _taxiLocationPin = null;
            }
        }
        private void UpdateTaxiLocation(TaxiLocation value)
        {
            if (_taxiLocationPin != null && value == null)
            {
                RemoveAnnotation(_taxiLocationPin);
                _taxiLocationPin = null;

                return;
            }

            if (value == null)
            {
                return;
            }

            var showOrientedPins = ViewModel.Settings.ShowOrientedPins && value.CompassCourse.HasValue;

            _automatedMapChanged = true;

            // Update Marker and Animate it to see it move on the map
            if (_taxiLocationPin != null && value.Longitude.HasValue && value.Latitude.HasValue)
            {
                var taxiLocationPin = (AddressAnnotation)_taxiLocationPin;

                taxiLocationPin.Degrees = value.CompassCourse ?? 0;

                taxiLocationPin.ShowOrientation = showOrientedPins;

                AnimateAnnotationOnMap(taxiLocationPin, new Position {
                    Latitude = value.Latitude.Value, Longitude = value.Longitude.Value
                });

                return;
            }

            // Create Marker the first time
            var coord = new CLLocationCoordinate2D(0, 0);

            var vehicleLatitude  = value.Latitude ?? 0;
            var vehicleLongitude = value.Longitude ?? 0;

            // ReSharper disable CompareOfFloatsByEqualityOperator
            if (vehicleLatitude != 0 && vehicleLongitude != 0 && value.VehicleNumber.HasValue())
            // ReSharper enable CompareOfFloatsByEqualityOperator
            {
                // Refresh vehicle position
                coord = new CLLocationCoordinate2D(vehicleLatitude, vehicleLongitude);
            }

            _taxiLocationPin = new AddressAnnotation(
                coord,
                AddressAnnotationType.Taxi,
                Localize.GetValue("TaxiMapTitle"),
                value.VehicleNumber,
                _useThemeColorForPickupAndDestinationMapIcons,
                _showAssignedVehicleNumberOnPin,
                null,
                value.Market,
                value.CompassCourse ?? 0,
                showMedallionOnStart: true,
                showOrientation: showOrientedPins);

            AddAnnotation(_taxiLocationPin);
            SetNeedsDisplay();
        }