Example #1
0
        public void SchedulerTick()
        {
            if (_currentMeeting != null)
            {
                var now = DateTime.Now;
                if (now.CompareTo(_currentMeeting.Time.Add(TimeSpan.FromMinutes(_currentMeeting.DurationMinutes))) >= 0)
                {
                    Logger.Info($"Meeting in {_currentMeeting.Channel.Name}, {_currentMeeting.Channel.ClassroomName} ends now ({now}), after {(int)(now - _lastMeetingStart).TotalMinutes} minutes.");
                    _controller.LeaveMeeting();
                    _currentMeeting = null;
                }
            }
            else
            {
                lock (ScheduledMeetings)
                {
                    foreach (ScheduledMeeting meeting in ScheduledMeetings)
                    {
                        DateTime time = DateTime.Now;
                        if (meeting.DayOfWeek.Equals(time.DayOfWeek.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            DateTime target = new(time.Year, time.Month, time.Day, meeting.StartHour, meeting.StartMinute, 0);

                            if (time.CompareTo(target) > 0)
                            {
                                var difference  = time - target;
                                var timeToBreak = (int)meeting.DurationMinutes / 3;

                                if (difference.TotalMinutes <= (timeToBreak < 15 ? meeting.DurationMinutes : timeToBreak / 3))
                                {
                                    Logger.Info($"Meeting in {meeting.Channel.Name}, {meeting.Channel.ClassroomName} starts now ({time}) for {meeting.DurationMinutes} minutes.");

                                    var minuteDifference = (int)difference.TotalMinutes;

                                    if (minuteDifference > 0)
                                    {
                                        Logger.Info($"We are {minuteDifference} minute{(minuteDifference > 1 ? "s" : "")} late.");
                                    }
                                    bool isSuccess = _controller.JoinMeeting(meeting.Channel);
                                    if (isSuccess)
                                    {
                                        _currentMeeting   = meeting;
                                        _lastMeetingStart = time;
                                        Logger.Info("Successfully joined the meeting.");
                                    }
                                    else
                                    {
                                        Logger.Error("Cannot join meeting.");
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
            if (args.Contains("-h") || args.Contains("--help"))
            {
                Console.WriteLine($"autoteams v{VERSION}");
                Console.WriteLine("Usage: autoteams [options]\n");
                Console.WriteLine("Options:\n"
                                  + " -h, --help\t- print this message\n"
                                  + " -v, --verbose\t- output debug logs\n"
                                  + " -u, --update\t- discover classes and synchronize the database only\n"
                                  + " -c, --console\t- use an in-built command line interface\n");
                Console.WriteLine("Internal commands (only with -c):\n"
                                  + " exit\t\t- terminates the application\n"
                                  + " db\t\t- reloads data from the database\n"
                                  + " switch <c>\t- switches to the specified channel\n"
                                  + " join <c>\t- joins a meeting in specified channel\n");
                Console.WriteLine("Argument types:\n"
                                  + " <c>\t\t - channel locator (<teamName>:<channelName>)");
                return;
            }

            ConfigurationManager.LoadConfiguration();

            if (args.Contains("-v") || args.Contains("--verbose"))
            {
                ConfigurationManager.CONFIG.OutputDebug = true;
            }

            using var db = new StorageContext();
            bool isNewlyCreated = db.Database.EnsureCreated();

            if (isNewlyCreated)
            {
                Logger.Info("Created new database.");
            }

            teamsController = new TeamsController(ConfigurationManager.CONFIG.AllowMicrophone,
                                                  ConfigurationManager.CONFIG.AllowWebcam,
                                                  ConfigurationManager.CONFIG.HeadlessMode);

            Console.CancelKeyPress += (_, _) => Exit();

            teamsController.Login().Wait();
            teamsController.DiscoverClasses();

            if (args.Contains("-u") || args.Contains("--update"))
            {
                return;
            }


            teamsController.MeetingScheduler.Initialize();

            if (args.Contains("-c") || args.Contains("--console"))
            {
                while (true)
                {
                    try
                    {
                        string command = Console.ReadLine();

                        if (command.Equals("exit", StringComparison.OrdinalIgnoreCase))
                        {
                            Exit();
                            return;
                        }
                        else if (command.Equals("leave", StringComparison.OrdinalIgnoreCase))
                        {
                            if (teamsController.LeaveMeeting())
                            {
                                Logger.Debug("Left meeting...");
                            }
                            else
                            {
                                Logger.Error("Cannot leave meeting (no meeting to leave).");
                            }
                        }
                        else if (command.Equals("db", StringComparison.OrdinalIgnoreCase))
                        {
                            Logger.Info("Reloading data from DB.");
                            teamsController.MeetingScheduler.LoadMeetingsFromDb();
                        }
                        else if (command.StartsWith("join", StringComparison.OrdinalIgnoreCase) || command.StartsWith("switch", StringComparison.OrdinalIgnoreCase))
                        {
                            string[]     locator = command[5..].Split(':');