dispatch_device_base.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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_Base::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_Base::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_Base::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_Base::Task_statu::TASK_OVER);
  222. }
  223. else
  224. {
  225. //强制改为 TASK_ERROR,不管它当前在做什么。
  226. p_task->set_task_statu(Task_Base::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_Base::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. int Dispatch_device_base::get_device_id()
  335. {
  336. return m_device_id;
  337. }
  338. //执行外界任务的执行函数
  339. void Dispatch_device_base::execute_thread_fun()
  340. {
  341. LOG(INFO) << " Dispatch_device_base::execute_thread_fun() start " << this;
  342. Error_manager t_error;
  343. while (m_execute_condition.is_alive())
  344. {
  345. m_execute_condition.wait();
  346. if ( m_execute_condition.is_alive() )
  347. {
  348. std::this_thread::sleep_for(std::chrono::microseconds(1));
  349. std::this_thread::sleep_for(std::chrono::seconds(1));
  350. std::this_thread::yield();
  351. std::cout << " huli test :::: " << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa m_dispatch_device_status = " << m_dispatch_device_status << std::endl;
  352. switch ( (Dispatch_device_status)m_dispatch_device_status )
  353. {
  354. //核心任务, (三级任务)
  355. case E_THREE_LEVEL_WORK:
  356. {
  357. if ( mp_device_three_level_task.get() != NULL )
  358. {
  359. mp_device_three_level_task->set_task_statu(Task_Base::Task_statu::TASK_WORKING);
  360. //执行三级任务
  361. write_task_to_memory(mp_device_three_level_task);
  362. //更新通信
  363. update_device_communication();
  364. //从内存中读数据到任务单
  365. t_error = check_and_read_memory_to_task(mp_device_three_level_task);
  366. if (t_error == NODATA)
  367. {
  368. //设备正常运行
  369. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  370. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  371. }
  372. else
  373. {
  374. if (t_error != SUCCESS)
  375. {
  376. mp_device_three_level_task->set_task_error_manager(t_error);
  377. }
  378. end_task(mp_device_three_level_task);
  379. m_dispatch_device_status = E_THREE_LEVEL_OVER;
  380. }
  381. }
  382. else
  383. {
  384. //直接降级
  385. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  386. }
  387. break;
  388. }
  389. case E_THREE_LEVEL_OVER:
  390. {
  391. //更新通信
  392. update_device_communication();
  393. if ( mp_device_three_level_task.get() != NULL )
  394. {
  395. //检查任务状态,
  396. //在 E_THREE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  397. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  398. if ( mp_device_three_level_task->get_task_statu() == Task_Base::Task_statu::TASK_WITHDRAW )
  399. {
  400. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  401. mp_device_three_level_task->set_task_statu(Task_Base::Task_statu::TASK_FREE);
  402. mp_device_three_level_task.reset();
  403. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  404. }
  405. //任务单重新创建, 调度创建的新的任务,那么回去继续工作
  406. else if ( mp_device_three_level_task->get_task_statu() == Task_Base::Task_statu::TASK_CREATED )
  407. {
  408. mp_device_three_level_task->set_task_statu(Task_Base::Task_statu::TASK_SIGNED);
  409. m_dispatch_device_status = E_THREE_LEVEL_WORK;
  410. }
  411. //else //保持不动, 直到发送方给定新的任务,
  412. }
  413. else
  414. {
  415. //直接降级
  416. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  417. }
  418. break;
  419. }
  420. case E_TWO_LEVEL_WORK:
  421. {
  422. if ( mp_device_two_level_task.get() != NULL )
  423. {
  424. mp_device_two_level_task->set_task_statu(Task_Base::Task_statu::TASK_WORKING);
  425. //执行二级任务
  426. write_task_to_memory(mp_device_two_level_task);
  427. //更新通信
  428. update_device_communication();
  429. //从内存中读数据到任务单
  430. t_error = check_and_read_memory_to_task(mp_device_two_level_task);
  431. if (t_error == NODATA)
  432. {
  433. //设备正常运行
  434. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  435. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  436. }
  437. else
  438. {
  439. if (t_error != SUCCESS)
  440. {
  441. mp_device_two_level_task->set_task_error_manager(t_error);
  442. }
  443. end_task(mp_device_two_level_task);
  444. m_dispatch_device_status = E_TWO_LEVEL_OVER;
  445. }
  446. }
  447. else
  448. {
  449. //直接降级
  450. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  451. }
  452. break;
  453. }
  454. case E_TWO_LEVEL_OVER:
  455. {
  456. //更新通信
  457. update_device_communication();
  458. if ( mp_device_two_level_task.get() != NULL )
  459. {
  460. //检查任务状态,
  461. //在 E_TWO_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  462. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  463. if ( mp_device_two_level_task->get_task_statu() == Task_Base::Task_statu::TASK_WITHDRAW )
  464. {
  465. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  466. mp_device_two_level_task->set_task_statu(Task_Base::Task_statu::TASK_FREE);
  467. mp_device_two_level_task.reset();
  468. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  469. }
  470. //任务单重新创建, 调度创建的新的任务,那么回去继续工作
  471. else if ( mp_device_two_level_task->get_task_statu() == Task_Base::Task_statu::TASK_CREATED )
  472. {
  473. mp_device_two_level_task->set_task_statu(Task_Base::Task_statu::TASK_SIGNED);
  474. m_dispatch_device_status = E_TWO_LEVEL_WORK;
  475. }
  476. //else //保持不动, 直到发送方给定新的任务,
  477. }
  478. else
  479. {
  480. //直接降级
  481. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  482. }
  483. break;
  484. }
  485. case E_ONE_LEVEL_WORK:
  486. {
  487. if ( mp_device_one_level_task.get() != NULL )
  488. {
  489. mp_device_one_level_task->set_task_statu(Task_Base::Task_statu::TASK_WORKING);
  490. //执行一级任务,
  491. write_task_to_memory(mp_device_one_level_task);
  492. //更新通信
  493. update_device_communication();
  494. //从内存中读数据到任务单
  495. t_error = check_and_read_memory_to_task(mp_device_one_level_task);
  496. if ( t_error == NODATA )
  497. {
  498. //设备正常运行
  499. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  500. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  501. }
  502. else
  503. {
  504. if ( t_error != SUCCESS )
  505. {
  506. mp_device_one_level_task->set_task_error_manager(t_error);
  507. }
  508. end_task(mp_device_one_level_task);
  509. m_dispatch_device_status = E_ONE_LEVEL_OVER;
  510. }
  511. }
  512. else
  513. {
  514. //直接降级
  515. m_dispatch_device_status = E_READY;
  516. }
  517. break;
  518. }
  519. case E_ONE_LEVEL_OVER:
  520. {
  521. //更新通信
  522. update_device_communication();
  523. if ( mp_device_one_level_task.get() != NULL )
  524. {
  525. //检查任务状态,
  526. //在 E_ONE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  527. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  528. if ( mp_device_one_level_task->get_task_statu() == Task_Base::Task_statu::TASK_WITHDRAW )
  529. {
  530. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  531. mp_device_one_level_task->set_task_statu(Task_Base::Task_statu::TASK_FREE);
  532. mp_device_one_level_task.reset();
  533. m_dispatch_device_status = E_READY;
  534. }
  535. //任务单重新创建, 调度创建的新的任务,那么回去继续工作
  536. else if ( mp_device_one_level_task->get_task_statu() == Task_Base::Task_statu::TASK_CREATED )
  537. {
  538. mp_device_one_level_task->set_task_statu(Task_Base::Task_statu::TASK_SIGNED);
  539. m_dispatch_device_status = E_ONE_LEVEL_WORK;
  540. }
  541. //else //保持不动, 直到发送方给定新的任务,
  542. }
  543. else
  544. {
  545. //直接降级
  546. m_dispatch_device_status = E_READY;
  547. }
  548. break;
  549. }
  550. case E_FAULT:
  551. {
  552. //更新通信
  553. update_device_communication();
  554. //所有任务报错, 并销毁任务.
  555. if ( mp_device_one_level_task.get() != NULL )
  556. {
  557. //添加错误码
  558. Error_manager t_error(DISPATCH_DEVICE_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  559. mp_device_one_level_task->set_task_error_manager(t_error);
  560. end_task(mp_device_one_level_task);
  561. mp_device_one_level_task.reset();
  562. }
  563. if ( mp_device_two_level_task.get() != NULL )
  564. {
  565. //添加错误码
  566. Error_manager t_error(DISPATCH_DEVICE_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  567. mp_device_two_level_task->set_task_error_manager(t_error);
  568. end_task(mp_device_two_level_task);
  569. mp_device_two_level_task.reset();
  570. }
  571. if ( mp_device_three_level_task.get() != NULL )
  572. {
  573. //添加错误码
  574. Error_manager t_error(DISPATCH_DEVICE_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  575. mp_device_three_level_task->set_task_error_manager(t_error);
  576. end_task(mp_device_three_level_task);
  577. mp_device_three_level_task.reset();
  578. }
  579. break;
  580. }
  581. case E_DISCONNECT:
  582. {
  583. //更新通信
  584. update_device_communication();
  585. //所有任务报错, 并销毁任务.
  586. if ( mp_device_one_level_task.get() != NULL )
  587. {
  588. //添加错误码
  589. Error_manager t_error(DISPATCH_DEVICE_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  590. mp_device_one_level_task->set_task_error_manager(t_error);
  591. end_task(mp_device_one_level_task);
  592. mp_device_one_level_task.reset();
  593. }
  594. if ( mp_device_two_level_task.get() != NULL )
  595. {
  596. //添加错误码
  597. Error_manager t_error(DISPATCH_DEVICE_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  598. mp_device_two_level_task->set_task_error_manager(t_error);
  599. end_task(mp_device_two_level_task);
  600. mp_device_two_level_task.reset();
  601. }
  602. if ( mp_device_three_level_task.get() != NULL )
  603. {
  604. //添加错误码
  605. Error_manager t_error(DISPATCH_DEVICE_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  606. mp_device_three_level_task->set_task_error_manager(t_error);
  607. end_task(mp_device_three_level_task);
  608. mp_device_three_level_task.reset();
  609. }
  610. break;
  611. }
  612. case E_READY:
  613. {
  614. //更新通信
  615. update_device_communication();
  616. break;
  617. }
  618. default:
  619. {
  620. break;
  621. }
  622. }
  623. }
  624. }
  625. LOG(INFO) << " Dispatch_device_base::execute_thread_fun() end "<< this;
  626. return;
  627. }
  628. //执行线程工作函数, 正在执行任务单.
  629. Error_manager Dispatch_device_base::execute_thread_working(std::shared_ptr<Task_Base> p_task, Dispatch_device_status & device_status)
  630. {
  631. return Error_code::SUCCESS;
  632. }
  633. //执行线程工作函数, 已经完成任务单. 等待新的指令
  634. Error_manager Dispatch_device_base::execute_thread_over(std::shared_ptr<Task_Base> p_task, Dispatch_device_status & device_status)
  635. {
  636. return Error_code::SUCCESS;
  637. }
  638. //把任务单写入到内存中, 子类必须重载
  639. Error_manager Dispatch_device_base::write_task_to_memory(std::shared_ptr<Task_Base> p_task)
  640. {
  641. return Error_code::SUCCESS;
  642. }
  643. //更新设备底层通信数据, 子类必须重载
  644. Error_manager Dispatch_device_base::update_device_communication()
  645. {
  646. return Error_code::SUCCESS;
  647. }
  648. //从内存中读数据到任务单, 子类必须重载
  649. Error_manager Dispatch_device_base::check_and_read_memory_to_task(std::shared_ptr<Task_Base> p_task)
  650. {
  651. return Error_code::SUCCESS;
  652. }
  653. //取消下发的指令
  654. Error_manager Dispatch_device_base::cancel_command()
  655. {
  656. //以后再写 need programe
  657. //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
  658. //这里先空着,以后再写.
  659. //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
  660. return Error_code::SUCCESS;
  661. }