コード例 #1
0
ファイル: XMLHelper.cs プロジェクト: emitsakis/glosa-mobile
        public static SPAT LoadSPATDataForIntersection(string intersectionId)
        {
            SPAT data = null;

            string file     = $"GreenLight.Core.Test.SPAT-{intersectionId}-WiFi.xml";
            var    assembly = typeof(XMLHelper).GetTypeInfo().Assembly;

            Stream stream = assembly.GetManifestResourceStream(file);

            if (stream != null)
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SPAT));
                    data = serializer.Deserialize(stream) as SPAT;
                }
                catch
                {
                }
            }
            else
            {
                throw new FileNotFoundException("Could not find file", file);
            }

            return(data);
        }
コード例 #2
0
ファイル: GLOSAHelper.cs プロジェクト: emitsakis/glosa-mobile
        public static DateTime SPATMessageTimeStamp(SPAT spat)
        {
            Debug.Assert(spat != null, "Parameter null, SPAT");

            var now           = DateTime.Now;
            var timestamp     = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0);
            var spanInMinutes = TimeSpan.FromMinutes(spat.intersections.IntersectionState.moy);

            timestamp = timestamp.AddMinutes(spanInMinutes.Minutes);
            timestamp = timestamp.AddMilliseconds(spat.intersections.IntersectionState.timeStamp);

            return(timestamp);
        }
コード例 #3
0
ファイル: GLOSAHelper.cs プロジェクト: emitsakis/glosa-mobile
        public static GLOSAResult TimeToTraficLight(MapData map, SPAT spat, List <GPSLocation> gpsHistory, double?deviceHeading, ulong maneuver, int crocsTime)
        {
            GLOSAResult result = new GLOSAResult()
            {
                Errors = GLOSAErrors.NoErrors,
            };

            result = ProjectedLaneForManeuver(map, gpsHistory, deviceHeading, maneuver);

            if (result.Errors == GLOSAErrors.NoErrors)
            {
                var lane        = (MapDataIntersectionsIntersectionGeometryGenericLane)result.Object;
                var description = result.Description;
                result             = ProjectedSignalTimingsForLane(lane, spat, maneuver, crocsTime);
                result.Object      = lane;
                result.Description = description;

                if (result.Errors == GLOSAErrors.NoErrors)
                {
                    var timeToTrafficNode = -1.0;

                    if (result.CurrentStateTimeMovement.MovementEvent == MovementEvent.Green)
                    {
                        timeToTrafficNode = result.CurrentStateTimeMovement.MovementTimespan.TotalSeconds;
                    }
                    else if (result.CurrentStateTimeMovement.MovementEvent == MovementEvent.Red)
                    {
                        //WAIT!
                    }
                    else if (result.CurrentStateTimeMovement.MovementEvent == MovementEvent.Amber)
                    {
                        //TODO
                    }

                    result.TimeToTrafficLight = timeToTrafficNode;
                }
            }
            else
            {
                result.Errors = GLOSAErrors.UnableToFindProjectedLaneForMovement;
            }

            return(result);
        }
コード例 #4
0
        private SPAT GetWiFISPAT()
        {
            SPAT data = null;

            if (_socketService != null && _socketService.Listening == true)
            {
                try
                {
                    if (Settings.EnableIntersectionMode == true)
                    {
                        string s = _socketService.GetData();

                        XmlSerializer serializer = new XmlSerializer(typeof(SPAT));

                        using (TextReader reader = new StringReader(s))
                        {
                            data = serializer.Deserialize(reader) as SPAT;
                        }
                    }
                    else
                    {
                        string s = _socketService.GetData();

                        XmlSerializer serializer = new XmlSerializer(typeof(SPAT));

                        using (TextReader reader = new StringReader(s))
                        {
                            data = serializer.Deserialize(reader) as SPAT;
                        }
                    }
                }
                catch
                {
                }
                finally {
                }
            }

            return(data);
        }
コード例 #5
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);
        }
コード例 #6
0
ファイル: GLOSAHelper.cs プロジェクト: emitsakis/glosa-mobile
        public static SPATIntersectionsIntersectionStateMovementState LocateSignalMovementStateForLane(SPAT spat, MapDataIntersectionsIntersectionGeometryGenericLane lane, ulong maneuver)
        {
            SPATIntersectionsIntersectionStateMovementState movementState = null;

            if (lane.connectsTo != null)
            {
                var laneConnection = lane.connectsTo.Where(connection => connection.connectingLane.maneuver == maneuver);
                if (laneConnection.Count() > 0)
                {
                    byte signalGroup = laneConnection.First().signalGroup;
                    movementState = spat.intersections.IntersectionState.states.Where(state => state.signalGroup == signalGroup).FirstOrDefault();
                }
            }

            return(movementState);
        }
コード例 #7
0
ファイル: GLOSAHelper.cs プロジェクト: emitsakis/glosa-mobile
        public static GLOSAResult ProjectedSignalTimingsForLane(MapDataIntersectionsIntersectionGeometryGenericLane lane, SPAT spat, ulong maneuver, int crocsTime)
        {
            GLOSAResult crocsResult = new GLOSAResult();

            SPATIntersectionsIntersectionStateMovementState state = LocateSignalMovementStateForLane(spat, lane, maneuver);

            if (state != null)
            {
                List <StateTimeMovementEvent> stateTimeMovementEvents = ProjectedMovementEventStates(state, crocsTime);
                crocsResult.StateTimeMovementEvents = stateTimeMovementEvents;

                if (IsTimeWithinCurrentMovementSequence(crocsTime, stateTimeMovementEvents) == true)
                {
                    crocsResult.CurrentStateTimeMovement = FindNextMovementEvent(crocsTime, stateTimeMovementEvents);
                }
                else
                {
                    crocsResult.Errors = GLOSAErrors.UnableToProjectMovementStates;
                }
            }
            else
            {
                crocsResult.Errors = GLOSAErrors.UnableToFindProjectedStateForLane;
            }

            return(crocsResult);
        }
コード例 #8
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;
            }
        }