Exemple #1
0
/**
 * Given a string, parse either a color pattern or color code
 * and trigger system based on that.
 * 
 * FIXME: this knows too much about emitting signals
 *
 * @param str string to parse
 * @param type type of monitor running ("ifttt", "url", "file", "script")
 * @param lastModTime time parsing occurred
 * @return bool true if match was found, false if not
 */
bool DataInput::parsePatternOrColor(QString str, QString type, int lastModTime)
{
    bool success = false;
    // look for pattern
    QString patternName = readColorPattern( str );
    bool patternFound = !patternName.isEmpty();
    qDebug() << "type:"<<type<< " patternName:"<<patternName << "str: "<<str;
    if( patternFound ) {
        emit runPattern(patternName, false);
        emit addReceiveEvent( lastModTime, patternName, type);
        input->setArg2(patternName);
        success = true;
    } 
    // or look for hex color
    else { 
        QColor c = readColorCode( str );
        bool colorFound = c.isValid();
        if( colorFound ) { 
            QString colorstr = c.name().toUpper();
            emit setColor( c );
            emit addReceiveEvent( lastModTime, colorstr, type);
            input->setArg2( colorstr );  // FIXME: arg2 should not be used for lastVal
            success = true;
        }
        else { 
            qDebug() << "parsePatternOrColor no color found";
        }
    }
    return success;
}
Exemple #2
0
int OSSex::previousPattern() {
	volatile patternList *prev = _first;

	if (!_currentPattern) return -1;

	while (prev->nextPattern != _currentPattern && prev->nextPattern != NULL) {
		prev = prev->nextPattern;
	}
	_currentPattern = prev;
	runPattern(_currentPattern->patternFunc);
	return 1;
}
Exemple #3
0
void DataInput::onProcessFinished()
{
    while(readingProcess)
        QThread::usleep(200);
    process->close();
    process->deleteLater();
    //qDebug()<<processOutput;
    int idx = processOutput.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
    QColor c=QColor("#000000");
    if(idx!=-1)
    {
        c=QColor(processOutput.mid(idx,7));
        emit setColor(c);
        emit addReceiveEvent(QDateTime::currentDateTime().toTime_t(), c.name().toUpper(), "SCRIPT");
        input->setArg2(c.name());
    }
    else
    {
        bool found = false;
        int currentIndex = 0;
        int idx2;
        QString pName;
        while(!found)
        {
            idx = processOutput.indexOf("\"", currentIndex);
            if(idx == -1)
                break;
            currentIndex = idx+1;
            idx2 = processOutput.indexOf("\"", currentIndex);
            if(idx2 == -1)
                break;
            currentIndex = idx2+1;

            pName = processOutput.mid(idx+1, idx2-idx-1);
            for(int i=0; i<patternList.count(); i++)
            {
                if(patternList[i] == pName)
                {
                    emit runPattern(pName, false);
                    emit addReceiveEvent(QDateTime::currentDateTime().toTime_t(), pName, "SCRIPT");
                    input->setArg2(pName);
                    found = true;
                    break;
                }
            }

        }
        if(!found)
            input->setArg2("NO VALUE");
    }
    emit toDelete(this);
}
Exemple #4
0
int OSSex::nextPattern() {
  if (!_currentPattern) {
    return -1;
  }

  if (_currentPattern->nextPattern == NULL) {
    _currentPattern = _first;
  } else {
    _currentPattern = _currentPattern->nextPattern;
  }
  runPattern(_currentPattern->patternFunc);
  return 1;
}
Exemple #5
0
// run a specific pattern from the queue
int OSSex::runPattern(unsigned int pos) {
  if (!_currentPattern) {
    return -1;
  }
  _currentPattern = _first;
  for (int i = 0; i < pos; i++) {
    _currentPattern = _currentPattern->nextPattern;
    if (_currentPattern == NULL) {
        return -2;
    }
  }
  return runPattern(_currentPattern->patternFunc);
}
Exemple #6
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);
}
Exemple #7
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;
    }
}
Exemple #8
0
void Search::Run(const Inputs &inputs) {
    for(Inputs::const_iterator ngi = inputs.begin(); ngi!=inputs.end(); ++ngi) {
        node_iter ni;
        for(ni = nodes().begin(); ni!=nodes().end(); ++ni)
            if(gd<Name>(*ni)==ngi->first) {
                StrGraph &dest = gd<SearchStage>(*ni).result;
                dest = *ngi->second;
                break;
            }
        /*
        if(ni==nodes().end())
            throw StageNotFound(ngi->first);
        */
    }
    while(1) {
        Node *stage = 0;
        // find a stage for which all prereq stages are done
        for(node_iter ni = nodes().begin(); ni!=nodes().end(); ++ni) {
            if(gd<SearchStage>(*ni).done)
                continue;
            inedge_iter ei;
            for(ei = (*ni)->ins().begin(); ei!=(*ni)->ins().end(); ++ei)
                if(!gd<SearchStage>((*ei)->tail).done)
                    break;
            if(ei==(*ni)->ins().end()) {
                stage = *ni;
                break;
            }
        }
        if(!stage)
            break;
        SearchStage &earchage = gd<SearchStage>(stage);
        Search::inedge_iter ei = stage->ins().begin();
        if(ei!=stage->ins().end())
            switch(earchage.type) {
            case UnionInquiry:
                earchage.result = gd<SearchStage>((*ei)->tail).result;
                for(; ei!=stage->ins().end(); ++ei)
                    earchage.result |= gd<SearchStage>((*ei)->tail).result;
                break;
            case IntersectionInquiry:
                earchage.result = gd<SearchStage>((*ei)->tail).result;
                for(; ei!=stage->ins().end(); ++ei)
                    earchage.result &= gd<SearchStage>((*ei)->tail).result;
                break;
            case PatternInquiry: {
                if(!earchage.pattern)
                    throw PatternNotThere();
                earchage.result.clear();
                queue<Match> Q;
                PathsFollowed followed;
                for(; ei!=stage->ins().end(); ++ei) {
                    Name inputname = gd<Name>(*ei);
                    Pattern::node_iter ni;
                    for(ni = earchage.pattern->nodes().begin();
                            ni!=earchage.pattern->nodes().end(); ++ni)
                        if(gd<Name>(*ni)==inputname) {
                            StrGraph &input = gd<SearchStage>((*ei)->tail).result;
                            for(StrGraph::node_iter inpi = input.nodes().begin(); inpi !=input.nodes().end(); ++inpi) {
                                earchage.result.insert(*inpi);
                                Q.push(Match(*ni,source.find(*inpi)));
                            }
                            break;
                        }
                    if(ni==earchage.pattern->nodes().end())
                        throw PatternStateNotFound(inputname);
                }
                runPattern(Q,followed,earchage.limit,&earchage.result);
                break;
            }
            case PathInquiry: {
                earchage.result.clear();
                StrGraph *a = 0, *b = 0;
                for(; ei!=stage->ins().end(); ++ei) {
                    DString inputname = gd<Name>(*ei);
                    if(inputname=="a")
                        a = &gd<SearchStage>((*ei)->tail).result;
                    else if(inputname=="b")
                        b = &gd<SearchStage>((*ei)->tail).result;
                }
                if(a&&b)
                    if(earchage.shortest)
                        findShortestPaths(source,*a,*b,earchage.weightattr,earchage.limit,earchage.result);
                    else
                        findAllPaths(source,*a,*b,earchage.goOut,earchage.goIn,earchage.firstOnly,earchage.limit,earchage.result);
            }
            }
        earchage.done = true;
    }
}