dispatch_device_base.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //
  2. // Created by huli on 2021/3/3.
  3. //
  4. #include "dispatch_device_base.h"
  5. Dispatch_device_base::Dispatch_device_base()
  6. {
  7. m_dispatch_device_status = E_UNKNOW;
  8. m_device_id = -1;
  9. mp_execute_thread = NULL;
  10. }
  11. Dispatch_device_base::~Dispatch_device_base()
  12. {
  13. dispatch_device_base_uninit();
  14. }
  15. //设备 初始化
  16. Error_manager Dispatch_device_base::dispatch_device_base_init(int device_id)
  17. {
  18. m_device_id = device_id;
  19. // 线程默认开启
  20. m_execute_condition.reset(false, true, false);
  21. mp_execute_thread = new std::thread(&Dispatch_device_base::execute_thread_fun, this);
  22. m_dispatch_device_status = E_READY;
  23. return Error_code::SUCCESS;
  24. }
  25. //设备 反初始化
  26. Error_manager Dispatch_device_base::dispatch_device_base_uninit()
  27. {
  28. if (mp_execute_thread)
  29. {
  30. m_execute_condition.kill_all();
  31. }
  32. if (mp_execute_thread)
  33. {
  34. mp_execute_thread->join();
  35. delete mp_execute_thread;
  36. mp_execute_thread = NULL;
  37. }
  38. m_dispatch_device_status = E_UNKNOW;
  39. return Error_code::SUCCESS;
  40. }
  41. //执行任务
  42. Error_manager Dispatch_device_base::execute_task(std::shared_ptr<Task_Base> p_task, Dispatch_task_level dispatch_task_level)
  43. {
  44. LOG(INFO) << " ---Dispatch_device_base::execute_task ---"<< this;
  45. Error_manager t_error;
  46. Error_manager t_result;
  47. //检查指针
  48. if (p_task.get() == NULL) {
  49. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  50. "Dispatch_device_base::execute_task failed, POINTER_IS_NULL");
  51. }
  52. //检查任务类型,
  53. t_error = check_task_type(p_task);
  54. if ( t_error != Error_code::SUCCESS )
  55. {
  56. return t_error;
  57. }
  58. //检查接收方能否接受任务
  59. t_error = check_task_level(dispatch_task_level);
  60. if ( t_error != SUCCESS )
  61. {
  62. t_result.compare_and_cover_error(t_error);
  63. }
  64. else
  65. {
  66. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  67. t_error = sign_for_task(p_task, dispatch_task_level);
  68. if ( t_error != Error_code::SUCCESS )
  69. {
  70. return t_error;
  71. }
  72. //这里不用检查任务内容, 直接下发给底层设备
  73. //永远启动到三级任务工作状态.
  74. // 启动定位管理模块,的核心工作线程
  75. m_dispatch_device_status = E_THREE_LEVEL_WORK;
  76. m_execute_condition.notify_all(true);
  77. //通知 thread_work 子线程启动。
  78. //只签收,并不一定进入工作状态, 在线程真正的执行的时候,才改为工作中, (执行线程可能会先处理更高优先级的任务单)
  79. }
  80. if ( t_result != Error_code::SUCCESS )
  81. {
  82. return t_result;
  83. }
  84. return Error_code::SUCCESS;
  85. }
  86. //检查任务类型, 子类必须重载, 用来检查输入的任务是否为子类所需的.
  87. Error_manager Dispatch_device_base::check_task_type(std::shared_ptr<Task_Base> p_task)
  88. {
  89. //检查任务类型,
  90. // if (p_task->get_task_type() != CARRIER_TASK)
  91. // {
  92. // return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  93. // "Dispatch_device_base::execute_one_level_task get_task_type() != CARRIER_TASK ");
  94. // }
  95. return Error_code::SUCCESS;
  96. }
  97. //判断能否执行任务
  98. Error_manager Dispatch_device_base::check_task_level(Dispatch_task_level dispatch_task_level)
  99. {
  100. std::cout << " huli test :::: " << " m_dispatch_device_status = " << m_dispatch_device_status << std::endl;
  101. //加锁
  102. std::unique_lock<std::mutex> t_lock(m_lock);
  103. //只有当状态 不是正在执行当前等级的任务,并且当前等级的任务为空. 此时返回成功, 才能接受新的任务
  104. if ( m_dispatch_device_status >= E_READY && m_dispatch_device_status <= E_THREE_LEVEL_WORK )
  105. {
  106. switch ( dispatch_task_level )
  107. {
  108. case E_ONE_LEVEL:
  109. {
  110. if ( ( m_dispatch_device_status == E_READY ) && mp_device_one_level_task.get() == NULL)
  111. {
  112. return Error_code::SUCCESS;
  113. }
  114. break;
  115. }
  116. case E_TWO_LEVEL:
  117. {
  118. if ( ( m_dispatch_device_status == E_READY ||
  119. m_dispatch_device_status == E_ONE_LEVEL_WORK ||
  120. m_dispatch_device_status == E_ONE_LEVEL_OVER ) && mp_device_two_level_task.get() == NULL)
  121. {
  122. return Error_code::SUCCESS;
  123. }
  124. break;
  125. }
  126. case E_THREE_LEVEL:
  127. {
  128. if ( ( m_dispatch_device_status == E_READY ||
  129. m_dispatch_device_status == E_ONE_LEVEL_OVER ) && mp_device_three_level_task.get() == NULL)
  130. {
  131. return Error_code::SUCCESS;
  132. }
  133. break;
  134. }
  135. default:
  136. {
  137. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  138. " Dispatch_device_base::check_task PARAMETER_ERROR ");
  139. break;
  140. }
  141. }
  142. return Error_manager(Error_code::DISPATCH_DEVICE_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  143. " Dispatch_device_base::check_task is busy ");
  144. }
  145. else
  146. {
  147. return Error_manager(Error_code::DISPATCH_DEVICE_STATUS_ERROR, Error_level::MINOR_ERROR,
  148. " Dispatch_device_base::check_task error ");
  149. }
  150. return Error_code::SUCCESS;
  151. }
  152. //签收任务
  153. Error_manager Dispatch_device_base::sign_for_task(std::shared_ptr<Task_Base> p_task, Dispatch_task_level dispatch_task_level)
  154. {
  155. //加锁
  156. std::unique_lock<std::mutex> t_lock(m_lock);
  157. switch ( dispatch_task_level )
  158. {
  159. case E_ONE_LEVEL:
  160. {
  161. mp_device_one_level_task = p_task;
  162. mp_device_one_level_task->set_task_statu(TASK_SIGNED);
  163. break;
  164. }
  165. case E_TWO_LEVEL:
  166. {
  167. mp_device_two_level_task = p_task;
  168. mp_device_two_level_task->set_task_statu(TASK_SIGNED);
  169. break;
  170. }
  171. case E_THREE_LEVEL:
  172. {
  173. mp_device_three_level_task = p_task;
  174. mp_device_three_level_task->set_task_statu(TASK_SIGNED);
  175. break;
  176. }
  177. default:
  178. {
  179. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  180. " Dispatch_device_base::check_task PARAMETER_ERROR ");
  181. break;
  182. }
  183. }
  184. return Error_code::SUCCESS;
  185. }
  186. //检查状态,是否正常运行. (工作状态 和 是否能接受对应的任务无关)
  187. Error_manager Dispatch_device_base::check_status()
  188. {
  189. if ( m_dispatch_device_status == E_READY )
  190. {
  191. return Error_code::SUCCESS;
  192. }
  193. else if ( m_dispatch_device_status >= E_BUSY && m_dispatch_device_status <= E_THREE_LEVEL_WORK)
  194. {
  195. return Error_manager(Error_code::DISPATCH_DEVICE_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  196. " Dispatch_device_base::check_status is busy ");
  197. }
  198. else
  199. {
  200. return Error_manager(Error_code::DISPATCH_DEVICE_STATUS_ERROR, Error_level::MINOR_ERROR,
  201. " Dispatch_device_base::check_status error ");
  202. }
  203. return Error_code::SUCCESS;
  204. }
  205. //判断是否为待机,如果已经准备好,则可以执行任务。
  206. bool Dispatch_device_base::is_ready()
  207. {
  208. return (m_dispatch_device_status == E_READY);
  209. }
  210. //结束任务单,里面会根据任务的故障等级修正 任务单的状态
  211. Error_manager Dispatch_device_base::end_task(std::shared_ptr<Task_Base> p_task)
  212. {
  213. LOG(INFO) << " ---Dispatch_device_base::end_task ---"<< this;
  214. std::unique_lock<std::mutex> t_lock(m_lock);
  215. //注:这里只修改任务单的状态, 搬运器的状态不管
  216. //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
  217. //判断任务单的错误等级,
  218. if ( p_task->get_task_error_manager().get_error_level() < Error_level::MINOR_ERROR)
  219. {
  220. //强制改为TASK_OVER,不管它当前在做什么。
  221. p_task->set_task_statu(TASK_OVER);
  222. }
  223. else
  224. {
  225. //强制改为 TASK_ERROR,不管它当前在做什么。
  226. p_task->set_task_statu(TASK_ERROR);
  227. }
  228. return Error_code::SUCCESS;
  229. }
  230. //取消任务单,由发送方提前取消任务单
  231. Error_manager Dispatch_device_base::cancel_task(std::shared_ptr<Task_Base> p_task, Dispatch_task_level dispatch_task_level)
  232. {
  233. //找到对应的任务单
  234. switch ( dispatch_task_level )
  235. {
  236. case E_ONE_LEVEL:
  237. {
  238. if ( m_dispatch_device_status == E_ONE_LEVEL_WORK || m_dispatch_device_status == E_ONE_LEVEL_OVER )
  239. {
  240. //如果正在执行一级任务, 那么取消当前指令, 然后降级
  241. m_execute_condition.notify_all(false);
  242. //确保内部线程已经停下
  243. while (m_execute_condition.is_working())
  244. {
  245. }
  246. cancel_command();
  247. {
  248. std::unique_lock<std::mutex> t_lock(m_lock);
  249. mp_device_one_level_task.reset();
  250. m_dispatch_device_status = E_READY;
  251. }
  252. m_execute_condition.notify_all(true);
  253. }
  254. else
  255. {
  256. std::unique_lock<std::mutex> t_lock(m_lock);
  257. //否则直接销毁任务单
  258. mp_device_one_level_task.reset();
  259. }
  260. break;
  261. }
  262. case E_TWO_LEVEL:
  263. {
  264. if ( m_dispatch_device_status == E_TWO_LEVEL_WORK || m_dispatch_device_status == E_TWO_LEVEL_OVER )
  265. {
  266. //如果正在执行一级任务, 那么取消当前指令, 然后降级
  267. m_execute_condition.notify_all(false);
  268. //确保内部线程已经停下
  269. while (m_execute_condition.is_working())
  270. {
  271. }
  272. cancel_command();
  273. {
  274. std::unique_lock<std::mutex> t_lock(m_lock);
  275. mp_device_two_level_task.reset();
  276. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  277. }
  278. m_execute_condition.notify_all(true);
  279. }
  280. else
  281. {
  282. std::unique_lock<std::mutex> t_lock(m_lock);
  283. //否则直接销毁任务单
  284. mp_device_one_level_task.reset();
  285. }
  286. break;
  287. }
  288. case E_THREE_LEVEL:
  289. {
  290. if ( m_dispatch_device_status == E_THREE_LEVEL_WORK || m_dispatch_device_status == E_THREE_LEVEL_OVER )
  291. {
  292. //如果正在执行一级任务, 那么取消当前指令, 然后降级
  293. m_execute_condition.notify_all(false);
  294. //确保内部线程已经停下
  295. while (m_execute_condition.is_working())
  296. {
  297. }
  298. cancel_command();
  299. {
  300. std::unique_lock<std::mutex> t_lock(m_lock);
  301. mp_device_three_level_task.reset();
  302. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  303. }
  304. m_execute_condition.notify_all(true);
  305. }
  306. else
  307. {
  308. std::unique_lock<std::mutex> t_lock(m_lock);
  309. //否则直接销毁任务单
  310. mp_device_one_level_task.reset();
  311. }
  312. break;
  313. }
  314. default:
  315. {
  316. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  317. " Dispatch_device_base::cancel_task PARAMETER_ERROR ");
  318. break;
  319. }
  320. }
  321. //上级的任务单指针,改为死亡,表示任务已经失效.
  322. p_task->set_task_statu(TASK_DEAD);
  323. return Error_code::SUCCESS;
  324. }
  325. Dispatch_device_base::Dispatch_device_status Dispatch_device_base::get_dispatch_device_status()
  326. {
  327. return m_dispatch_device_status;
  328. }
  329. //获取硬件设备的状态, 必须子类继承
  330. Dispatch_device_base::Device_status Dispatch_device_base::get_actual_device_status()
  331. {
  332. return DEVICE_UNKNOWN;
  333. }
  334. //执行外界任务的执行函数
  335. void Dispatch_device_base::execute_thread_fun()
  336. {
  337. LOG(INFO) << " Dispatch_device_base::execute_thread_fun() start " << this;
  338. Error_manager t_error;
  339. while (m_execute_condition.is_alive())
  340. {
  341. m_execute_condition.wait();
  342. if ( m_execute_condition.is_alive() )
  343. {
  344. std::this_thread::sleep_for(std::chrono::microseconds(1));
  345. std::this_thread::sleep_for(std::chrono::seconds(1));
  346. std::this_thread::yield();
  347. std::cout << " huli test :::: " << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa m_dispatch_device_status = " << m_dispatch_device_status << std::endl;
  348. switch ( (Dispatch_device_status)m_dispatch_device_status )
  349. {
  350. //核心任务, (三级任务)
  351. case E_THREE_LEVEL_WORK:
  352. {
  353. if ( mp_device_three_level_task.get() != NULL )
  354. {
  355. mp_device_three_level_task->set_task_statu(TASK_WORKING);
  356. //执行三级任务
  357. write_task_to_memory(mp_device_three_level_task);
  358. //更新通信
  359. update_device_communication();
  360. //从内存中读数据到任务单
  361. t_error = check_and_read_memory_to_task(mp_device_three_level_task);
  362. if (t_error == NODATA)
  363. {
  364. //设备正常运行
  365. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  366. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  367. }
  368. else
  369. {
  370. if (t_error != SUCCESS)
  371. {
  372. mp_device_three_level_task->set_task_error_manager(t_error);
  373. }
  374. end_task(mp_device_three_level_task);
  375. m_dispatch_device_status = E_THREE_LEVEL_OVER;
  376. }
  377. }
  378. else
  379. {
  380. //直接降级
  381. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  382. }
  383. break;
  384. }
  385. case E_THREE_LEVEL_OVER:
  386. {
  387. //更新通信
  388. update_device_communication();
  389. if ( mp_device_three_level_task.get() != NULL )
  390. {
  391. //检查任务状态,
  392. //在 E_THREE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  393. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  394. if ( mp_device_three_level_task->get_task_statu() == TASK_WITHDRAW )
  395. {
  396. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  397. mp_device_three_level_task->set_task_statu(TASK_FREE);
  398. mp_device_three_level_task.reset();
  399. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  400. }
  401. //任务单重新创建, 调度创建的新的任务,那么回去继续工作
  402. else if ( mp_device_three_level_task->get_task_statu() == TASK_CREATED )
  403. {
  404. mp_device_three_level_task->set_task_statu(TASK_SIGNED);
  405. m_dispatch_device_status = E_THREE_LEVEL_WORK;
  406. }
  407. //else //保持不动, 直到发送方给定新的任务,
  408. }
  409. else
  410. {
  411. //直接降级
  412. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  413. }
  414. break;
  415. }
  416. case E_TWO_LEVEL_WORK:
  417. {
  418. if ( mp_device_two_level_task.get() != NULL )
  419. {
  420. mp_device_two_level_task->set_task_statu(TASK_WORKING);
  421. //执行二级任务
  422. write_task_to_memory(mp_device_two_level_task);
  423. //更新通信
  424. update_device_communication();
  425. //从内存中读数据到任务单
  426. t_error = check_and_read_memory_to_task(mp_device_two_level_task);
  427. if (t_error == NODATA)
  428. {
  429. //设备正常运行
  430. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  431. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  432. }
  433. else
  434. {
  435. if (t_error != SUCCESS)
  436. {
  437. mp_device_two_level_task->set_task_error_manager(t_error);
  438. }
  439. end_task(mp_device_two_level_task);
  440. m_dispatch_device_status = E_TWO_LEVEL_OVER;
  441. }
  442. }
  443. else
  444. {
  445. //直接降级
  446. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  447. }
  448. break;
  449. }
  450. case E_TWO_LEVEL_OVER:
  451. {
  452. //更新通信
  453. update_device_communication();
  454. if ( mp_device_two_level_task.get() != NULL )
  455. {
  456. //检查任务状态,
  457. //在 E_TWO_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  458. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  459. if ( mp_device_two_level_task->get_task_statu() == TASK_WITHDRAW )
  460. {
  461. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  462. mp_device_two_level_task->set_task_statu(TASK_FREE);
  463. mp_device_two_level_task.reset();
  464. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  465. }
  466. //任务单重新创建, 调度创建的新的任务,那么回去继续工作
  467. else if ( mp_device_two_level_task->get_task_statu() == TASK_CREATED )
  468. {
  469. mp_device_two_level_task->set_task_statu(TASK_SIGNED);
  470. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  471. }
  472. //else //保持不动, 直到发送方给定新的任务,
  473. }
  474. else
  475. {
  476. //直接降级
  477. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  478. }
  479. break;
  480. }
  481. case E_ONE_LEVEL_WORK:
  482. {
  483. if ( mp_device_one_level_task.get() != NULL )
  484. {
  485. mp_device_one_level_task->set_task_statu(TASK_WORKING);
  486. //执行一级任务,
  487. write_task_to_memory(mp_device_one_level_task);
  488. //更新通信
  489. update_device_communication();
  490. //从内存中读数据到任务单
  491. t_error = check_and_read_memory_to_task(mp_device_one_level_task);
  492. if ( t_error == NODATA )
  493. {
  494. //设备正常运行
  495. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  496. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  497. }
  498. else
  499. {
  500. if ( t_error != SUCCESS )
  501. {
  502. mp_device_one_level_task->set_task_error_manager(t_error);
  503. }
  504. end_task(mp_device_one_level_task);
  505. m_dispatch_device_status = E_ONE_LEVEL_OVER;
  506. }
  507. }
  508. else
  509. {
  510. //直接降级
  511. m_dispatch_device_status = E_READY;
  512. }
  513. break;
  514. }
  515. case E_ONE_LEVEL_OVER:
  516. {
  517. //更新通信
  518. update_device_communication();
  519. if ( mp_device_one_level_task.get() != NULL )
  520. {
  521. //检查任务状态,
  522. //在 E_ONE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  523. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  524. if ( mp_device_one_level_task->get_task_statu() == TASK_WITHDRAW )
  525. {
  526. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  527. mp_device_one_level_task->set_task_statu(TASK_FREE);
  528. mp_device_one_level_task.reset();
  529. m_dispatch_device_status = E_READY;
  530. }
  531. //任务单重新创建, 调度创建的新的任务,那么回去继续工作
  532. else if ( mp_device_one_level_task->get_task_statu() == TASK_CREATED )
  533. {
  534. mp_device_one_level_task->set_task_statu(TASK_SIGNED);
  535. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  536. }
  537. //else //保持不动, 直到发送方给定新的任务,
  538. }
  539. else
  540. {
  541. //直接降级
  542. m_dispatch_device_status = E_READY;
  543. }
  544. break;
  545. }
  546. case E_FAULT:
  547. {
  548. //更新通信
  549. update_device_communication();
  550. //所有任务报错, 并销毁任务.
  551. if ( mp_device_one_level_task.get() != NULL )
  552. {
  553. //添加错误码
  554. Error_manager t_error(DISPATCH_DEVICE_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  555. mp_device_one_level_task->set_task_error_manager(t_error);
  556. end_task(mp_device_one_level_task);
  557. mp_device_one_level_task.reset();
  558. }
  559. if ( mp_device_two_level_task.get() != NULL )
  560. {
  561. //添加错误码
  562. Error_manager t_error(DISPATCH_DEVICE_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  563. mp_device_two_level_task->set_task_error_manager(t_error);
  564. end_task(mp_device_two_level_task);
  565. mp_device_two_level_task.reset();
  566. }
  567. if ( mp_device_three_level_task.get() != NULL )
  568. {
  569. //添加错误码
  570. Error_manager t_error(DISPATCH_DEVICE_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  571. mp_device_three_level_task->set_task_error_manager(t_error);
  572. end_task(mp_device_three_level_task);
  573. mp_device_three_level_task.reset();
  574. }
  575. break;
  576. }
  577. case E_DISCONNECT:
  578. {
  579. //更新通信
  580. update_device_communication();
  581. //所有任务报错, 并销毁任务.
  582. if ( mp_device_one_level_task.get() != NULL )
  583. {
  584. //添加错误码
  585. Error_manager t_error(DISPATCH_DEVICE_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  586. mp_device_one_level_task->set_task_error_manager(t_error);
  587. end_task(mp_device_one_level_task);
  588. mp_device_one_level_task.reset();
  589. }
  590. if ( mp_device_two_level_task.get() != NULL )
  591. {
  592. //添加错误码
  593. Error_manager t_error(DISPATCH_DEVICE_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  594. mp_device_two_level_task->set_task_error_manager(t_error);
  595. end_task(mp_device_two_level_task);
  596. mp_device_two_level_task.reset();
  597. }
  598. if ( mp_device_three_level_task.get() != NULL )
  599. {
  600. //添加错误码
  601. Error_manager t_error(DISPATCH_DEVICE_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  602. mp_device_three_level_task->set_task_error_manager(t_error);
  603. end_task(mp_device_three_level_task);
  604. mp_device_three_level_task.reset();
  605. }
  606. break;
  607. }
  608. case E_READY:
  609. {
  610. //更新通信
  611. update_device_communication();
  612. break;
  613. }
  614. default:
  615. {
  616. break;
  617. }
  618. }
  619. }
  620. }
  621. LOG(INFO) << " Dispatch_device_base::execute_thread_fun() end "<< this;
  622. return;
  623. }
  624. //执行线程工作函数, 正在执行任务单.
  625. Error_manager Dispatch_device_base::execute_thread_working(std::shared_ptr<Task_Base> p_task, Dispatch_device_status & device_status)
  626. {
  627. return Error_code::SUCCESS;
  628. }
  629. //执行线程工作函数, 已经完成任务单. 等待新的指令
  630. Error_manager Dispatch_device_base::execute_thread_over(std::shared_ptr<Task_Base> p_task, Dispatch_device_status & device_status)
  631. {
  632. return Error_code::SUCCESS;
  633. }
  634. //把任务单写入到内存中, 子类必须重载
  635. Error_manager Dispatch_device_base::write_task_to_memory(std::shared_ptr<Task_Base> p_task)
  636. {
  637. return Error_code::SUCCESS;
  638. }
  639. //更新设备底层通信数据, 子类必须重载
  640. Error_manager Dispatch_device_base::update_device_communication()
  641. {
  642. return Error_code::SUCCESS;
  643. }
  644. //从内存中读数据到任务单, 子类必须重载
  645. Error_manager Dispatch_device_base::check_and_read_memory_to_task(std::shared_ptr<Task_Base> p_task)
  646. {
  647. return Error_code::SUCCESS;
  648. }
  649. //取消下发的指令
  650. Error_manager Dispatch_device_base::cancel_command()
  651. {
  652. //以后再写 need programe
  653. //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
  654. //这里先空着,以后再写.
  655. //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
  656. return Error_code::SUCCESS;
  657. }