123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #include "three_rates_impl.h"
- #include "operating_rate.h"
- #include "regularcycle_rate.h"
- #include "common_data.h"
- #include <thread>
- #include <unistd.h>
- #include <string>
- #include <sys/time.h>
- #include <log.h>
- #include <iostream>
- #include <float.h>
- #include<numeric>
- //使用自己写的无锁队列
- //#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<operating_rate>();
- //采煤机正规循环率
- m_pregularcycle_rate = make_shared<regularcycle_rate>();
- m_pregularcycle_rate->init();
- //工作面司机与车辆距离告警
- m_pvehicle_driver_alarm = make_shared<vehicle_driver_alarm>();
- m_pvehicle_driver_alarm->init();
- m_pvehicle_driver_alarm->set_send_callback(para.driving_face_alarm);
- m_drivingface_alarm = make_shared<DrivingFaceAlarm>();
- m_drivingface_alarm->init();
- m_drivingface_alarm->set_send_callback(para.driving_face_alarm);
- //新增加的工作面初始化数据及公共数据
- m_common_data = make_shared<common_data>();
- 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<std::mutex> 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<card_pos> p_card_pos = std::make_shared<card_pos>();
- 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_pos> &card_ptr, std::shared_ptr<area_data> &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_pos> &card_ptr, std::shared_ptr<area_data> &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<coaldrivingface_card> & 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<std::mutex> 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<int,int>::iterator it = m_card_sync_num.find(id);
- if (it == m_card_sync_num.end())
- {
- m_card_sync_num[id] = 1;
- }
- else
- {
- ++ it->second;
- }
- }
|