Пример #1
0
        private async Task <bool> SaveTimetableBackup(TimetableObjectsList timetableObjects)
        {
            try {
                StorageFile saveFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                using (Stream writeStream = await saveFile.OpenStreamForWriteAsync()) {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(TimetableObjectsList));
                    serializer.WriteObject(writeStream, timetableObjects);
                    await writeStream.FlushAsync();

                    writeStream.Dispose();
                }
                return(true);
            }
            catch (Exception e) {
                throw new Exception("Unable to save the timetable", e);
            }
        }
Пример #2
0
        /*
         * Get the timetable asynchronous from the server.
         * Returns an empty list of objects if anything fails.
         */
        public async Task <TimetableObjectsList> GetTimetable(string stgJhr, string stg, string stgGrp)
        {
            // TODO: Regex zum Prüfen der Werte
            if (stgJhr != null && stg != null && stgGrp != null && !stgJhr.Equals("") && !stg.Equals("") && !stgGrp.Equals(""))
            {
                try {
                    string requestData = WebUtility.UrlEncode("StgJhr") + "=" + WebUtility.UrlEncode(stgJhr) + "&"
                                         + WebUtility.UrlEncode("Stg") + "=" + WebUtility.UrlEncode(stg) + "&"
                                         + WebUtility.UrlEncode("StgGrp") + "=" + WebUtility.UrlEncode(stgGrp);

                    Uri                 uri      = new Uri("https://www2.htw-dresden.de/~app/API/GetTimetable.php?" + requestData);
                    HttpClient          client   = new HttpClient();
                    HttpResponseMessage response = await client.GetAsync(uri);

                    string content = await response.Content.ReadAsStringAsync();

                    Debug.WriteLine(content);

                    List <TimetableObject> timetableObjects     = JsonConvert.DeserializeObject <List <TimetableObject> >(content);
                    TimetableObjectsList   timetableObjectsList = new TimetableObjectsList();
                    timetableObjectsList.timetableObjects = timetableObjects;
                    timetableObjectsList.timestamp        = DateTime.Now;

                    // backup timetable if connection fails the next time
                    await SaveTimetableBackup(timetableObjectsList);

                    return(timetableObjectsList);
                }
                catch (Exception e) {
                    Debug.WriteLine(e.ToString());
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
        public static async Task <XmlDocument> GetNextLessonXml()
        {
            // don´t download a new timetable, use the stored one
            TimetableObjectsList timetableObjects = await LoadTimetableBackup();

            if (timetableObjects != null)
            {
                // find the next lesson
                TimetableObject    nextLesson = null;
                DateTime           now        = DateTime.Now;
                DateTimeFormatInfo dtfi       = DateTimeFormatInfo.CurrentInfo;
                Calendar           cal        = dtfi.Calendar;
                int weekNum    = cal.GetWeekOfYear(now, dtfi.CalendarWeekRule, dtfi.FirstDayOfWeek);
                int weekParity = (weekNum % 2) == 0 ? 2 : 1; // 1 odd, 2 even

                // create sorted list of lessons
                Dictionary <String, int> timeToIntDictionary = new Dictionary <String, int>();
                timeToIntDictionary.Add("07:30:00", 0);
                timeToIntDictionary.Add("09:20:00", 1);
                timeToIntDictionary.Add("11:10:00", 2);
                timeToIntDictionary.Add("13:20:00", 3);
                timeToIntDictionary.Add("15:10:00", 4);
                timeToIntDictionary.Add("17:00:00", 5);
                timeToIntDictionary.Add("18:40:00", 6);
                timeToIntDictionary.Add("20:20:00", 7);

                Dictionary <int, Dictionary <int, TimetableObject> > thisWeekDictionary = new Dictionary <int, Dictionary <int, TimetableObject> >();
                Dictionary <int, Dictionary <int, TimetableObject> > nextWeekDictionary = new Dictionary <int, Dictionary <int, TimetableObject> >();
                for (int i = 0; i < 7; i++)
                {
                    Dictionary <int, TimetableObject> dayDictionary     = new Dictionary <int, TimetableObject>();
                    Dictionary <int, TimetableObject> dayDictionaryNext = new Dictionary <int, TimetableObject>();
                    for (int k = 0; k < 8; k++)
                    {
                        dayDictionary.Add(k, null);
                        dayDictionaryNext.Add(k, null);
                    }
                    thisWeekDictionary.Add(i, dayDictionary);
                    nextWeekDictionary.Add(i, dayDictionaryNext);
                }

                foreach (TimetableObject timetableObject in timetableObjects.timetableObjects)
                {
                    if (timetableObject.Week == 0)
                    {
                        //add to this and next week
                        thisWeekDictionary[timetableObject.Day][timeToIntDictionary[timetableObject.BeginTime]] = timetableObject;
                        nextWeekDictionary[timetableObject.Day][timeToIntDictionary[timetableObject.BeginTime]] = timetableObject;
                    }
                    else if (timetableObject.Week == weekParity)
                    {
                        // add to this week only
                        thisWeekDictionary[timetableObject.Day][timeToIntDictionary[timetableObject.BeginTime]] = timetableObject;
                    }
                    else
                    {
                        // add to next week only
                        nextWeekDictionary[timetableObject.Day][timeToIntDictionary[timetableObject.BeginTime]] = timetableObject;
                    }
                }

                // search through the list
                int nextLessonInt = 8; // 8 = after last lesson
                if (now < new DateTime(now.Year, now.Month, now.Day, 7, 30, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 0;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 9, 20, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 1;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 11, 10, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 2;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 13, 20, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 3;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 15, 10, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 4;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 17, 00, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 5;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 18, 40, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 6;
                }
                else if (now < new DateTime(now.Year, now.Month, now.Day, 20, 20, 0, DateTimeKind.Local))
                {
                    nextLessonInt = 7;
                }
                // is there a lesson on this day
                while (nextLesson == null && nextLessonInt < 8)
                {
                    nextLesson = thisWeekDictionary[(int)now.DayOfWeek][nextLessonInt];
                    nextLessonInt++;
                }
                // if there is nothing on this day get next lesson in this week
                int j = 1;
                while (nextLesson == null && ((int)now.DayOfWeek) + j < 7)
                {
                    nextLessonInt = 0;
                    while (nextLesson == null && nextLessonInt < 8)
                    {
                        nextLesson = thisWeekDictionary[(int)now.DayOfWeek + j][nextLessonInt];
                        nextLessonInt++;
                    }
                    j++;
                }
                // if there is nothing in this week get next week
                int dayOfWeek = 0;
                while (nextLesson == null && dayOfWeek < 7)
                {
                    nextLessonInt = 0;
                    while (nextLesson == null && nextLessonInt < 8)
                    {
                        nextLesson = nextWeekDictionary[dayOfWeek][nextLessonInt];
                        nextLessonInt++;
                    }
                    dayOfWeek++;
                }
                // if it found nothing yet the timetable is totally empty...
                XmlDocument doc = null;
                if (nextLesson != null)
                {
                    // build the tile
                    TileBindingContentAdaptive bindingContent = new TileBindingContentAdaptive()
                    {
                        Children =
                        {
                            new TileText()
                            {
                                Text  = "Nächste Stunde",
                                Style = TileTextStyle.CaptionSubtle
                            },
                            new TileText()
                            {
                                Text = nextLesson.LessonTag + " (" + nextLesson.Type + ")"
                            },
                            new TileText()
                            {
                                Text = nextLesson.Rooms[0] + (nextLesson.Rooms.Count > 1 ? " u. a." : "")
                            },
                            new TileText()
                            {
                                Text = nextLesson.BeginTime.Remove(5, 3) + " Uhr"
                            }
                        }
                    };

                    TileContent content = new TileContent()
                    {
                        Visual = new TileVisual()
                        {
                            TileMedium = new TileBinding()
                            {
                                Branding = TileBranding.Name,
                                Content  = bindingContent
                            },
                            TileLarge = new TileBinding()
                            {
                                Branding = TileBranding.Name,
                                Content  = bindingContent
                            },
                            TileWide = new TileBinding()
                            {
                                Branding = TileBranding.Name,
                                Content  = bindingContent
                            }
                        }
                    };

                    doc = content.GetXml();
                }
                return(doc);
            }
            return(null);
        }
Пример #4
0
        async Task LoadTimetables()
        {
            timetableModel = TimetableModel.GetInstance();
            timetableUtils = new TimetableUtils();
            settingsModel  = SettingsModel.GetInstance();
            TimetableObjectsList timetableObjects = await timetableModel.GetTimetable(settingsModel.StgJhr, settingsModel.Stg, settingsModel.StgGrp);

            if (timetableObjects != null)
            {
                // clear the timetables
                timetableUtils.clearTimetable(timetableThisWeek);
                timetableUtils.clearTimetable(timetableNextWeek);

                Lessons = timetableObjects.timetableObjects;

                UpdateLiveTile();

                /*find out if current week is even or odd*/
                int evenOdd = timetableUtils.isCurrentWeekEvenOrOdd();

                foreach (TimetableObject timetableObject in Lessons)
                {
                    int[] row = timetableUtils.getRowForTable(timetableObject);
                    if (row[0] != -1 && row[1] != -1)
                    {
                        for (int currentRow = row[0]; currentRow <= row[1]; currentRow++)
                        {
                            // prepare the button for this lesson
                            TimetableItem timetableItem = new TimetableItem();
                            string        room          = "";
                            if (timetableObject.Rooms.Count > 1)
                            {
                                room = timetableObject.Rooms[0] + " ...";
                            }
                            else
                            {
                                room = timetableObject.Rooms[0];
                            }
                            timetableItem.TextBlock.Text = timetableObject.LessonTag + "\n"
                                                           + timetableObject.Type + "\n"
                                                           + room;
                            Grid.SetColumn(timetableItem, timetableObject.Day);
                            Grid.SetRow(timetableItem, currentRow);
                            // switch for the week number
                            switch (timetableObject.Week)
                            {
                            /*lesson takes place every week*/
                            case 0: {
                                timetableUtils.addLessonToWeek(timetableThisWeek, currentRow, timetableItem);
                                // clone the button
                                TimetableItem clone = new TimetableItem();
                                Grid.SetColumn(clone, timetableObject.Day);
                                Grid.SetRow(clone, currentRow);
                                clone.TextBlock.Text = timetableItem.TextBlock.Text;
                                timetableUtils.addLessonToWeek(timetableNextWeek, currentRow, clone);
                                break;
                            }

                            /*only at odd weeks*/
                            case 1: {
                                /*if current week is even add it to next week*/
                                if (evenOdd == 0)
                                {
                                    timetableUtils.addLessonToWeek(timetableNextWeek, currentRow, timetableItem);
                                }
                                else
                                {
                                    timetableUtils.addLessonToWeek(timetableThisWeek, currentRow, timetableItem);
                                }
                                break;
                            }

                            /*only at even weeks*/
                            case 2: {
                                /*if current week is even add it to this week*/
                                if (evenOdd == 0)
                                {
                                    timetableUtils.addLessonToWeek(timetableThisWeek, currentRow, timetableItem);
                                }
                                else
                                {
                                    timetableUtils.addLessonToWeek(timetableNextWeek, currentRow, timetableItem);
                                }
                                break;
                            }

                            default: {
                                break;
                            }
                            }
                        }
                    }
                }
                // update timestamp
                timestampThisWeek.Text = timestampNextWeek.Text = "Stand: " + timetableObjects.timestamp.ToLocalTime().ToString();
            }
        }