Cooller_ModBusController::Cooller_ModBusController(CoolerStateWidget *view, ModBusDialog *config) :
    m_view(view), 
    m_configDialog(config),
    m_recheckTimer(new QTimer(this)),
    m_available(false),
    m_comunicationSpeedIndex(6), //9600
    m_currentDeviceID(1),
    m_connector(this),
    m_externalManager(this),
    m_modbus(this)
{
    connect(m_recheckTimer, SIGNAL(timeout()), this, SLOT(updateState()));
    m_recheckTimer->setInterval(500);
    m_recheckTimer->start();
    config->setExternalCommunicator(&m_connector);

    connect(m_configDialog, SIGNAL(speedChanged(int)), this, SLOT(newSpeed(int)));
    connect(m_configDialog, SIGNAL(portChanged(int)), this, SLOT(newPort(int)));
    connect(m_configDialog, SIGNAL(deviceIDChanged(int)), this, SLOT(newDevice(int)));
    connect(&m_connector, SIGNAL(connectionEstablished()), config, SLOT(connectionEstablished()));
    connect(&m_connector, SIGNAL(connectionEstablished()), this, SLOT(sendConfiguration()));
    connect(&m_connector, SIGNAL(connectionBroken()), config, SLOT(connectionBroken()));
    connect(&m_connector, SIGNAL(connectionErrorOccured(QString)), config, SLOT(connectionErrorOccured(QString)));
    connect(&m_externalManager, SIGNAL(stateChanged()), this, SLOT(externalStateChanged()));
    connect(&m_externalManager, SIGNAL(listChanged()), this, SLOT(externalListChanged()));
    connect(&m_modbus, SIGNAL(deviceListUpdated(void)), this, SLOT(deviceObserverWaked()));

    QString configsPath = Configurator::getConfigFilesPath();
    QDirIterator iter(configsPath, QStringList() << "*.xml", QDir::Files | QDir::NoDotAndDotDot, QDirIterator::NoIteratorFlags);
    int configsRed = 0;
    while (iter.hasNext())
    {
        iter.next();
        QString xmlFilePath = iter.filePath();
        if (readXMLConfig(xmlFilePath))
            configsRed++;
    }
    if (0 == configsRed )
    {
        emit newStatus(tr("No config files were found"));
    }
}
void ExternalConnector::handshakeProc(boost::property_tree::ptree* params)
{
    bool isSuccess = params->get<bool>("success");
    if (isSuccess)
    { 
        emit connectionEstablished();
        m_active = true;
    }
    else
    {
        emit connectionErrorOccured(tr("Version is not supported"));
        m_socket.close();
    }
}
void ClientConnectionManager::incomingConnection()
{
    QTcpSocket * connection;
    while((connection = mServer.nextPendingConnection()) != nullptr)
    {
        mSockets.append(connection);
        mRequestMan.addWatchList(connection);
        QString socket_id = QString::number(int(connection->socketDescriptor()));
        emit connectionCreated(socket_id);
        connect(connection,SIGNAL(disconnected()),this,SLOT(socketDisconnected()));
        connect(connection,
                SIGNAL(error(QAbstractSocket::SocketError)),
                this,
                SLOT(connectionErrorOccured(QAbstractSocket::SocketError)));
        connect(connection,
                SIGNAL(stateChanged(QAbstractSocket::SocketState)),
                this,
                SLOT(connectionStateChanged(QAbstractSocket::SocketState)));
    }
}
void ExternalConnector::connectionError(QAbstractSocket::SocketError err)
{
    m_active = false;
    QString errorDescription = m_socket.errorString();
    emit connectionErrorOccured(errorDescription);
}