Пример #1
0
void DataInput::onFinishedQml()
{
    QString txt;

    int typeNumber = typeToInt(type);
    switch(typeNumber)
    {
    case 0: {
//        emit runPattern(pattern->name(), true);
    }
        break;

    case 1: {

        txt = reply->readAll();
        int idx=txt.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
        QColor c=QColor("#000000");
        if(idx!=-1)
        {
            c=QColor(txt.mid(idx,7));
            emit setColor(c);
            emit setValueRet(c.name().toUpper());
        }
        else
        {
            emit setColor(c);
            emit setValueRet("NO VALUE");
        }
    }
        break;
    }
    delete reply;
    emit toDelete(this);
}
Пример #2
0
void DataInput::startQml()
{
    QNetworkRequest nr;
    QString url;
    int typeNumber = typeToInt(type);
    switch(typeNumber)
    {
    case 0: {
        url = "http://api.thingm.com/blink1/events/" + iftttKey;
//        url = "http://api.thingm.com/blink1/events/04CE5FA11A002B8E";
        nr.setUrl(QUrl(url));
        reply = networkManager->get(nr);
        connect(reply, SIGNAL(finished()), this, SLOT(onFinishedQml()));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
    }
        break;

    case 1: {
        url = rule;
        QUrl correctUrl(url);
        if(correctUrl.isValid())
        {
            nr.setUrl(QUrl(url));
            reply = networkManager->get(nr);
            connect(reply, SIGNAL(finished()), this, SLOT(onFinishedQml()));
            connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
        }
        else
        {
            emit setValueRet("NOT FOUND");
            emit toDelete(this);
        }
    }
        break;

    case 2: {
        QFile f(rule);
        if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            //qDebug() << "File error:";
            //qDebug() << "File not found.";
            emit setValueRet("NOT FOUND");
            emit toDelete(this);
            return;
        }
        QString txt = "";
        QTextStream in(&f);
        txt.append(in.readAll());
        int idx=txt.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
        QColor c=QColor("#000000");
        f.close();
        if(idx!=-1)
        {
            c=QColor(txt.mid(idx,7));
            emit setColor(c);
            emit setValueRet(c.name().toUpper());
        }
        else
        {
            emit setColor(c);
            emit setValueRet("NO VALUE");

        }
        emit toDelete(this);
    }
        break;

    case 3: {
        process = new QProcess;
        connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessOutput()));
        connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onError()));
        connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError()));
        connect(process, SIGNAL(finished(int)), this, SLOT(onProcessFinishedQml()));
        QString path = QStandardPaths::locate(QStandardPaths::DocumentsLocation, rule);
        QFile f(path);
        if(f.exists())
            process->start(path);
        else
        {
            //qDebug() << "Script error:";
            //qDebug() << "Script doesn't' exist!";
            emit setValueRet("NOT FOUND");
            emit toDelete(this);
        }
    }
        break;

    default: {
        emit toDelete(this);
    }
        break;
    }
}
Пример #3
0
void DataInput::start()
{
    QNetworkRequest nr;
    QString url;
    int typeNumber = typeToInt(type);
    switch(typeNumber)
    {
    case 0: {
        url = "http://api.thingm.com/blink1/eventsall/" + iftttKey;
//        url = "http://api.thingm.com/blink1/events/04CE5FA11A002B8E";
        nr.setUrl(QUrl(url));
        reply = networkManager->get(nr);
        connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
    }
        break;

    case 1: {
        url = input->arg1();
        if(!url.startsWith("http://") && !url.startsWith("https://"))
            url="http://"+url;
        QUrl correctUrl(url);
        if(correctUrl.isValid())
        {
            nr.setUrl(QUrl(url));
            reply = networkManager->get(nr);
            connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
            connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
        }
        else
        {
            emit setValueRet("NOT FOUND");
            input->setArg2("NOT FOUND");
            input->setDate(-1);
            emit toDelete(this);
        }
    }
        break;

    case 2: {
        QFileInfo fileInfo;
        fileInfo.setFile(input->arg1());
        if(!fileInfo.exists()){
            input->setArg2("NOT FOUND");
            input->setDate(-1);
        }
        if(fileInfo.lastModified().toTime_t() != (uint)input->date()) {
            QFile f(input->arg1());
            if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
            {
                //qDebug() << "File error:";
                //qDebug() << "File not found.";
                emit setValueRet("NOT FOUND");
                input->setArg2("NOT FOUND");
                input->setDate(-1);
                emit toDelete(this);
                return;
            }
            input->setDate(fileInfo.lastModified().toTime_t());
            QString txt = "";
            QTextStream in(&f);
            txt.append(in.readAll());
            int idx=txt.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
            QColor c=QColor("#000000");
            f.close();
            if(idx!=-1)
            {
                c=QColor(txt.mid(idx,7));
                emit setColor(c);
                emit addReceiveEvent(fileInfo.lastModified().toTime_t(), c.name().toUpper(), "FILE");
                input->setArg2(c.name());
            }
            else
            {
                QStringList list;
                list.append("pattern:"); list.append("\"pattern\":");
                QString patternName;
                int index=-1;
                for(int i=0; i<list.count(); i++) {
                    index = txt.indexOf(list.at(i),0);
                    if(index != -1)
                        break;
                }
                if(index != -1) {
                    index = txt.indexOf(":", index);
                    int idx1, idx2;
                    idx1 = txt.indexOf("\"", index);
                    idx2 = txt.indexOf("\"", idx1+1);
                    patternName = txt.mid(idx1+1, idx2-idx1-1);
                    for(int i=0; i<patternList.count(); i++) {
                        if(patternList[i] == patternName) {
                            emit runPattern(patternName, false);
                            emit addReceiveEvent(fileInfo.lastModified().toTime_t(), patternName, "FILE");
                            input->setArg2(patternName);
                            break;
                        }
                    }

                }else{
                    input->setArg2("NO VALUE");
                }
            }
        }
        emit toDelete(this);
    }
        break;

    case 3: {
        process = new QProcess;
        connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessOutput()));
        connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onError()));
        connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError()));
        connect(process, SIGNAL(finished(int)), this, SLOT(onProcessFinished()));
//        QString path = QStandardPaths::displayName(QStandardPaths::DocumentsLocation);
        QString path = QStandardPaths::locate(QStandardPaths::DocumentsLocation, input->arg1());
        QFile f(path);
        QFileInfo fileInfo;
        fileInfo.setFile(path);
        if(f.exists()){
            if(fileInfo.lastModified().toTime_t() != (uint)input->date()){
                input->setDate(fileInfo.lastModified().toTime_t());
                process->start(path);
            }else{
                emit toDelete(this);
            }
        }else
        {
            //qDebug() << "Script error:";
            //qDebug() << "Script doesn't' exist!";
            emit setValueRet("NOT FOUND");
            input->setArg2("NOT FOUND");
            input->setDate(-1);
            emit toDelete(this);
        }
    }
        break;

    default: {
        emit toDelete(this);
    }
        break;
    }
}
Пример #4
0
void DataInput::onFinished()
{
    int date;
    QString txt;

    int typeNumber = typeToInt(type);
    switch(typeNumber)
    {
    case 0: {
        txt = reply->readAll();
        QString dateString = "";
        if(input->type().toUpper() == "IFTTT.COM") {

            if(responseTo==NULL) emit iftttToCheck(txt); else emit iftttToCheck(txt,input);
        }
    }
        break;

    case 1: {
        QDateTime dt = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime();
        date = dt.toTime_t();
        if(date != -1) {
            if(input->date() != date) {
                input->setDate(date);

                txt = reply->readAll();
                int idx=txt.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
                QColor c=QColor("#000000");
                if(idx!=-1)
                {
                    c=QColor(txt.mid(idx,7));
                    emit setColor(c);
                    emit addReceiveEvent(date, c.name().toUpper(), "URL");
                    input->setArg2(c.name());
                }
                else
                {
                    QStringList list;
                    list.append("pattern:"); list.append("\"pattern\":");
                    QString patternName;
                    int index=-1;
                    for(int i=0; i<list.count(); i++) {
                        index = txt.indexOf(list.at(i),0);
                        if(index != -1)
                            break;
                    }

                    if(index != -1) {
                        index = txt.indexOf(":", index);
                        int idx1, idx2;
                        idx1 = txt.indexOf("\"", index);
                        idx2 = txt.indexOf("\"", idx1+1);
                        patternName = txt.mid(idx1+1, idx2-idx1-1);

                        for(int i=0; i<patternList.count(); i++)
                        {
                            if(patternName == patternList[i]) {
                                emit runPattern(patternName, false);
                                emit addReceiveEvent(date, patternName, "URL");
                                input->setArg2(patternName);
                                break;
                            }
                        }
                    }else{
                        input->setArg2("NO VALUE");
                    }
                }
            }
        }else{
            input->setArg2("NO VALUE");
        }
    }
        break;
    }
    delete reply;
    emit toDelete(this);
}
Пример #5
0
int API::select(const string tableName, const vector<string> &attr, const vector<string> &op, const vector<string> &value,
	const vector<int>& type, const vector<int>& begin, int recordSize, vector<vector<char>>& returnRecord, int toDelete){
	vector<int> end;
	vector<char> temp;
	vector<vector<char>> valuex;
	vector<int> opx;
	vector<Address> returnAddr;
	if (op.size()==0){//selete * from xxx;//无条件
		if (toDelete){
			//判断有无索引
			vector<char> tName;
			sToVchar(tableName, tName);
			vector<vector<char>> returnRecord;
			vector<int> op; op.push_back(0);
			vector<vector<char>> value; value.push_back(tName);
			vector<int> begin; begin.push_back(50);
			vector<int> end; end.push_back(100);
			vector<int> type; type.push_back(53);
			Index index;
			rmanager.findAllRecord("index.catalog", op, value, begin, end, type, index.recordSize, returnRecord, 0);
			for (int i = 0; i < returnRecord.size(); i++){//若有 则清空索引文件
				string temp = "";
				for (int j = 0; j < 50; j++)
					if (returnRecord[i][j])
						temp += returnRecord[i][j];
				//bmanager.deleteFile(temp + ".index");
				//cmanager.clearFile(temp + ".index");
					
					imanager.rebuildIndex(temp);
			}
			return rmanager.deleteAll(tableName, recordSize);
		}
		else{
			//cout << "okok" << endl;
			returnAddr=rmanager.getAllRecord(tableName, recordSize, returnRecord);
			//cout << "okok" << endl;
			return returnAddr.size();
		}
	}
	else{//有条件
		for (int i = 0; i < op.size(); i++){
			if (type[i] == 1){//int
				end.push_back(begin[i] + 4);
				intToChar4(atoi(value[i].c_str()), temp);
				valuex.push_back(temp);
				opx.push_back(typeToInt(op[i]));
			}
			else if (type[i] == 2){//float
				end.push_back(begin[i] + 4);
				floatToChar4(atof(value[i].c_str()), temp);
				valuex.push_back(temp);
				opx.push_back(typeToInt(op[i]));
			}
			else{//char(n)
				end.push_back(begin[i] + type[i]-3);
				temp.clear();
				for (int k = 0; k < value[i].size(); k++)
					temp.push_back(value[i][k]);
				//sToVchar(value[i], temp);
				valuex.push_back(temp);
				opx.push_back(typeToInt(op[i]));
			}
		}
		
		//判断属性是否都有索引
		vector<int> in;
		vector<string> attrHasIndex;

		Index index;
		string indexName;
		int useIndex = 1;
		vector<char> key;
		vector<char> key1;
		sToVchar(tableName, key);
		for (int i = 0; i < attr.size(); i++){
			int flag = 0;
			for (int j = 0; j < attrHasIndex.size(); j++)//查找这个属性有无在attrHasIndex中
				if (attr[i] == attrHasIndex[j]){
					flag = 1;
					break;
				}
			if (flag)
				continue;
			
			//查找这个属性上有无index
			key.erase(key.begin() + 50, key.end());
			sToVchar(attr[i], key1);
			key.insert(key.end(), key1.begin(), key1.end());
			Address addr = rmanager.findLastRecord("index.catalog", key, index.recordSize, 50);
			if (addr.isNullAddr()){//条件中有一个属性没有索引
				//cout << attr[i] << ": no index!" << endl;
				useIndex = 0;
				break;
			}
			else{//有索引
				//cout << attr[i] << ": index!" << endl;
				temp = rmanager.getNextRecord(addr, "index.catalog", index.recordSize);
				indexName = "";
				for (int i = 0; i < 50; i++){
					if (temp[i])
						indexName += temp[i];
				}
				attrHasIndex.push_back(indexName);
				in.push_back(i);
			}

		}

		//选择select方式
		if (!useIndex||attrHasIndex.size()>1){
			clock_t start = clock();
			returnAddr = rmanager.findAllRecord(tableName, opx, valuex, begin, end, type, recordSize, returnRecord, toDelete);
			clock_t finish = clock();
			printf("Time consumed: %f second\n", (double)(finish - start) / CLOCKS_PER_SEC);
		}
		else{//符合使用index加速的条件
			if (toDelete == 0){//select
				cout << "use index" << endl;
				clock_t start = clock();
				/*for (int i = 0; i < valuex.size(); i++)
					printvc(valuex[i]);*/
				returnAddr = imanager.select(indexName, valuex, opx);
				//cout << "ok" << endl;
				returnRecord.clear();
				for (int i = 0; i < returnAddr.size(); i++){
					returnAddr[i].setAddrFile(tableName);
					returnRecord.push_back(rmanager.getRecord(returnAddr[i], recordSize));
				}
				clock_t finish = clock();
				printf("Time consumed: %f second\n", (double)(finish - start) / CLOCKS_PER_SEC);

			}
			else{//delete
				returnAddr = rmanager.findAllRecord(tableName, opx, valuex, begin, end, type, recordSize, returnRecord, toDelete);

				for (int i = 0; i < attrHasIndex.size(); i++)
					for (int j = 0; j < returnRecord.size(); j++)
						imanager.deleteKey(attrHasIndex[i], subVector(returnRecord[j], begin[in[i]], end[in[i]]));				
			}
		}

		//returnAddr = rmanager.findAllRecord(tableName, opx, valuex, begin, end, type, recordSize, returnRecord, toDelete);
			
		return returnAddr.size();
	}
}