Ejemplo n.º 1
0
        /// <summary>
        /// Main method to return the full XML answer
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="locationEPC"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public XElement XmlAnswer(DateTime start, DateTime end, String locationEPC, string location, string user, bool authenticated, bool inputError)
        {
            // Fetches all the events and the specified location and between the specified timespan
            IEnumerable <XElement> allEvents = epcisEventClient.GetEvents(start, end, locationEPC);

            XElement objectEventList = new XElement("Events");

            foreach (var a in allEvents)
            {
                foreach (var b in a.Descendants("ObjectEvent"))
                {
                    objectEventList.Add(b);
                }
            }
            // The variable that will hold the final Xml to be returned to the client
            XElement xmlAnswer;

            // Handles the three cases specified in the assignment
            if (allEvents == null || allEvents.Descendants("ObjectEvent").Any() == false || inputError == true)
            {
                xmlAnswer = new XElement("XMLSvar",
                                         new XElement("TjänsteMeddelande",
                                                      new XElement("ResponseInformation",
                                                                   new XElement("responseCode", 2),
                                                                   new XElement("Message", "Kunde inte genomföra operationen"),
                                                                   new XElement("Reciever", "Grupp 14"),
                                                                   new XElement("ApplicationInformation", "Fordonservice 1.0"),
                                                                   new XElement("TimeOfRequest", DateTime.Now)),
                                                      new XElement("RequestInformation",
                                                                   new XElement("reciever", user),
                                                                   new XElement("RequestArguments",
                                                                                new XElement("fromTime", start),
                                                                                new XElement("toTime", end),
                                                                                new XElement("location", location)))));
            }
            // User not authenticated
            else if (authenticated == false)
            {
                xmlAnswer = new XElement("XMLSvar",
                                         new XElement("TjänsteMeddelande",
                                                      new XElement("ResponseInformation",
                                                                   new XElement("responseCode", 3),
                                                                   new XElement("Message", "Autentisering misslyckades"),
                                                                   new XElement("Reciever", "Grupp 14"),
                                                                   new XElement("ApplicationInformation", "Fordonservice 1.0"),
                                                                   new XElement("TimeOfRequest", DateTime.Now)),
                                                      new XElement("RequestInformation",
                                                                   new XElement("reciever", user),
                                                                   new XElement("RequestArguments",
                                                                                new XElement("fromTime", start),
                                                                                new XElement("toTime", end),
                                                                                new XElement("location", location)))));
            }
            else
            {
                xmlAnswer = new XElement("XmlSvar");

                // IEnumerable<XElement> vehicleInformation;

                // For each event identified, build a FordonPassage and add it to the xmlSvar
                foreach (var epcisEvent in objectEventList.Descendants("ObjectEvent"))
                {
                    XElement svar = FordonPassageConstruct(epcisEvent, location);
                    xmlAnswer.Add(svar);
                }

                XElement xmlServiceMessage = new XElement("TjänsteMeddelande",
                                                          new XElement("ResponseInformation",
                                                                       new XElement("responseCode", 1),
                                                                       new XElement("Message", "Anropet gick bra"),
                                                                       new XElement("Reciever", "Grupp 14"),
                                                                       new XElement("ApplicationInformation", "Fordonservice 1.0"),
                                                                       new XElement("TimeOfRequest", DateTime.Now)),
                                                          new XElement("RequestInformation",
                                                                       new XElement("reciever", user),
                                                                       new XElement("RequestArguments",
                                                                                    new XElement("fromTime", start),
                                                                                    new XElement("toTime", end),
                                                                                    new XElement("location", location))));

                // Adds the service message to the end of the xmlSvar
                xmlAnswer.Add(xmlServiceMessage);
            }
            return(xmlAnswer);
        }
        /// <summary>
        /// End point to get FordonPasssage
        /// </summary>
        /// <param name="request">XElement containing Start, End and location EPC</param>
        /// <returns>Fordonspassage Element</returns>
        public XElement GetFordonPassage(XElement request)
        {
            //Instansiera klinter för services
            EpcisEventServiceClient eventClient = new EpcisEventServiceClient();
            NamingServiceClient namingClient = new NamingServiceClient();

            //Kolla så att requestens element är valida
            XElement response = GetResponseInfoAndCheckRequest(request);
            if(response.Element("Svarsinformation").Element("Svarskod").Value != "OK")
            {
                return response;
            }

            //Inputvariabler definieras
            DateTime start = (DateTime)request.Element("Start");
            DateTime end = (DateTime)request.Element("End");
            string epc = (string)request.Element("EPC");
            int sida;
            int resultatPerSida;

            if(request.Elements("Sida").Any())
            {
                sida = (int)request.Element("Sida");
            }
            else
            {
                sida = 1;
            }

            if(request.Elements("ResultatPerSida").Any())
            {
                resultatPerSida = (int)request.Element("ResultatPerSida");
            }
            else
            {
                resultatPerSida = 10;
            }

            //Hämta events
            XNamespace ns = "urn:epcglobal:epcis:xsd:1";
            IEnumerable<XElement> result = eventClient.GetEvents(start, end, epc);
            eventClient.Close();
            List<XElement> objEvents = new List<XElement>();

            //Hämta ut OjbectEventelement
            foreach (XElement epcisdoc in result)
            {
                objEvents.AddRange(epcisdoc.Elements("EPCISBody").Elements("EventList").Elements("ObjectEvent"));
            }

            //Hämta antalet träffar
            int hits = objEvents.Count();

            //Skip och take för pagination
            int skip = (sida - 1) * resultatPerSida;
            objEvents = objEvents.Skip(skip).Take(resultatPerSida).ToList();

            XElement fordonsPassager = new XElement("FordonsPassager");
            //int antalSidor = (int)Math.Ceiling((decimal)((int)hits / resultatPerSida));
            int antalSidor;
            if(hits % resultatPerSida == 0)
            {
                antalSidor = hits / resultatPerSida;
            }
            else
            {
                antalSidor = (int)Math.Floor((double)hits / (double)resultatPerSida) + 1;
            }
            //Hämta alla locations, går snabbare än att hämta varje
               // XElement locations = namingClient.GetAllLocations().Element("Locations");
            XElement location = GetLocationFromEpc(epc);
            namingClient.Close();

            //Iterera igenom events för att populera resposne
            foreach(XElement objEvent in objEvents)
            {
                //Hämta location data
                //XElement location = locations.Elements("Location").Where(l => l.Element("Epc").Value == (objEvent.Element("readPoint").Element("id").Value)).First();

                //Skapa fordonspassageelementet
                fordonsPassager.Add(new XElement("FordonsPassage",
                                GetVehicleFromEpc(objEvent.Element("epcList").Element("epc").Value),
                                location,
                                new XElement("Tid", objEvent.Element("eventTime").Value)
                        ));
            }

            //Lägg till fordonspassage i responseobjektet
            response.Add(fordonsPassager);
            response.Element("Svarsinformation").Add(
                    new XElement("Sida", sida),
                    new XElement("Träffar", hits),
                    new XElement("AntalSidor", antalSidor),
                    new XElement("ResultatPerSida", resultatPerSida)
                );

            return response;
        }
        public IEnumerable <XElement> HämtaEvents(DateTime fromInclusive, DateTime toInclusive, String readPointEPC)
        {
            IEnumerable <XElement> allaEvent = client.GetEvents(fromInclusive, toInclusive, readPointEPC);

            return(allaEvent);
        }