Пример #1
0
        public IPostResponse GetCurrentLoans([FromContent] LoanHistoryRequest Request)
        {
            var db = new SqliteDB();
            //TODO: Check for email..
            var loans = db.GetCurrentLoans();

            if (loans == null || loans.Count == 0)
            {
                return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData {
                    Code = 1, Message = "No loans found"
                }));
            }

            var returnList = new List <LoanHistoryRespose>();

            foreach (var loan in loans)
            {
                if (loan.ExpireDate > DateTime.Now.Ticks)
                {
                    if (loan.Email == Request.Email)
                    {
                        var dev = db.GetDevice(new DeviceList {
                            SerialNumber = loan.Mac
                        });

                        returnList.Add(new LoanHistoryRespose {
                            ExpireDate = DateTime.FromBinary(loan.ExpireDate).ToString(), ExpireDateTick = loan.ExpireDate, SerialNumber = loan.Mac, Username = loan.GeneratedUsername, Password = loan.GeneratedPassword, Host = dev.Host, Model = dev.Model
                        });
                    }
                }
            }
            if (returnList.Count > 0)
            {
                return(new PostResponse(PostResponse.ResponseStatus.Created, null, returnList));
            }
            return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData {
                Code = 1, Message = "No loans found"
            }));
        }
Пример #2
0
        public IPostResponse LoanCamera([FromContent] LoanRequest LoanRequest)
        {
            var Loan = new DeviceLoan {
                Mac = LoanRequest.SerialNumber, Email = LoanRequest.Email, LoanReason = LoanRequest.Reason
            };

            if ((LoanRequest.LoanDays == 1 || LoanRequest.LoanDays == 7 || LoanRequest.LoanDays == 14) == false)
            {
                return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData {
                    Code = 4, Message = "Loan days can only be set to 1,7 or 14", Status = "Failed"
                }));
            }
            var db = new SqliteDB();

            Loan.ExpireDate = DateTime.Now.AddDays(LoanRequest.LoanDays).Ticks;
            var dbres = db.GetLastLoanInfo(Loan);

            if (dbres == null)
            {
                var username = "******" + new Random().Next(1000, 9999).ToString();
                var password = RandomString(16);
                Loan.GeneratedPassword = password;
                Loan.GeneratedUsername = username;

                DeviceList loandevice = new DeviceList {
                    SerialNumber = Loan.Mac
                };
                var loanres = db.GetDevice(loandevice);
                if (loanres == null)
                {
                    return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData {
                        Code = 2, Message = "Device not found", Status = "Failed"
                    }));
                }

                var device = new AxisVapixLib.Device();
                device.Host     = loanres.Host;
                device.Username = loanres.Username;
                device.Password = loanres.Password;

                var userres = Task.Run(async() => await device.AddUser(username, password, AxisVapixLib.Device.UserGroups.Admin));
                while (!userres.IsCompleted)
                {
                    Task.Delay(100);
                }



                if (!userres.Result)
                {
                    return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData {
                        Code = 3, Message = "Failed to create username", Status = "Failed"
                    }));
                }
                var overlay = new AxisVapixLib.Overlay.Overlay(device);
                Task.Run(() => overlay.SetTextOverlayAsync(0, AxisVapixLib.Overlay.Overlay.TextOverlayBackgroundColor.White, false, false, AxisVapixLib.Overlay.Overlay.TextColor.Black, AxisVapixLib.Overlay.Overlay.TextOverlayPosition.Top, "Reserved to " + Loan.Email));

                Loan.LoanDate = DateTime.Now.Ticks;

                db.SaveLoanInfo(Loan);

                EmailController e = new EmailController();
                Task.Run(async() => await e.SendEmail(Loan.Email, "Device Loaned", string.Format("You have successfully loaned\nUrl: {0}\nUsername: {1}\nPassword: {2}, Expires: {3}", device.Host, Loan.GeneratedUsername, Loan.GeneratedPassword, DateTime.FromBinary(Loan.ExpireDate).ToString())));


                LoanResponse lr = new LoanResponse();
                lr.DateExpire     = DateTime.FromBinary(Loan.ExpireDate).ToString();
                lr.DateExpireTick = Loan.ExpireDate;
                lr.SerialNumber   = Loan.Mac;
                lr.Username       = username;
                lr.Password       = password;
                lr.Host           = device.Host;

                return(new PostResponse(PostResponse.ResponseStatus.Created, null, lr));
            }
            var d = new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData()
            {
                Code = 1, Message = "Already loaned", Status = "Failed"
            });

            return(d);
        }