void BluetoothSensor::run()
{
    bool updateDB = false;
    bool scan = false;

    print("Running, CTRL+C to quit...");

    // index of the device currently scanned
    unsigned int deviceIndex = 0;

    while(!quit)
    {
        // update device db if previous update didn't succeed
        if (m_updateDBNeeded) m_updateDBNeeded = !updateDeviceData();

        // make sure that current device index is valid.
        // this also guarantees that no scanning is made when there are no devices.
        if (deviceIndex < m_devices.size())
        {
            checkDevice(deviceIndex);
        }

        // move to next device, start from beginning when at the end
        deviceIndex++;
        if (deviceIndex >= m_devices.size()) deviceIndex = 0;

        // check if there are arrived mqtt messages (commands)
        do
        {
            processIncomingMessages(updateDB, scan);
            if (updateDB)
            {
                m_updateDBNeeded = !updateDeviceData();
            }
            if (scan)
            {
                discoverDevices();
            }
        }

        // check arrived messages again after database update or device discovery,
        // so that a possible request for those is processed before continuing
        while ((updateDB || scan) && !quit);

        // check that Mosquitto is still connected.
        // if Mosquitto disconnects there can be messages from this iteration
        // which aren't sent at all, but missing some outgoing messages
        // shouldn't be too big of a problem.
        if (!m_mosquitto->isConnected())
        {
            printError("Mosquitto disconnected");
            if (connectMosquitto())
            {
                // update device db and send hello after mosquitto reconnect
                m_updateDBNeeded = !updateDeviceData();
                sendHello();
            }
        }
    }
}
Пример #2
0
void NetMIDIInputPrivate::open(QString portName)
{
    int p = m_inputDevices.indexOf(portName);
    if (p > -1)
    {
        m_socket = new QUdpSocket();
        m_parser = new MIDIParser(m_inp);
        m_port = MULTICAST_PORT + p;
        m_currentInput = portName;
        m_socket->bind(QHostAddress::AnyIPv4, m_port, QUdpSocket::ShareAddress);
        m_socket->setSocketOption(QAbstractSocket::MulticastLoopbackOption, 0);
        m_socket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1);
        if (m_iface.isValid()) {
            m_socket->joinMulticastGroup(m_groupAddress, m_iface);
        } else {
            m_socket->joinMulticastGroup(m_groupAddress);
        }
        connect(m_socket, SIGNAL(readyRead()), this, SLOT(processIncomingMessages()));
        //qDebug() << Q_FUNC_INFO << portName;
    }
}
Пример #3
0
void LANSensor::run()
{
    bool updateDB = false;
    bool scan = false;

    print("Running, CTRL+C to quit...");

    while(!quit)
    {
        // update device db if previous update didn't succeed
        if (m_updateDBNeeded) m_updateDBNeeded = !updateDeviceData();

        checkDevices();

        std::stringstream ss;
        ss << "Waiting scan interval (" << m_scanInterval << " sec)...";
        print(ss.str());

        // wait scan interval, check for incoming messages every second while doing so
        for (int timer = 0; timer < m_scanInterval; timer++)
        {
            sleep(1);

            // check if there are arrived mqtt messages (commands)
            do
            {
                processIncomingMessages(updateDB, scan);
                if (updateDB)
                {
                    m_updateDBNeeded = !updateDeviceData();

                    // exit from wait loop
                    timer = m_scanInterval;
                }
                if (scan)
                {
                    discoverDevices();

                    // exit from wait loop
                    timer = m_scanInterval;
                }
            }

            // check arrived messages again after database update or device discovery,
            // so that a possible request for those is processed before continuing
            while ((updateDB || scan) && !quit);

            // check that Mosquitto is still connected.
            // if Mosquitto disconnects there can be messages from this iteration
            // which aren't sent at all, but missing some outgoing messages
            // shouldn't be too big of a problem.
            if (!m_mosquitto->isConnected())
            {
                printError("Mosquitto disconnected");
                if (connectMosquitto())
                {
                    // update device db and send hello after mosquitto reconnect
                    m_updateDBNeeded = !updateDeviceData();
                    sendHello();
                }
                break;
            }
            if (quit) break;
        }   
    }
}