public void Select()
        {
            this.BingMap.Visibility = System.Windows.Visibility.Visible;
            MobileSrc.Commuter.Shared.CommuteDefinition definition = this.Source;

            if (!_isRefreshing)
            {
                SetRouteRecommendation(false);
            }
            else
            {
            }
            AddRoutes();
        }
Пример #2
0
        public bool AddCommute(Guid deviceId, MobileSrc.Commuter.Shared.CommuteDefinition commute)
        {
            LogRequest(deviceId, "AddCommute");
            try
            {
                using (SqlConnection connection = new SqlConnection(@"Data Source=wpcommuter.db.3448251.hostedresource.com; Initial Catalog=wpcommuter; User ID=wpcommuter; Password='******';"))
                {
                    connection.Open();

                    SqlCommand command = new SqlCommand("AddCommute", connection);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.AddWithValue("idCommute", commute.Id);
                    command.Parameters.AddWithValue("idDevice", deviceId);
                    command.Parameters.AddWithValue("DepartTime", commute.DepartureTime);
                    command.Parameters.AddWithValue("ReturnTime", commute.ReturnTime);
                    command.Parameters.AddWithValue("DaysOfWeek", commute.DaysOfWeek);

                    using (StringWriter writer = new StringWriter())
                    {
                        _commuteSerializer.Serialize(writer, commute);
                        command.Parameters.AddWithValue("Definition", writer.ToString());
                    }

                    command.ExecuteNonQuery();
                }

                foreach (MobileSrc.Commuter.Shared.RouteDefinition route in commute.Routes)
                {
                    AddRoute(deviceId, commute.Id, route);
                }

                using (SqlConnection connection = new SqlConnection(@"Data Source=wpcommuter.db.3448251.hostedresource.com; Initial Catalog=wpcommuter; User ID=wpcommuter; Password='******';"))
                {
                    connection.Open();

                    SqlCommand command = new SqlCommand("CleanupCommute", connection);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.AddWithValue("idCommute", commute.Id);
                    command.ExecuteNonQuery();
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Пример #3
0
        private static void ProcessUpdate(object param)
        {
            // We want to loop through
            try
            {
                object[] args = (object[])param;

                AutoResetEvent waiter         = (AutoResetEvent)args[0];
                bool           isDeparture    = (bool)args[2];
                string         channelUri     = (string)args[3];
                double         timeZoneOffset = (double)args[4];
                string         accentColor    = (string)args[5];
                DateTime       sendTime       = (DateTime)args[6];

                MobileSrc.Commuter.Shared.CommuteDefinition commute = (MobileSrc.Commuter.Shared.CommuteDefinition)args[1];

                Directory.CreateDirectory("logs");
                using (FileStream fs = File.Create(string.Format(@"logs\{0}_{1}.log", commute.Id, DateTime.Now.ToString("MM_dd_hh_tt"))))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.AutoFlush = true;

                        DateTime startTime = DateTime.Now;
                        //DateTime sendTime = startTime.AddMinutes(DurationQueryRange);
                        DateTime endTime = sendTime.AddMinutes(DurationQueryRange);

                        Console.WriteLine(string.Format("{0} - Processing Update for {1}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name));
                        sw.WriteLine(string.Format("{0} - Processing Update for {1}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name));

                        bool hasSentUpdate = false;
                        while (DateTime.Now <= endTime)
                        {
                            try
                            {
                                SortedList <TimeSpan, MobileSrc.Commuter.Shared.RouteDefinition> times = new SortedList <TimeSpan, MobileSrc.Commuter.Shared.RouteDefinition>();
                                MobileSrc.Commuter.Shared.RouteDefinition bestRoute = null;

                                bool sendUpdate = false;

                                if (!hasSentUpdate && DateTime.Now >= sendTime)
                                {
                                    sendUpdate    = true;
                                    hasSentUpdate = true;
                                }

                                foreach (MobileSrc.Commuter.Shared.RouteDefinition route in commute.Routes)
                                {
                                    TimeSpan duration = MobileSrc.Commuter.Shared.Utils.RefreshRoute(commute, route, !isDeparture);

                                    while (times.ContainsKey(duration))
                                    {
                                        duration = duration.Add(TimeSpan.FromSeconds(1));
                                    }
                                    times.Add(duration, route);

                                    try
                                    {
                                        using (SqlConnection writeConnection = new SqlConnection(@"Data Source=wpcommuter.db.3448251.hostedresource.com; Initial Catalog=wpcommuter; User ID=wpcommuter; Password='******';"))
                                        {
                                            writeConnection.Open();
                                            SqlCommand updateCommand = new SqlCommand("UpdateRoute", writeConnection);
                                            updateCommand.CommandType = CommandType.StoredProcedure;
                                            updateCommand.Parameters.AddWithValue("idRoute", route.Id);
                                            updateCommand.Parameters.AddWithValue("idCommute", commute.Id);
                                            updateCommand.Parameters.AddWithValue("Date", DateTime.Now.ToUniversalTime());
                                            updateCommand.Parameters.AddWithValue("Duration", duration.TotalMinutes);
                                            updateCommand.Parameters.AddWithValue("IsDeparture", isDeparture);
                                            updateCommand.Parameters.AddWithValue("IsRequested", sendUpdate);

                                            updateCommand.ExecuteNonQuery();
                                        }
                                    }
                                    catch (Exception ex2)
                                    {
                                        Console.WriteLine(string.Format("{0} - Exception for {1} - {2}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name, ex2.ToString()));
                                        sw.WriteLine(string.Format("{0} - Exception for {1} - {2}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name, ex2.ToString()));
                                    }
                                }

                                if (sendUpdate && times.Count > 0)
                                {
                                    bestRoute = times.Values[0];

                                    DateTime updateTime = DateTime.Now.AddHours(timeZoneOffset);
                                    string   tileUrl    = string.Format(@"http://mobilesrc.com/commuter/LiveTile.aspx?commute={0}&route={1}&duration={2}&interval={3}&day={4}&color={5}", commute.Name, bestRoute.Name, (int)bestRoute.EstimatedDuration.TotalMinutes, "min", updateTime.AddHours(-timeZoneOffset).ToString("dddd @ hh:mm tt"), accentColor);
                                    MobileSrc.Commuter.Shared.NotificationResponse toast = MobileSrc.Commuter.Shared.Notifications.SendToast(channelUri, commute.Name, string.Format("{0} {1} minutes", times.Values[0].Name, (int)times.Keys[0].TotalMinutes));
                                    MobileSrc.Commuter.Shared.NotificationResponse tile  = MobileSrc.Commuter.Shared.Notifications.SendTileUpdate(channelUri, "", 0, tileUrl);

                                    Console.WriteLine(string.Format("{0} - Responses for {1} [toast: {2}, tile: {3}]", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name, toast, tile));
                                    sw.WriteLine(string.Format("{0} - Responses for {1} [toast: {2}, tile: {3}]", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name, toast, tile));
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(string.Format("{0} - Exception for {1} - {2}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name, ex.ToString()));
                                sw.WriteLine(string.Format("{0} - Exception for {1} - {2}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name, ex.ToString()));
                            }
                            finally
                            {
                                waiter.Set();
                            }
                            Thread.Sleep(DurationQueryInterval * 60 * 1000);
                        }
                        Console.WriteLine(string.Format("{0} - Finished Update for {1}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name));
                        sw.WriteLine(string.Format("{0} - Finished Update for {1}", DateTime.Now.ToString("MM/dd hh:mm tt"), commute.Name));
                    }
                }
            }
            catch
            {
            }
        }