public void CheckValidGetDeliverMidasTrafficDataResponseTest()
        {
            IMidasTrafficDataService midasTrafficDataService = new MidasTrafficDataService();
            DeliverMIDASTrafficDataRequest deliverMIDASTrafficDataRequest = new DeliverMIDASTrafficDataRequest();
            deliverMIDASTrafficDataRequest.d2LogicalModel = model;
            string expected = "DeliverMIDASTrafficDataRequest: Successful Delivery";
            string actual;
            actual = (midasTrafficDataService.GetDeliverMidasTrafficDataResponse(deliverMIDASTrafficDataRequest)).status;

            Assert.AreEqual(expected, actual);
        }
        public void CheckErrorInGetDeliverMidasTrafficDataResponseTest()
        {
            IMidasTrafficDataService midasTrafficDataService = new MidasTrafficDataService();
            DeliverMIDASTrafficDataRequest deliverMIDASTrafficDataRequest = new DeliverMIDASTrafficDataRequest();

            model = null; // This will be checked by ExampleDataCheckOk(d2LogicalModel)

            deliverMIDASTrafficDataRequest.d2LogicalModel = model;
            string expected = "DeliverMIDASTrafficDataRequest: Successful Delivery";
            string actual;
            // This should cause a SoapException
            actual = (midasTrafficDataService.GetDeliverMidasTrafficDataResponse(deliverMIDASTrafficDataRequest)).status;
            Assert.AreEqual(expected, actual);
        }
        public DeliverMIDASTrafficDataResponse GetDeliverMidasTrafficDataResponse(DeliverMIDASTrafficDataRequest deliverMIDASTrafficDataRequest)
        {
            log.Info("New DeliverMIDASTrafficDataRequest received.");
            D2LogicalModel d2LogicalModel = deliverMIDASTrafficDataRequest.d2LogicalModel;
            MeasuredDataPublication measuredDataPublication = null;
            DeliverMIDASTrafficDataResponse response = new DeliverMIDASTrafficDataResponse();

            // Validate the D2Logical Model
            if (!ExampleDataCheckOk(d2LogicalModel))
            {
                throw new SoapException("Incoming request does not appear to be valid!", SoapException.ClientFaultCode);
            }
            
            // MeasuredDataPublication class contains the feed description, feed
            // type, site measurements, publication time and other header information.
            try
            {
                measuredDataPublication = d2LogicalModel.payloadPublication as MeasuredDataPublication;

                if (measuredDataPublication != null && measuredDataPublication.headerInformation != null)
                {

                    // Eash MIDAS site is encapsulated within a SiteMeasurements object.
                    // Cycle through these to get to the sensor readings for a MIDAS site.
                    foreach (SiteMeasurements siteMeasurement in measuredDataPublication.siteMeasurements)
                    {
                        log.Debug("measurementDataPublication ID is " + siteMeasurement.measurementSiteReference.id);
                        log.Debug("measurementDataPublication time default is " + siteMeasurement.measurementTimeDefault.ToString());

                        // Cycle through the MeasuredValues to get the individual sensor readings for a MIDAS site.
                        foreach (_SiteMeasurementsIndexMeasuredValue singleMeasuredValue in siteMeasurement.measuredValue)
                        {
                            int index = singleMeasuredValue.index;

                            // Each sensor reading has an index. This represents the lane number the sensor reading is for.
                            // On retrieving the index, you can use getLaneNumberFromTrafficDataIndex to get the lane number.
                            int laneNumber = GetLaneNumberFromTrafficDataIndex(index);
                            log.Debug("lane number is " + laneNumber);

                            // To determine what type the sensore reading is, cast the basic data value to the appropriate
                            // type and retrieve the value of interest.
                            MeasuredValue mv = singleMeasuredValue.measuredValue;
                            BasicData basicData = mv.basicData;

                            if (basicData is TrafficFlow)
                            {
                                // For a lane, TrafficFlow will appear 4 times. i.e.
                                // flow1, flow2 ... flow4. It will appear in order.
                            }
                            else if (basicData is TrafficSpeed)
                            {
                                // Now you have TrafficSpeed. Cast it appropriately
                                // to retrieve values you are interested in.
                            }
                            else if (basicData is TrafficHeadway)
                            {
                                // Now you have TrafficHeadway. Cast it appropriately
                                // to retrieve values you are interested in.
                            }
                            else if (basicData is TrafficConcentration)
                            {
                                // Now you have TrafficConcentration. Cast it 
                                // appropriately to retrieve values you are
                                // interested in.
                            }

                        }
                    }

                    // You can convert the site measurements to you model objects
                    // and subsequently persist/manipulate your model objects.
                    List<TrafficData> trafficData = ConvertToModelObjects(measuredDataPublication.siteMeasurements.ToList());
                }
            }
            catch (Exception e)
            {
                log.Error("Error while obtaining MeasuredDataPublication.");
                log.Error(e.Message);
                throw new SoapException("Error while obtaining MeasuredDataPublication.", SoapException.ServerFaultCode, e);
            }
                        
            response.status = "DeliverMIDASTrafficDataRequest: Successful Delivery";

            return response;
        }