bool UFunctionSeq::handleCommand(UServerInMsg * msg, void * extra) { // extract parameters bool result = true; // decode vars char attName[MAX_SML_NAME_LENGTH]; const int VAL_BUFF_LNG = 1000; char attValue[VAL_BUFF_LNG]; // camera and source image variables bool ask4help = false; bool loadFile = false; bool appendFile = false; bool getMissions = false; bool getStatus = false; bool getPgm = false; const char * label = NULL; bool sendReply = false; const int MNL = MAX_FILENAME_LENGTH; char loadName[MNL]; FILE * errLog = NULL; const char * errLogName = "loadPlanErr.log"; const int MELL = 300; char se[MELL]; int i, n; FILE * mf; char * pMf; const int MFL = 100; char mfn[MFL]; USeqLine * line; const int MRL = 300; char reply[MRL]; char lineXml[MRL]; // // extract parameters while (msg->tag.getNextAttribute(attName, attValue, VAL_BUFF_LNG)) { // camera device if (strcasecmp(attName, "help") == 0) ask4help = true; else if (strcasecmp(attName, "load") == 0) { strncpy(loadName, attValue, MNL); loadFile = true; } else if (strcasecmp(attName, "append") == 0) { strncpy(loadName, attValue, MNL); appendFile = true; } else if (strcasecmp(attName, "add") == 0) { // new mission line sendReply = addCmdLine(msg, attValue); } else if (strcasecmp(attName, "goto") == 0) { // new mission line if (label == NULL) label = seq->addLabel(); sendReply = addCmdLine(msg, attValue); } else if (strcasecmp(attName, "clear") == 0) { // clear current pgm seq->clear(); } else if (strcasecmp(attName, "sim") == 0) { // set simulation flag - if true, logfile must be set from cmd-line seq->setSimulated(str2bool(attValue)); } else if (strcasecmp(attName, "missions") == 0) getMissions = true; else if (strcasecmp(attName, "status") == 0) getStatus = true; else if (strcasecmp(attName, "pgm") == 0) getPgm = true; else if (strcasecmp(attName, "list") == 0) getPgm = true; else // unknown attribute result = false; } // if (ask4help) { sendHelpStart(msg, "SEQ"); sendText(msg, "---- Available SEQ options:\n"); sendText(msg, "missions Get list of all mission files\n"); sendText(msg, "status Get plan sequencer status\n"); sendText(msg, "pgm | list Get list of all loaded statements\n"); sendText(msg, "goto=\"cmd\" Give a manual mission line (in quotes), and skip to this\n"); sendText(msg, "load=\"file\" Load a mission file, and skip to this\n"); sendText(msg, "append=\"file\" Append a mission file, and skip to this\n"); sendText(msg, "add=\"command\" Add a manual command line\n"); sendText(msg, "stop Stop current mission (goto end of mission)\n"); sendText(msg, "clear remove all mission lines\n"); sendText(msg, "sim=true | false Set simulation flag (not implemented yet)\n"); sendMsg(msg, "</help>\n"); sendHelpDone(msg); } else { if (loadFile or appendFile) { errLog = fopen(errLogName, "w"); if (seq->loadPlan(loadName, errLog, appendFile)) { // loaded OK sendInfo(msg, "Loaded without errors"); } else { if (errLog != NULL) { fclose(errLog); errLog = fopen(errLogName, "r"); if (errLog != NULL) { // there is a file sendInfo(msg, "Load error list:"); while (fgets(se, MELL, errLog) != NULL) { // return all filenames in separate frames i = strlen(se); if (i > 1) { // cut off the linefeed at end of line se[i-1] = 0; result = sendInfo(msg, se); } if (not result) break; } } else result = sendWarning(msg, "Could not read error file"); if (errLog != NULL) fclose(errLog); } else sendWarning(msg, "There were errors, but could not create a logfile"); sendReply = true; } } if (label != NULL) { // start execution from label result = seq->skipToLabel(label, false); } } if (getMissions) { // return a list of available missions n = system("ls *.txt >missions.list"); if (n >= 0) { // there should be a file mf = fopen("missions.list", "r"); n = -1; if (mf != NULL) { // there is a file while (true) { // return all filenames in separate frames pMf = fgets(mfn, MFL, mf); if (pMf == NULL) break; if (n < 0) sendInfo(msg, "List of mission files:"); i = strlen(pMf); if (i > 1) { // cut off the linefeed at end of line pMf[i-1] = 0; result = sendInfo(msg, pMf); n++; } if (not result) break; } if (n < 0) result = sendWarning(msg, "No mission files in default dir"); } else result = sendWarning(msg, "Could not create listing of mission files"); if (mf != NULL) fclose(mf); sendReply = true; } } if (getStatus) { sendStatusMessage(msg, msg->tag.getTagName()); sendReply = true; } // last if - if noone else has send a reply ... if (getPgm) { if (seq->getLineCnt() > 0) { // make start tag for lines snprintf(reply, MRL, "<%s lineCnt=\"%d\" cmdLineNum=\"%d\" file=\"%s\">\n", msg->tag.getTagName(), seq->getLineCnt(), seq->getActiveLine(), seq->getPlanName()); sendMsg(msg, reply); for (i = 0; i < seq->getLineCnt(); i++) { line = seq->getLine(i); str2xml(lineXml, MRL, line->getCmdLine()); snprintf(reply, MRL, "<seqLine ln=\"%d\" cmd=\"%s\"/>\n", i, lineXml); result = sendMsg(msg, reply); if (not result) // stop if line-buffers are full break; } // and end tag (always try) snprintf(reply, MRL, "</%s>\n", msg->tag.getTagName()); result = sendMsg(msg, reply); } else { // no plan lines - send status only snprintf(reply, MRL, "<%s lineCnt=\"%d\" cmdLineNum=\"%d\"/>\n", msg->tag.getTagName(), seq->getLineCnt(), seq->getActiveLine()); sendMsg(msg, reply); } sendReply = true; } if (result and not sendReply) sendInfo(msg, "done"); if (not result) sendWarning(msg, "One or more attributes to SEQ is not used!"); return result; }
CommandDispatcher::CommandDispatcher() { logLineCounter = 0; cmdLineCounter = 0; addCmdLine("<no command>"); addLogLine("start logging"); }