Exemple #1
0
INT analyzer_init()
{

   HNDLE hDB, hKey;
   char str[80];

   RUNINFO_STR(runinfo_str);
   EXP_PARAM_STR(exp_param_str);
   GLOBAL_PARAM_STR(global_param_str);
   TRIGGER_SETTINGS_STR(trigger_settings_str);

   /* open ODB structures */
   cm_get_experiment_database(&hDB, NULL);
   db_create_record(hDB, 0, "/Runinfo", strcomb((const char **)runinfo_str));
   db_find_key(hDB, 0, "/Runinfo", &hKey);
   if (db_open_record(hDB, hKey, &runinfo, sizeof(runinfo), MODE_READ, NULL, NULL) !=
       DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init", "Cannot open \"/Runinfo\" tree in ODB");
      return 0;
   }

   db_create_record(hDB, 0, "/Experiment/Run Parameters", strcomb((const char **)exp_param_str));
   db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey);
   if (db_open_record(hDB, hKey, &exp_param, sizeof(exp_param), MODE_READ, NULL, NULL) !=
       DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init",
             "Cannot open \"/Experiment/Run Parameters\" tree in ODB");
      return 0;
   }

   sprintf(str, "/%s/Parameters/Global", analyzer_name);
   db_create_record(hDB, 0, str, strcomb((const char **)global_param_str));
   db_find_key(hDB, 0, str, &hKey);
   if (db_open_record
       (hDB, hKey, &global_param, sizeof(global_param), MODE_READ, NULL,
        NULL) != DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init", "Cannot open \"%s\" tree in ODB", str);
      return 0;
   }

   db_create_record(hDB, 0, "/Equipment/Trigger/Settings", strcomb((const char **)trigger_settings_str));
   db_find_key(hDB, 0, "/Equipment/Trigger/Settings", &hKey);

   if (db_open_record
       (hDB, hKey, &trigger_settings, sizeof(trigger_settings), MODE_READ, NULL,
        NULL) != DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init",
             "Cannot open \"/Equipment/Trigger/Settings\" tree in ODB");
      return 0;
   }

   //Initialize gData
   gData = new TEnvi();

   return SUCCESS;
}
Exemple #2
0
/*-- Analyzer Init -------------------------------------------------*/
INT analyzer_init()
{
    HNDLE hDB, hKey;
    char str[80];

    RUNINFO_STR(runinfo_str);            // RUNINFO_STR in experim.h      rn:  not true !?!?!
    EXP_PARAM_STR(exp_param_str);        // EXP_PARAM_STR in experim.h    rn:  not true !!?!?!
    //GLOBAL_PARAM_STR(global_param_str);  // GLOBAL_PARAM_STR in experim.h

    /* open ODB structures */
    cm_get_experiment_database(&hDB, NULL);
    db_create_record(hDB, 0, "/Runinfo", strcomb(runinfo_str));
    db_find_key(hDB, 0, "/Runinfo", &hKey);
    if (db_open_record(hDB, hKey, &runinfo, sizeof(runinfo), MODE_READ, NULL, NULL) !=
            DB_SUCCESS) {
        cm_msg(MERROR, "analyzer_init", "Cannot open \"/Runinfo\" tree in ODB");
        return 0;
    }


    db_create_record(hDB, 0, "/Experiment/Run Parameters", strcomb(exp_param_str));
    db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey);
    if (db_open_record(hDB, hKey, &exp_param, sizeof(exp_param), MODE_READ, NULL, NULL) !=
            DB_SUCCESS) {
        cm_msg(MERROR, "analyzer_init",
               "Cannot open \"/Experiment/Run Parameters\" tree in ODB");
        return 0;
    }

    //printf("\n---testing testing 123----------------------------------------------- %i  \n\n\n\n ",runinfo.run_number);
    //runnr=runinfo.run_number;


    ParameterInit();


    return SUCCESS;
}
Exemple #3
0
INT frontend_init()
{
   /* hardware initialization */
  INT status;
  char set_str[80];
  /* Book Setting space */
  TRIGGER_SETTINGS_STR(trigger_settings_str);

  /* Map /equipment/Trigger/settings for the sequencer */
  sprintf(set_str, "/Equipment/Trigger/Settings");
  status = db_create_record(hDB, 0, set_str, strcomb(trigger_settings_str));
  status = db_find_key (hDB, 0, set_str, &hSet);
  if (status != DB_SUCCESS)
    cm_msg(MINFO,"FE","Key %s not found", set_str);


    rs232(CMD_INIT, hSet, &info);
   /* print message and return FE_ERR_HW if frontend should not be started */

   return SUCCESS;
}
Exemple #4
0
/**
Scan ODB for alarms.
@return AL_SUCCESS
*/
INT al_check()
{
   if (rpc_is_remote())
      return rpc_call(RPC_AL_CHECK);

#ifdef LOCAL_ROUTINES
   {
   INT i, status, size, semaphore;
   HNDLE hDB, hkeyroot, hkey;
   KEY key;
   ALARM a;
   char str[256], value[256];
   time_t now;
   PROGRAM_INFO program_info;
   BOOL flag;
   
   ALARM_CLASS_STR(alarm_class_str);
   ALARM_ODB_STR(alarm_odb_str);
   ALARM_PERIODIC_STR(alarm_periodic_str);
   
   cm_get_experiment_database(&hDB, NULL);
   
   if (hDB == 0)
      return AL_SUCCESS;     /* called from server not yet connected */
   
   /* check online mode */
   flag = TRUE;
   size = sizeof(flag);
   db_get_value(hDB, 0, "/Runinfo/Online Mode", &flag, &size, TID_INT, TRUE);
   if (!flag)
      return AL_SUCCESS;
   
   /* check global alarm flag */
   flag = TRUE;
   size = sizeof(flag);
   db_get_value(hDB, 0, "/Alarms/Alarm system active", &flag, &size, TID_BOOL, TRUE);
   if (!flag)
      return AL_SUCCESS;
   
   /* request semaphore */
   cm_get_experiment_semaphore(&semaphore, NULL, NULL, NULL);
   status = ss_semaphore_wait_for(semaphore, 100);
   if (status == SS_TIMEOUT)
      return AL_SUCCESS;        /* someone else is doing alarm business */
   if (status != SS_SUCCESS) {
      printf("al_check: Something is wrong with our semaphore, ss_semaphore_wait_for() returned %d, aborting.\n", status);
      //abort(); // DOES NOT RETURN
      printf("al_check: Cannot abort - this will lock you out of odb. From this point, MIDAS will not work correctly. Please read the discussion at https://midas.triumf.ca/elog/Midas/945\n");
      // NOT REACHED
      return AL_SUCCESS;
   }
   
   /* check ODB alarms */
   db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyroot);
   if (!hkeyroot) {
      /* create default ODB alarm */
      status = db_create_record(hDB, 0, "/Alarms/Alarms/Demo ODB", strcomb(alarm_odb_str));
      db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyroot);
      if (!hkeyroot) {
         ss_semaphore_release(semaphore);
         return AL_SUCCESS;
      }
      
      status = db_create_record(hDB, 0, "/Alarms/Alarms/Demo periodic", strcomb(alarm_periodic_str));
      db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyroot);
      if (!hkeyroot) {
         ss_semaphore_release(semaphore);
         return AL_SUCCESS;
      }
      
      /* create default alarm classes */
      status = db_create_record(hDB, 0, "/Alarms/Classes/Alarm", strcomb(alarm_class_str));
      status = db_create_record(hDB, 0, "/Alarms/Classes/Warning", strcomb(alarm_class_str));
      if (status != DB_SUCCESS) {
         ss_semaphore_release(semaphore);
         return AL_SUCCESS;
      }
   }
   
   for (i = 0;; i++) {
      status = db_enum_key(hDB, hkeyroot, i, &hkey);
      if (status == DB_NO_MORE_SUBKEYS)
         break;
      
      db_get_key(hDB, hkey, &key);
      
      size = sizeof(a);
      status = db_get_record(hDB, hkey, &a, &size, 0);
      if (status != DB_SUCCESS || a.type < 1 || a.type > AT_LAST) {
         /* make sure alarm record has right structure */
         db_check_record(hDB, hkey, "", strcomb(alarm_odb_str), TRUE);
         size = sizeof(a);
         status = db_get_record(hDB, hkey, &a, &size, 0);
         if (status != DB_SUCCESS || a.type < 1 || a.type > AT_LAST) {
            cm_msg(MERROR, "al_check", "Cannot get alarm record");
            continue;
         }
      }
      
      /* check periodic alarm only when active */
      if (a.active &&
          a.type == AT_PERIODIC &&
          a.check_interval > 0 && (INT) ss_time() - (INT) a.checked_last > a.check_interval) {
         /* if checked_last has not been set, set it to current time */
         if (a.checked_last == 0) {
            a.checked_last = ss_time();
            db_set_record(hDB, hkey, &a, size, 0);
         } else
            al_trigger_alarm(key.name, a.alarm_message, a.alarm_class, "", AT_PERIODIC);
      }
      
      /* check alarm only when active and not internal */
      if (a.active &&
          a.type == AT_EVALUATED &&
          a.check_interval > 0 && (INT) ss_time() - (INT) a.checked_last > a.check_interval) {
         /* if condition is true, trigger alarm */
         if (al_evaluate_condition(a.condition, value)) {
            sprintf(str, a.alarm_message, value);
            al_trigger_alarm(key.name, str, a.alarm_class, "", AT_EVALUATED);
         } else {
            a.checked_last = ss_time();
            status = db_set_value(hDB, hkey, "Checked last", &a.checked_last, sizeof(DWORD), 1, TID_DWORD);
            if (status != DB_SUCCESS) {
               cm_msg(MERROR, "al_check", "Cannot change alarm record");
               continue;
            }
         }
      }
   }
   
   /* check /programs alarms */
   db_find_key(hDB, 0, "/Programs", &hkeyroot);
   if (hkeyroot) {
      for (i = 0;; i++) {
         status = db_enum_key(hDB, hkeyroot, i, &hkey);
         if (status == DB_NO_MORE_SUBKEYS)
            break;
         
         db_get_key(hDB, hkey, &key);
         
         /* don't check "execute on xxx" */
         if (key.type != TID_KEY)
            continue;
         
         size = sizeof(program_info);
         status = db_get_record(hDB, hkey, &program_info, &size, 0);
         if (status != DB_SUCCESS) {
            cm_msg(MERROR, "al_check", "Cannot get program info record");
            continue;
         }
         
         now = ss_time();
         
         rpc_get_name(str);
         str[strlen(key.name)] = 0;
         if (!equal_ustring(str, key.name) && cm_exist(key.name, FALSE) == CM_NO_CLIENT) {
            if (program_info.first_failed == 0) {
               program_info.first_failed = (DWORD) now;
               db_set_record(hDB, hkey, &program_info, sizeof(program_info), 0);
            }
            
            /* fire alarm when not running for more than what specified in check interval */
            if (now - program_info.first_failed >= program_info.check_interval / 1000) {
               /* if not running and alarm calss defined, trigger alarm */
               if (program_info.alarm_class[0]) {
                  sprintf(str, "Program %s is not running", key.name);
                  al_trigger_alarm(key.name, str, program_info.alarm_class,
                                   "Program not running", AT_PROGRAM);
               }
               
               /* auto restart program */
               if (program_info.auto_restart && program_info.start_command[0]) {
                  ss_system(program_info.start_command);
                  program_info.first_failed = 0;
                  cm_msg(MTALK, "al_check", "Program %s restarted", key.name);
               }
            }
         } else {
            if (program_info.first_failed != 0) {
               program_info.first_failed = 0;
               db_set_record(hDB, hkey, &program_info, sizeof(program_info), 0);
            }
         }
      }
   }
   
   ss_semaphore_release(semaphore);
   }
#endif                          /* LOCAL_COUTINES */

   return SUCCESS;
}
Exemple #5
0
/**
Trigger a certain alarm.
\code  ...
  lazy.alarm[0] = 0;
  size = sizeof(lazy.alarm);
  db_get_value(hDB, pLch->hKey, "Settings/Alarm Class", lazy.alarm, &size, TID_STRING, TRUE);

  // trigger alarm if defined
  if (lazy.alarm[0])
    al_trigger_alarm("Tape", "Tape full...load new one!", lazy.alarm, "Tape full", AT_INTERNAL);
  ...
\endcode
@param alarm_name Alarm name, defined in /alarms/alarms
@param alarm_message Optional message which goes with alarm
@param default_class If alarm is not yet defined under
                    /alarms/alarms/\<alarm_name\>, a new one
                    is created and this default class is used.
@param cond_str String displayed in alarm condition
@param type Alarm type, one of AT_xxx
@return AL_SUCCESS, AL_INVALID_NAME
*/
INT al_trigger_alarm(const char *alarm_name, const char *alarm_message, const char *default_class, const char *cond_str, INT type)
{
   if (rpc_is_remote())
      return rpc_call(RPC_AL_TRIGGER_ALARM, alarm_name, alarm_message, default_class, cond_str, type);

#ifdef LOCAL_ROUTINES
   {
      int status, size;
      HNDLE hDB, hkeyalarm, hkey;
      char str[256];
      ALARM a;
      BOOL flag;
      ALARM_ODB_STR(alarm_odb_str);

      cm_get_experiment_database(&hDB, NULL);

      /* check online mode */
      flag = TRUE;
      size = sizeof(flag);
      db_get_value(hDB, 0, "/Runinfo/Online Mode", &flag, &size, TID_INT, TRUE);
      if (!flag)
         return AL_SUCCESS;

      /* find alarm */
      sprintf(str, "/Alarms/Alarms/%s", alarm_name);
      db_find_key(hDB, 0, str, &hkeyalarm);
      if (!hkeyalarm) {
         /* alarm must be an internal analyzer alarm, so create a default alarm */
         status = db_create_record(hDB, 0, str, strcomb(alarm_odb_str));
         db_find_key(hDB, 0, str, &hkeyalarm);
         if (!hkeyalarm) {
            cm_msg(MERROR, "al_trigger_alarm", "Cannot create alarm record");
            return AL_ERROR_ODB;
         }

         if (default_class && default_class[0])
            db_set_value(hDB, hkeyalarm, "Alarm Class", default_class, 32, 1, TID_STRING);
         status = TRUE;
         db_set_value(hDB, hkeyalarm, "Active", &status, sizeof(status), 1, TID_BOOL);
      }

      /* set parameters for internal alarms */
      if (type != AT_EVALUATED && type != AT_PERIODIC) {
         db_set_value(hDB, hkeyalarm, "Type", &type, sizeof(INT), 1, TID_INT);
         strcpy(str, cond_str);
         db_set_value(hDB, hkeyalarm, "Condition", str, 256, 1, TID_STRING);
      }

      size = sizeof(a);
      status = db_get_record(hDB, hkeyalarm, &a, &size, 0);
      if (status != DB_SUCCESS || a.type < 1 || a.type > AT_LAST) {
         /* make sure alarm record has right structure */
         db_check_record(hDB, hkeyalarm, "", strcomb(alarm_odb_str), TRUE);

         size = sizeof(a);
         status = db_get_record(hDB, hkeyalarm, &a, &size, 0);
         if (status != DB_SUCCESS) {
            cm_msg(MERROR, "al_trigger_alarm", "Cannot get alarm record");
            return AL_ERROR_ODB;
         }
      }

      /* if internal alarm, check if active and check interval */
      if (a.type != AT_EVALUATED && a.type != AT_PERIODIC) {
         /* check global alarm flag */
         flag = TRUE;
         size = sizeof(flag);
         db_get_value(hDB, 0, "/Alarms/Alarm system active", &flag, &size, TID_BOOL, TRUE);
         if (!flag)
            return AL_SUCCESS;

         if (!a.active)
            return AL_SUCCESS;

         if ((INT) ss_time() - (INT) a.checked_last < a.check_interval)
            return AL_SUCCESS;

         /* now the alarm will be triggered, so save time */
         a.checked_last = ss_time();
      }

      /* write back alarm message for internal alarms */
      if (a.type != AT_EVALUATED && a.type != AT_PERIODIC) {
         strncpy(a.alarm_message, alarm_message, 79);
         a.alarm_message[79] = 0;
      }

      /* now trigger alarm class defined in this alarm */
      if (a.alarm_class[0])
         al_trigger_class(a.alarm_class, alarm_message, a.triggered > 0);

      /* check for and trigger "All" class */
      if (db_find_key(hDB, 0, "/Alarms/Classes/All", &hkey) == DB_SUCCESS)
         al_trigger_class("All", alarm_message, a.triggered > 0);

      /* signal alarm being triggered */
      cm_asctime(str, sizeof(str));

      if (!a.triggered)
         strcpy(a.time_triggered_first, str);

      a.triggered++;
      strcpy(a.time_triggered_last, str);

      a.checked_last = ss_time();

      status = db_set_record(hDB, hkeyalarm, &a, sizeof(a), 0);
      if (status != DB_SUCCESS) {
         cm_msg(MERROR, "al_trigger_alarm", "Cannot update alarm record");
         return AL_ERROR_ODB;
      }

   }
#endif                          /* LOCAL_ROUTINES */

   return AL_SUCCESS;
}