#include "three_rates_impl.h" #include "operating_rate.h" #include "regularcycle_rate.h" #include "common_data.h" #include #include #include #include #include #include #include #include //使用自己写的无锁队列 //#define TR_QUEUE long long ustime(void) { struct timeval tv; long long ust; gettimeofday(&tv, NULL); ust = ((long long)tv.tv_sec)*1000000; ust += tv.tv_usec; return ust; } /* Return the UNIX time in milliseconds */ long long mstime(void) { return ustime()/1000; } three_rates_impl::proxy_creator three_rates_impl::m_proxy; // 输入日志ID==log.ini中对应的日志属性(日志文件等) int three_rates_impl::g_three_rates_log_id = 2; // 更新数据的日志ID int three_rates_impl::g_three_rates_sql_log_id = 2; // three_rates_impl* three_rates_impl::_instance() { static three_rates_impl instance; return &instance; } /* * Functon: Init the three_rates module * Parameters: * init_param& para - in&out, three_rates parameters * Returns: void * Throws: void */ void three_rates_impl::init(const init_para& para, const db_para& dpara ) { logn_info(2,"TR-three_rates init call"); m_bstop = true; m_shutdown_timeout_threshold = para.m_shutdown_timeout; if (m_shutdown_timeout_threshold <= 0) { m_shutdown_timeout_threshold = 480; } if (!m_runSo) { int ret = init_db(dpara); logn_info(2,"TR-init_db returns: %d",ret); } #ifdef TR_QUEUE p_tr_queue = new tr_queue(1024*1024); if(!p_tr_queue->initialize()) { logn_error(2,"TR-Failed to init tr_queue!"); } #endif //掘进面初始化数据 m_drivingface_module.init_all(para.m_restart); m_poperating_rate = make_shared(); //采煤机正规循环率 m_pregularcycle_rate = make_shared(); m_pregularcycle_rate->init(); //工作面司机与车辆距离告警 m_pvehicle_driver_alarm = make_shared(); m_pvehicle_driver_alarm->init(); m_pvehicle_driver_alarm->set_send_callback(para.driving_face_alarm); m_drivingface_alarm = make_shared(); m_drivingface_alarm->init(); m_drivingface_alarm->set_send_callback(para.driving_face_alarm); //新增加的工作面初始化数据及公共数据 m_common_data = make_shared(); m_common_data->init_data(); m_time = 0; m_worktime.init_shift(); return; } /* * Functon: Start the three_rates module * Parameters: void * Returns: void * Throws: void */ void three_rates_impl::start() { logn_info(2,"TR-three_rates start call"); m_bstop = false; m_pthree_rates_thread.reset(new std::thread(std::bind(&three_rates_impl::run,this))); m_pthree_rates_thread->detach(); return; } /* * Functon: thread function * Parameters: void * Returns: void * Throws: void */ void three_rates_impl::run() { logn_info(2,"TR-three_rates thread running"); while(!m_bstop) { unsigned int ret = 0; if (m_DB_Changed_list.size()>0) { //处理工作面参数更改 std::lock_guard lock(m_mutex); for (size_t i = 0; i < m_DB_Changed_list.size(); i++) { deal_db_data_changed(m_DB_Changed_list[i]); } m_DB_Changed_list.clear(); } card_pos pos; #ifdef TR_QUEUE ret = p_tr_queue->get((unsigned char*)&pos, sizeof(card_pos)); #if 0 //for test if(pos.type == 1 || pos.type == 2 || pos.type == 0) { std::this_thread::sleep_for (std::chrono::milliseconds(1)); continue; } #endif #else m_boost_queue.pop(pos); ret = sizeof(pos); if (pos.id <= 0) { std::this_thread::sleep_for (std::chrono::milliseconds(1)); continue ; } #endif if(sizeof(card_pos) == ret) { //cout << "_______get ret: "<< ret <<",type: " << pos.type << ",id: " << pos.id << endl; logn_debug(2,"TR-FIFO three_rates_impl::run, type= %d , id: %d",pos.type, pos.id); } else { std::this_thread::sleep_for (std::chrono::milliseconds(1)); continue; } add_cardsyncNum(pos.id); int vehicle_type = 0; if(pos.type !=THREE_RATES_CARD_TYPE::CT_PERSON) { //更新卡数据 std::string scard_id = tr_helper::merge_typy_id_to_card(pos.type, pos.id); auto it_map = m_poperating_rate->m_coaldrivingface_map.find(pos.identifier_id); if (it_map != m_poperating_rate->m_coaldrivingface_map.end()) { vehicle_type = it_map->second->m_vehicle_type; // store vibrate state it_map->second->m_cb_state.push_back(pos.vibration); //it_map->second->m_nvehicle_id = it_map->second->id = m_poperating_rate->m_coaldrivingface_map[it_map->second->m_scard_id]->m_nvehicle_id; it_map->second->m_ncard_type = pos.type; it_map->second->rec_time = pos.rec_time; it_map->second->x = pos.x; it_map->second->y = pos.y; it_map->second->z = pos.z; it_map->second->final_v = pos.final_v; it_map->second->m_ndept_id = pos.dpt_id; //设置当前使用的地图 m_common_data->set_map_id(pos.map_id); // process operating rate 开机率 m_poperating_rate->store_machine_state(it_map->second); if (tr_helper::is_coalminingface(it_map->second)) { // process regularcycle rate 采煤机业务 m_pregularcycle_rate->Calc_CoalMining_RegularCycleRate(it_map->second); } else if (tr_helper::is_drivingface(it_map->second)) { //deal drivingface card 掘进机业务 std::shared_ptr p_card_pos = std::make_shared(); p_card_pos->copy(pos); auto it_drivingface_card = m_drivingface_module.mp_drivingface_card_list.find(scard_id); if (it_drivingface_card != m_drivingface_module.mp_drivingface_card_list.end()) { m_drivingface_module.deal_drivingface_service(it_drivingface_card->second, p_card_pos); } //处理分站距离超限报警 if (m_drivingface_alarm != nullptr) { DrivingCard tmpCard; tmpCard.x = pos.x; tmpCard.y = pos.y; tmpCard.vehicle_card_id = pos.id; tmpCard.vehicle_id = it_map->second->m_nvehicle_id; //车卡ID tmpCard.card_type = pos.type; tmpCard.reader_id = pos.reader_id; tmpCard.reader_x = pos.reader_x; tmpCard.reader_y = pos.reader_y; tmpCard.map_scale = m_common_data->m_map_scale; if (tmpCard.map_scale > 0) { m_drivingface_alarm->driving_alarm(tmpCard); } } } } // end of if } //工作面司机与车辆的距离告警 SDeal_Card_Pos s; s.Copy(pos); s.dentifier_type = vehicle_type; m_pvehicle_driver_alarm->deal_vehicle_driver_alarm(s); std::this_thread::sleep_for (std::chrono::milliseconds(1)); }// end of while } /* * Functon: Stop the three_rates module * Parameters: void * Returns: void * Throws: void */ void three_rates_impl::stop() { logn_info(2,"TR-three_rates stop call"); m_bstop = true; #ifdef TR_QUEUE delete p_tr_queue; p_tr_queue = nullptr; #endif return; } /* * Functon: Input the located datas * Parameters: * card_pos& pos - in, position informations * Returns: void * Throws: void */ void three_rates_impl::put(const card_pos& pos) { // if( pos.type == 2) //{ // return; //} #ifdef TR_QUEUE unsigned int ret = p_tr_queue->put((unsigned char*)&pos, sizeof(card_pos)); //cout << "put ret: "<< ret <<",type: " << pos.type << ",id: " << pos.id << endl; logn_info(2,"TR-FIFO three_rates_impl::put size=%d , type= %d , id: %d",ret,pos.type, pos.id); return ; #endif m_boost_queue.push(pos); return; } /* * Functon: Enter one area * Parameters: * card_pos& pos - in, the located datas * area_data& area - in, the area informations * Returns: void * Throws: void */ void three_rates_impl::enter_area(std::shared_ptr &card_ptr, std::shared_ptr &area_ptr) { logn_info(2,"TR-three_rates enter_area call"); m_worktime.enter_area(card_ptr, area_ptr); } /* * Functon: Leave one area * Parameters: * card_pos& pos - in, the located datas * area_data& area - in, the area informations * Returns: void * Throws: void */ void three_rates_impl::leave_area(std::shared_ptr &card_ptr, std::shared_ptr &area_ptr) { logn_info(2,"TR-three_rates leave_area call"); m_worktime.leave_area(card_ptr, area_ptr); return; } /* * Functon: Init database settings * Parameters: dpara, dp_rara database configure parameters * Returns: int, returns 0 normally, otherwise it returns 1 * Throws: void */ int three_rates_impl::init_db(const db_para& dpara) { m_db_setting.Host = dpara.Host; m_db_setting.User = dpara.User; m_db_setting.PWD = dpara.PWD; m_db_setting.DBName = dpara.DBName; m_db_setting.CharSet = dpara.CharSet; m_db_setting.TimeOut = dpara.TimeOut; m_db_setting.PoolSize = dpara.PoolSize; m_db_setting.Port = dpara.Port; string Error; if ( !sDBConnPool.Create( m_db_setting, Error ) ) { logn_error(2,"TR-database pool create failed: %s", Error.c_str()); return 1; } logn_info(2,"TR-database pool creat successed!"); return 0; } /* * 获取掘进机或者采煤机的开机状态 */ bool three_rates_impl::GetVehicleState(int vehicleId) { std::string card_id = m_common_data->get_card_id(vehicleId); if (card_id == "") { return false; } auto it_map = m_poperating_rate->m_coaldrivingface_map.find( vehicleId ); if (it_map == m_poperating_rate->m_coaldrivingface_map.end()) { return false; } std::shared_ptr & card = it_map->second; if(tr_helper::is_drivingface(card) || tr_helper::is_coalminingface(card)) { int sum = std::accumulate(card->m_cb_state.begin(), card->m_cb_state.end(), 0); // 判断采煤机/掘进机惯导开关标 int openThreshold = operating_rate::STARTUP_STATE_COUNT * card->m_nshake_threshold; // 判断为开机, if (sum >= openThreshold || card->m_bopen) { return true; } // 判断为关机 int closeThreshold = operating_rate::SHUTDOWN_STATE_COUNT * card->m_nshake_threshold; if (sum <= closeThreshold || !card->m_bopen) { return false; } } return false; } void three_rates_impl::update_db_data(const std::string& webname, const std::string& param,int edit_type_id) { ChangedDBDate Db; Db.webname = webname; Db.param = param; Db.edit_type_id = edit_type_id; std::lock_guard lock(m_mutex); m_DB_Changed_list.push_back(Db); logn_info(2,"update_db_data : edit_typeid=%d,webname=%s,Param=%s Size=%d" ,edit_type_id,webname.c_str(),param.c_str(),m_DB_Changed_list.size()); } void three_rates_impl::deal_db_data_changed(const ChangedDBDate& Db) { logn_info(2,"deal_db_data_changed : edit_typeid=%d,webname=%s,Param=%s" ,Db.edit_type_id,Db.webname.c_str(),Db.param.c_str()); if (Db.webname == "coalface_vehicle") { //增加 if (Db.edit_type_id==0 || Db.edit_type_id==1) { m_pregularcycle_rate->init_data_coalmining_RegularCycle(Db.param); } else if (Db.edit_type_id == 2 ) { m_pregularcycle_rate->remove_data_coalmining_RegularCycle(atoi(Db.param.c_str())); } m_pvehicle_driver_alarm->init_coalface_driver_warningparam(); m_poperating_rate->init_coaldrivingface_vehicle(); } else if (Db.webname == "coalface") { //增加和更新 if (Db.edit_type_id == 0 || Db.edit_type_id == 1) { m_pregularcycle_rate->init_data_coalminingface_RegularCycle(Db.param); } else if (Db.edit_type_id == 2) { m_pregularcycle_rate->remove_data_coalminingface_RegularCycle(atoi(Db.param.c_str())); } m_pvehicle_driver_alarm->init_coalface_driver_warningparam(); m_poperating_rate->init_coaldrivingface_vehicle(); } else if (Db.webname == "drivingface") { //增加和更新 if (Db.edit_type_id == 0 || Db.edit_type_id == 1) { m_drivingface_module.init_drivingface_card(Db.param); m_pvehicle_driver_alarm->init_driving_driver_warningparam(Db.param); } else if (Db.edit_type_id == 2) { m_drivingface_module.remove_drivingface(atoi(Db.param.c_str())); } m_poperating_rate->init_coaldrivingface_vehicle(); } else if (Db.webname == "drivingface_vehicle") { if(Db.edit_type_id == 2) { m_drivingface_alarm->del_drivingface(atoi(Db.param.c_str())); } else { m_pvehicle_driver_alarm->init_driving_driver_warningparam(); m_drivingface_alarm->init_drivingface_db(atoi(Db.param.c_str())); } m_poperating_rate->init_coaldrivingface_vehicle(); } else if (Db.webname == "drivingface_ref_point") { m_drivingface_module.init_drivingface_ref_points(Db.param); } } //添加次数 void three_rates_impl::add_cardsyncNum(int id) { std::map::iterator it = m_card_sync_num.find(id); if (it == m_card_sync_num.end()) { m_card_sync_num[id] = 1; } else { ++ it->second; } }