dispatch_device_base.cpp 22 KB

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