/*************************************************************************\ * Copyright (c) 1994-2004 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 1997-2003 Southeastern Universities Research Association, * as Operator of Thomas Jefferson National Accelerator Facility. * Copyright (c) 1997-2002 Deutches Elektronen-Synchrotron in der Helmholtz- * Gemelnschaft (DESY). * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ #define NO_X11_HERE /* Albert */ #define MAX_CAR_HIST_DEFAULT 1024*100 /* 100kB */ #define DEBUG 0 extern "C" { #include "StripHistory.h" #include "StripDataSource.h" #include "getHistory.h" #include "StripMisc.h" } #include #include "osiTime.h" #include "osiTimeHelper.h" /* #include #include */ #include #include #include "ArchiveException.h" #include #include #define ARCHIVE_NAME_ENVIRONMENT_VAR "STRIP_ARCHIVE" #define ARCHIVE_REQUEST_ENVIRONMENT_VAR "STRIP_VERBOSE" #define CAR_MAX_BUF_ENVIRONMENT_VAR "CAR_MAX_BUF" /*USING_NAMESPACE_CHANARCH new version of CAR doesn't use namespace */ #ifdef USE_NAMESPACE_STD using namespace std; #endif extern unsigned int historySize; // AccessChanArch does the actual data acquisition from the archiver int AccessChanArch(ArchiveI *, const stdString &, const osiTime &, const osiTime &, double *, short *, struct timeval *, size_t); // CountSamples finds out how many distinct data points we have in the requested range // in order to allocate memory properly size_t CountSamples(ArchiveI *, const stdString &, const osiTime &, const osiTime &); static bool VERBOSE = false; /* StripHistory_init */ extern "C" int CAR_init (char ** archiverInfoP) { // StripHistoryInfo *shi = 0; stdString ARCHIVE_NAME, VERBOSE_SWITCH; char *envString; ARCHIVE_NAME = getenv(ARCHIVE_NAME_ENVIRONMENT_VAR); if(ARCHIVE_NAME.empty()) { cout << "You must declare environment variable STRIP_ARCHIVE" << endl; return -1; } VERBOSE_SWITCH = getenv(ARCHIVE_REQUEST_ENVIRONMENT_VAR); if(VERBOSE_SWITCH.empty()) { cout << "You may want to declare environment variable STRIP_VERBOSE (=on or =off)" << endl; } else if(VERBOSE_SWITCH == "on") { cout << "Strip_History Verbose is turned on" << endl; VERBOSE = true; } // historySize=MAX_CAR_HIST_DEFAULT; if(envString=getenv(CAR_MAX_BUF_ENVIRONMENT_VAR)) if(historySize=atoi(envString)) { if(historySize>MAX_CAR_HIST_DEFAULT) historySize=MAX_CAR_HIST_DEFAULT; } cout <<"historySize="<archiverInfo; free (shi); } /* StripHistoryResult_release */ extern "C" void CAR_Result_release (StripHistoryResult *result) { if(result->data) delete[] result->data; if(result->times) delete[] result->times; if(result->status) delete[] result->status; } size_t CountSamples(ArchiveI *archiveI, const stdString &channel_name, const osiTime &start, const osiTime &end) { size_t chunk_size=0, chunk_cumulative=0; Archive archive (archiveI); ChannelIterator channel(archive); ValueIterator value(archive); if(!archive.findChannelByName (channel_name, channel)) { cout <getValueAfterTime (start, value); while (value && (end == nullTime || value->getTime() < end)) { chunk_size = value.determineChunk(end); for(size_t i = 0; i < chunk_size; i++) ++value; chunk_cumulative += chunk_size; if(chunk_cumulative > historySize) { History_MessageBox_popup ("BIG REQUEST", "Ok", "So many Data from Archiver\nPlease, decrease interval or increase # historyPoints.\n"); archive.detach(); return 2; } } archive.detach(); chunk_cumulative += 2; return chunk_cumulative; } int AccessChanArch(ArchiveI *archiveI, const stdString &channel_name, const osiTime &start, const osiTime &end, double *data, short *status, struct timeval *times, size_t samples) { Archive archive (archiveI); ChannelIterator channel(archive); ValueIterator value(archive); size_t i = 0; size_t real_count=0; archive.findChannelByName (channel_name, channel); channel->getValueAfterTime (start, value); if(!value) { channel->getValueBeforeTime (start, value); data[real_count] = value->getDouble(); times[real_count].tv_sec = start.getSec() + 1; times[real_count].tv_usec = 0; archive.detach(); return 1; } --value; if(value) { /* left fitting */ if(DEBUG) cerr<< "left fitting OK" <getDouble(); if(DEBUG) cerr<< "right fitting val="<< data[real_count] <getTime() getDouble(); times[real_count].tv_sec = value->getTime().getSec(); times[real_count].tv_usec = value->getTime().getUSec(); ++value; ++real_count; } if(value) { if(DEBUG) cerr<< "right fitting OK" <getDouble(); if(DEBUG) cerr<< "right fitting val="<< data[real_count] <tv_sec, (begin->tv_usec * 1000)); osiTime t1 = osiTime(end->tv_sec, (end->tv_usec * 1000)); try { ArchiveI* arI=(ArchiveI*) shi->archiverInfo; samples = CountSamples((ArchiveI*) shi->archiverInfo, name, t0, t1); if(VERBOSE == true) { cout << "Requested Channel: " << name << " " << t0 << " - " << t1 << " (" << samples << ")\n"; } } catch (ArchiveException &e) { cerr<<"get_CAR_data:CountSamples problem " << e.what() <archiverInfo,name,t0,t1,data,status,times,samples); *timesP=times; *statusP=status; *dataP=data; *count=no_of_p; } catch (ArchiveException &e) { LOG_MSG ("BinChannel::getValueAfterTime caught: " << e.what()); if(times) free(times) ; if(data) free(data) ; if(status) free(status); return (1); } return(0); } #undef NO_X11_HERE /* Albert */