//fill mbank from /equip/environ/variables INT read_periodic_event(char *pevent, INT off){ printf("Reading Periodic Event\n"); HNDLE hDB; cm_get_experiment_database(&hDB, NULL); HNDLE hkey; float temp1[3]; INT size; // get key handle for temp db_find_key(hDB, 0, "/Equipment/Temperature/Variables/Input", &hkey); // return temp size = sizeof(temp1); db_get_data(hDB, hkey, &temp1, &size, TID_FLOAT); //char *pevent; bk_init(pevent); float *pdata; // *pdata = temp1[0]; // create SCLR bank bk_create(pevent, "NEW1", TID_FLOAT, &pdata); // *pdata = 29.3; printf("%f\n",temp1[0]); // close SCLR bank bk_close(pevent, pdata); printf("eo fB\n"); return bk_size(pevent); }
void read_custom_from_db(void) { gint n, page, rows; int len; unsigned char *data; num_custom_brackets = 0; // find out the blob keys if ((rows = db_get_table("SELECT \"key\" FROM blobs")) > 0) { for (n = 0; n < rows; n++) { guint a = atoi(db_get_data(n, "key")); if ((a & 0x1f) == 0 && num_custom_brackets < NUM_CUSTOM_BRACKETS) hash_values[num_custom_brackets++] = a; } } if (rows >= 0) db_close_table(); LOG("%d custom brackets", num_custom_brackets); for (n = 0; n < num_custom_brackets; n++) { unsigned char *blob = NULL; db_read_blob(hash_values[n], &blob, &len); if (blob) { custom_brackets[n] = g_malloc0(sizeof(struct custom_data)); decode_custom_data(blob, len, custom_brackets[n]); free(blob); } if (custom_brackets[n] == NULL) { num_custom_brackets = n; g_print("ERROR: cannot read blob %d\n", n); break; } for (page = 1; page <= 10; page++) { db_read_blob(hash_values[n] | page, &data, &len); if (data == NULL) { if (page == 1) { g_print("ERROR: Cannot read svg data from db\n"); } break; } add_custom_svg((gchar *)data, len, hash_values[n], page); #if 0 gchar buf[32]; sprintf(buf, "%x.svg", (n << 8) | page); if (g_file_set_contents(buf, (gchar *)data, len, NULL)) g_print("g_file_set_contents %s OK len=%d\n", buf, len); else g_print("g_file_set_contents %s NOK len=%d\n", buf, len); #endif } } }
/* Gets last insert id. Returns int. */ int db_last_insert_id(ConnHandle *c, const char *str) { int id = 0; QueryHandle *res = db_query(c, "SELECT LAST_INSERT_ID() AS id"); if(db_nrows(res)) id = atoi(db_get_data(res, 0, "id")); db_free(&res); return id; }
INT readout(char* pevent, INT off) { DWORD *pdata; bk_init32(pevent); bk_create(pevent,"INPT",TID_DWORD,&pdata); int val = 98; HNDLE hDB; HNDLE hKey; INT size; cm_get_experiment_database(&hDB,NULL); db_find_key(hDB,0,"/Equipment/Environment 2/Variables/Secret",&hKey); size = sizeof(val); db_get_data(hDB,hKey,&val,&size,TID_INT); *pdata++ = val; struct timeval t; int status; status = gettimeofday(&t,NULL); printf(" t_tv.sec: %d",t.tv_sec); printf(" ro: val is %d",val); val++; db_set_value(hDB,0,"/Equipment/Environment 2/Variables/Secret",&val,sizeof(val),1,TID_INT); bk_close(pevent,pdata); return bk_size(pevent); }
/*! * <p>The function returns single values or whole arrays which are contained in an ODB key. * Since the data buffer is of type void, no type checking can be performed by the compiler. * Therefore the type has to be explicitly supplied, which is checked against the type * stored in the ODB. * * <p><b>Return:</b> * - DB_SUCCESS on successful completion. * - DB_INVALID_HANDLE if ODB or key is invalid. * - DB_TRUNCATED if there is a size mismatch between the local structure and the ODB. * - DB_TYPE_MISMATCH if there is a type discrepancy between the local structure and the ODB. * * \param data Pointer to the data structure. * \param size Size of data buffer. * \param type Type of key, one of TID_xxx (see MIDAS documentation) */ INT PKey::GetData(void *data, INT *size, DWORD type) { return db_get_data(fHDB, fHKey, data, size, type); }
INT fgd_init(EQUIPMENT * pequipment) { int status, size, i, j, index, offset; char str[256]; HNDLE hDB, hKey, hNames, hThreshold; FGD_INFO *fgd_info; /* allocate private data */ pequipment->cd_info = calloc(1, sizeof(FGD_INFO)); fgd_info = (FGD_INFO *) pequipment->cd_info; /* get class driver root key */ cm_get_experiment_database(&hDB, NULL); sprintf(str, "/Equipment/%s", pequipment->name); db_create_key(hDB, 0, str, TID_KEY); db_find_key(hDB, 0, str, &fgd_info->hKeyRoot); /* save event format */ size = sizeof(str); db_get_value(hDB, fgd_info->hKeyRoot, "Common/Format", str, &size, TID_STRING, TRUE); if (equal_ustring(str, "Fixed")) fgd_info->format = FORMAT_FIXED; else if (equal_ustring(str, "MIDAS")) fgd_info->format = FORMAT_MIDAS; else if (equal_ustring(str, "YBOS")) fgd_info->format = FORMAT_YBOS; /* count total number of channels */ for (i = 0, fgd_info->num_channels = 0; pequipment->driver[i].name[0]; i++) { if (pequipment->driver[i].channels == 0) { cm_msg(MERROR, "fgd_init", "Driver with zero channels not allowed"); return FE_ERR_ODB; } fgd_info->num_channels += pequipment->driver[i].channels; } if (fgd_info->num_channels == 0) { cm_msg(MERROR, "fgd_init", "No channels found in device driver list"); return FE_ERR_ODB; } /* Allocate memory for buffers */ fgd_info->names = (char *) calloc(fgd_info->num_channels, NAME_LENGTH); fgd_info->demand = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->measured = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->temp1 = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->temp2 = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->temp3 = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->update_threshold = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->demand_mirror = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->measured_mirror = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->channel_offset = (INT *) calloc(fgd_info->num_channels, sizeof(INT)); fgd_info->driver = (void *) calloc(fgd_info->num_channels, sizeof(void *)); if (!fgd_info->driver) { cm_msg(MERROR, "hv_init", "Not enough memory"); return FE_ERR_ODB; } /*---- Initialize device drivers ----*/ /* call init method */ for (i = 0; pequipment->driver[i].name[0]; i++) { sprintf(str, "Settings/Devices/%s", pequipment->driver[i].name); status = db_find_key(hDB, fgd_info->hKeyRoot, str, &hKey); if (status != DB_SUCCESS) { db_create_key(hDB, fgd_info->hKeyRoot, str, TID_KEY); status = db_find_key(hDB, fgd_info->hKeyRoot, str, &hKey); if (status != DB_SUCCESS) { cm_msg(MERROR, "hv_init", "Cannot create %s entry in online database", str); free_mem(fgd_info); return FE_ERR_ODB; } } status = device_driver(&pequipment->driver[i], CMD_INIT, hKey); if (status != FE_SUCCESS) { free_mem(fgd_info); return status; } } /* compose device driver channel assignment */ for (i = 0, j = 0, index = 0, offset = 0; i < fgd_info->num_channels; i++, j++) { while (j >= pequipment->driver[index].channels && pequipment->driver[index].name[0]) { offset += j; index++; j = 0; } fgd_info->driver[i] = &pequipment->driver[index]; fgd_info->channel_offset[i] = offset; } /*---- create demand variables ----*/ /* get demand from ODB */ status = db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand); if (status == DB_SUCCESS) { size = sizeof(float) * fgd_info->num_channels; db_get_data(hDB, fgd_info->hKeyDemand, fgd_info->demand, &size, TID_FLOAT); } /* let device driver overwrite demand values, if it supports it */ for (i = 0; i < fgd_info->num_channels; i++) { if (fgd_info->driver[i]->flags & DF_PRIO_DEVICE) { device_driver(fgd_info->driver[i], CMD_GET_DEMAND, i - fgd_info->channel_offset[i], &fgd_info->demand[i]); fgd_info->demand_mirror[i] = fgd_info->demand[i]; } else fgd_info->demand_mirror[i] = -12345.f; /* use -12345 as invalid value */ } /* write back demand values */ status = db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand); if (status != DB_SUCCESS) { db_create_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand); } size = sizeof(float) * fgd_info->num_channels; db_set_data(hDB, fgd_info->hKeyDemand, fgd_info->demand, size, fgd_info->num_channels, TID_FLOAT); db_open_record(hDB, fgd_info->hKeyDemand, fgd_info->demand, fgd_info->num_channels * sizeof(float), MODE_READ, fgd_demand, pequipment); /*---- create measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Current Measured", fgd_info->measured, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Current Measured", &fgd_info->hKeyMeasured); memcpy(fgd_info->measured_mirror, fgd_info->measured, fgd_info->num_channels * sizeof(float)); /*---- create Temp1 measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp1", fgd_info->temp1, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp1", &fgd_info->hKeyTemp1); /*---- create Temp2 measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp2", fgd_info->temp2, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp2", &fgd_info->hKeyTemp2); /*---- create Temp3 measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp3", fgd_info->temp3, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp3", &fgd_info->hKeyTemp3); /*---- get default names from device driver ----*/ for (i = 0; i < fgd_info->num_channels; i++) { sprintf(fgd_info->names + NAME_LENGTH * i, "Default%%CH %d", i); device_driver(fgd_info->driver[i], CMD_GET_LABEL, i - fgd_info->channel_offset[i], fgd_info->names + NAME_LENGTH * i); } db_merge_data(hDB, fgd_info->hKeyRoot, "Settings/Names", fgd_info->names, NAME_LENGTH * fgd_info->num_channels, fgd_info->num_channels, TID_STRING); /*---- set labels form midas SC names ----*/ for (i = 0; i < fgd_info->num_channels; i++) { fgd_info = (FGD_INFO *) pequipment->cd_info; device_driver(fgd_info->driver[i], CMD_SET_LABEL, i - fgd_info->channel_offset[i], fgd_info->names + NAME_LENGTH * i); } /* open hotlink on channel names */ if (db_find_key(hDB, fgd_info->hKeyRoot, "Settings/Names", &hNames) == DB_SUCCESS) db_open_record(hDB, hNames, fgd_info->names, NAME_LENGTH*fgd_info->num_channels, MODE_READ, fgd_update_label, pequipment); /*---- get default update threshold from device driver ----*/ for (i = 0; i < fgd_info->num_channels; i++) { fgd_info->update_threshold[i] = 1.f; /* default 1 unit */ device_driver(fgd_info->driver[i], CMD_GET_THRESHOLD, i - fgd_info->channel_offset[i], &fgd_info->update_threshold[i]); } db_merge_data(hDB, fgd_info->hKeyRoot, "Settings/Update Threshold Measured", fgd_info->update_threshold, sizeof(float)*fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); /* open hotlink on update threshold */ if (db_find_key(hDB, fgd_info->hKeyRoot, "Settings/Update Threshold Measured", &hThreshold) == DB_SUCCESS) db_open_record(hDB, hThreshold, fgd_info->update_threshold, sizeof(float)*fgd_info->num_channels, MODE_READ, NULL, NULL); /*---- set initial demand values ----*/ // fgd_demand(hDB, fgd_info->hKeyDemand, pequipment); /* initially read all channels */ for (i = 0; i < fgd_info->num_channels; i++) fgd_read(pequipment, i); return FE_SUCCESS; }