fdhwlib  2.0.25
/home/kopmann/git-mirror/fdhwlib/fdhwlib/FEdata/tsbackgroundloop.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           tsbackgroundloop.h  -  description
00003                              -------------------
00004     begin                : Thu Oct 31 2002
00005     copyright            : (C) 2002 by Andreas Kopmann
00006     email                : kopmann@ipe.fzk.de
00007  ***************************************************************************/
00008 
00009 
00010 #ifndef TSBACKGROUNDLOOP_H
00011 #define TSBACKGROUNDLOOP_H
00012 
00013 #include <cstdio>
00014 #include <string>
00015 #include <map>
00016 
00017 #include <FEdata/tsdef.h>
00018 
00019 #ifdef USE_MYSQL
00020 #include <mysql/mysql.h>
00021 #endif // of USE_MYSQL
00022 
00023 #include <akutil/simpleserver.h>
00024 #include <akutil/procDuration.h>
00025 
00026 #define BGLOOP_PORT 5103
00027 #define BGCMD_LEN 4000
00028 
00029 class SimpleServer;
00030 class procDuration;
00031 class TsBackgroundBase;
00032 
00174 class TsBackgroundLoop : public SimpleServer  {
00175 public: 
00177   TsBackgroundLoop();
00179   ~TsBackgroundLoop();
00180 
00182   typedef enum {
00183       eDebug,           
00184       eInfo,            
00185       eWarning,         
00186       eError,           
00187       eCritical,        
00188       eSevere,          
00189       eFatal            
00190   } DBMessageSeverity;
00191 
00193   void readInifile(const char *inifile);
00194 
00196   void replaceTemplates();  
00197   
00199   int setParameter(char *file="bg_bayX.txt",float tSample=30,
00200                    int selTelescopes=0, int selStore=0, int selDisplay=0,
00201                    const char *runType = "test");
00202 
00204   void enablePixelRecording();
00205   
00207   void disablePixelRecording();
00208 
00209 #ifdef USE_MYSQL
00210 
00211   int setDatabaseParameter(const char *server, 
00212          const char *user, const char *password,
00213          const char *database, const char *runtable,
00214          const char *sampletable, const char *errortable,
00215          const char *pixeltable=0);
00216 
00217   void enableDatabase();
00218 
00219   void disableDatabase();
00220 #endif // of USE_MYSQL
00221 
00226   void setLocation(const char *name, int id);
00227 
00229   void getLocation(const char *name, int *id);
00230   
00232   void setSamplingTime(float tSample);
00233 
00235   float getSamplingTime();
00236 
00238   void setLogfile(FILE *flog);
00239 
00241   void setFilename(const char *file);
00242 
00244   const char *getFilename();
00245 
00247   void selectTelescopes(unsigned long selTelelescopes);
00248 
00250   unsigned long getTelescopes();
00251 
00253   void selectItems(unsigned long selStore);
00254 
00256   unsigned long getItems();  
00257 
00259   void selectDisplayItems(unsigned long selDisplay);
00260 
00262   unsigned long getDisplayItems();
00263 
00265   void setVarianceLimits(unsigned int limit);
00266 
00268   unsigned long getVarianceLimits();
00269 
00276   void setPixelStatus(int col, int row, bool enabled);
00277 
00279   bool getPixelStatus(int col, int row);
00280 
00282   void displayParameter();
00283 
00288   //int read_from_client(int client);
00289 
00291   int openFile();
00292 
00294   void setFDDASRunId(unsigned long runId);
00295 
00296   
00297 #ifdef USE_MYSQL
00298 
00299   int openDatabase();
00300 
00304   int loadFromDatabase(TsBackgroundBase *bg);
00305 
00309   int saveSampleToDatabase(TsBackgroundBase *bg);
00310 
00315   int savePixelToDatabase(TsBackgroundBase *bg);
00316   
00317 #endif // of USE_MYSQL
00318 
00320   void executeCmd(int client, short cmd, unsigned long *arg, short n);
00321   void executeCmd(int client, short cmd, unsigned int *arg, short n);
00322 
00326   void runReadout(FILE *fout, int eyeId=0);
00327 
00330   void displayRuns(FILE *fout, char *cmd=0);
00331    
00333   void displayHeader(TsBackgroundBase *bg, int select);
00334   
00336   void displaySummary(TsBackgroundBase *bg, int select);
00337 
00343   void setVarianceLevel(int low, int high);
00344   
00346   void displayError(const char *msg, int tel=0);
00347 
00348 #ifdef USE_MYSQL
00349 
00354   void writeMessageToDatabase(const char *msg, 
00355                               DBMessageSeverity severity=eInfo, int tel=0);
00356 #endif // USE_MYSQL
00357 
00372   void checkStatus(TsBackgroundBase *bg);
00373 
00375   void writeLogfileHeader();
00376 
00377   void writeLogfileFooter();
00378 
00380   void changeToDataDir(FILE *fout);
00381  
00383   void enableDaemonMode();
00384   
00386   void enableInteractiveMode();
00387 
00388 
00389 private:
00391   int debug;
00392 
00393   std::string inifile;
00394 
00396   std::string filename;
00397 
00399   std::string filenameTmpl;
00400 
00402   std::string basedir;
00403   
00405   std::string basedirTmpl;
00406   
00408   int locationId;
00409 
00411   std::string location;
00412 
00414   int uniqueId;
00415   
00417   FILE *fout;
00418 
00420   FILE *bgFile;
00421 
00423   bool record;
00424 
00426   procDuration tRef;
00427 
00429   float tSample;
00430 
00432   int selStore;
00433 
00435   int selDisplay;
00436 
00438   int selTelescopes;
00439 
00444   int limitVar;
00445 
00447   unsigned long statSamples;
00448 
00450   unsigned long statOffset;
00451 
00454   unsigned long disabledPixel[20];
00455 
00458   unsigned long pixelStatus[20][24];
00459 
00461   unsigned long fddasRunId;
00462 
00464   std::string bgrunFile;
00465   
00467   std::string bgcalFile;
00468 
00470   int bgrunTSample;
00471 
00473   int bgcalTSample;
00474 
00476   bool runAsDaemon;
00477   
00478 #ifdef USE_MYSQL
00479 
00480   bool recordDatabase;
00481 
00483   bool recordPixel;
00484 
00488   bool recordBroken;
00489 
00491   std::string dbServer;
00492 
00494   std::string dbUser;
00495 
00497   std::string dbPassword;
00498 
00500   std::string dbName;
00501 
00503   std::string dbNameTmpl;
00504   
00505 
00507   std::string dbRuntable;
00508 
00510   std::string dbSampletable;
00511 
00513   std::string dbPixeltable;
00514 
00516   std::string dbErrortable;
00517 
00519   unsigned long dbRunId;
00520 
00522   unsigned long dbSampleId;
00523   
00525   unsigned long nSamples;
00526 
00528   unsigned long nCmdsInFile;
00529   
00531   typedef struct {
00532   
00533     time_t       lastGPS;     // when message occurred last time
00534     unsigned int nOccur;      // number of occurrences
00535     unsigned int maxOccur;    // max. number of occurrences until we stay quiet
00536   
00537   } MsgInfo;
00538   
00540   std::map<std::string,MsgInfo> dbMessageMap;
00541   
00543   MYSQL *db;
00544 
00548   FILE *dbBackup;  
00549 
00553   void dbWrite(const char *cmd, const char *cmdBackup = 0);
00554     
00557   int compareFields(char *cmd, std::string *fields, int n);
00558   
00559 #endif // of USE_MYSQL
00560 
00562   FILE *flog;
00563 
00565   std::string logfile;
00566 
00570   int runMode;
00571 
00573   std::string runType;
00574 
00576   int telActive[24];
00577 
00579   unsigned long lastDataSec[24];
00580 
00582   int hitrateOverflow[24];
00583 
00585   unsigned long lastSecondDiff[24];
00586 
00588   int varianceLevel[24];
00589 
00591   float varianceClosedShutter;
00592 
00594   float varianceOverflow;
00595 
00597   int lastVariance[24];
00598 
00600   int actualVariance[24];
00601 
00603   unsigned long lastTimeStamp;
00604 
00606   unsigned long actualTimeStamp;
00607   
00609   int thresholdLevel[24];
00610 
00612   int missingTel[24];
00613 
00615   int errorLevel[24];
00616 
00618   int errorGeneral;
00619 
00621   //int faultyPixelMask[24][20][22];
00622 
00624   unsigned long long lastDeadtime[24];
00625 
00627   int deadtimeActive[24];
00628 
00630   int tDatabase;
00631   
00632 };
00633 
00634 #endif