Example #1
0
        /// <summary>
        /// Initializes a new instance from the specified <paramref name="model"/>.
        /// </summary>
        /// <param name="model">The instance of <see cref="OpeningHoursModel"/> representing the model.</param>
        /// <param name="timeZone">The time zone the new model should be based on.</param>
        public OpeningHoursOffsetModel(OpeningHoursModel model, TimeZoneInfo timeZone)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            TimeZone = timeZone;
            Model    = model;
        }
        public static OpeningHoursWeekdayItem TodaysOpeningHours(this OpeningHoursModel model, DateTime today)
        {
            if (model == null)
            {
                return(null);
            }

            // Find any holidays with todays date
            if (model.Holidays != null)
            {
                OpeningHoursHolidayItem holiday = model.Holidays.FirstOrDefault(x => x.Date.Date == today.Date);
                if (holiday != null)
                {
                    return(holiday);
                }
            }

            // No holidays, so check general weekday opening hours
            return(model.Weekdays != null && model.Weekdays.ContainsKey(today.DayOfWeek) ? model.Weekdays[today.DayOfWeek] : null);
        }
        public static int ClosesInHowManyMinutes(this OpeningHoursModel model, DateTime?now = null)
        {
            throw new NotImplementedException();

            //if (model == null) return -1; // Default to store closed

            //if (!now.HasValue)
            //{
            //    now = DateTime.Now;
            //}

            //// Check to see if store is open today at all
            //var todaysOpeningHours = model.TodaysOpeningHours(now);
            //if (todaysOpeningHours == null || !todaysOpeningHours.IsOpen)
            //{
            //    return -1; // store closed
            //}

            //// Check to see if store is 24 hour
            //if (todaysOpeningHours.IsOpen24Hours())
            //{
            //    return 1440; // 24 hours
            //}

            //// Check to see if store has opened yet
            //var timeOfDay = now.Value.TimeOfDay;
            //if (timeOfDay < todaysOpeningHours.Opens)
            //{
            //    return -2; // store not opened yet
            //}

            //// Check to see when store closes
            //var midnight = new TimeSpan(0, 0, 0);
            //var closingTime = todaysOpeningHours.Closes;
            //if(closingTime == midnight) closingTime = new TimeSpan(23,59,59);

            //var closesIn = (int)(closingTime - timeOfDay).TotalMinutes;
            //return closesIn > 0 ? closesIn : -3;
        }
 public static OpeningHoursWeekdayItem TodaysOpeningHours(this OpeningHoursModel model)
 {
     return(TodaysOpeningHours(model, DateTime.Now));
 }
        public static OpeningHoursOffsetModel GetOpeningHours(this IPublishedContent content, string propertyAlias, TimeZoneInfo timeZone)
        {
            OpeningHoursModel model = GetOpeningHours(content, propertyAlias);

            return(new OpeningHoursOffsetModel(model, timeZone));
        }
        /// <summary>
        /// Gets opening hours from the property with the specified <code>propertyAlias</code>. If the property value
        /// doesn't match a valid instance of <see cref="OpeningHoursModel"/>, an instance with empty values will be
        /// returned instead.
        /// </summary>
        /// <param name="content">The instance of <see cref="IPublishedContent"/> holding the opening hours.</param>
        /// <param name="propertyAlias">The alias of the property.</param>
        /// <returns>Returns an instance of <see cref="OpeningHoursModel"/>.</returns>
        public static OpeningHoursModel GetOpeningHours(this IPublishedContent content, string propertyAlias)
        {
            OpeningHoursModel openingHours = content == null ? null : content.GetPropertyValue <OpeningHoursModel>(propertyAlias);

            return(openingHours ?? new OpeningHoursModel());
        }
 public PlaceModel()
 {
     OpeningHours = new OpeningHoursModel();
     Geometry = new GeometryModel();
 }
Example #8
0
        public dynamic Search(float lat = -1, float lon = -1, int hours = 1, double speed = 0.0)
        {
            Response.Headers.Add("Access-Control-Allow-Origin", new string[] { "*" });

            // constant parameters
            int    TAKE           = 5;
            double PARKING_RADIUS = 5.0;
            double SLOW_SPEED     = 3.0;

            // validity check: are lat and lon specified?
            if (lat < 0 || lon < 0)
            {
                Response.StatusCode = 400;

                Dictionary <string, string> err = new Dictionary <string, string>();
                err.Add("error", "either 'lat' or 'lon' not defined as parameters");

                return(err);
            }

            Console.WriteLine("----------------");

            // create coordinate model form user given parameter
            CoordinateModel currentPosition = new CoordinateModel();

            currentPosition.Latitude  = lat;
            currentPosition.Longitude = lon;

            // use connection to mongodb
            var server            = StaticGlobal.MongoDBClient.GetServer();
            var database          = server.GetDatabase("parkeasy");
            var collectionParking = database.GetCollection <ParkingModel>("parking");
            var collectionStatus  = database.GetCollection <StatusModel>("status");

            // load all parking options from mongodb
            List <ParkingModel> parkingModels = new List <ParkingModel>();

            StringBuilder osrmTableQueryBuilder = new StringBuilder("http://router.project-osrm.org/table?");

            osrmTableQueryBuilder.AppendFormat("loc={0},{1}&", Math.Round(lat, 5), Math.Round(lon, 5));

            // fetch all parking options sorted by nearest
            var query = Query.Near("Coordinates", currentPosition.Longitude, currentPosition.Latitude, 1.5 / 111.12);

            foreach (ParkingModel model in collectionParking.Find(query).SetLimit(100))
            {
                model.CalcFreeLikelihood();

                // calculate the distance to the user and add to working list
                //model.DistanceToUser = model.Coordinate.DistanceTo(currentPosition);
                osrmTableQueryBuilder.AppendFormat("loc={0},{1}&", Math.Round(model.Coordinate.Latitude, 5), Math.Round(model.Coordinate.Longitude, 5));

                parkingModels.Add(model);
            }

            String osrmTableQuery = osrmTableQueryBuilder.ToString();

            osrmTableQuery = osrmTableQuery.Remove(osrmTableQuery.Length - 1);

            // query osrm for distance table
            using (var client = new System.Net.WebClient())
            {
                client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
                client.Headers.Add("Accept", "*/*");

                String  body  = client.DownloadString(osrmTableQuery);
                dynamic table = JsonConvert.DeserializeObject(body);

                int i = 0;
                foreach (int distance in table.distance_table[0])
                {
                    if (i == 0)
                    {
                        i++;
                        continue;
                    }

                    Console.WriteLine(distance);
                    parkingModels.ElementAt(i - 1).DistanceToUser = Convert.ToDouble(distance);
                    i++;
                }
            }

            Console.WriteLine("{0} parking options", parkingModels.Count);

            // filter places that have maximum parking hours lower than needed or
            // opening hours that exceed the amount of time the user wants to park
            parkingModels = parkingModels.Where(delegate(ParkingModel a)
            {
                if (a.FreeLikelihood == 0.0 || a.Price == null)
                {
                    return(false);
                }

                // are there limitations on the amount of parking hours?
                if (a.MaximumParkingHours.HasValue)
                {
                    // does this exceed the amount of hours the user wants to park?
                    if (a.MaximumParkingHours.Value > 0.0 && a.MaximumParkingHours.Value > hours)
                    {
                        return(false);
                    }
                }

                // are there opening hours?
                if (a.OpeningHours != null)
                {
                    // find opening hours of current day
                    int day = (int)DateTime.Now.DayOfWeek;
                    OpeningHoursModel openingModel = a.OpeningHours[day];

                    // is the parking closed today?
                    if (openingModel.Closed == true)
                    {
                        return(false);
                    }

                    // check if current time + parking duration in limits of opening hours
                    int parkingDurationAbsolute = Convert.ToInt32(string.Format("{0:HHmm}", DateTime.Now.AddHours(hours)));
                    if (openingModel.Open > parkingDurationAbsolute && parkingDurationAbsolute > openingModel.Close)
                    {
                        return(false);
                    }
                }

                return(true);
            }).ToList <ParkingModel>();

            Console.WriteLine("{0} parking options that are open/available", parkingModels.Count);

            // dictionary to store return values that translate to JSON
            Dictionary <string, object> returnValues = new Dictionary <string, object>();

            // there are no available parking options in the area
            if (parkingModels.Count == 0)
            {
                // set appropriate state
                returnValues.Add("state", "driving");
                returnValues.Add("parking", parkingModels);
                return(returnValues);
            }

            // store the current first one in the list since it will be the closest
            // due to mongodb's geosearch
            ParkingModel closestModel = parkingModels.First();

            // sort by closeness to current position
            parkingModels.Sort(delegate(ParkingModel a, ParkingModel b)
            {
                double scoreA = a.DistanceToUser * PriceParser.Interpret(a.Price, hours) * a.FreeLikelihood;
                double scoreB = b.DistanceToUser * PriceParser.Interpret(b.Price, hours) * b.FreeLikelihood;

                if (scoreA > scoreB)
                {
                    return(1);
                }
                else if (scoreA < scoreB)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });

            // check if a user is right on the closest parkingspot,
            // that would mean, that the STATE 'parked' would be induced
            if (closestModel.DistanceToUser <= PARKING_RADIUS / 1000.0 && speed <= SLOW_SPEED)
            {
                // set appropriate state
                returnValues.Add("state", "parking");
                returnValues.Add("distance", Math.Round(closestModel.DistanceToUser, 2));

                // append information about the parking spot we are on right now
                Dictionary <string, object> data = new Dictionary <string, object>();
                data.Add("id", closestModel.Id);
                data.Add("name", closestModel.Name);
                data.Add("price", closestModel.Price.PerHour.Price);
                data.Add("type", closestModel.Type);
                data.Add("redpoint", closestModel.RedPointText);
                data.Add("description", closestModel.Description);

                returnValues.Add("parking", data);
            }

            // user is still driving, we will offer him $(TAKE) of the best
            // parking spots sorrounding him
            else
            {
                // prepare the parking models for displaying on
                // the enduser device
                List <Dictionary <string, object> > parking = new List <Dictionary <string, object> >();

                // append good parking options around user
                foreach (ParkingModel model in parkingModels.Take(TAKE))
                {
                    Console.WriteLine(PriceParser.Interpret(model.Price, hours));

                    Dictionary <string, object> data = new Dictionary <string, object>();
                    data.Add("id", model.Id);
                    data.Add("name", model.Name);
                    data.Add("price", PriceParser.Interpret(model.Price, hours));
                    data.Add("type", model.Type);
                    data.Add("coord", model.Coordinates);
                    data.Add("free", model.FreeLikelihood);

                    parking.Add(data);
                }

                // set appropriate state
                returnValues.Add("state", "driving");
                returnValues.Add("parking", parking);
            }

            return(returnValues);
        }
Example #9
0
        public dynamic Scrape()
        {
            List <ParkingModel> parkingModels = new List <ParkingModel>();

            // open connection to mongodb
            var server     = StaticGlobal.MongoDBClient.GetServer();
            var database   = server.GetDatabase("parkeasy");
            var collection = database.GetCollection <ParkingModel>("parking");

            // GARAGE DATA //
            Dictionary <string, dynamic> jsonGarage;

            // load data from remote source
            Console.WriteLine("Garages from WWW");
            using (var client = new System.Net.WebClient())
            {
                var body = client.DownloadString("http://www.koeln.de/apps/parken/json/current");
                jsonGarage = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(body);
            }

            List <string> prices = new List <string>();

            // parse parking garages into our parking model
            foreach (string key in jsonGarage.Keys)
            {
                dynamic obj = jsonGarage[key];

                ParkingModel model = new ParkingModel();

                // parse garage data
                model.Type     = ParkingType.Garage;
                model.Name     = obj.title;
                model.Id       = obj.shortname;
                model.Capacity = obj.capacity;
                model.Free     = obj.free;

                try
                {
                    model.CapacityWomen = obj.womens_parking;
                }
                catch (Exception e) {}

                try
                {
                    model.Price = PriceParser.Parse(Convert.ToString(obj.price));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                model.Description = obj.fulltext;

                var collectionStatus = database.GetCollection <ParkingModel>("status");

                // store the "free" parking spaces information
                StatusModel status = new StatusModel();
                status.ParkingId         = model.Id;
                status.Amount            = obj.free;
                status.Time              = DateTime.UtcNow;
                status.Id                = ObjectId.GenerateNewId();
                status.HighQualitySample = true;

                collectionStatus.Insert(status);

                // parse coordinates
                CoordinateModel coordinateModel = new CoordinateModel();
                coordinateModel.Latitude  = Convert.ToDouble(obj.lat);
                coordinateModel.Longitude = Convert.ToDouble(obj.lng);

                model.Coordinate  = coordinateModel;
                model.Coordinates = new double[2] {
                    coordinateModel.Longitude, coordinateModel.Latitude
                };

                try
                {
                    model.OpeningHours = new OpeningHoursModel[7];

                    // loop a whole week
                    for (int i = 0; i <= 6; i++)
                    {
                        // extract opening times for every day of the week
                        OpeningHoursModel hoursModel = new OpeningHoursModel();
                        hoursModel.Open  = Convert.ToInt32(Convert.ToString(obj.open_time).Replace(":", ""));
                        hoursModel.Close = Convert.ToInt32(Convert.ToString(obj.close_time).Replace(":", ""));

                        model.OpeningHours[i] = hoursModel;
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.ToString());
                }

                parkingModels.Add(model);
            }

            // MACHINE DATA //
            dynamic jsonMachine;

            // load data from remote source
            Console.WriteLine("Machine from WWW");
            using (var client = new System.Net.WebClient())
            {
                var body = client.DownloadString("http://geoportal1.stadt-koeln.de/ArcGIS/rest/services/66/Parkscheinautomaten/MapServer/0/query?text=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=id%20is%20not%20null&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&maxAllowableOffset=&outSR=4326&outFields=%2A&f=json");
                jsonMachine = JsonConvert.DeserializeObject(body);
            }

            // parse parking machines into our parking model
            foreach (dynamic machine in jsonMachine.features)
            {
                ParkingModel model = new ParkingModel();

                // parse garage data
                model.Type                = ParkingType.TicketMachine;
                model.Name                = machine.attributes.Aufstellort;
                model.Id                  = Convert.ToString(machine.attributes.ID);
                model.Capacity            = machine.attributes.Stellplaetze;
                model.Price               = new PriceModel(Convert.ToDouble(machine.attributes.Gebuehr));
                model.MaximumParkingHours = Convert.ToDouble(machine.attributes.Hoechstparkdauer);
                model.RedPointText        = machine.attributes.Roter_Punkt_Text;
                model.SectionFrom         = machine.attributes.Abschnitt_Von;
                model.SectionTo           = machine.attributes.Abschnitt_Bis;

                // parse coordinates
                CoordinateModel coordinateModel = new CoordinateModel();
                coordinateModel.Latitude  = machine.geometry.y;
                coordinateModel.Longitude = machine.geometry.x;
                model.Coordinate          = coordinateModel;

                model.Coordinates = new double[2] {
                    coordinateModel.Longitude, coordinateModel.Latitude
                };

                parkingModels.Add(model);
            }

            // UNI DATA //
            dynamic jsonUni;

            // load data from remote source
            Console.WriteLine("Uni from WWW");
            using (var client = new System.Net.WebClient())
            {
                var body = client.DownloadString("https://github.com/ParkEasy/tools/releases/download/v1/uni.json");
                jsonUni = JsonConvert.DeserializeObject(body);
            }

            // loop university parking spaces
            foreach (dynamic uniparking in jsonUni)
            {
                ParkingModel model = new ParkingModel();

                model.Id               = uniparking.name;
                model.Type             = ParkingType.University;
                model.Name             = uniparking.name;
                model.Description      = string.Join(", ", uniparking.descriptions);
                model.Capacity         = uniparking.num_spaces;
                model.CapacityDisabled = uniparking.num_disabled;
                model.CapacityService  = uniparking.num_service;
                model.Gates            = uniparking.gates;
                model.Price            = new PriceModel(1.3);

                // parse opening hours
                if (DynamicExist(uniparking, "hours"))
                {
                    model.OpeningHours = new OpeningHoursModel[7];

                    int i = 0;
                    foreach (dynamic hour in uniparking.hours)
                    {
                        OpeningHoursModel hoursModel = new OpeningHoursModel();
                        if (hour != null)
                        {
                            hoursModel.Open  = hour.open;
                            hoursModel.Close = hour.close;
                        }
                        else
                        {
                            hoursModel.Closed = true;
                        }

                        model.OpeningHours[i] = hoursModel;
                        i++;
                    }
                }

                // parse coordinates
                CoordinateModel coordinateModel = new CoordinateModel();
                coordinateModel.Latitude  = uniparking.coordinates.latitude;
                coordinateModel.Longitude = uniparking.coordinates.longitude;
                model.Coordinate          = coordinateModel;

                model.Coordinates = new double[2] {
                    coordinateModel.Longitude, coordinateModel.Latitude
                };

                parkingModels.Add(model);
            }

            // upsert each of the available parking options
            foreach (ParkingModel model in parkingModels)
            {
                collection.Save(model);
            }

            return(parkingModels.Count);
        }