//
// Created by zx on 2019/11/25.
//

#include "MeasureTopicPublisher.h"
#include "sys/time.h"
#include "unistd.h"
#include "math.h"
MeasureTopicPublisher* MeasureTopicPublisher::g_publisher=0;
MeasureTopicPublisher* MeasureTopicPublisher::GetInstance()
{
    if(g_publisher==0)
    {
        g_publisher=new MeasureTopicPublisher(CONNECTSTRING);
    }
    return g_publisher;
}
MeasureTopicPublisher::MeasureTopicPublisher(std::string connectStr)
{
    m_sock.bind(connectStr);
    gettimeofday(&m_last_time,0);
}

bool MeasureTopicPublisher::Publish(std::string data)
{
    std::lock_guard<std::mutex> lk(m_lock);
    /*double tick=0;
    do{
        struct timeval tm;
        gettimeofday(&tm, 0);
        tick=fabs((tm.tv_sec-m_last_time.tv_sec)*1000.0+(tm.tv_usec-m_last_time.tv_usec)/1000.0);
        m_last_time=tm;
    }while(tick<10);*/
    usleep(100);
    return m_sock.send(data)==data.length();
}
