123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- //
- // Created by huli on 2021/11/18.
- //
- #include "dispatch_singlechip.h"
- Dispatch_singlechip::Dispatch_singlechip()
- {
- m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_UNKNOW;
- m_plc_id = 0;
- m_singlechip_id = 0;
- m_singlechip_data_msg_updata_flag = false;
- m_validity = false;
- }
- Dispatch_singlechip::~Dispatch_singlechip()
- {
- dispatch_singlechip_uninit();
- }
- //调度单片机 初始化
- Error_manager Dispatch_singlechip::dispatch_singlechip_init(int plc_id, int singlechip_id, int singlechip_direction)
- {
- m_plc_id = plc_id;
- m_singlechip_id = singlechip_id;
- m_singlechip_direction = singlechip_direction;
- mp_execute_thread = nullptr ;
- m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
- m_singlechip_data_msg_updata_flag = false;
- // 线程默认开启
- m_execute_condition.reset(false, true, false);
- mp_execute_thread = new std::thread(&Dispatch_singlechip::execute_thread_fun, this);
- m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
- return Error_code::SUCCESS;
- }
- //调度单片机 反初始化
- Error_manager Dispatch_singlechip::dispatch_singlechip_uninit()
- {
- if (mp_execute_thread)
- {
- m_execute_condition.kill_all();
- }
- if (mp_execute_thread)
- {
- mp_execute_thread->join();
- delete mp_execute_thread;
- mp_execute_thread = NULL;
- }
- m_dispatch_singlechip_status = Dispatch_singlechip::DISPATCH_SINGLECHIP_UNKNOW;
- return Error_code::SUCCESS;
- }
- //调度单片机 执行状态消息
- Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg(message::Singlechip_data &singlechip_data_msg, bool validity)
- {
- std::unique_lock<std::mutex> t_lock2(m_lock);
- m_singlechip_data_msg = singlechip_data_msg;
- m_validity = validity;
- m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
- m_singlechip_data_msg_updata_flag = true;
- return Error_code::SUCCESS;
- }
- Dispatch_singlechip::Dispatch_singlechip_status Dispatch_singlechip::get_dispatch_singlechip_status()
- {
- return m_dispatch_singlechip_status;
- }
- //判断出口是否空闲
- bool Dispatch_singlechip::is_outlet_ready()
- {
- if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
- m_singlechip_data_msg.insideexistenceflag() == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //执行外界任务的执行函数
- void Dispatch_singlechip::execute_thread_fun()
- {
- LOG(INFO) << " Dispatch_singlechip::execute_thread_fun() start " << this;
- Error_manager t_error;
- while (m_execute_condition.is_alive())
- {
- m_execute_condition.wait();
- if (m_execute_condition.is_alive())
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- // std::this_thread::sleep_for(std::chrono::seconds(1));
- std::this_thread::yield();
- if (m_singlechip_data_msg_updata_flag)
- {
- // std::cout << " huli test :::: " << " m_singlechip_data_msg = " << m_singlechip_data_msg.DebugString() << std::endl;
- std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
- std::unique_lock<std::mutex> t_lock2(m_lock);
- //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
- int t_temp = (m_singlechip_data_msg.terminalid() % 2) + (m_singlechip_data_msg.dispatchdirection()-1)*2;
- Dispatch_communication::Singlechip_response_from_manager_to_plc *p_response = &Dispatch_communication::get_instance_references().m_singlechip_response_from_manager_to_plc[t_temp];
- Dispatch_communication::Singlechip_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_temp];
- if (p_request->m_request_communication_mode != 2)
- {
- p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
- p_response->m_response_communication_mode = p_request->m_request_communication_mode;
- p_response->m_response_refresh_command = p_request->m_request_refresh_command;
- p_response->m_response_data_validity = m_validity;
- p_response->m_process_status = m_singlechip_data_msg.processstatus();
- p_response->m_process_control = m_singlechip_data_msg.processcontrol();
- p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
- p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
- p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
- p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
- p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
- p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
- p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
- p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
- p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
- p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
- p_response->m_error_code = 0;
- p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
- p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
- p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
- p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
- p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
- p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
- p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
- p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
- p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
- p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
- p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
- p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
- p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
- p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
- p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
- p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
- }
- else if (p_response->m_response_refresh_command == p_request->m_request_refresh_command)
- {
- p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
- p_response->m_response_communication_mode = p_request->m_request_communication_mode;
- p_response->m_response_refresh_command = p_request->m_request_refresh_command;
- }
- else
- {
- p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
- p_response->m_response_communication_mode = p_request->m_request_communication_mode;
- p_response->m_response_refresh_command = p_request->m_request_refresh_command;
- p_response->m_response_data_validity = m_validity;
- p_response->m_process_status = m_singlechip_data_msg.processstatus();
- p_response->m_process_control = m_singlechip_data_msg.processcontrol();
- p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
- p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
- p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
- p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
- p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
- p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
- p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
- p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
- p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
- p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
- p_response->m_error_code = 0;
- p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
- p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
- p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
- p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
- p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
- p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
- p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
- p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
- p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
- p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
- p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
- p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
- p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
- p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
- p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
- p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
- }
- m_singlechip_data_msg_updata_flag = false;
- }
- //判断超时
- {
- std::unique_lock<std::mutex> t_lock3(m_lock);
- if (std::chrono::system_clock::now() - m_singlechip_data_msg_updata_time > std::chrono::seconds(5))
- {
- m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_DISCONNECT;
- }
- else
- {
- m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
- }
- }
- }
- }
- }
|