private void ConsumerOnReceived(object sender, BasicDeliverEventArgs e)
        {
            using var scope = _scopeFactory.CreateScope();
            var userRepo      = scope.ServiceProvider.GetRequiredService <IDataRepository <UserEntry> >();
            var machineRepo   = scope.ServiceProvider.GetRequiredService <IDataRepository <MachineEntry> >();
            var authEventRepo = scope.ServiceProvider.GetRequiredService <IDataRepository <AuthEventEntry> >() as AuthEventRepository;

            var message = JsonConvert.DeserializeObject <AgentAuthMessage>(Encoding.UTF8.GetString(e.Body));
            var user    = userRepo.Get(message.Username);
            var machine = machineRepo.Get(message.MachineName);

            if (user == null)
            {
                user = new UserEntry
                {
                    Username = message.Username,
                    Id       = Guid.NewGuid()
                };

                userRepo.Add(user);
            }

            if (machine == null)
            {
                machine = new MachineEntry
                {
                    Name = message.MachineName,
                    Ip   = message.MachineIp,
                    Id   = Guid.NewGuid()
                };

                machineRepo.Add(machine);
            }

            var eventDate = DateTime.Parse(message.EventDate);

            if (!authEventRepo.HasEvent(eventDate, machine.Id, user.Id, message.EventType))
            {
                authEventRepo.Add(new AuthEventEntry
                {
                    EventType = message.EventType,
                    EventTime = eventDate,
                    UserId    = user.Id,
                    MachineId = machine.Id,
                    Id        = Guid.NewGuid()
                });
            }

            agentUserModel.BasicAck(e.DeliveryTag, false);
        }
        /// <summary>
        /// Creates a machine from parameters and adds it to the database.
        /// </summary>
        /// <returns>Bool reporting whether the operation was successful or not</returns>
        public static bool AddMachine(MachineType type)
        {
            bool? result = null;
            MachineEntry entry = new MachineEntry(type);
            try
            {
                Database.AddMachine(entry);
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }

            return (bool)result;
        }
        /// <summary>
        /// Part of schedule creating
        /// </summary>
        /// <param name="allMachines"></param>
        /// <returns></returns>
        public MachineEntry findMachine(List<MachineEntry> allMachines)
        {
            List<MachineEntry> specificMachines = allMachines.FindAll(machineX => machineX.Type == MachineTypeRequired);

            TimeSpan lowestTimespan = TimeSpan.MaxValue;
            MachineEntry machine = new MachineEntry();

            foreach (MachineEntry item in specificMachines)
            {
                List<ProductionTask> assignedTasks = item.MachineSchedule.AssignedTasks;
                TimeSpan totalAssignedTime = new TimeSpan(0, 0, 0);
                foreach (var smtgh in assignedTasks)
                {
                    totalAssignedTime += smtgh.Duration;
                }
                if (lowestTimespan >= totalAssignedTime)
                {
                    lowestTimespan = totalAssignedTime;
                    machine = item;
                }
            }
            return machine;
        }