//
// Created by huli on 2020/9/25.
//

#include "snap7_clamp.h"

Snap7Clamp::Snap7Clamp() = default;
Snap7Clamp::~Snap7Clamp() = default;

//初始化 通信 模块。如下三选一
Error_manager Snap7Clamp::communication_init()
{
	int t_index = 0;
	std::vector<Snap7_buf::Variable_information>		t_variable_information_vector;

	//往map通信缓存里面添加所需要的buf
	std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
	std::unique_lock<std::mutex> t_lock2(m_send_buf_lock);
	Snap7_buf t_snap7_buf;

    // plcData
    t_index = 0;
    t_variable_information_vector.clear();
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"pingpong", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"wheel_exist", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"offset", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"gap", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"clamp_completed", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"wheel_exist", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"offset", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"gap", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"clamp_completed", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"wheel_exist", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"offset", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"gap", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"clamp_completed", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"wheel_exist", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"offset", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"gap", typeid(float ).name(), t_index,sizeof(float), 1 });
    t_index += sizeof(float)*1;
    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"clamp_completed", typeid(unsigned short ).name(), t_index,sizeof(unsigned short), 1 });
    t_index += sizeof(unsigned short)*1;

    LOG(INFO) << t_index;

    t_snap7_buf.init(CLAMP_SAFETY_PLC_DBNUMBER , 0, sizeof(PLCData), t_variable_information_vector, Snap7_buf::LOOP_COMMUNICATION);
	m_send_buf_map[0] = t_snap7_buf;

//    t_index = 0;
//    t_variable_information_vector.clear();
//    t_variable_information_vector.push_back(Snap7_buf::Variable_information{"heart", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
//
//    t_snap7_buf.init(CLAMP_SAFETY_PLC_DBNUMBER , 50, sizeof(unsigned short), t_variable_information_vector, Snap7_buf::LOOP_COMMUNICATION);
//    m_receive_buf_map[0] = t_snap7_buf;

    plcJsonConfig config(ETC_PATH"/etc/plc.json");
    return Snap7_communication_base::communication_init(config.ip());
    return SUCCESS;
}

//反初始化 通信 模块。
Error_manager Snap7Clamp::communication_uninit()
{
	return Snap7_communication_base::communication_uninit();
}

//更新数据
Error_manager Snap7Clamp::updata_receive_buf()
{
//	std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
//	std::unique_lock<std::mutex> t_lock2(m_data_lock);
//    static unsigned short heart;
//    memcpy(&heart, m_receive_buf_map[0].mp_buf_obverse, m_receive_buf_map[0].m_size);

    Error_manager ret = Error_code::SUCCESS;

//    if (heart == m_heart) {
//        printf("---Debug %s %d : heart not change %d ---> %d\n", __func__, __LINE__, m_heart, heart);
//        ret = Error_code::FAILED;
//    }
//    m_heart = heart;
	return ret;
}

Error_manager Snap7Clamp::updata_send_buf()
{
	std::unique_lock<std::mutex> t_lock1(m_send_buf_lock);
	std::unique_lock<std::mutex> t_lock2(m_data_lock);

    if (plcData.pingpong != 0) {
        plcData.info();
    }

	memcpy(m_send_buf_map[0].mp_buf_obverse, &plcData, m_send_buf_map[0].m_size);

    return Error_code::SUCCESS;
}




