1
0

main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <ev++.h>
  2. #include <iterator>
  3. #include <log.h>
  4. #include <net-service.h>
  5. #include "db_api/CDBSingletonDefine.h"
  6. #include "websocket/wsClientMgr.h"
  7. #include "websocket/wsTimerThread.h"
  8. #include "card.h"
  9. #include "ant.h"
  10. #include "area.h"
  11. #include "card_path.h"
  12. #include "landmark.h"
  13. #include "special_area.h"
  14. #include "mine.h"
  15. #include "event.h"
  16. #include "module_service/module_mgr.h"
  17. #include <config_file.h>
  18. #include "three_rates.h"
  19. #include "mine_business.h"
  20. #include "sys_setting.h"
  21. #include "websocket/web_connect.h"
  22. #include "forbid_staff_down_mine.h"
  23. #include "bulletin_broad_show.h"
  24. #include "sync_time/sync_manager.h"
  25. #include "module_service/module_traffic_light_manager.h"
  26. config_file config;
  27. void handlereader(uint32_t readerid,bool duration,uint32_t t)
  28. {}
  29. //三率模块中告警回调
  30. void Handle_ThreeRates_Event_Callback(const int evType, const int evId, uint64_t id
  31. , double limitVal, double curVal, bool bFalg)
  32. {
  33. if (evId >= EVENT_TYPE::CARD_EVENT_COUNT_MAX || evType > OBJECT_TYPE::OT_CARD)
  34. {
  35. return ;
  36. }
  37. event_tool::instance()->handle_event((OBJECT_TYPE)evType,(EVENT_TYPE)evId,id,limitVal,curVal,bFalg);
  38. }
  39. struct Init_Setting
  40. {
  41. void init()
  42. {
  43. three_rates_flag=config.get("service.three_rates_flag", 0);
  44. traffic_light_flag = config.get("service.traffic_light_flag", 0);
  45. YADB::_DB_POOL_SETTING_ DBSetting;
  46. DBSetting.Host = config.get("db.host","127.0.0.1");
  47. DBSetting.User = config.get("db.user","root");
  48. DBSetting.PWD = config.get("db.passwd","123456");
  49. DBSetting.DBName = config.get("db.dbname","yaxt");
  50. DBSetting.CharSet = config.get("db.charset","utf8");
  51. DBSetting.TimeOut = config.get("db.conn_timeout",5);
  52. DBSetting.PoolSize = config.get("db.pool_size",30);
  53. DBSetting.Port = config.get("db.port",3306);
  54. if(!_mysql_init(DBSetting))
  55. {
  56. std_info("连接DB[%s:%s] 失败,采集服务器无法启动!",DBSetting.Host.c_str(),DBSetting.User.c_str());
  57. log_warn("连接DB[%s:%s] 失败,采集服务器无法启动!",DBSetting.Host.c_str(),DBSetting.User.c_str());
  58. exit(0);
  59. }
  60. log_info("数据库线程池创建成功");
  61. std::string url = config.get("service.websocket_url","ws://127.0.0.1:8086");
  62. std::string url_2 = config.get("service.websocket_url_bak","");
  63. int32_t send_interval = config.get("service.interval_send_json_postion",1);
  64. std_info("json_interval:%d,[%s]",send_interval,url.c_str());
  65. std::vector<std::string> url_list;
  66. url_list.push_back(url);
  67. if(!url_2.empty())
  68. url_list.push_back(url_2);
  69. if(!wsClientMgr_init(url_list, send_interval))
  70. {
  71. std_info("连接webServer[%s] 失败,采集服务器无法启动!",url.c_str());
  72. log_warn("连接webServer[%s] 失败!",url.c_str());
  73. }
  74. CYaSetting::Init_sys_setting();
  75. //公告牌功能
  76. //bulletin_broad_show::inst()->OnInit(&config);
  77. sit_list::instance()->load_from_db();
  78. card_list::instance()->init_card_from_db();
  79. area_list::instance()->init_from_db();
  80. area_list::instance()->init_area_persons_dynamic_thre_from_db();
  81. //forbid_staff_down_mine::instance()->init_forbid_staff();
  82. //Landmark_list::instance()->init_from_db();
  83. //special_area_list::instance()->init_from_db();
  84. mine_business::inst()->load();
  85. card_list::instance()->load_his_card_postion_from_db();
  86. //event_list::instance()->load_his_data_from_db();
  87. if(three_rates_flag){
  88. db_para dp;
  89. dp.Host=DBSetting.Host;
  90. dp.User=DBSetting.User;
  91. dp.PWD=DBSetting.PWD ;
  92. dp.DBName=DBSetting.DBName;
  93. dp.CharSet=DBSetting.CharSet ;
  94. dp.TimeOut=DBSetting.TimeOut;
  95. dp.PoolSize=DBSetting.PoolSize;
  96. init_three_rates(dp);
  97. }
  98. if(traffic_light_flag){
  99. init_traffic_light();
  100. }
  101. init_ant_sync();
  102. log_info("Init_Setting::init Success. \n" );
  103. }
  104. void init_three_rates(const db_para& dbs)
  105. {
  106. log_info("init three_rates ...................");
  107. init_para ip;
  108. ip.send_pt = handlereader;
  109. ip.driving_face_alarm = Handle_ThreeRates_Event_Callback;
  110. three_rates::get_instance()->init(ip,dbs);
  111. three_rates::get_instance()->start();
  112. std_info("加载三率模块成功");
  113. }
  114. void init_traffic_light()
  115. {
  116. log_info("init traffic_light ...................");
  117. traffic_light_manager::instance()->init(CYaSetting::m_sys_setting.light_group_count);
  118. traffic_light_manager::instance()->start();
  119. std_info("成功加载信号灯模块");
  120. }
  121. bool _mysql_init(YADB::_DB_POOL_SETTING_ &dps)
  122. {
  123. std::string szError = "";
  124. bool flag = true;
  125. //创建连接池
  126. if ( !sDBConnPool.Create( dps, szError ) )
  127. {
  128. log_error("数据库线程池创建失败,Err=%s", szError.c_str());
  129. std_error("数据库线程池创建失败,Err=%s", szError.c_str());
  130. flag = false;
  131. }
  132. return flag;
  133. }
  134. bool wsClientMgr_init(const std::vector<std::string>&uri_list,int32_t st)
  135. {
  136. std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
  137. module_mgr::init(config, MsgFuncList);
  138. web_connect::init(uri_list, st, MsgFuncList);
  139. return web_connect::connect();
  140. }
  141. // 初始化时间同步内的天线间距离信息
  142. void init_ant_sync()
  143. {
  144. for(auto site : sit_list::instance()->m_map){
  145. ssync_manager.update_anchor(site.second->m_id, 1, site.second->m_ant[0].x, site.second->m_ant[0].y, site.second->m_ant[0].z);
  146. }
  147. double d = 0.0;
  148. for(auto it = ssync_manager.ump_anchors.begin(); it != ssync_manager.ump_anchors.end(); ++it){
  149. for(auto it2 = std::next(it,1);it2 != ssync_manager.ump_anchors.end(); ++it2){
  150. d = sqrt(pow((it->second.x - it2->second.x),2)
  151. + pow((it->second.y - it2->second.y),2)
  152. + pow((it->second.z - it2->second.z),2)) * 10000 / (2.99702547 * 15.65);
  153. ssync_manager.update_distance(it->first>>8, it->first&0xFF, it2->first>>8, it2->first&0xFF, d);
  154. }
  155. }
  156. }
  157. };
  158. void cleanup()
  159. {
  160. module_mgr::stop();
  161. web_connect::close();
  162. }
  163. void usage(char ** argv)
  164. {
  165. std::string cmd(argv[1]);
  166. if(cmd=="--help" || cmd == "-H")
  167. {
  168. printf("用法:yals [选项]文件...\n");
  169. printf("选项:\n");
  170. printf("--help 显示此帮助说明\n");
  171. printf("-v|--version 显示采集程序版本信息\n");
  172. }
  173. else if(cmd == "--version" || cmd == "-v")
  174. {
  175. printf("dcserver (采集程序) 1.0.0 build 11\n");
  176. }
  177. else
  178. {
  179. printf("Unknown option:%s\n",cmd.c_str());
  180. printf( "usage:yals [--version][--help][-v][-H]\n");
  181. }
  182. }
  183. int main(int argc ,char * argv[])
  184. {
  185. if(argc >1)
  186. {
  187. usage(argv);
  188. return 0;
  189. }
  190. log_init("../etc/log.ini");
  191. if(config.open("../etc/config.ini"))
  192. return -1;
  193. Init_Setting is;
  194. is.init();
  195. module_mgr::start();
  196. //web_connect::start_beatheart_monitor();
  197. //atexit(&cleanup);
  198. net_service mh;
  199. int port = config.get("service.port",4000);
  200. int interface_port = config.get("service.interface_port",7001);
  201. log_info("service-position.run(%d)",interface_port);
  202. std_info("service-position.run(%d)",interface_port);
  203. ios_service::start_service_position(interface_port);
  204. log_info("service_handle::instance(&mh)->run(%d)",port);
  205. std_info("service_handle::instance(&mh)->run(%d)",port);
  206. service_handle::instance(&mh)->run(port);
  207. sDBConnPool.Close();
  208. sleep(1);
  209. printf("YA-SERV Server Exit ...\n");
  210. return 0;
  211. }