示例#1
0
void CreateAccount::setupValidators()
{
    // simple alphabetic regex
    // TODO: update for Unicode characters
    QRegularExpression rxAlpha("^[a-zA-Z-]+$");
    QValidator* alphaValidator = new QRegularExpressionValidator(rxAlpha, this);

    // firstname

    ui->editFirstname->setValidator(alphaValidator);
    ui->editFirstname->setPlaceholderText(tr("e.g. Mike"));

    // middle name

    ui->editMiddlename->setValidator(alphaValidator);
    ui->editMiddlename->setPlaceholderText(tr("e.g. Joe"));

    // lastname

    ui->editLastname->setValidator(alphaValidator);
    ui->editLastname->setPlaceholderText(tr("e.g. Stones"));

    // profession

    ui->editProfession->setValidator(alphaValidator);
    ui->editProfession->setPlaceholderText(tr("e.g. Engineer"));

    QRegularExpression rxNum("^\\d+$");
    QValidator* numValidator = new QRegularExpressionValidator(rxNum, this);

    // contact

    ui->editContact->setValidator(numValidator);
    ui->editContact->setPlaceholderText("e.g. 01202876543");

    // email

    QRegularExpression rxEmail("^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.])[a-zA-Z]{2,6}$");
    QValidator* emailValidator = new QRegularExpressionValidator(rxEmail, this);

    ui->editEmail->setValidator(emailValidator);
    ui->editEmail->setPlaceholderText("e.g. [email protected]");

    // amount
    ui->editAmount->setValidator(numValidator);

    // cardnumber
    ui->editCardNumber->setInputMask("9999-9999-9999-9999");
}
bool Mosq2QuickSurf::send(DataPoint* dp, QString data)
{
    QString url = dp->getBaseURL();
    url.append("?");
    url.append(dp->getDeviceId());

    //qDebug() << "URL" << url;

    MessageParser parser(data, dp->getType());
    if(!parser.parse())
    {
        qDebug() << "Failed to parse data:" << data << dp->getType();
        return false;
    }

    bool dataOK = false;

    //is this a alarm or normal data?
    if(parser.isAlarm())
    {
        //Send a alarm
        url.append("&Larm=");
        QString alarm(dp->getName());
        alarm.append(" ");
        alarm.append(parser.getValue());
        url.append( QUrl::toPercentEncoding(alarm, "", " ") );
        qDebug() << "Alarm url:" << url;

        dataOK = true;
    }
    else
    {
        url.append("&");
        url.append(dp->getName());
        if( parser.getType() == DATAPOINT_REGULATOR )
        {
            url.append("_Supply");
        }
        url.append("=");
        url.append(parser.getValue());

        if( parser.getType() == DATAPOINT_RH )
        {
            QString val2 = parser.getValue2();
            if(!val2.isEmpty())
            {
                QString name = dp->getName();

                QRegExp rxNum(".*(\\d{1,2})");
                int pos = rxNum.indexIn(name);
                //if(rxNum.captureCount() == 1)
                if (pos > -1)
                {
                    bool ok;
                    QString numStr = rxNum.cap(1);
                    int num = numStr.toInt(&ok, 10);
                    num++;

                    if(ok)
                    {
                        name.chop(numStr.size());
                        name.append( QString("%1").arg(num, 2, 10, QChar('0')) );

                        url.append("&");
                        url.append(name);
                        url.append("=");
                        url.append(val2);
                    }
                }
            }
        }


        if(parser.getType() == DATAPOINT_REGULATOR)
        {
            url.append("&");
            url.append(dp->getName());
            url.append("_SetPoint");
            url.append("=");
            url.append(parser.getSetpoint());

            url.append("&");
            url.append(dp->getName());
            url.append("_Actuator");
            url.append("=");
            url.append(parser.getOutput());

            if(!parser.getActive().isEmpty())
            {
                url.append("&");
                url.append(dp->getName());
                url.append("_Activator");
                url.append("=");
                url.append(parser.getActive());
            }
        }

        dataOK = true;
    }

    if(dataOK)
    {
        bool result = false;
        int  retry = 3;
        do
        {
            result = QuickSurf::doSurf(url);
            if(result == false)
            {
                qDebug() << "Error: QuickSurf failed: retry" << retry;
            }
            retry--;
        } while(result == false && retry != 0);

        return result;
    }

    return false;
}