WienerLinien.Api.Realtime.EchtzeitdatenSchnittstelle.ParseMonitorResponse C# (CSharp) Method

ParseMonitorResponse() public method

public ParseMonitorResponse ( string jsonResponse ) : MonitorInformation
jsonResponse string
return MonitorInformation
        public MonitorInformation ParseMonitorResponse(string jsonResponse)
        {
            var rootObj = JsonConvert.DeserializeObject<MP.RootObject>(jsonResponse);

            if (null == rootObj)
            {
                return new MonitorInformation(MonitorInformationErrorCode.ResponseParsingFailed);
            }

            // Check response error codes first (add to ErrorCode Collection with same # as in actual return values
            if (null == rootObj.data)
            {
                if (null != rootObj.message)
                {
                    var errCode = MonitorInformationErrorCode.ResponseParsingFailed;
                    bool parseOk = Enum.TryParse<MonitorInformationErrorCode>(rootObj.message.messageCode.ToString(), out errCode);

                    return new MonitorInformation(errCode);
                }
                else
                {
                    return new MonitorInformation(MonitorInformationErrorCode.ResponseParsingFailed);
                }
            }

            // Is there anything at all?
            if (!rootObj.data.monitors.Any())
            {
                return new MonitorInformation(MonitorInformationErrorCode.MonitorsEmpty);
            }

            // Parse monitor information

            var parsedMonitorLines = new List<MonitorLine>();

            foreach (var monitor in rootObj.data.monitors)
            {
                foreach (var ml in monitor.lines)
                {
                    var line = new MonitorLine()
                    {
                        Name = ml.name,
                        Towards = ml.towards,
                        Type = MonitorLineTypeMapper.TypeStringToType(ml.type),
                        RealtimeSupported = ml.realtimeSupported,
                        BarrierFree = ml.barrierFree,
                        Departures = new List<Api.Departure>()
                    };

                    if (null != ml.departures && null != ml.departures.departure)
                    {
                        foreach (var departure in ml.departures.departure)
                        {
                            var dt = departure.departureTime;

                            // Empty departureTime object show all values as null or 0
                            if (dt.timePlanned == null && dt.timeReal == null && dt.countdown == 0)
                            {
                                continue;
                            }

                            var timePlanned = ToLocalTime(dt.timePlanned);
                            var timeReal = ToLocalTime(dt.timeReal);

                            var dep = new Api.Departure()
                            {
                                Name = line.Name,
                                Towards = line.Towards,
                                Type = line.Type,
                                RealtimeSupported = line.RealtimeSupported,
                                BarrierFree = line.BarrierFree,
                                Countdown = dt.countdown,
                                TimeReal = timeReal,
                                TimePlanned = timePlanned
                            };

                            // Override line defaults for this departure if necessary
                            if (null != departure.vehicle)
                            {
                                dep.OverridesLineInformation = true;
                                dep.Name = departure.vehicle.name;
                                dep.Towards = departure.vehicle.towards;
                                dep.Type = MonitorLineTypeMapper.TypeStringToType(departure.vehicle.type);
                                dep.RealtimeSupported = departure.vehicle.realtimeSupported;
                                dep.BarrierFree = departure.vehicle.barrierFree;
                            }

                            line.Departures.Add(dep);
                        }
                    }

                    // Do not add empty lines (NICHT EINSTEIGEN, ALLE ZÜGE GLEIS 1, &c)
                    if (line.Departures.Any())
                    {
                        parsedMonitorLines.Add(line);
                    }
                }
            }

            var orderForReturn = parsedMonitorLines.OrderBy(moli => moli.Type).ThenBy(moli => moli.Name).ThenBy(moli => moli.Towards);
            return new MonitorInformation(orderForReturn.ToList());
        }

Usage Example

        public void EmptyResponseTest()
        {
            var schnittstelle = new EchtzeitdatenSchnittstelle();
            MonitorInformation result = schnittstelle.ParseMonitorResponse(ResponseFiles.LoadJson(ResponseFiles.EmptyOkResponse));

            Assert.That(result.ErrorCode, Is.EqualTo(MonitorInformationErrorCode.MonitorsEmpty));
        }
All Usage Examples Of WienerLinien.Api.Realtime.EchtzeitdatenSchnittstelle::ParseMonitorResponse