1
0

module_traffic_light_manager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #include "module_traffic_light_manager.h"
  2. #include "db_api/CDBSingletonDefine.h"
  3. #include <db_api/CDBResultSet.h>
  4. #include "websocket/constdef.h"
  5. //#include "websocket/wsTimerThread.h"
  6. #include "websocket/ws_common.h"
  7. #include "event.h"
  8. traffic_light_manager* traffic_light_manager::instance()
  9. {
  10. static traffic_light_manager tlm;
  11. return &tlm;
  12. }
  13. /*
  14. * 加载红绿灯基础信息
  15. * */
  16. void traffic_light_manager::init_light_from_db(int lid)
  17. {
  18. std::string sql = "select light_id, lights_group_id, ip, x, y ,z, reader_id, state, port, physics_light_id, physics_light_direction, special_flag, stream_state from dat_light";
  19. if(0 == lid){
  20. sql += ";";
  21. }else{
  22. sql += " where light_id = " + std::to_string(lid);
  23. sql += ";";
  24. }
  25. std::string err = "";
  26. YADB::CDBResultSet res;
  27. sDBConnPool.Query(sql.c_str(), res, err);
  28. int count = res.GetRecordCount(err);
  29. if(count < 1){
  30. log_error("增加或修改失败,error:%s", sql.c_str());
  31. return;
  32. }
  33. while(res.GetNextRecod(err)){
  34. int light_id = 0;
  35. res.GetField("light_id", light_id, err);
  36. int group_id = 0;
  37. res.GetField("lights_group_id", group_id, err);
  38. std::string ip = "";
  39. res.GetField("ip", ip, err);
  40. double x = 0.0, y = 0.0, z = 0.0;
  41. res.GetField("x", x, err);
  42. res.GetField("y", y, err);
  43. res.GetField("z", z, err);
  44. int site_id = 0, state = 0, port = 0, phy_light_id = 0, phy_direction = 0, special = 0, stream = 0;
  45. res.GetField("reader_id", site_id, err);
  46. res.GetField("state", state, err);
  47. res.GetField("port", port, err);
  48. res.GetField("physics_light_id", phy_light_id, err);
  49. res.GetField("physics_light_direction", phy_direction, err);
  50. res.GetField("special_flag", special, err);
  51. res.GetField("stream_state", stream, err);
  52. traffic_light_ptr pl = find_light(light_id);
  53. if(nullptr == pl){
  54. pl = std::make_shared<traffic_light>(x, y, z, light_id, group_id, ip, state, port, phy_light_id, phy_direction, special, stream);
  55. m_unmap_lights.insert(std::make_pair(light_id, pl));
  56. }else{
  57. pl->update(x, y , z, light_id, group_id, ip, state, port, phy_light_id, phy_direction, special, stream);
  58. }
  59. // 将红绿灯分配到对应组中
  60. auto it_group = m_unmap_groups.find(pl->m_group_id);
  61. if(it_group != m_unmap_groups.end())
  62. {
  63. it_group->second->m_vt_lights.push_back(pl);
  64. pl->m_area_id = it_group->second->m_area_id;
  65. pl->m_map_id = it_group->second->m_map_id;
  66. }
  67. }
  68. logn_info(2, "sql:%s",sql.c_str());
  69. }
  70. /*
  71. * 加载红绿灯组基础信息
  72. * */
  73. void traffic_light_manager::init_light_group_from_db(int gid)
  74. {
  75. std::string sql = "select lights_group_id, x, y, z, scope, map_id, area_id, manual_control_time, light_auto_interval, up_stream_idx from dat_lights_group";
  76. if(gid > 0){
  77. sql += " where lights_group_id = " + std::to_string(gid);
  78. }
  79. sql += ";";
  80. std::string err = "";
  81. YADB::CDBResultSet res;
  82. sDBConnPool.Query(sql.c_str(), res, err);
  83. int count = res.GetRecordCount(err);
  84. if(count < 1){
  85. log_error("增加或修改失败,数据库中找不到:sql=%s", sql.c_str());
  86. return;
  87. }
  88. while(res.GetNextRecod(err)){
  89. int group_id = 0;
  90. res.GetField("lights_group_id", group_id, err);
  91. double x = 0.0, y = 0.0, z = 0.0, scope = 0.0;
  92. res.GetField("x", x, err);
  93. res.GetField("y", y, err);
  94. res.GetField("z", z, err);
  95. res.GetField("scope", scope, err);
  96. int map_id = 0, area_id = 0;
  97. res.GetField("map_id", map_id, err);
  98. res.GetField("area_id", area_id, err);
  99. int ai = 0;
  100. res.GetField("light_auto_interval", ai, err);
  101. int usi = 0;
  102. res.GetField("up_stream_idx", usi, err);
  103. traffic_light_group_ptr pg = find_group(group_id);
  104. if(nullptr == pg){
  105. pg = std::make_shared<traffic_light_group>(x, y, z, group_id, scope, ai, map_id, area_id);
  106. m_unmap_groups.insert(std::make_pair(group_id, pg));
  107. }else{
  108. pg->update(x, y, z, group_id, scope, ai, map_id, area_id);
  109. }
  110. pg->set_down_stream_idx(usi);
  111. auto ppg = m_map_groups.find(usi);
  112. if(ppg == m_map_groups.end()){
  113. m_map_groups.insert(std::make_pair(usi, pg));
  114. }
  115. }
  116. logn_info(2, "sql:%s",sql.c_str());
  117. output_upstreamidx();
  118. }
  119. void traffic_light_manager::init_light_group_path()
  120. {
  121. std::string sql = "select reader_id, tof_flag, b_x, b_y, b_z, e_x, e_y, e_z, spacing_ratio from dat_reader_path_tof_n;";
  122. //std::map<int, std::vector<line_v>> map_path;
  123. std::string error;
  124. YADB::CDBResultSet res;
  125. sDBConnPool.Query(sql.c_str(), res, error);
  126. int count = res.GetRecordCount(error);
  127. log_info("init_light_group_path, the record count=%d\n", count);
  128. while(res.GetNextRecod(error)){
  129. int reader_id = 0;
  130. res.GetField("reader_id", reader_id, error);
  131. int pid=0;
  132. res.GetField("tof_flag", pid, error);
  133. double b_x = 0;
  134. res.GetField("b_x", b_x, error);
  135. double b_y = 0;
  136. res.GetField("b_y", b_y, error);
  137. double b_z = 0;
  138. res.GetField("b_z", b_z, error);
  139. double e_x = 0;
  140. res.GetField("e_x", e_x, error);
  141. double e_y = 0;
  142. res.GetField("e_y", e_y, error);
  143. double e_z = 0;
  144. res.GetField("e_z", e_z, error);
  145. double spacing_ratio = 0;
  146. res.GetField("spacing_ratio", spacing_ratio, error);
  147. log_info("[traffic_light] src-path: site=%d, x0=%.2f, y0=%.2f, x1=%.2f, y1=%.2f", reader_id, b_x, b_y, e_x, e_y);
  148. point p1(b_x, -b_y, spacing_ratio);
  149. point p2(e_x, -e_y, spacing_ratio);
  150. m_map_path.insert(std::make_pair(reader_id, std::vector<line_v>()));
  151. m_map_path.find(reader_id)->second.push_back(line_v(p1, p2));
  152. }
  153. for(auto itg : m_unmap_groups){
  154. for(auto itp : m_map_path){
  155. bool tag = false;
  156. for(size_t i = 0;i < itp.second.size();i++){
  157. if(itp.second[i].contain(point(itg.second->x, itg.second->y),1)){
  158. tag = true;
  159. }
  160. }
  161. if(tag){
  162. itg.second->set_path(itp.second);
  163. }
  164. }
  165. }
  166. }
  167. void traffic_light_manager::init_map(int id)
  168. {
  169. std::string sql = "select map_id, scale from dat_map";
  170. if(id > 0){
  171. sql += " where map_id = " + std::to_string(id);
  172. }
  173. sql += ";";
  174. std::string err = "";
  175. YADB::CDBResultSet res;
  176. sDBConnPool.Query(sql.c_str(), res, err);
  177. int count = res.GetRecordCount(err);
  178. if(count < 1){
  179. log_error("增加或修改失败,数据库中找不到:sql=%s", sql.c_str());
  180. return;
  181. }
  182. while(res.GetNextRecod(err)){
  183. int map_id = 0;
  184. res.GetField("map_id", map_id, err);
  185. double scale = 0.0;
  186. res.GetField("scale", scale, err);
  187. set_scale(scale);
  188. log_info("init_map: map_id=%d, scale=%.2f", map_id, scale);
  189. }
  190. logn_info(2, "sql:%s" ,sql.c_str());
  191. }
  192. void traffic_light_manager::init(const int& lgc)
  193. {
  194. // 1.先获得红绿灯组数据
  195. init_light_group_from_db();
  196. init_light_group_path();
  197. // 2.再获得红绿灯数据
  198. init_light_from_db();
  199. init_map();
  200. for(hashmap_light::iterator it_light = m_unmap_lights.begin(); it_light != m_unmap_lights.end(); ++it_light){
  201. if(it_light->second->m_group_id >0){
  202. hashmap_group::iterator it_group = m_unmap_groups.find(it_light->second->m_group_id);
  203. if(it_group != m_unmap_groups.end()){
  204. it_group->second->insert(it_light->second);
  205. }
  206. }
  207. }
  208. for(auto it : m_unmap_groups){
  209. log_info("[traffic_light] gid=%d, size=%d", it.first, it.second->m_vt_lights.size());
  210. }
  211. m_light_group_ctrl_num = lgc;
  212. }
  213. void traffic_light_manager::start()
  214. {
  215. log_info("[traffic_light] start traffic light service, light_group's size=%d", m_unmap_groups.size());
  216. m_stop = false;
  217. m_crossing_rule = std::unique_ptr<crossing_rule>(new crossing_rule);
  218. if(nullptr == m_crossing_rule){
  219. log_info("[traffic_light] create crossing rule failed");
  220. return;
  221. }else{
  222. for(auto it = m_unmap_groups.begin(); it != m_unmap_groups.end(); ++it)
  223. {
  224. m_crossing_rule->put(it->second);
  225. }
  226. }
  227. m_avoidance_rule = std::unique_ptr<avoidance_rule>(new avoidance_rule);
  228. if(nullptr == m_avoidance_rule){
  229. log_info("[traffic_light] create avoidance rule failed");
  230. return;
  231. }else{
  232. for(auto it = m_unmap_groups.begin(); it != m_unmap_groups.end(); ++it)
  233. {
  234. log_info("[traffic_light] insert light group %d into area, area_id=%d", it->second->m_group_id, it->second->m_area_id);
  235. m_avoidance_rule->put(it->second);
  236. }
  237. }
  238. log_info("[traffic_light] rule's map size, crossing=%d, avoidance=%d", m_crossing_rule->size(), m_avoidance_rule->size());
  239. m_thread_light.reset(new std::thread(std::bind(&traffic_light_manager::run, this)));
  240. }
  241. void traffic_light_manager::stop()
  242. {
  243. log_info("[traffic_light] stop traffic light service");
  244. m_stop = true;
  245. m_thread_light->join();
  246. }
  247. void traffic_light_manager::put(const light_message& msg)
  248. {
  249. std::unique_lock<std::mutex> lock(m_mutex);
  250. m_list_data.push_back(msg);
  251. lock.unlock();
  252. m_condition.notify_all();
  253. }
  254. void traffic_light_manager::run()
  255. {
  256. while(!m_stop){
  257. std::list<light_message> tmp_data;
  258. tmp_data.clear();
  259. {
  260. std::unique_lock<std::mutex> lock(m_mutex);
  261. while(m_list_data.empty()){
  262. m_condition.wait(lock);
  263. }
  264. if(m_list_data.size() > 0){
  265. m_list_data.swap(tmp_data);
  266. }
  267. lock.unlock();
  268. }
  269. for(std::list<light_message>::iterator it = tmp_data.begin(); it != tmp_data.end(); ++it){
  270. switch(it->m_cmd){
  271. case cmd_reload:
  272. log_info("[traffic_light] cmd=%d, gid=%d, lid=%d",it->m_cmd, it->m_group_id, it->m_light_id);
  273. handle_reload(it->m_group_id, it->m_light_id);
  274. break;
  275. case cmd_manual_ctrl:
  276. log_info("[traffic_light] cmd=%d, gid=%d, lid=%d, name=%s, lc=%d", it->m_cmd, it->m_group_id, it->m_light_id, it->m_ctrl_name.c_str(), it->m_light_state);
  277. handle_manual(it->m_group_id,it->m_light_id, it->m_ctrl_name, it->m_light_state);
  278. break;
  279. case cmd_card_data:
  280. log_info("[traffic_light] cmd=%d, cid=%d, ctype=%d, bigger=%d, aid=%d, speed=%.2f, sid=%d", it->m_cmd, it->m_pos.m_card_id, it->m_pos.m_type, it->m_pos.m_bigger, it->m_pos.m_area_id, it->m_pos.m_speed, it->m_pos.m_site_id);
  281. handle_position(it->m_pos);
  282. break;
  283. default:
  284. log_warn("[traffic_light] message cmd error, cmd=%d", it->m_cmd);
  285. break;
  286. }
  287. }
  288. }
  289. log_info("[traffic_light] working thread is exit. m_stop=%d", m_stop);
  290. }
  291. // 红绿灯及灯组基础数据更新
  292. void traffic_light_manager::handle_reload(const int& gid, const int& lid)
  293. {
  294. if(lid > 0){
  295. init_light_from_db(lid);
  296. }
  297. if(gid > 0){
  298. init_light_group_from_db(gid);
  299. }
  300. }
  301. // 红绿灯基础数据更新
  302. int traffic_light_manager::reload_light(const int& lid)
  303. {
  304. init_light_from_db(lid);
  305. return 0;
  306. }
  307. // 灯组基础数据更新
  308. int traffic_light_manager::reload_group(const int& gid)
  309. {
  310. init_light_group_from_db(gid);
  311. return 0;
  312. }
  313. std::vector<traffic_light_group_ptr> traffic_light_manager::find_stream_groups(traffic_light_group_ptr ptr_group, const move_stream& stream)
  314. {
  315. std::vector<traffic_light_group_ptr> vt_group;
  316. switch(stream){
  317. case move_stream::up_stream:
  318. {
  319. auto itg = m_map_groups.end();
  320. for(auto it = m_map_groups.begin(); it != m_map_groups.end(); ++it){
  321. if(it->second->m_group_id == ptr_group->m_group_id){
  322. itg = it;
  323. }
  324. }
  325. auto ite = itg;
  326. std::advance(ite, m_light_group_ctrl_num);
  327. std::for_each(itg, ite, [&vt_group](std::pair<int, traffic_light_group_ptr> p){
  328. if(p.second != nullptr){
  329. vt_group.push_back(p.second);
  330. }
  331. });
  332. }
  333. break;
  334. case move_stream::down_stream:
  335. {
  336. auto itg = m_map_groups.rend();
  337. for(auto it = m_map_groups.rbegin(); it != m_map_groups.rend(); ++it){
  338. if(it->second->m_group_id == ptr_group->m_group_id){
  339. itg = it;
  340. }
  341. }
  342. auto ite = itg;
  343. std::advance(ite, m_light_group_ctrl_num);
  344. std::for_each(itg, ite, [&vt_group](std::pair<int, traffic_light_group_ptr> p){
  345. if(p.second != nullptr){
  346. vt_group.push_back(p.second);
  347. }
  348. });
  349. }
  350. break;
  351. default:
  352. break;
  353. }
  354. return std::move(vt_group);
  355. }
  356. // 处理web端发起的手工控制
  357. void traffic_light_manager::manual_ctrl(sio::message::ptr const& data)
  358. {
  359. std::map<std::string, sio::message::ptr> mp = data->get_map()[JSON_ROOT_KEY_DATA]->get_map();
  360. log_info("[traffic_light] map's size=%d", mp.size());
  361. light_message lm;
  362. lm.m_group_id = mp["group_id"]->get_int();
  363. lm.m_light_id = mp["light_id"]->get_int();
  364. lm.m_light_state = mp["light_color"]->get_int();
  365. lm.m_ctrl_name = mp["ctrl_name"]->get_string();
  366. int ctrl = mp["control"]->get_int();
  367. // 如果手工控制,否则取消手工控制
  368. (ctrl == 1)?set_manual(lm):cancel_manual(lm);
  369. }
  370. void traffic_light_manager::handle_manual(const int& gid, const int& ld, const std::string& name, const int& lc)
  371. {
  372. traffic_light_group_ptr pg = find_group(gid);
  373. if(nullptr != pg){
  374. pg->set_manual_ctrl(name, ld, lc);
  375. }
  376. }
  377. bool traffic_light_manager::on_path(const std::vector<pos_data>& vtp, const point& p)
  378. {
  379. if(vtp.size() < 2){
  380. return false;
  381. }
  382. int sid = vtp[0].m_site_id;
  383. auto it = m_map_path.find(sid);
  384. bool exist = false;
  385. if(it != m_map_path.end()){
  386. for(size_t i = 0;i<it->second.size();++i)
  387. {
  388. if(it->second[i].contain(p, 20)){
  389. exist = true;
  390. break;
  391. }
  392. }
  393. }
  394. return exist;
  395. }
  396. // 红绿灯处理模块入口
  397. void traffic_light_manager::handle_position(pos_data& p)
  398. {
  399. if(p.m_type == 3 || p.m_type == 1){
  400. std::map<uint64_t, pos_data>::iterator it = m_map_card.find(p.m_card_id);
  401. if(it != m_map_card.end()){
  402. m_map_card.erase(it);
  403. }
  404. }else if(p.m_type == 2){
  405. update(p.x, p.y, p.m_card_id);
  406. m_map_card[p.m_card_id] = p;
  407. log_info("[traffic_light] handle position, card_id=%d, x=%.2f, y=%.2f, aid=%d, speed=%.2f, sid=%d", p.m_card_id, p.x, p.y, p.m_area_id, p.m_speed, p.m_site_id);
  408. // 路口规则
  409. if(nullptr != m_crossing_rule){
  410. m_crossing_rule->put_pos(p);
  411. m_crossing_rule->handle_rule(p);
  412. }
  413. if(!p.m_bigger && nullptr != m_avoidance_rule){
  414. m_avoidance_rule->put_pos(p);
  415. m_avoidance_rule->handle_rule(p);
  416. }
  417. }
  418. }
  419. // 手工控制红绿灯
  420. void traffic_light_manager::set_manual(light_message& msg)
  421. {
  422. auto g = m_unmap_groups.find(msg.m_group_id);
  423. if(g != m_unmap_groups.end()){
  424. for(auto it : g->second->m_vt_lights){
  425. if(it->m_light_id == msg.m_light_id){
  426. send_light_data(msg.m_light_id, 0, msg.m_light_state);
  427. }
  428. }
  429. //log_info("[traffic_light] manual ctrl's group info, group_id=%d, light %d's shape is %d, other's shape is %d", msg.m_group_id, msg.m_light_id, msg.m_light_state, state);
  430. }
  431. }
  432. // 取消手工控制
  433. void traffic_light_manager::cancel_manual(light_message& msg)
  434. {
  435. auto g = m_unmap_groups.find(msg.m_group_id);
  436. if(g != m_unmap_groups.end()){
  437. g->second->get_turn();
  438. g->second->reset();
  439. g->second->release_turn();
  440. for(auto it_light : g->second->m_vt_lights){
  441. send_light_data(it_light->m_light_id, 0, it_light->m_state);
  442. }
  443. log_info("[traffic_light] %s cancel manual control %d's light group of %d light", msg.m_ctrl_name.c_str(), msg.m_group_id, msg.m_light_id);
  444. }
  445. }
  446. int traffic_light_manager::remove_light(const int& id)
  447. {
  448. std::lock_guard<std::mutex> lg(m_mutex);
  449. auto it = m_unmap_lights.find(id);
  450. if(it != m_unmap_lights.end()){
  451. m_unmap_lights.erase(it);
  452. }
  453. log_info("[meta_data_change] remove light, lid=%d", id);
  454. return 0;
  455. }
  456. /*
  457. * @brief 删除红绿灯组
  458. * @param 红绿灯组id号
  459. * @return 成功返回0
  460. * @note
  461. * @warning
  462. * @bug
  463. * */
  464. int traffic_light_manager::remove_light_group(const int& id)
  465. {
  466. std::lock_guard<std::mutex> lg(m_mutex);
  467. auto it = m_unmap_groups.find(id);
  468. if(it != m_unmap_groups.end()){
  469. m_unmap_groups.erase(it);
  470. }
  471. log_info("[meta_data_change] remove light group, gid=%d", id);
  472. return 0;
  473. }
  474. /*
  475. * @brief 获得红绿灯实时状态
  476. * @param 无
  477. * @return 无
  478. * @note
  479. * @warning
  480. * @bug
  481. * */
  482. std::string traffic_light_manager::get_light_state()
  483. {
  484. std::lock_guard<std::mutex> lg(m_vtlg_mutex);
  485. std::vector<sys::light_state> lights;
  486. for(auto itg : m_unmap_groups){
  487. for(auto itl : itg.second->m_vt_lights){
  488. sys::light_state state;
  489. state.m_group_id = itg.second->m_group_id;
  490. state.m_light_id = itl->m_light_id;
  491. state.m_light_state = itl->m_state;
  492. //state.m_card_id = itg->second->m_card_id;
  493. lights.push_back(state);
  494. }
  495. }
  496. return m_jsBuilder.build_traffic_light(lights);
  497. }
  498. /*
  499. * @brief 红绿灯通信异常告警
  500. * @param 无
  501. * @return 无
  502. * @note
  503. * @warning
  504. * @bug
  505. * */
  506. void traffic_light_manager::visit_light_status()
  507. {
  508. std::lock_guard<std::mutex> lg(m_mutex);
  509. time_t now = time(0);
  510. int diff = 0;
  511. for(auto it : m_unmap_lights){
  512. diff = now - it.second->m_rec_time;
  513. event_tool::instance()->handle_event(OT_DEVICE_LIGHT, ET_LIGHT_ERROR, it.second->m_light_id, LIGHT_TIMEOUT, diff, (diff > LIGHT_TIMEOUT));
  514. }
  515. }