Beispiel #1
0
        public static ReservationStateArgs Create(IReservationItem rsv, ReservationClient client, DateTime now)
        {
            var isAuthorized = client.IsAuthorized(rsv);
            var args         = new ReservationStateArgs(rsv.ReservationID, client.InLab, client.IsReserver, client.IsInvited, isAuthorized, !rsv.Editable, rsv.IsFacilityDownTime, rsv.MinCancelTime, rsv.MinReservTime, rsv.BeginDateTime, rsv.EndDateTime, rsv.ActualBeginDateTime, rsv.ActualEndDateTime, client.UserAuth);

            return(args);
        }
Beispiel #2
0
        public static ReservationClient Create(IReservationItem rsv, IClient client, IEnumerable <IResourceClient> resourceClients, IEnumerable <IReservationInviteeItem> invitees, bool inlab)
        {
            var userAuth   = Reservations.GetAuthLevel(resourceClients, client);
            var isReserver = rsv.ClientID == client.ClientID;
            var isInvited  = invitees.Any(x => x.InviteeID == client.ClientID);

            var result = new ReservationClient
            {
                ClientID      = client.ClientID,
                ReservationID = rsv.ReservationID,
                ResourceID    = rsv.ResourceID,
                IsReserver    = isReserver,
                IsInvited     = isInvited,
                InLab         = inlab,
                UserAuth      = userAuth
            };

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Starts a reservation, and turns on Interlock.
        /// </summary>
        /// <param name="item">The reservation to start</param>
        /// <param name="client">The current user starting the reservation</param>
        public void Start(IReservationItem item, ReservationClient client, int?modifiedByClientId)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item", "A null ReservationItem object is not allowed.");
            }

            if (item.IsStarted)
            {
                return;
            }

            var args = ReservationStateArgs.Create(item, client, Now);
            ReservationState state = ReservationStateUtility.Create(Now).GetReservationState(args);

            if (state != ReservationState.StartOnly && state != ReservationState.StartOrDelete)
            {
                if (!args.IsInLab)
                {
                    throw new Exception($"Reservation #{item.ReservationID} is not startable at this time. You must be inside the lab to start reservations.");
                }
                else if (!args.IsAuthorized)
                {
                    throw new Exception($"Reservation #{item.ReservationID} is not startable at this time. You are not authorized to start reservations on this tool.");
                }
                else
                {
                    throw new Exception($"Reservation #{item.ReservationID} is not startable at this time. State: {state}.");
                }
            }

            // End Previous un-ended reservations
            var endableQuery = Provider.Scheduler.Reservation.SelectEndableReservations(item.ResourceID);

            foreach (var endable in endableQuery)
            {
                // no need to disable interlock or send open slot notifications for each of these
                // so calling IReservationManager.EndReservation directly instead of ReservationUtility.End
                Provider.Scheduler.Reservation.EndReservation(new EndReservationArgs
                {
                    ReservationID     = endable.ReservationID,
                    ActualEndDateTime = Now,
                    EndedByClientID   = modifiedByClientId.GetValueOrDefault(-1)
                });
            }

            // Start Reservation
            Provider.Scheduler.Reservation.StartReservation(item.ReservationID, modifiedByClientId);

            // If Resource authorization type is rolling and the reserver is a regular user for the resource then reset reserver's expiration date
            int authLevel = 0, resourceClientId = 0;

            var resourceClients = Provider.Scheduler.Resource.GetResourceClients(item.ResourceID, clientId: client.ClientID);

            if (resourceClients.Any())
            {
                var rc = resourceClients.First();
                authLevel        = Convert.ToInt32(rc.AuthLevel);
                resourceClientId = rc.ResourceClientID;
            }

            if (item.AuthState && (authLevel == (int)ClientAuthLevel.AuthorizedUser))
            {
                DateTime expiration = Now.AddMonths(item.AuthDuration);
                Provider.Scheduler.Resource.UpdateExpiration(resourceClientId, expiration);
            }

            // Turn Interlock On
            if (CacheManager.Current.WagoEnabled)
            {
                uint duration     = OnTheFlyUtility.GetStateDuration(item.ResourceID);
                bool hasInterlock = WagoInterlock.ToggleInterlock(item.ResourceID, true, duration);
                if (hasInterlock)
                {
                    bool interlockState = WagoInterlock.GetPointState(item.ResourceID);
                    if (!interlockState)
                    {
                        throw new InvalidOperationException($"Failed to start interlock for ResourceID {item.ResourceID}.");
                    }
                }
            }
        }