/*If sufficient number of condition is met, will invoke an action*/
void mapConditions()
{
    if (strcmp(mapName, "filtering") == 0)
    {
        if (action1 != FALSE && action2 != FALSE)
            openDoor(R, RIGHT);
        else
            closeDoor(R, RIGHT);
    }
}
Exemple #2
0
void handle_connection(int fd)
{
    int ret;
    char recv_buf[256];
    for (;;)
    {
        memset(recv_buf, 0, sizeof(recv_buf));
        ret = recv(fd, recv_buf, sizeof(recv_buf), 0);
        if (ret > 0)
        {
            printf("Request received : %s\n", recv_buf);
            int return_code = 0;
            if (strcmp(recv_buf, "openDoor") == 0)
            {
                return_code = openDoor();
            }
            else if (strcmp(recv_buf, "closeDoor") == 0)
            {
                return_code = closeDoor();
            }
            else if (strcmp(recv_buf, "getState") == 0)
            {
            	return_code = getDoorState();
            }
            /* Send data back */
            char result[512];
            if (return_code >= 0)
            {
                sprintf(result, "success:%d", return_code);
            }
            else
            {
                sprintf(result, "fail:%d", return_code);
            }
            ret = send(fd, result, strlen(result) + 1, 0);
            if (ret <= 0)
            {
                puts("Cannot send data to server, disconnect");
                close(fd);
                break;
            }
        }
        else
        {
            puts("Cannot receive data from server, disconnect");
            close(fd);
            break;
        }
    }
}
int funcDoor(String command)
{
  int status = -1;

  if( strcmp(command, "Status") == 0 )
  {
    status = door_status == DS_Open ? 1 : 0;
  }
  else
  if( strcmp(command, "Open") == 0 )
  {
    openDoor();
    status = 3;
  }
  else
  if( strcmp(command, "Close") == 0 )
  {
    closeDoor();
    status = 4;
  }

  return status;
}
/*Will open/close the doors*/
int useDoor(int searchFor, int direction)
{
    if (openDoor(searchFor, direction) == FALSE)
        closeDoor(searchFor, direction);
    return 0;
}
/***********************************************************
	函数名称: state_trans()
	函数功能: 控制电梯的状态
	入口参数: 无
	出口参数: 无
	备 注: 使用到的全局变量
			buttonUp,buttonDown,buttonNumber,currentFloor,state,nextFloor,moveDirection
			其中nextFloor,moveDirection为只读 不可修改
			使用自动机模型 电梯运行速度2秒每层 电梯停靠时间为4秒
***********************************************************/
void state_trans()
{
    static int reopen = 0;//电梯关门时按下开门键的标示

    switch (state) //根据状态和事件,进行相应的处理
    {
        case
                IDLE: //电梯空闲状态
            if (openbutton == 1)
            {
                state = STOP;//电梯按下开门按钮进入停靠状态
            }

            if (moveDirection != STOP) //由control函数发现有不同层响应呼叫 电梯进入运行状态
            {
                state = RUN;//电梯进入运行状态
                startIDLE = 1;
            }
            else
                if (currentFloor == nextFloor) //由control函数发现有同层响应呼叫 电梯进入停靠状态
                {
                    state = STOP;
                    startIDLE = 1;
                }
                else
                {
                    if (Profile == C)
                    {
                        if (startIDLE == 1)
                        {
                            a = clock();
                            startIDLE = 0;
                        }
                        else
                        {
                            b = clock();
                            c = (double) (b - a) / CLOCKS_PER_SEC;

                            if (c > waitTime) //等待waitTime秒
                            {
                                if (currentFloor != 1)
                                {
                                    nextFloor = -1;
                                    moveDirection = DOWN;
                                    state = RUN;
                                    startIDLE = 1;
                                }

                            }
                        }
                    }
                    else
                    {
                        moveDirection = STOP;
                    }
                }

            break;

        case
                RUN://电梯运行状态
            if (currentFloor == ignore)//安全运行结束
            {
                ignore = 0;
            }

            if (nextFloor == -1 || profileCfindnextfloor == 1)
            {

                if (currentFloor != 1) //未到目标楼层
                {

                    if (startRUN == 1)//计时器开始计时
                    {
                        directa = ar = clock();
                        startRUN = 0;
                    }
                    else
                    {
                        directb = br = clock();
                        cr = (double) (br - ar) / CLOCKS_PER_SEC;
                        directc = (double) (directb - directa) / CLOCKS_PER_SEC;
                        elevatorMove((double)cr / velocity);//电梯运行动画

                        if (directc >= 0.05)
                        {
                            directa = clock();
                            showDirection_run(0);//指示灯动画
                        }

                        if (cr > velocity)//时间到velocity秒
                        {
                            currentFloor--;
                            profileCfindnextfloor = 0;
                            showFloor();
                            elevatorMove(0);
                            startRUN = 1;
                        }

                    }

                }

                if (currentFloor == 1) //电梯到达目标楼层
                {
                    nextFloor = 0;
                    state = IDLE;
                    moveDirection = STOP;
                    showDirection_stop();
                }

            }
            else//profileCfindnextfloor==0
            {

                if (startRun == 1)
                {
                    staticStart = clock();//统计计时
                    startRun = 0;
                }

                if (currentFloor != nextFloor || profileEFfindnextfloor == 1) //未到目标楼层
                {
                    if (startRUN == 1)
                    {
                        directa = ar = clock();
                        startRUN = 0;
                        profileEFfindnextfloor = 0;
                    }
                    else
                    {

                        if (profileEFfindnextfloor == 1)
                        {
                            static double temptime1, temptime = 0;

                            if (startRUNEF == 1)
                            {
                                br = clock();
                                temptime = (double) (br - ar) / CLOCKS_PER_SEC;
                                temptime1 = temptime;
                                ar = clock();
                                startRUNEF = 0;
                            }
                            else
                            {

                                directb = br = clock();
                                cr = (double) (br - ar) / CLOCKS_PER_SEC;
                                directc = (double) (directb - directa) / CLOCKS_PER_SEC;


                                temptime = temptime1 - cr;

                                if (temptime > 0)
                                {

                                    elevatorMove((double)(velocity - temptime) / velocity);

                                }

                                if (directc >= 0.05)
                                {
                                    directa = clock();
                                    showDirection_run(0);//指示灯动画
                                }

                                if (temptime < 0)
                                {

                                    showFloor();//显示当前楼层

                                    profileEFfindnextfloor = 0;
                                    elevatorMove(0);//电梯运行动画
                                    startRUNEF = 1;
                                    startRUN = 1;
                                    temptime = 0;
                                }


                            }


                        }

                        else

                        {
                            directb = br = clock();

                            cr = (double) (br - ar) / CLOCKS_PER_SEC;
                            directc = (double) (directb - directa) / CLOCKS_PER_SEC;

                            if (profileEFfindnextfloor == 0)
                            {
                                elevatorMove((double)cr / velocity);//电梯运行动画
                            }

                            if (directc >= 0.05)
                            {
                                directa = clock();
                                showDirection_run(0);//指示灯动画
                            }

                            if (cr > velocity)
                            {
                                currentFloor += moveDirection;
                                showFloor();//显示当前楼层

                                if (profileEFfindnextfloor == 0)
                                {
                                    elevatorMove(0);//电梯运行动画
                                }

                                startRUN = 1;
                            }
                        }


                    }
                }




                if ((currentFloor == nextFloor) && (profileEFfindnextfloor == 0)) //电梯到达目标楼层
                {
                    if (moveDirection == UP) //关闭相应的按钮 电梯进入停靠状态
                    {
                        if (Profile == B)//B策略单独处理
                        {
                            if (headPtr != NULL)
                            {
                                closeButton();
                                eachNumber[previousProfile]++;
                                deleteNode(headPtr->data);
                            }
                        }

                        else//关闭按钮
                        {
                            if((buttonUp[currentFloor] == 0) && (buttonNumber[currentFloor] == 0))
                            {
                                buttonDown[currentFloor] = 0;
                                deleteNode(listDown[currentFloor]);
                                lightOff(listDown[currentFloor]);

                                eachNumber[previousProfile]++;
                            }
                            else//关闭按钮
                            {
                                buttonUp[currentFloor] = 0;
                                buttonNumber[currentFloor] = 0;
                                lightOff(listUp[currentFloor]);
                                lightOff(listNumber[currentFloor]);

                                if (headPtr != NULL)
                                {
                                    eachNumber[previousProfile]++;
                                    deleteNode(listUp[currentFloor]);

                                    deleteNode(listNumber[currentFloor]);
                                }


                            }
                        }

                        state = STOP;
                        startRun = 1;
                        staticStop = clock();
                        totalTime[previousProfile] += (double) (staticStop - staticStart) / CLOCKS_PER_SEC;//统计时间

                    }

                    if (moveDirection == DOWN) //关闭相应的按钮 电梯进入停靠状态

                    {
                        if (Profile == B)//B策略需单独处理
                        {
                            if (headPtr != NULL)
                            {
                                closeButton();
                                deleteNode(headPtr->data);


                                eachNumber[previousProfile]++;
                            }
                        }
                        else//关闭按钮
                        {
                            if ((buttonDown[currentFloor] == 0) && (buttonNumber[currentFloor] == 0))
                            {
                                buttonUp[currentFloor] = 0;
                                lightOff(listUp[currentFloor]);
                                deleteNode(listUp[currentFloor]);

                                eachNumber[previousProfile]++;
                            }
                            else//关闭按钮
                            {

                                buttonDown[currentFloor] = 0;
                                buttonNumber[currentFloor] = 0;
                                lightOff(listDown[currentFloor]);
                                lightOff(listNumber[currentFloor]);

                                if (headPtr != NULL)
                                {
                                    eachNumber[previousProfile]++;
                                    deleteNode(listDown[currentFloor]);
                                    deleteNode(listNumber[currentFloor]);
                                }
                            }
                        }

                        state = STOP;//进入停靠
                        startRun = 1;
                        staticStop = clock();

                        totalTime[previousProfile] += (double) (staticStop - staticStart) / CLOCKS_PER_SEC;//统计时间

                    }

                    if (moveDirection == STOP) //如果电梯无运行方向 电梯进入停靠状态
                    {
                        state = STOP;
                        startRun = 1;
                        staticStop = clock();
                        //时间统计
                        totalTime[previousProfile] += (double) (staticStop - staticStart) / CLOCKS_PER_SEC;//统计时间
                    }
                }

            }

            break;

        case
                STOP: //电梯停靠状态

            if (startSTOP == 1)//刚开始停靠
            {
                DWORD ThreadIDa = 1, ThreadIDb = 2;
                HANDLE hReada, hReadb;
                as = clock();//计时器初始化

                if (reopen == 1)//判断是否开门
                {
                    reopen = 0;
                }
                else
                {
                    //开门动画,指示灯动画并且播放语音
                    hReada = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)arriveSound, NULL, 0, &ThreadIDa);
                    hReadb = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)showDirection_stop, NULL, 0, &ThreadIDb);
                    openDoor();

                    startSTOP = 0;
                }

            }
            else
            {

                if (openbutton == 1)//长按开门键则不关门
                {
                    as = clock();
                    restTime = stopTime;
                }
                else
                {
                    showDirection_stop();
                    bs = clock();
                    cs = (double) (bs - as) / CLOCKS_PER_SEC;
                    restTime = stopTime - cs;

                    if (cs > stopTime || Go == 1)//Go指令判断
                    {
                        startSTOP = 1;

                    }

                }

            }

            if (startSTOP == 1)
            {

                if (nextFloor == 0) //如果无目标楼层 电梯进入空闲状态
                {
                    if (closeDoor() == 1)
                    {
                        state = STOP;
                        startSTOP = 1;
                        reopen = 1;
                        break;
                    }

                    moveDirection = STOP;
                    state = IDLE;

                    if (Profile == B)
                    {
                        showDirection_stop();
                    }

                }
                else//有目标楼层则电梯进入运行状态
                {
                    if (closeDoor())
                    {
                        state = STOP;
                        startSTOP = 1;
                        reopen = 1;
                        break;
                    }

                    state = RUN;
                    control();
                    gosound();

                }

            }

            break;

    }//end of switch
}
/*Define here what the action characters on the map will do when entered on their area*/
void pAAction(int aChar)
{
    if (strcmp(mapName, "menu") == 0)
    {
        switch (aChar)
        {
        case 1:
            initializeSort();
            switchMap("filtering");
            break;
        case 2:
            initializeSort();
            switchMap("sorting");
            break;
        case 3:
            teleport(aChar);
            break;
        case 4:
            closeProg();
            break;
        }
    }
    else if (strcmp(mapName, "sorting") == 0)
    {
        switch (aChar)
        {
        case A:
            teleport(B);
            scrollData(UP);
            break;
        case C:
            teleport(B);
            scrollData(DOWN);
            break;
        }
    }
    else if (strcmp(mapName, "filtering") == 0)
    {
        switch (aChar)
        {
        case 1:
            teleport(2);
            scrollData(UP);
            break;
        case 3:
            teleport(2);
            scrollData(DOWN);
            break;
        case 7:
            closeDoor(Q, DOWN);
            break;
        case 8:
            closeDoor(W, UP);
            break;
        }
    }
    else
    {
        perror ("'mapName' out of speck");
        SC(mapName);
        assert(!TRUE);
    }
}
void onClosePressed(Button & button)
{
  closeDoor();
}