Example #1
0
        private void LogDataEvent(CalculationResult calculationResult, GLOSAResult glosaResult, double latency, double currentTimeCROCS, DataConnection dataConnection)
        {
            // Log event to server
            var lane          = (MapDataIntersectionsIntersectionGeometryGenericLane)glosaResult.Object;
            var advisoryValue = $"{calculationResult.AdvisorySpeed.ToString()} {calculationResult.Errors.ToString()}";
            var spatValue     = glosaResult.CurrentStateTimeMovement.Discription;

            var detailedMessage = $"Device CROCS Time - { currentTimeCROCS} : Lane Id - { lane.laneID} : {spatValue}";

            Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : Advisory Calculation - {advisoryValue} : {detailedMessage}");

            if (_loggingEnabled == false)
            {
                return;
            }

            LogDataEvent(null, null, advisoryValue, null, detailedMessage, latency, Convert.ToInt16(_navigationService.DeviceHeading), glosaResult.Description, dataConnection);
        }
Example #2
0
 private void PostVehicleMessage(CalculationResult calculationResult, GLOSAResult glosaResult)
 {
     if (VehicleEventHandler != null)
     {
         VehicleEventHandler.Invoke(this, new VehicleServiceEventArgs()
         {
             Status                  = VehicleServiceStatus.Ok,
             CalculationResult       = calculationResult,
             GLOSAResult             = glosaResult,
             Longitude               = _navigationService.Longitude,
             Latitude                = _navigationService.Latitude,
             CurrentSpeedMPH         = _navigationService.CurrentSpeed,
             IntersectionDescription = _navigationService.WaypointDescription,
             IntersectionId          = _navigationService.WayPointId,
             DistanceToIntersection  = _navigationService.DistanceToWaypoint,
             IsWiFiSPATData          = _isUsingWiFiSPATData,
         });
     }
 }
Example #3
0
        public void IsVehicleHeadingInSameDirectionAsLaneDirection_WithLaneInSameDirection_ShouldReturnTrue()
        {
            // arrange
            //SETTINGS_ROUTE_DIRECTION_NS = 1: - Not Supported Yet
            //SETTINGS_ROUTE_DIRECTION_SN = 2: - Not Supported Yet
            //SETTINGS_ROUTE_DIRECTION_EW = 3;
            //SETTINGS_ROUTE_DIRECTION_WE = 4;
            var intersectionId = "2111";
            var gpsHistory     = KMLHelper.GLOSATestRouteIntersectionHistory(intersectionId, 2);

            // Load Map from file
            string      file   = $"MAP-{Settings.IntersectionId}.xml";
            MapData     map    = XMLHelper.LoadMAPDataFromFile(file);
            GLOSAResult result = GLOSAHelper.ProjectedLaneForManeuver(map, gpsHistory, 0, Constants.MANEUVER_DIRECTION_AHEAD);
            var         lane   = (MapDataIntersectionsIntersectionGeometryGenericLane)result.Object;
            var         nodes  = GLOSAHelper.ExtractTrafficNodesFromLane(lane);

            // Let's sort all lane nodes from MAP Ref Point
            var refPoint    = map.intersections.IntersectionGeometry.refPoint;
            var mapLocation = new GPSLocation()
            {
                Latitude  = refPoint.lat / Constants.MAPCoordinateIntConverterUnit,
                Longitude = refPoint.@long / Constants.MAPCoordinateIntConverterUnit,
            };

            // Sort the nodes by distance ascending
            var sortedNodes = nodes.OrderBy(node => Distance.CalculateDistanceBetween2PointsKMs(node.GPSLocation.Latitude, node.GPSLocation.Longitude, mapLocation.Latitude, mapLocation.Longitude)).ToList();

            bool expectedResult = true;

            //act
            result = GLOSAHelper.IsDirectionOfVehicleInSameDirectionAsLane(map.intersections.IntersectionGeometry.id.id, sortedNodes, 0, gpsHistory, 50, Constants.MANEUVER_DIRECTION_AHEAD);
            bool actualResult = result.Errors == GLOSAErrors.NoErrors;

            //assert
            Assert.Equal(expectedResult, actualResult);
        }
Example #4
0
        private bool TimerServiceCallback()
        {
            bool @continue = true;

            if (TimerHasFinished())
            {
                @continue = false;
            }
            else
            {
                var before = DateTime.Now;

                if (Settings.EnableIntersectionMode == false && CheckLocationServicesPermission() == false)
                {
                    PostVehicleMessage(VehicleServiceStatus.GPSPermissionError);
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : GPS not enabled");
                    LogDataEvent("GPS not enabled (Permissions)");
                    return(@continue);
                }

                if (Settings.EnableIntersectionMode == false && CheckLocationServices() == false)
                {
                    PostVehicleMessage(VehicleServiceStatus.GPSNotAvailable);
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : Waiting for GPS");
                    LogDataEvent("Waiting for GPS");
                    return(@continue);
                }

                if (Settings.EnableIntersectionMode == false && string.IsNullOrEmpty(Settings.UniqueVehicleDeviceAppId.Trim()) == true)
                {
                    PostVehicleMessage(VehicleServiceStatus.VehicleIdNotSet);
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : Vehicle Id missing");
                    LogDataEvent("Vehicle Id missing");
                    return(@continue);
                }

                if (CheckNetworkStatus() == false)
                {
                    PostVehicleMessage(VehicleServiceStatus.NetworkConnectionError);
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : Waiting for connection (WiFi Mode: {Settings.EnableWiFiMode})");
                    LogDataEvent("No Network Connection");
                    return(@continue);
                }

                Task.Run(() => SyncTime());

                if (_navigationService.IsNavigating == false || _navigationService.IsNavigatingToWaypoint == false || _navigationService.Waypoint == null)
                {
                    PostVehicleMessage(null, null);
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : Locating intersection");
                    LogDataEvent("Locating intersection");
                    return(@continue);
                }

                // GET MAP SPAT data of intersection
                var nextWaypoint   = _navigationService.LocateWaypointWithLineOfSight(WaypointDetectionMethod.GPSHistoryDirection, 1, 50);
                var nexyWaypointId = nextWaypoint != null?nextWaypoint.intersections.IntersectionGeometry.id.id.ToString() : null;

                Task.Run(async() => await _GLOSAWebService.SyncMAPSPATAsync(_navigationService.WayPointId, nexyWaypointId));

                if (HasMapSPATDataFromCellular() == false && HasMapSPATDataFromWiFi() == false)
                {
                    PostVehicleMessage(null, null);
                    LogDataEvent($"Waiting for data");
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : Waiting for data {_navigationService.Waypoint.name}");
                    return(@continue);
                }

                MapData map            = null;
                SPAT    spat           = null;
                var     dataConnection = DataConnection.Cellular;

                if (Settings.EnableWiFiMode == true && HasMapSPATDataFromWiFi() == true)
                {
                    _isUsingWiFiSPATData = true;
                    map            = GetWiFIMAP();
                    spat           = GetWiFISPAT();
                    dataConnection = DataConnection.WiFi_Beacon;
                }

                // revert to celluar data
                if (spat == null && HasMapSPATDataFromCellular() == true)
                {
                    _isUsingWiFiSPATData = false;
                    spat           = _GLOSAWebService.SPATData(_navigationService.WayPointId);
                    dataConnection = DataConnection.Cellular;
                }

                // revert to celluar data
                if (map == null && HasMapSPATDataFromCellular() == true)
                {
                    map = _GLOSAWebService.MAPData(_navigationService.WayPointId);
                }

                var history = _navigationService.GPSHistory;

                DateTime date             = CurrentTime();
                int      currentTimeCROCS = GLOSAHelper.ConvertTimeToCROCSTime(date);

                GLOSAResult glosaResult = GLOSAHelper.TimeToTraficLight(map, spat, history, _navigationService.DeviceHeading, _allowedVehicleManeuvers, currentTimeCROCS);

                if (glosaResult.Errors == GLOSAErrors.NoErrors)
                {
                    CalculationResult calculation = AdvisorySpeedCalculationResult.CalculateAdvisorySpeed(_navigationService.DistanceToWaypoint, glosaResult.TimeToTrafficLight, _navigationService.CurrentSpeed, _advisoryCalculatorMode);
                    PostVehicleMessage(calculation, glosaResult);

                    var    after   = DateTime.Now;
                    double latency = (after - before).TotalMilliseconds;
                    LogDataEvent(calculation, glosaResult, latency, currentTimeCROCS, dataConnection);
                }
                else
                {
                    PostVehicleMessage(null, glosaResult);
                    LogDataEvent("GLOSA Result", null, null, $"{map.intersections.IntersectionGeometry.id.id}", null, 0, Convert.ToInt16(_navigationService.DeviceHeading), glosaResult.Description);
                    Debug.WriteLine($"Vehicle Service Timer {DateTime.Now} : GLOSA Error - {glosaResult.Errors}");
                }
            }

            return(@continue);
        }
Example #5
0
        private async Task SPATRequest(string intersectionId)
        {
            // Should request every 10 seconds
            if (ShouldSyncSPATData(intersectionId) == false)
            {
                return;
            }

            if (_spatRequestActive == true)
            {
                return;
            }

            if (SPATRequestErrors.ContainsKey(intersectionId) && SPATRequestErrors[intersectionId] > 5)
            {
                return;
            }

            var before = DateTime.Now;

            _spatRequestActive = true;
            var    requestMethod = "GET";
            var    URL           = $"{Constants.API_GLOSA_SPAT_ENDPPOINT_URL}&param={intersectionId}";
            int    statusCode    = 0;
            string value         = null;

            try
            {
                Envelope envelope = await GLOSACommunicateAsync <Envelope>(intersectionId, Constants.API_GLOSA_SPAT_ENDPPOINT_URL);

                SPAT data = envelope.Body.SPAT;
                if (data != null)
                {
                    if (SPATDataStore.ContainsKey(intersectionId))
                    {
                        SPATDataStore[intersectionId] = data;
                    }
                    else
                    {
                        SPATDataStore.Add(intersectionId, data);
                    }

                    UpdateSPATSyncTime(intersectionId);
                }
                statusCode = 200;
            }
            catch (Exception e)
            {
                if (SPATRequestErrors.ContainsKey(intersectionId))
                {
                    SPATRequestErrors[intersectionId]++;
                }
                else
                {
                    SPATRequestErrors.Add(intersectionId, 1);
                }

                var result = new GLOSAResult();
                result.Errors = GLOSAErrors.WebServiceError;
                if (e.GetType() == typeof(XmlException))
                {
                    result.Errors = GLOSAErrors.WebServiceXMLParsingError;
                }
                else
                {
                    statusCode = 500;
                    value      = e.Message;
                }
            }
            finally
            {
                if (statusCode > 0)
                {
                    var    after   = DateTime.Now;
                    double latency = (after - before).TotalMilliseconds;

                    var log = new GLOSAMonitoringLog()
                    {
                        URL        = URL,
                        StatusCode = statusCode,
                        Method     = requestMethod,
                        Latency    = latency,
                        Value      = value,
                    };

                    if (_dataAnalyticsService != null)
                    {
                        Logger.LogMonitoring(_dataAnalyticsService, log);
                    }
                }
                _spatRequestActive = false;
            }
        }
Example #6
0
        private async Task MAPRequest(string intersectionId)
        {
            if (_mapRequestActive == true)
            {
                return;
            }

            if (MAPRequestErrors.ContainsKey(intersectionId) && MAPRequestErrors[intersectionId] > 5)
            {
                return;
            }

            _mapRequestActive = true;
            var    before        = DateTime.Now;
            var    requestMethod = "GET";
            var    URL           = $"{Constants.API_GLOSA_MAP_ENDPPOINT_URL}&param={intersectionId}";
            int    statusCode    = 0;
            string value         = null;

            try
            {
                var map = MAPDataStore.ContainsKey(intersectionId) ? MAPDataStore[intersectionId] : null;
                if (map == null)
                {
                    Envelope envelope = await GLOSACommunicateAsync <Envelope>(intersectionId, Constants.API_GLOSA_MAP_ENDPPOINT_URL);

                    MapData data = envelope.Body.MapData;
                    statusCode = 200;
                    if (data != null)
                    {
                        if (MAPDataStore.ContainsKey(intersectionId))
                        {
                            MAPDataStore[intersectionId] = data;
                        }
                        else
                        {
                            MAPDataStore.Add(intersectionId, data);
                        }

                        MAPDataLastSyncTime = DateTime.Now;
                    }
                }
            }
            catch (Exception e)
            {
                if (MAPRequestErrors.ContainsKey(intersectionId))
                {
                    MAPRequestErrors[intersectionId]++;
                }
                else
                {
                    MAPRequestErrors.Add(intersectionId, 1);
                }

                var result = new GLOSAResult();
                result.Errors = GLOSAErrors.WebServiceError;
                if (e.GetType() == typeof(XmlException))
                {
                    result.Errors = GLOSAErrors.WebServiceXMLParsingError;
                }
                else
                {
                    statusCode = 500;
                    value      = e.Message;
                }
            }
            finally
            {
                if (statusCode > 0)
                {
                    var    after   = DateTime.Now;
                    double latency = (after - before).TotalMilliseconds;

                    var log = new GLOSAMonitoringLog()
                    {
                        URL        = URL,
                        StatusCode = statusCode,
                        Method     = requestMethod,
                        Latency    = latency,
                        Value      = value,
                    };

                    if (_dataAnalyticsService != null)
                    {
                        Logger.LogMonitoring(_dataAnalyticsService, log);
                    }
                }

                _mapRequestActive = false;
            }
        }