Exemple #1
0
        private async Task <SchedulesResult> _GetSchedules(string subject)
        {
            if (string.IsNullOrWhiteSpace(_subjectRequested) && _subjectRequested != subject)
            {
                NetworkComms.RemoveGlobalIncomingPacketHandler($"{SchedulesResult.GetHeader()}{_subjectRequested}");
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                return(null);
            }

            _subjectRequested = subject;

            if (Server == null)
            {
                await FindServer();
            }

            if (Server == null)
            {
                return(null);
            }

            if (_subjectRequested != subject)
            {
                return(null);
            }

            SchedulesResult result = null;

            NetworkComms.AppendGlobalIncomingPacketHandler <SchedulesResult>(SchedulesResult.GetHeader() + subject,
                                                                             (h, c, i) =>
            {
                NetworkComms.RemoveGlobalIncomingPacketHandler(SchedulesResult.GetHeader() + subject);
                result = i;
            });

            await new SchedulesRequest()
            {
                SubjectCode = subject, StudentId = CurrentStudent.Id
            }
            .Send(new IPEndPoint(IPAddress.Parse(Server.IP), Server.Port));

            var start = DateTime.Now;

            while ((DateTime.Now - start).TotalSeconds < 17)
            {
                if (result != null)
                {
                    return(result);
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            Server = null;
            NetworkComms.RemoveGlobalIncomingPacketHandler(SchedulesResult.GetHeader() + subject);
            return(null);
        }
Exemple #2
0
        public static void ScheduleRequestHandler(PacketHeader packetheader, Connection con, SchedulesRequest incomingobject)
        {
            var dev = AndroidDevice.Cache.FirstOrDefault(
                d => d.IP == ((IPEndPoint)con.ConnectionInfo.RemoteEndPoint).Address.ToString());

            //Maybe do not ignore this on production
            if (dev == null)
            {
                return;
            }

            var result = new SchedulesResult()
            {
                Serial = incomingobject.Serial, Subject = incomingobject.SubjectCode
            };

            var student = Models.Student.Cache.FirstOrDefault(x => x.Id == incomingobject.StudentId);
            var subject = Models.Subject.Cache
                          .FirstOrDefault(x => x.Code.ToLower() == incomingobject.SubjectCode.ToLower());

            if (student == null || subject == null)
            {
                result.Success      = false;
                result.ErrorMessage = $"{incomingobject.SubjectCode.ToUpper()} not found!";
            }
            else
            {
                if (!(Models.CourseSubject.Cache
                      .Any(x => x.CourseId == student?.CourseId &&
                           x.SubjectId == subject.Id)))
                {
                    result.Success      = false;
                    result.ErrorMessage = $"{incomingobject.SubjectCode.ToUpper()} is not in your course.";
                }
                else
                {
                    var schedules = Models.ClassSchedule.Cache.Where(x => x.Subject.Code == subject.Code);
                    result.Success   = true;
                    result.Schedules = new List <ClassSchedule>();
                    foreach (var sched in schedules)
                    {
                        result.Schedules.Add(new ClassSchedule()
                        {
                            ClassId     = sched.Id,
                            Instructor  = sched.Instructor,
                            Schedule    = sched.Description,
                            SubjectCode = sched.Subject.Code,
                            Room        = sched.Room,
                            Slots       = sched.Slots,
                            Enrolled    = GetEnrolled(sched.Id)
                        });
                    }
                }
            }
            result.Send(new IPEndPoint(IPAddress.Parse(dev.IP), dev.Port));
        }