Beispiel #1
0
        private static void Elev_NoticeEvent(object sender, NoticeEventArgs e)
        {
            Elevator elev = (Elevator)sender;

            NoticeEvent(sender, e);
            switch (e.Status)
            {
            case StatusStyle.Arrive:
                #region 到达:如果有下梯或上梯的则“开门”,判断是否要“调头”,是否“前进”
                if (elev.CurrentFloor == Building.AboveFloorCount || elev.CurrentFloor == -Building.UnderFloorCount) //电梯到顶或到底
                {
                    elev.Direction = elev.Direction == DirectionStyle.Up ? DirectionStyle.Down : DirectionStyle.Up;  //调头
                    elev.OpenDoorAsync();
                    return;
                }
                if (elev.PersonList.Exists(x => x.ToFloor == elev.CurrentFloor) || elev.ReachList.Exists(x => x.Direction == elev.Direction && x.Floor == elev.CurrentFloor))    //有下梯或同方向上梯的
                {
                    elev.OpenDoorAsync();
                    return;
                }
                if (elev.PersonList.Count == 0)
                {
                    if (elev.ReachList.Count == 0)    //无内呼 + 无外呼,则“空闲”
                    {
                        elev.Status    = StatusStyle.Free;
                        elev.Direction = DirectionStyle.None;
                    }
                    else if ((elev.Direction == DirectionStyle.Up && !elev.ReachList.Exists(x => x.Floor > elev.CurrentFloor)) || (elev.Direction == DirectionStyle.Down && !elev.ReachList.Exists(x => x.Floor < elev.CurrentFloor)))
                    {
                        elev.Direction = elev.Direction == DirectionStyle.Up ? DirectionStyle.Down : DirectionStyle.Up;    //调头
                        if (elev.ReachList.Exists(x => x.Floor == elev.CurrentFloor && x.Direction == elev.Direction))
                        {
                            elev.OpenDoorAsync();
                        }
                        else
                        {
                            elev.RunAsync();
                        }
                    }
                    else
                    {
                        elev.RunAsync();    //前进
                    }
                }
                else
                {
                    elev.RunAsync();    //前进
                }
                #endregion
                break;

            case StatusStyle.Opened:
                #region 开门后:根据内呼和外呼情况进行“下梯、上梯”动作,最后“关门”
                Func <Person[], bool> AllIn = (arrIn) =>   //如果全部进入电梯则返回True,否则False
                {
                    bool result = false;
                    if (elev.FreeSpace < arrIn.Count())
                    {
                        var tmpIn = arrIn.Take(elev.FreeSpace).ToArray();
                        CallPersons.RemoveEx(tmpIn);
                        elev.PersonIn(tmpIn);
                    }
                    else
                    {
                        CallPersons.RemoveEx(arrIn);
                        elev.PersonIn(arrIn);
                        result = true;
                    }
                    return(result);
                };
                if (elev.PersonList.Exists(x => x.ToFloor == elev.CurrentFloor))    //有下梯的
                {
                    elev.PersonOut(elev.PersonList.Where(x => x.ToFloor == elev.CurrentFloor).ToArray());
                }
                if (CallPersons.Exists(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor))    //有同方向上梯的
                {
                    Person[] pIn = CallPersons.Where(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor).ToArray();
                    if (!AllIn(pIn))
                    {
                        Call(CallPersons.Where(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor).ToArray());
                    }
                }
                if (elev.PersonList.Count == 0)    //梯内无人,同方向无外呼,反方向有叫梯,则转换方向,上梯
                {
                    if (elev.Direction == DirectionStyle.Up && !elev.ReachList.Exists(x => x.Floor > elev.CurrentFloor) && elev.ReachList.Exists(x => x.Floor < elev.CurrentFloor))
                    {
                        elev.Direction = DirectionStyle.Down;                                                           //调头
                        if (CallPersons.Exists(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor)) //有上梯的
                        {
                            Person[] pIn = CallPersons.Where(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor).ToArray();
                            if (!AllIn(pIn))
                            {
                                Call(CallPersons.Where(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor).ToArray());
                            }
                        }
                    }
                    else if (elev.Direction == DirectionStyle.Down && !elev.ReachList.Exists(x => x.Floor < elev.CurrentFloor) && elev.ReachList.Exists(x => x.Floor > elev.CurrentFloor))
                    {
                        elev.Direction = DirectionStyle.Up;                                                             //调头
                        if (CallPersons.Exists(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor)) //有上梯的
                        {
                            Person[] pIn = CallPersons.Where(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor).ToArray();
                            if (!AllIn(pIn))
                            {
                                Call(CallPersons.Where(x => x.Direction == elev.Direction && x.FromFloor == elev.CurrentFloor).ToArray());
                            }
                        }
                    }
                }
                elev.CloseDoorAsync();
                #endregion
                break;

            case StatusStyle.Closed:
                #region 关门后: 根据内呼和外呼情况决定电梯“前进、调头、停止(空闲)”
                if (elev.PersonList.Count > 0)    //有内呼
                {
                    elev.RunAsync();
                    return;
                }
                else if (elev.ReachList.Count == 0)    //无内呼 + 无外呼
                {
                    elev.Status    = StatusStyle.Free;
                    elev.Direction = DirectionStyle.None;
                    return;
                }
                else if (elev.ReachList.Exists(x => x.Direction == elev.Direction))                                                                                                 //无内呼 + 有外呼 + 同向
                {
                    if (elev.Direction == DirectionStyle.Up && !elev.ReachList.Exists(x => x.Floor > elev.CurrentFloor) && elev.ReachList.Exists(x => x.Floor < elev.CurrentFloor)) //电梯向上 + 上面无外呼 + 下面有外呼
                    {
                        elev.RunAsync(DirectionStyle.Down);                                                                                                                         //调头
                        return;
                    }
                    if (elev.Direction == DirectionStyle.Down && !elev.ReachList.Exists(x => x.Floor < elev.CurrentFloor) && elev.ReachList.Exists(x => x.Floor > elev.CurrentFloor)) //电梯向下 + 下面无外呼 + 上面有外呼
                    {
                        elev.RunAsync(DirectionStyle.Up);                                                                                                                             //调头
                        return;
                    }
                    elev.RunAsync();
                    return;
                }
                else if (elev.ReachList.Exists(x => x.Direction != elev.Direction))    //无内呼 + 有外呼 + 反向
                {
                    elev.Direction = elev.Direction == DirectionStyle.Up ? DirectionStyle.Down : DirectionStyle.Up;
                    elev.RunAsync(elev.Direction);
                }
                #endregion
                break;

            case StatusStyle.Opening:
                elev.RemoveFromReachList();
                break;
            }
        }