LiftThread.cpp
资源名称:LIFTT.rar [点击查看]
上传用户:jmteguang
上传日期:2022-05-24
资源大小:61k
文件大小:9k
源码类别:
进程与线程
开发平台:
Visual C++
- // LiftThread.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "LIFTT.h"
- #include "LiftThread.h"
- // CLiftThread
- IMPLEMENT_DYNAMIC(CLiftThread, CWnd)
- CLiftThread::CLiftThread()
- {
- liftWarn = FALSE;
- liftState = LIFTCLOSE;
- liftFloor = 1;
- liftRunning = NULL;
- liftTurn.liftMax = 1;
- liftTurn.liftMin = 20;
- liftTurn.upDown.clear();
- }
- CLiftThread::~CLiftThread()
- {
- }
- BEGIN_MESSAGE_MAP(CLiftThread, CWnd)
- END_MESSAGE_MAP()
- // CLiftThread 消息处理程序
- UINT CLiftThread::RunningLift(LPVOID pParam)
- {
- while (1)
- {
- CLiftThread * moveLift = (CLiftThread *)pParam;
- while (!moveLift->liftTurn.upDown.empty())
- {
- /*if (moveLift->liftTurn.upDown.front().allNumber == LIFTWARN)
- {
- moveLift->liftState = LIFTWARN;
- moveLift->liftTurn.upDown.clear();
- }
- else*/ if (moveLift->liftTurn.upDown.front().allNumber == LIFTCLOSE)
- {
- moveLift->liftMove.SetWindowText(L"关");
- moveLift->liftState = LIFTCLOSE;
- moveLift->liftTurn.upDown.erase(moveLift->liftTurn.upDown.begin());
- }
- else if (moveLift->liftTurn.upDown.front().allNumber == LIFTOPEN)
- {
- moveLift->liftMove.SetWindowText(L"开");
- moveLift->liftState = LIFTOPEN;
- Sleep(1000);
- moveLift->liftMove.SetWindowText(L"关");
- moveLift->liftState = LIFTCLOSE;
- moveLift->liftTurn.upDown.erase(moveLift->liftTurn.upDown.begin());
- }
- else
- {
- if (moveLift->liftFloor < moveLift->liftTurn.upDown.front().buttonNumber)
- {
- moveLift->liftState = LIFTUP;
- CRect temp;
- moveLift->liftMove.GetWindowRect(&temp);
- moveLift->liftMove.GetParent()->ScreenToClient(&temp);
- moveLift->liftMove.MoveWindow(temp.left,temp.top-25,100,25);
- CString cstr;
- cstr.Format(L"%d",moveLift->GetLiftFloor()+1);
- moveLift->liftFloor++;
- moveLift->liftMove.SetWindowText(cstr);
- }
- if (moveLift->liftFloor > moveLift->liftTurn.upDown.front().buttonNumber)
- {
- moveLift->liftState = LIFTDOWN;
- CRect temp;
- moveLift->liftMove.GetWindowRect(&temp);
- moveLift->liftMove.GetParent()->ScreenToClient(&temp);
- moveLift->liftMove.MoveWindow(temp.left,temp.top+25,100,25);
- CString cstr;
- cstr.Format(L"%d",moveLift->GetLiftFloor()-1);
- moveLift->liftFloor--;
- moveLift->liftMove.SetWindowText(cstr);
- }
- if (moveLift->liftFloor == moveLift->liftTurn.upDown.front().buttonNumber)
- {
- moveLift->liftMove.SetWindowText(L"开");
- Sleep(500);
- moveLift->liftMove.SetWindowText(L"关");
- Sleep(500);
- CString cstr;
- cstr.Format(L"%d",moveLift->GetLiftFloor());
- moveLift->liftMove.SetWindowText(cstr);
- moveLift->liftTurn.upDown.erase(moveLift->liftTurn.upDown.begin());
- moveLift->liftTurn.liftMax = 1;
- moveLift->liftTurn.liftMin = 20;
- for (std::vector<lift>::iterator i = moveLift->liftTurn.upDown.begin();i != moveLift->liftTurn.upDown.end();i ++)
- {
- if (moveLift->liftTurn.liftMax < i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
- {
- moveLift->liftTurn.liftMax = i->buttonNumber;
- }
- if (moveLift->liftTurn.liftMin > i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
- {
- moveLift->liftTurn.liftMin = i->buttonNumber;
- }
- }
- }
- }
- Sleep(500);
- }
- moveLift->liftState = LIFTCLOSE;
- Sleep(1000);
- }
- return 0;
- }
- void CLiftThread::SetLiftFloor(int temp)
- {
- liftFloor = temp;
- }
- void CLiftThread::SetLiftState(int temp)
- {
- liftState = temp;
- }
- void CLiftThread::SetLiftTo(lift numberState)
- {
- if (numberState.allNumber == LIFTWARN)
- {
- if (!liftWarn)
- {
- liftWarn = TRUE;
- liftTurn.upDown.clear();
- //liftTurn.upDown.push_back(numberState);
- liftTurn.liftMax = 1;
- liftTurn.liftMin = 20;
- setTo = 1000000;
- liftMove.SetWindowText(L"警");
- }
- else
- {
- liftWarn = FALSE;
- liftMove.SetWindowText(L"关");
- }
- }
- if (!liftWarn && numberState.allNumber != LIFTWARN)
- {
- if (numberState.allNumber == LIFTOPEN || numberState.allNumber == LIFTCLOSE)
- {
- if (liftState == LIFTCLOSE)
- {
- liftTurn.upDown.push_back(numberState);
- goto asfd;
- }
- }
- if (liftState == LIFTCLOSE || liftState == LIFTOPEN)
- {
- liftTurn.upDown.push_back(numberState);
- goto asfd;
- }
- if (liftState == numberState.allNumber)
- {
- if (liftState == LIFTUP)
- {
- if (numberState.buttonNumber > liftFloor)
- {
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->allNumber == LIFTUP && i->buttonNumber > liftFloor)
- {
- if (numberState.buttonNumber < i->buttonNumber)
- {
- liftTurn.upDown.insert(i,numberState);
- goto asfd;
- }
- }
- }
- liftTurn.upDown.insert(liftTurn.upDown.begin(),numberState);
- goto asfd;
- }
- else
- {
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->allNumber == LIFTUP && i->buttonNumber < liftFloor)
- {
- if (numberState.buttonNumber < i->buttonNumber)
- {
- liftTurn.upDown.insert(i,numberState);
- goto asfd;
- }
- }
- }
- std::vector<lift>::iterator i = liftTurn.upDown.end()-1;
- while (liftTurn.upDown.back().allNumber == LIFTOPEN || liftTurn.upDown.back().allNumber == LIFTCLOSE)
- {
- i --;
- }
- if ((int)liftTurn.upDown.size() == 1)
- {
- liftTurn.upDown.push_back(numberState);
- goto asfd;
- }
- else
- {
- liftTurn.upDown.insert(i+1,numberState);
- goto asfd;
- }
- }
- }
- else
- {
- if (numberState.buttonNumber < liftFloor)
- {
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->allNumber == LIFTDOWN && i->buttonNumber < liftFloor)
- {
- if (numberState.buttonNumber > i->buttonNumber)
- {
- liftTurn.upDown.insert(i,numberState);
- goto asfd;
- }
- }
- }
- liftTurn.upDown.insert(liftTurn.upDown.begin(),numberState);
- goto asfd;
- }
- else
- {
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->allNumber == LIFTDOWN && i->buttonNumber > liftFloor)
- {
- if (numberState.buttonNumber > i->buttonNumber)
- {
- liftTurn.upDown.insert(i,numberState);
- goto asfd;
- }
- }
- }
- std::vector<lift>::iterator i = liftTurn.upDown.end()-1;
- while (liftTurn.upDown.back().allNumber == LIFTOPEN || liftTurn.upDown.back().allNumber == LIFTCLOSE)
- {
- i --;
- }
- if ((int)liftTurn.upDown.size() == 1)
- {
- liftTurn.upDown.push_back(numberState);
- goto asfd;
- }
- else
- {
- liftTurn.upDown.insert(i+1,numberState);
- goto asfd;
- }
- }
- }
- }
- if ((liftState == LIFTUP && numberState.allNumber == LIFTDOWN) || (liftState == LIFTDOWN && numberState.allNumber == LIFTUP))
- {
- if (liftState == LIFTUP)
- {
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->allNumber == LIFTDOWN)
- {
- if (numberState.buttonNumber > i->buttonNumber)
- {
- liftTurn.upDown.insert(i,numberState);
- goto asfd;
- }
- }
- }
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->buttonNumber == liftTurn.liftMax)
- {
- if ((int)liftTurn.upDown.size() == 1)
- {
- liftTurn.upDown.push_back(numberState);
- goto asfd;
- }
- else
- {
- liftTurn.upDown.insert(i+1,numberState);
- goto asfd;
- }
- }
- }
- }
- else
- {
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->allNumber == LIFTUP)
- {
- if (numberState.buttonNumber < i->buttonNumber)
- {
- liftTurn.upDown.insert(i,numberState);
- goto asfd;
- }
- }
- }
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (i->buttonNumber == liftTurn.liftMin)
- {
- if ((int)liftTurn.upDown.size() == 1)
- {
- liftTurn.upDown.push_back(numberState);
- goto asfd;
- }
- else
- {
- liftTurn.upDown.insert(i+1,numberState);
- goto asfd;
- }
- }
- }
- }
- }
- asfd:
- liftTurn.liftMax = 1;
- liftTurn.liftMin = 20;
- for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
- {
- if (liftTurn.liftMax < i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
- {
- liftTurn.liftMax = i->buttonNumber;
- }
- if (liftTurn.liftMin > i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
- {
- liftTurn.liftMin = i->buttonNumber;
- }
- }
- }
- }
- void CLiftThread::StartLiftThread()
- {
- liftRunning = AfxBeginThread(RunningLift,this);
- }
- int CLiftThread::GetLiftFloor()
- {
- return liftFloor;
- }
- int CLiftThread::GetLiftState()
- {
- return liftState;
- }