Ejemplo n.º 1
0
        private async Task <AddScheduleResult> _AddSchedule(ClassSchedule schedule)
        {
            if (CurrentStudent == null)
            {
                return(null);
            }

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

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

            AddScheduleResult result = null;

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

            await new AddSchedule()
            {
                ClassId       = schedule.ClassId,
                StudentId     = CurrentStudent.StudentId,
                TransactionId = TransactionId
            }.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(AddScheduleResult.GetHeader());
            return(null);
        }
Ejemplo n.º 2
0
        public static async void AddScheduleHandler(PacketHeader packetheader, Connection connection, AddSchedule req)
        {
            var dev = AndroidDevice.Cache.FirstOrDefault(
                d => d.IP == ((IPEndPoint)connection.ConnectionInfo.RemoteEndPoint).Address.ToString());

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

            var request = Models.Request.Cache.FirstOrDefault(x => x.Id == req.TransactionId);
            var sched   = Models.ClassSchedule.Cache.FirstOrDefault(x => x.Id == req.ClassId);

            if ((request == null) || (request.StudentId != req.StudentId) || sched == null)
            {
                await new AddScheduleResult()
                {
                    Success      = false,
                    ErrorMessage = "Invalid request"
                }.Send(new IPEndPoint(IPAddress.Parse(dev.IP), dev.Port));
                return;
            }

            var prevSched = request.Details.FirstOrDefault(x =>
            {
                return(x.Schedule.SubjectId == sched.SubjectId && x.ScheduleId != req.ClassId);
            });

            var result = new AddScheduleResult()
            {
                Success    = true,
                ReplacedId = prevSched?.Id ?? 0,
            };

            prevSched?.Delete();

            new RequestDetail()
            {
                RequestId  = req.TransactionId,
                ScheduleId = req.ClassId,
            }.Save();

            await result.Send(new IPEndPoint(IPAddress.Parse(dev.IP), dev.Port));
        }