main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. // 初始化系统参数
  42. void init()
  43. {
  44. three_rates_flag = config.get("service.three_rates_flag", 0);
  45. traffic_light_flag = config.get("service.traffic_light_flag", 0);
  46. anti_coll_flag = config.get("service.anti_collision_flag", 0);
  47. use_original_algo = config.get("algo.use_original_algo", 0);
  48. YADB::_DB_POOL_SETTING_ DBSetting;
  49. DBSetting.Host = config.get("db.host","127.0.0.1");
  50. DBSetting.User = config.get("db.user","root");
  51. DBSetting.PWD = config.get("db.passwd","123456");
  52. DBSetting.DBName = config.get("db.dbname","yaxt");
  53. DBSetting.CharSet = config.get("db.charset","utf8");
  54. DBSetting.TimeOut = config.get("db.conn_timeout",5);
  55. DBSetting.PoolSize = config.get("db.pool_size",30);
  56. DBSetting.Port = config.get("db.port",3306);
  57. if(!_mysql_init(DBSetting))
  58. {
  59. std_info("连接DB[%s:%s] 失败,采集服务器无法启动!",DBSetting.Host.c_str(),DBSetting.User.c_str());
  60. log_warn("连接DB[%s:%s] 失败,采集服务器无法启动!",DBSetting.Host.c_str(),DBSetting.User.c_str());
  61. exit(0);
  62. }
  63. log_info("数据库线程池创建成功");
  64. std::string url = config.get("service.websocket_url","ws://127.0.0.1:8086");
  65. std::string url_2 = config.get("service.websocket_url_bak","");
  66. int32_t send_interval = config.get("service.interval_send_json_postion",1);
  67. std_info("json_interval:%d,[%s]",send_interval,url.c_str());
  68. std::vector<std::string> url_list;
  69. url_list.push_back(url);
  70. if(!url_2.empty())
  71. url_list.push_back(url_2);
  72. if(!wsClientMgr_init(url_list, send_interval))
  73. {
  74. std_info("连接webServer[%s] 失败,采集服务器无法启动!",url.c_str());
  75. log_warn("连接webServer[%s] 失败!",url.c_str());
  76. }
  77. CYaSetting::Init_sys_setting();
  78. CYaSetting::m_sys_setting.m_enable_anti_coll = anti_coll_flag;
  79. //公告牌功能
  80. //bulletin_broad_show::inst()->OnInit(&config);
  81. sit_list::instance()->load_from_db();
  82. sit_list_v::instance()->load_from_db();
  83. card_list::instance()->init_card_from_db();
  84. area_list::instance()->init_from_db();
  85. area_list::instance()->init_area_persons_dynamic_thre_from_db();
  86. //forbid_staff_down_mine::instance()->init_forbid_staff();
  87. //Landmark_list::instance()->init_from_db();
  88. //special_area_list::instance()->init_from_db();
  89. mine_business::inst()->load();
  90. card_list::instance()->load_his_card_postion_from_db();
  91. event_list::instance()->load_his_data_from_db(true);
  92. event_list::instance()->load_his_data_v_from_db();
  93. if(three_rates_flag){
  94. db_para dp;
  95. dp.Host = DBSetting.Host;
  96. dp.User = DBSetting.User;
  97. dp.PWD = DBSetting.PWD ;
  98. dp.DBName = DBSetting.DBName;
  99. dp.CharSet = DBSetting.CharSet ;
  100. dp.TimeOut = DBSetting.TimeOut;
  101. dp.PoolSize = DBSetting.PoolSize;
  102. init_three_rates(dp);
  103. }
  104. if(traffic_light_flag){
  105. init_traffic_light();
  106. }
  107. // tdoa基站天线间时间同步数据
  108. //init_ant_sync();
  109. log_info("Init_Setting::init Success. \n" );
  110. }
  111. void init_three_rates(const db_para& dbs)
  112. {
  113. /*log_info("init three_rates ...................");
  114. init_para ip;
  115. ip.send_pt = handlereader;
  116. ip.driving_face_alarm = Handle_ThreeRates_Event_Callback;
  117. three_rates::get_instance()->init(ip,dbs);
  118. three_rates::get_instance()->start();
  119. std_info("加载三率模块成功");
  120. */
  121. }
  122. void init_traffic_light()
  123. {
  124. log_info("init traffic_light ...................");
  125. traffic_light_manager::instance()->init(CYaSetting::m_sys_setting.light_group_count);
  126. traffic_light_manager::instance()->start();
  127. std_info("成功加载信号灯模块");
  128. }
  129. bool _mysql_init(YADB::_DB_POOL_SETTING_ &dps)
  130. {
  131. std::string szError = "";
  132. bool flag = true;
  133. //创建连接池
  134. if ( !sDBConnPool.Create( dps, szError ) )
  135. {
  136. log_error("数据库线程池创建失败,Err=%s", szError.c_str());
  137. std_error("数据库线程池创建失败,Err=%s", szError.c_str());
  138. flag = false;
  139. }
  140. return flag;
  141. }
  142. bool wsClientMgr_init(const std::vector<std::string>&uri_list,int32_t st)
  143. {
  144. std::map<std::string, sys::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
  145. module_mgr::init(config, MsgFuncList);
  146. web_connect::init(uri_list, st, MsgFuncList);
  147. return web_connect::connect();
  148. }
  149. // 初始化时间同步内的天线间距离信息
  150. void init_ant_sync()
  151. {
  152. for(auto site : sit_list::instance()->m_map){
  153. 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);
  154. }
  155. double d = 0.0;
  156. for(auto it = ssync_manager.ump_anchors.begin(); it != ssync_manager.ump_anchors.end(); ++it){
  157. for(auto it2 = std::next(it,1);it2 != ssync_manager.ump_anchors.end(); ++it2){
  158. d = sqrt(pow((it->second.x - it2->second.x),2)
  159. + pow((it->second.y - it2->second.y),2)
  160. + pow((it->second.z - it2->second.z),2)) * 10000 / (2.99702547 * 15.65);
  161. ssync_manager.update_distance(it->first>>8, it->first&0xFF, it2->first>>8, it2->first&0xFF, d);
  162. }
  163. }
  164. }
  165. };
  166. /*
  167. * @brief 停止业务模块和websocket server的连接
  168. * @param 无
  169. * @return 无
  170. * @note
  171. * @warning
  172. * @bug
  173. * */
  174. void cleanup()
  175. {
  176. module_mgr::stop();
  177. web_connect::close();
  178. }
  179. void usage(char ** argv)
  180. {
  181. std::string cmd(argv[1]);
  182. if(cmd=="--help" || cmd == "-H")
  183. {
  184. printf("用法:yals [选项]文件...\n");
  185. printf("选项:\n");
  186. printf("--help 显示此帮助说明\n");
  187. printf("-v|--version 显示采集程序版本信息\n");
  188. }
  189. else if(cmd == "--version" || cmd == "-v")
  190. {
  191. printf("zhuhai xinxingxin data collector server (采集程序) 1.3.0 build 11\n");
  192. }
  193. else
  194. {
  195. printf("Unknown option:%s\n",cmd.c_str());
  196. printf( "usage:yals [--version][--help][-v][-H]\n");
  197. }
  198. }
  199. /*
  200. * @brief 主程序入口
  201. * @param [in] int argc
  202. * @param [in] char* argv[]
  203. * @return 0表示正常运行,非零值表示错误
  204. * @note
  205. * @warning
  206. * @bug
  207. * */
  208. int main(int argc ,char * argv[])
  209. {
  210. if(argc >1)
  211. {
  212. usage(argv);
  213. return 0;
  214. }
  215. // 加载共享内存
  216. log_init("../etc/log.ini");
  217. if(config.open("../etc/config.ini"))
  218. return -1;
  219. // 系统初始化
  220. Init_Setting is;
  221. is.init();
  222. // 业务模块服务启动
  223. module_mgr::start();
  224. //web_connect::start_beatheart_monitor();
  225. //atexit(&cleanup);
  226. // 提供数据给第三方服务
  227. int interface_port = config.get("service.interface_port",7001);
  228. log_info("service-position.run(%d)",interface_port);
  229. std_info("service-position.run(%d)",interface_port);
  230. ios_service::start_service_position(interface_port);
  231. // 启动网络模块服务
  232. net_service mh;
  233. int port = config.get("service.port",4000);
  234. log_info("service_handle::instance(&mh)->run(%d)",port);
  235. std_info("service_handle::instance(&mh)->run(%d)",port);
  236. service_handle::instance(&mh)->run(port);
  237. // 关闭数据库连接池
  238. sDBConnPool.Close();
  239. sleep(1);
  240. printf("Collector Server Exit ...\n");
  241. return 0;
  242. }