Ejemplo n.º 1
0
        private void AddCheckin(Checkin checkin)
        {
            using (var context = ContextFactory())
            {
                context.IncludeOne<Checkin>(t => t.Sections);
                context.ObjectTrackingEnabled = true;

                var old = context.Checkins.FirstOrDefault(
                    c =>
                    c.ArrivalStation.Equals(checkin.ArrivalStation) &&
                    c.ArrivalTime.Equals(checkin.ArrivalTime) &&
                    c.DepartureStation.Equals(checkin.DepartureStation) &&
                    c.DepartureTime.Equals(checkin.DepartureTime));

                if(old != null)
                {
                    context.CheckinSections.DeleteAllOnSubmit(old.Sections);
                    context.Checkins.DeleteOnSubmit(old);
                }

                context.Checkins.InsertOnSubmit(checkin);
                context.SubmitChanges();
            }
        }
Ejemplo n.º 2
0
        private void ReloadCurrentCheckin()
        {
            using (var context = ContextFactory())
            {
                var checkin =
                    context
                        .Checkins
                        .OrderByDescending(c => c.CheckinTime)
                        .FirstOrDefault();

                if (checkin != null && !checkin.CheckedOut && checkin.ArrivalTime > DateTime.Now.Add(App.SearchTimeTolerance))
                    CurrentCheckin = checkin;
            }
        }
Ejemplo n.º 3
0
        /*
         *     departure_station: "Bern",
         *     departure_time: "16:34",
         *     arrival_station: "Basel SBB",
         *     arrival_time: "17:29",
         *     train_id: "IC 1080"
         */
        public async Task Checkin(Checkin checkin)
        {
            // Check for network connectivity
            if (!NetworkInterface.GetIsNetworkAvailable()) return;

            var client = new RestClient("http://trainshare.herokuapp.com/v1/");
            var request =
                new RestRequest("checkin", Method.POST)
                    .AddJson(
                        new JObject(
                            new JProperty("trainshare_id", Token.AccessToken),
                            new JProperty("trainshare_token", Token.AccessTokenSecret),
                            new JProperty("data",
                                          new JArray(
                                              checkin
                                                  .Sections
                                                  .Select(
                                                      section =>
                                                      new JObject(
                                                          new JProperty("departure_station", section.DepartureStation),
                                                          new JProperty("departure_time",
                                                                        section.DepartureTime.ToString("HH:mm")),
                                                          new JProperty("arrival_station", section.ArrivalStation),
                                                          new JProperty("arrival_time",
                                                                        section.ArrivalTime.ToString("HH:mm")),
                                                          new JProperty("train_id", section.TrainId),
                                                          new JProperty("position", checkin.Position)))))));

            try
            {
                //Skipping the result on purpouse
                await client.ExecutTaskAsync(request);

                AddCheckin(checkin);
                CurrentCheckin = checkin;

                Events.Publish(CurrentCheckin);
            }
            catch (Exception e)
            {
                _logger.Error(e);
            }
        }
Ejemplo n.º 4
0
        public Task Checkout()
        {
            return TaskEx.Run(
                () =>
                {
                    if (CurrentCheckin == null)
                        return;

                    using (var context = ContextFactory())
                    {
                        context.ObjectTrackingEnabled = true;

                        context.Checkins.UpdateOnSubmit(c => c.Id == CurrentCheckin.Id, c => c.CheckedOut = true);

                        context.SubmitChanges();
                    }

                    CurrentCheckin = null;

                    Events.Publish(Dismiss.Checkin);
                });
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create new checkin from <paramref name="connection"/>
        /// </summary>
        /// <param name="connection">The source connection</param>
        /// <returns>The created checkin object</returns>
        public static Checkin FromConnection(Connection connection)
        {
            var checkin =
                new Checkin
                {
                    DepartureStation = connection.From.Station.Name,
                    DepartureTime = connection.From.Departure,
                    ArrivalStation = connection.To.Station.Name,
                    ArrivalTime = connection.To.Arrival,
                    Position = 0.5f
                };

            checkin.Sections.AddRange(
                connection
                    .Sections
                    .Select(
                        section =>
                        new CheckinSection
                        {
                            TrainId = section.Journey.Name,
                            DepartureStation = section.Departure.Station.Name,
                            DepartureTime = section.Departure.Departure,
                            ArrivalStation = section.Arrival.Station.Name,
                            ArrivalTime = section.Arrival.Arrival
                        })
                    .ToList());

            return checkin;
        }