LiftThread.cpp
上传用户:jmteguang
上传日期:2022-05-24
资源大小:61k
文件大小:9k
源码类别:

进程与线程

开发平台:

Visual C++

  1. // LiftThread.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "LIFTT.h"
  5. #include "LiftThread.h"
  6. // CLiftThread
  7. IMPLEMENT_DYNAMIC(CLiftThread, CWnd)
  8. CLiftThread::CLiftThread()
  9. {
  10. liftWarn = FALSE;
  11. liftState = LIFTCLOSE;
  12. liftFloor = 1;
  13. liftRunning = NULL;
  14. liftTurn.liftMax = 1;
  15. liftTurn.liftMin = 20;
  16. liftTurn.upDown.clear();
  17. }
  18. CLiftThread::~CLiftThread()
  19. {
  20. }
  21. BEGIN_MESSAGE_MAP(CLiftThread, CWnd)
  22. END_MESSAGE_MAP()
  23. // CLiftThread 消息处理程序
  24. UINT CLiftThread::RunningLift(LPVOID pParam)
  25. {
  26. while (1)
  27. {
  28. CLiftThread * moveLift = (CLiftThread *)pParam;
  29. while (!moveLift->liftTurn.upDown.empty())
  30. {
  31. /*if (moveLift->liftTurn.upDown.front().allNumber == LIFTWARN)
  32. {
  33. moveLift->liftState = LIFTWARN;
  34. moveLift->liftTurn.upDown.clear();
  35. }
  36. else*/ if (moveLift->liftTurn.upDown.front().allNumber == LIFTCLOSE)
  37. {
  38. moveLift->liftMove.SetWindowText(L"关");
  39. moveLift->liftState = LIFTCLOSE;
  40. moveLift->liftTurn.upDown.erase(moveLift->liftTurn.upDown.begin());
  41. }
  42. else if (moveLift->liftTurn.upDown.front().allNumber == LIFTOPEN)
  43. {
  44. moveLift->liftMove.SetWindowText(L"开");
  45. moveLift->liftState = LIFTOPEN;
  46. Sleep(1000);
  47. moveLift->liftMove.SetWindowText(L"关");
  48. moveLift->liftState = LIFTCLOSE;
  49. moveLift->liftTurn.upDown.erase(moveLift->liftTurn.upDown.begin());
  50. }
  51. else
  52. {
  53. if (moveLift->liftFloor < moveLift->liftTurn.upDown.front().buttonNumber)
  54. {
  55. moveLift->liftState = LIFTUP;
  56. CRect temp;
  57. moveLift->liftMove.GetWindowRect(&temp);
  58. moveLift->liftMove.GetParent()->ScreenToClient(&temp);
  59. moveLift->liftMove.MoveWindow(temp.left,temp.top-25,100,25);
  60. CString cstr;
  61. cstr.Format(L"%d",moveLift->GetLiftFloor()+1);
  62. moveLift->liftFloor++;
  63. moveLift->liftMove.SetWindowText(cstr);
  64. }
  65. if (moveLift->liftFloor > moveLift->liftTurn.upDown.front().buttonNumber)
  66. {
  67. moveLift->liftState = LIFTDOWN;
  68. CRect temp;
  69. moveLift->liftMove.GetWindowRect(&temp);
  70. moveLift->liftMove.GetParent()->ScreenToClient(&temp);
  71. moveLift->liftMove.MoveWindow(temp.left,temp.top+25,100,25);
  72. CString cstr;
  73. cstr.Format(L"%d",moveLift->GetLiftFloor()-1);
  74. moveLift->liftFloor--;
  75. moveLift->liftMove.SetWindowText(cstr);
  76. }
  77. if (moveLift->liftFloor == moveLift->liftTurn.upDown.front().buttonNumber)
  78. {
  79. moveLift->liftMove.SetWindowText(L"开");
  80. Sleep(500);
  81. moveLift->liftMove.SetWindowText(L"关");
  82. Sleep(500);
  83. CString cstr;
  84. cstr.Format(L"%d",moveLift->GetLiftFloor());
  85. moveLift->liftMove.SetWindowText(cstr);
  86. moveLift->liftTurn.upDown.erase(moveLift->liftTurn.upDown.begin());
  87. moveLift->liftTurn.liftMax = 1;
  88. moveLift->liftTurn.liftMin = 20;
  89. for (std::vector<lift>::iterator i = moveLift->liftTurn.upDown.begin();i != moveLift->liftTurn.upDown.end();i ++)
  90. {
  91. if (moveLift->liftTurn.liftMax < i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
  92. {
  93. moveLift->liftTurn.liftMax = i->buttonNumber;
  94. }
  95. if (moveLift->liftTurn.liftMin > i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
  96. {
  97. moveLift->liftTurn.liftMin = i->buttonNumber;
  98. }
  99. }
  100. }
  101. }
  102. Sleep(500);
  103. }
  104. moveLift->liftState = LIFTCLOSE;
  105. Sleep(1000);
  106. }
  107. return 0;
  108. }
  109. void CLiftThread::SetLiftFloor(int temp)
  110. {
  111. liftFloor = temp;
  112. }
  113. void CLiftThread::SetLiftState(int temp)
  114. {
  115. liftState = temp;
  116. }
  117. void CLiftThread::SetLiftTo(lift numberState)
  118. {
  119. if (numberState.allNumber == LIFTWARN)
  120. {
  121. if (!liftWarn)
  122. {
  123. liftWarn = TRUE;
  124. liftTurn.upDown.clear();
  125. //liftTurn.upDown.push_back(numberState);
  126. liftTurn.liftMax = 1;
  127. liftTurn.liftMin = 20;
  128. setTo = 1000000;
  129. liftMove.SetWindowText(L"警");
  130. }
  131. else
  132. {
  133. liftWarn = FALSE;
  134. liftMove.SetWindowText(L"关");
  135. }
  136. }
  137. if (!liftWarn && numberState.allNumber != LIFTWARN)
  138. {
  139. if (numberState.allNumber == LIFTOPEN || numberState.allNumber == LIFTCLOSE)
  140. {
  141. if (liftState == LIFTCLOSE)
  142. {
  143. liftTurn.upDown.push_back(numberState);
  144. goto asfd;
  145. }
  146. }
  147. if (liftState == LIFTCLOSE || liftState == LIFTOPEN)
  148. {
  149. liftTurn.upDown.push_back(numberState);
  150. goto asfd;
  151. }
  152. if (liftState == numberState.allNumber)
  153. {
  154. if (liftState == LIFTUP)
  155. {
  156. if (numberState.buttonNumber > liftFloor)
  157. {
  158. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  159. {
  160. if (i->allNumber == LIFTUP && i->buttonNumber > liftFloor)
  161. {
  162. if (numberState.buttonNumber < i->buttonNumber)
  163. {
  164. liftTurn.upDown.insert(i,numberState);
  165. goto asfd;
  166. }
  167. }
  168. }
  169. liftTurn.upDown.insert(liftTurn.upDown.begin(),numberState);
  170. goto asfd;
  171. }
  172. else
  173. {
  174. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  175. {
  176. if (i->allNumber == LIFTUP && i->buttonNumber < liftFloor)
  177. {
  178. if (numberState.buttonNumber < i->buttonNumber)
  179. {
  180. liftTurn.upDown.insert(i,numberState);
  181. goto asfd;
  182. }
  183. }
  184. }
  185. std::vector<lift>::iterator i = liftTurn.upDown.end()-1;
  186. while (liftTurn.upDown.back().allNumber == LIFTOPEN || liftTurn.upDown.back().allNumber == LIFTCLOSE)
  187. {
  188. i --;
  189. }
  190. if ((int)liftTurn.upDown.size() == 1)
  191. {
  192. liftTurn.upDown.push_back(numberState);
  193. goto asfd;
  194. }
  195. else
  196. {
  197. liftTurn.upDown.insert(i+1,numberState);
  198. goto asfd;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. if (numberState.buttonNumber < liftFloor)
  205. {
  206. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  207. {
  208. if (i->allNumber == LIFTDOWN && i->buttonNumber < liftFloor)
  209. {
  210. if (numberState.buttonNumber > i->buttonNumber)
  211. {
  212. liftTurn.upDown.insert(i,numberState);
  213. goto asfd;
  214. }
  215. }
  216. }
  217. liftTurn.upDown.insert(liftTurn.upDown.begin(),numberState);
  218. goto asfd;
  219. }
  220. else
  221. {
  222. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  223. {
  224. if (i->allNumber == LIFTDOWN && i->buttonNumber > liftFloor)
  225. {
  226. if (numberState.buttonNumber > i->buttonNumber)
  227. {
  228. liftTurn.upDown.insert(i,numberState);
  229. goto asfd;
  230. }
  231. }
  232. }
  233. std::vector<lift>::iterator i = liftTurn.upDown.end()-1;
  234. while (liftTurn.upDown.back().allNumber == LIFTOPEN || liftTurn.upDown.back().allNumber == LIFTCLOSE)
  235. {
  236. i --;
  237. }
  238. if ((int)liftTurn.upDown.size() == 1)
  239. {
  240. liftTurn.upDown.push_back(numberState);
  241. goto asfd;
  242. }
  243. else
  244. {
  245. liftTurn.upDown.insert(i+1,numberState);
  246. goto asfd;
  247. }
  248. }
  249. }
  250. }
  251. if ((liftState == LIFTUP && numberState.allNumber == LIFTDOWN) || (liftState == LIFTDOWN && numberState.allNumber == LIFTUP))
  252. {
  253. if (liftState == LIFTUP)
  254. {
  255. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  256. {
  257. if (i->allNumber == LIFTDOWN)
  258. {
  259. if (numberState.buttonNumber > i->buttonNumber)
  260. {
  261. liftTurn.upDown.insert(i,numberState);
  262. goto asfd;
  263. }
  264. }
  265. }
  266. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  267. {
  268. if (i->buttonNumber == liftTurn.liftMax)
  269. {
  270. if ((int)liftTurn.upDown.size() == 1)
  271. {
  272. liftTurn.upDown.push_back(numberState);
  273. goto asfd;
  274. }
  275. else
  276. {
  277. liftTurn.upDown.insert(i+1,numberState);
  278. goto asfd;
  279. }
  280. }
  281. }
  282. }
  283. else
  284. {
  285. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  286. {
  287. if (i->allNumber == LIFTUP)
  288. {
  289. if (numberState.buttonNumber < i->buttonNumber)
  290. {
  291. liftTurn.upDown.insert(i,numberState);
  292. goto asfd;
  293. }
  294. }
  295. }
  296. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  297. {
  298. if (i->buttonNumber == liftTurn.liftMin)
  299. {
  300. if ((int)liftTurn.upDown.size() == 1)
  301. {
  302. liftTurn.upDown.push_back(numberState);
  303. goto asfd;
  304. }
  305. else
  306. {
  307. liftTurn.upDown.insert(i+1,numberState);
  308. goto asfd;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. asfd:
  315. liftTurn.liftMax = 1;
  316. liftTurn.liftMin = 20;
  317. for (std::vector<lift>::iterator i = liftTurn.upDown.begin();i != liftTurn.upDown.end();i ++)
  318. {
  319. if (liftTurn.liftMax < i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
  320. {
  321. liftTurn.liftMax = i->buttonNumber;
  322. }
  323. if (liftTurn.liftMin > i->buttonNumber && (i->allNumber == LIFTUP || i->allNumber == LIFTDOWN))
  324. {
  325. liftTurn.liftMin = i->buttonNumber;
  326. }
  327. }
  328. }
  329. }
  330. void CLiftThread::StartLiftThread()
  331. {
  332. liftRunning = AfxBeginThread(RunningLift,this);
  333. }
  334. int CLiftThread::GetLiftFloor()
  335. {
  336. return liftFloor;
  337. }
  338. int CLiftThread::GetLiftState()
  339. {
  340. return liftState;
  341. }