static void test()
{
    NTMultiChannelBuilderPtr builder = NTMultiChannel::createBuilder();
    testOk(builder.get() != 0, "Got builder");

    NTMultiChannelPtr multiChannel = builder->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addSeverity() ->
            add("extra1",fieldCreate->createScalar(pvString)) ->
            add("extra2",fieldCreate->createScalarArray(pvString)) ->
            create();
    testOk1(multiChannel.get() != 0);

    PVStructurePtr pvStructure = multiChannel->getPVStructure();
    testOk1(pvStructure.get()!=NULL);
    testOk1(NTMultiChannel::is_a(pvStructure->getStructure()));
    size_t nchan = 3;
    shared_vector<string> names(nchan);
    names[0] = "channel 0";
    names[1] = "channel 1";
    names[2] = "channel 2";
    shared_vector<const string> channelNames(freeze(names));
    PVStringArrayPtr pvChannelName = multiChannel->getChannelName();
    pvChannelName->replace(channelNames);
    if(debug) {cout << *pvStructure << endl;}
    UnionConstPtr unionPtr =
       fieldCreate->createFieldBuilder()->
           add("doubleValue", pvDouble)->
           add("intValue", pvInt)->
           createUnion();
    multiChannel = builder->
            value(unionPtr) ->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addSeverity() ->
            addIsConnected() ->
            create();
    testOk1(multiChannel.get() != 0);
    pvStructure = multiChannel->getPVStructure();
    if(debug) {cout << *pvStructure << endl;}
    pvChannelName = multiChannel->getChannelName();
    pvChannelName->replace(channelNames);
    PVUnionArrayPtr pvValue = multiChannel->getValue();
    shared_vector<PVUnionPtr> unions(nchan);
    unions[0] = pvDataCreate->createPVUnion(unionPtr);
    unions[1] = pvDataCreate->createPVUnion(unionPtr);
    unions[2] = pvDataCreate->createPVUnion(unionPtr);
    unions[0]->select("doubleValue");
    unions[1]->select("intValue");
    unions[2]->select("intValue");
    PVDoublePtr pvDouble = unions[0]->get<PVDouble>();
    pvDouble->put(1.235);
    PVIntPtr pvInt = unions[1]->get<PVInt>();
    pvInt->put(5);
    pvInt = unions[2]->get<PVInt>();
    pvInt->put(7);
    pvValue->replace(freeze(unions));
    shared_vector<int32> severities(nchan);
    severities[0] = 0;
    severities[1] = 1;
    severities[2] = 2;
    PVIntArrayPtr pvSeverity = multiChannel->getSeverity();
    pvSeverity->replace(freeze(severities));
    if(debug) {cout << *pvStructure << endl;}
    PVBooleanArrayPtr pvIsConnected = multiChannel->getIsConnected();
    shared_vector<const epics::pvData::boolean> isConnected = pvIsConnected->view();
    multiChannel = builder->
            value(unionPtr) ->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addSeverity() ->
            addStatus() ->
            addMessage() ->
            addSecondsPastEpoch() ->
            addNanoseconds() ->
            addUserTag() ->
            addIsConnected() ->
            create();
    testOk1(multiChannel.get() != 0);
    pvStructure = multiChannel->getPVStructure();
    if(debug) {cout << *pvStructure << endl;}
    testOk1(NTMultiChannel::isCompatible(pvStructure)==true);
    PVStructurePtr pvTimeStamp = multiChannel->getTimeStamp();
    testOk1(pvTimeStamp.get() !=0);
    PVStructurePtr pvAlarm = multiChannel->getAlarm();
    testOk1(pvAlarm.get() !=0);
    pvValue = multiChannel->getValue();
    testOk1(pvValue.get() !=0);
    pvChannelName = multiChannel->getChannelName();
    testOk1(pvChannelName.get() !=0);
    pvIsConnected = multiChannel->getIsConnected();
    testOk1(pvIsConnected.get() !=0);
    pvSeverity = multiChannel->getSeverity();
    testOk1(pvSeverity.get() !=0);
    PVIntArrayPtr pvStatus = multiChannel->getStatus();
    testOk1(pvStatus.get() !=0);
    PVStringArrayPtr pvMessage = multiChannel->getMessage();
    testOk1(pvMessage.get() !=0);
    PVLongArrayPtr pvSecondsPastEpoch = multiChannel->getSecondsPastEpoch();
    testOk1(pvSecondsPastEpoch.get() !=0);
    PVIntArrayPtr pvNanoseconds = multiChannel->getNanoseconds();
    testOk1(pvNanoseconds.get() !=0);
    PVIntArrayPtr pvUserTag = multiChannel->getUserTag();
    testOk1(pvUserTag.get() !=0);
    PVStringPtr pvDescriptor = multiChannel->getDescriptor();
    testOk1(pvDescriptor.get() !=0);
}
static void test()
{
    NTScalarMultiChannelBuilderPtr builder = NTScalarMultiChannel::createBuilder();
    testOk(builder.get() != 0, "Got builder");

    NTScalarMultiChannelPtr multiChannel = builder->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addSeverity() ->
            add("extra1",fieldCreate->createScalar(pvString)) ->
            add("extra2",fieldCreate->createScalarArray(pvString)) ->
            create();
    testOk1(multiChannel.get() != 0);

    PVStructurePtr pvStructure = multiChannel->getPVStructure();
    testOk1(pvStructure.get()!=NULL);
    testOk1(NTScalarMultiChannel::is_a(pvStructure->getStructure()));
    size_t nchan = 3;
    shared_vector<string> names(nchan);
    names[0] = "channel 0";
    names[1] = "channel 1";
    names[2] = "channel 2";
    shared_vector<const string> channelNames(freeze(names));
    PVStringArrayPtr pvChannelName = multiChannel->getChannelName();
    pvChannelName->replace(channelNames);
    if(debug) {cout << *pvStructure << endl;}
    multiChannel = builder->
            value(pvDouble) ->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addSeverity() ->
            addIsConnected() ->
            create();
    testOk1(multiChannel.get() != 0);
    pvStructure = multiChannel->getPVStructure();
    if(debug) {cout << *pvStructure << endl;}
    pvChannelName = multiChannel->getChannelName();
    pvChannelName->replace(channelNames);
    PVDoubleArrayPtr pvValue = multiChannel->getValue<PVDoubleArray>();
    PVDoubleArray::svector doubles(nchan);
    doubles.resize(nchan);
    doubles[0] = 3.14159;
    doubles[1] = 2.71828;
    doubles[2] = 137.036;
    pvValue->replace(freeze(doubles));
    shared_vector<int32> severities(nchan);
    severities[0] = 0;
    severities[1] = 1;
    severities[2] = 2;
    PVIntArrayPtr pvSeverity = multiChannel->getSeverity();
    pvSeverity->replace(freeze(severities));
    if(debug) {cout << *pvStructure << endl;}
    PVBooleanArrayPtr pvIsConnected = multiChannel->getIsConnected();
    shared_vector<const epics::pvData::boolean> isConnected = pvIsConnected->view();
    multiChannel = builder->
            value(pvDouble) ->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addSeverity() ->
            addStatus() ->
            addMessage() ->
            addSecondsPastEpoch() ->
            addNanoseconds() ->
            addUserTag() ->
            addIsConnected() ->
            create();
    testOk1(multiChannel.get() != 0);
    pvStructure = multiChannel->getPVStructure();
    if(debug) {cout << *pvStructure << endl;}
    testOk1(NTScalarMultiChannel::isCompatible(pvStructure)==true);
    PVStructurePtr pvTimeStamp = multiChannel->getTimeStamp();
    testOk1(pvTimeStamp.get() !=0);
    PVStructurePtr pvAlarm = multiChannel->getAlarm();
    testOk1(pvAlarm.get() !=0);
    pvValue = multiChannel->getValue<PVDoubleArray>();
    testOk1(pvValue.get() !=0);
    pvChannelName = multiChannel->getChannelName();
    testOk1(pvChannelName.get() !=0);
    pvIsConnected = multiChannel->getIsConnected();
    testOk1(pvIsConnected.get() !=0);
    pvSeverity = multiChannel->getSeverity();
    testOk1(pvSeverity.get() !=0);
    PVIntArrayPtr pvStatus = multiChannel->getStatus();
    testOk1(pvStatus.get() !=0);
    PVStringArrayPtr pvMessage = multiChannel->getMessage();
    testOk1(pvMessage.get() !=0);
    PVLongArrayPtr pvSecondsPastEpoch = multiChannel->getSecondsPastEpoch();
    testOk1(pvSecondsPastEpoch.get() !=0);
    PVIntArrayPtr pvNanoseconds = multiChannel->getNanoseconds();
    testOk1(pvNanoseconds.get() !=0);
    PVIntArrayPtr pvUserTag = multiChannel->getUserTag();
    testOk1(pvUserTag.get() !=0);
    PVStringPtr pvDescriptor = multiChannel->getDescriptor();
    testOk1(pvDescriptor.get() !=0);
}
Пример #3
0
void LongArrayMonitor::run()
{
    PvaClientPtr pva(PvaClient::get("pva"));
    string  request("record[queueSize=");
    char buff[20];
    sprintf(buff,"%d",queueSize);
    request += buff;
    request += "]field(value,timeStamp,alarm)";
    PvaClientMonitorPtr monitor = pva->channel(channelName,providerName,2.0)->monitor(request);
    TimeStamp timeStamp;
    TimeStamp timeStampLast;
    timeStampLast.getCurrent();
    long nElements = 0;
    long nSinceLastReport = 0;
    int64 first = 0;
    int64 last = 0;
    while(true) {   
        if(runStop.tryWait()) {
             runReturn.signal();
             return;
        }    
        if(!monitor->waitEvent(0.0)) {
	    cout << "waitEvent returned false. Why???" << endl;
            continue;
        }
        PvaClientMonitorDataPtr pvaData = monitor->getData();
    	PVStructurePtr pvStructure = pvaData->getPVStructure();
    	PVLongArrayPtr pvValue = pvStructure->getSubField<PVLongArray>("value");
        size_t len = pvValue->getLength();
        if(len>0) {
            shared_vector<const int64> data = pvValue->view();
            first = data[0];
            last = data[len-1];
            if(first!=last) cout << "error first=" << first << " last=" << last << endl;
       } else {
            cout << "len is 0" << endl;
       }
       nElements += len;
       timeStamp.getCurrent();
       double diff = TimeStamp::diff(timeStamp,timeStampLast);
       if(diff>=1.0) {
            ostringstream out;
            out << " monitors/sec " << nSinceLastReport << " ";
            if(len>0) out << "first " << first << " last " << last ;
            out << " changed " << *pvaData->getChangedBitSet();
            out << " overrun " << *pvaData->getOverrunBitSet();
            double elementsPerSec = nElements;
            elementsPerSec /= diff;
            if(elementsPerSec>10.0e9) {
                 elementsPerSec /= 1e9;
                 out << " gigaElements/sec " << elementsPerSec;
            } else if(elementsPerSec>10.0e6) {
                 elementsPerSec /= 1e6;
                 out << " megaElements/sec " << elementsPerSec;
            } else if(elementsPerSec>10.0e3) {
                 elementsPerSec /= 1e3;
                 out << " kiloElements/sec " << elementsPerSec;
            } else  {
                 out << " Elements/sec " << elementsPerSec;
            }
            cout << out.str() << endl;
            timeStampLast = timeStamp;
            nSinceLastReport = 0;
            nElements = 0;
        }
        ++nSinceLastReport;
        monitor->releaseEvent();
    }
}
Пример #4
0
static PyObject *getScalarArrayValue(PVScalarArrayPtr pvScalarArray)
{
    ScalarType scalarType = pvScalarArray->getScalarArray()->getElementType();
    switch(scalarType) {
    case pvBoolean: {
        PVBooleanArrayPtr pvArray = static_pointer_cast<PVBooleanArray>(pvScalarArray);
        shared_vector<const boolean> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            int value = (data[i] ? 1 : 0);
            PyObject *elem = Py_BuildValue("i",value);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvByte: {
        PVByteArrayPtr pvArray = static_pointer_cast<PVByteArray>(pvScalarArray);
        shared_vector<const int8> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            int value = data[i];
            PyObject *elem = Py_BuildValue("i",value);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvUByte: {
        PVUByteArrayPtr pvArray = static_pointer_cast<PVUByteArray>(pvScalarArray);
        shared_vector<const uint8> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            int value = data[i];
            PyObject *elem = Py_BuildValue("i",value);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvShort: {
        PVShortArrayPtr pvArray = static_pointer_cast<PVShortArray>(pvScalarArray);
        shared_vector<const int16> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            int value = data[i];
            PyObject *elem = Py_BuildValue("i",value);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvUShort: {
        PVUShortArrayPtr pvArray = static_pointer_cast<PVUShortArray>(pvScalarArray);
        shared_vector<const uint16> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            int value = data[i];
            PyObject *elem = Py_BuildValue("i",value);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvInt: {
        PVIntArrayPtr pvArray = static_pointer_cast<PVIntArray>(pvScalarArray);
        shared_vector<const int32> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("i",data[i]);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvUInt: {
        PVUIntArrayPtr pvArray = static_pointer_cast<PVUIntArray>(pvScalarArray);
        shared_vector<const uint32> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("i",data[i]);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvLong: {
        PVLongArrayPtr pvArray = static_pointer_cast<PVLongArray>(pvScalarArray);
        shared_vector<const int64> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("k",data[i]);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvULong: {
        PVULongArrayPtr pvArray = static_pointer_cast<PVULongArray>(pvScalarArray);
        shared_vector<const uint64> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("k",data[i]);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvFloat: {
        PVFloatArrayPtr pvArray = static_pointer_cast<PVFloatArray>(pvScalarArray);
        shared_vector<const float> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("f",data[i]);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvDouble: {
        PVDoubleArrayPtr pvArray = static_pointer_cast<PVDoubleArray>(pvScalarArray);
        shared_vector<const double> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("d",data[i]);
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    case pvString: {
        PVStringArrayPtr pvArray = static_pointer_cast<PVStringArray>(pvScalarArray);
        shared_vector<const string> data(pvArray->view());
        int num = data.size();
        PyObject *result = PyTuple_New(num);
        for(int i=0; i<num; i++) {
            PyObject *elem = Py_BuildValue("s",data[i].c_str());
            PyTuple_SetItem(result, i, elem);
        }
        return result;
    }
    }
    Py_INCREF(Py_None);
    return Py_None;
}
void RequestResponseHandler::makeStrings(epics::pvData::PVStructurePtr const & response)
{
    using namespace epics::pvData;
    using namespace std;

    PVStructurePtr responseValues = response->getStructureField("value");
    if (!responseValues)
    {
        cerr << "Data invalid: No value field in table." << endl;
        m_ok = false;  
        return; 
    }

    //  Handle each of the fields in the archiver query response in turn.

    //  Values.
    PVDoubleArrayPtr values = getDoubleArrayField(responseValues, "value");
    if (!values)
    {
        cerr << "Data invalid: No value field in table values." << endl;
        m_ok = false;  
        return;
    }    
    int valuesLength = values->getLength();

    if (isPresent(VALUE, m_parameters.outputtedFields))
    {

        arrayValuesToStrings(outputFieldValues[VALUE], values->view(),
            m_parameters.format, m_parameters.precision);
    }


    //  Seconds.
    PVLongArrayPtr secPastEpochs = getLongArrayField(responseValues, "secondsPastEpoch");
    if (!secPastEpochs)
    {
        cerr << "Data invalid: No secondsPastEpoch field in table values." << endl;
        m_ok = false;  
        return;
    }

    int secPastEpochsLength = secPastEpochs->getLength();
    if (secPastEpochsLength != valuesLength)
    {
        cerr << "Data invalid: secondsPastEpoch and value lengths don't match." << endl;
        m_ok = false;  
        return; 
    }

    PVLongArray::const_svector secPastEpochsData = secPastEpochs->view();

    if (isPresent(SECONDS_PAST_EPOCH, m_parameters.outputtedFields)
     || isPresent(REAL_TIME, m_parameters.outputtedFields))
    {
        arrayValuesToStrings(outputFieldValues[SECONDS_PAST_EPOCH], secPastEpochsData);
    }


    //  Nanoseconds.
    PVIntArrayPtr nsecs = getIntArrayField(responseValues, "nanoseconds");
    if (!nsecs)
    {
        cerr << "Data invalid: No nanoseconds field in table values." << endl;
        m_ok = false;  
        return;
    }

    int nsecsLength =  nsecs->getLength();
    if (nsecsLength != valuesLength)
    {
        cerr << "Data invalid: nanoseconds past epoch and value lengths don't match." << endl;
        m_ok = false;  
        return;  
    }

    PVIntArray::const_svector nsecsData = nsecs->view();

    if (isPresent(NANO_SECONDS, m_parameters.outputtedFields)
     || isPresent(REAL_TIME, m_parameters.outputtedFields))
    {
        arrayValuesToStrings(outputFieldValues[NANO_SECONDS], nsecsData);
    }


    //  Real time in seconds.
    if (isPresent(REAL_TIME, m_parameters.outputtedFields))
    {
        int realTimeLength = min(secPastEpochsLength, nsecsLength);
        vector<string> & realTimeStrings = outputFieldValues[REAL_TIME];
        realTimeStrings.reserve(realTimeLength);

        {
            ostringstream oss;
            for (int i = 0; i < realTimeLength; ++i)
            {
                oss << secPastEpochsData[i]  << ".";
                oss << setfill('0') << setw(9) << nsecsData[i];
                realTimeStrings.push_back(oss.str());
                oss.str("");
           }
        }
    }


    //  Dates.
    if (isPresent(DATE, m_parameters.outputtedFields))
    {
        vector<string> & dateStrings = outputFieldValues[DATE];
        int dateLength = min(secPastEpochsLength, nsecsLength);
        dateStrings.reserve(dateLength);

        for (int i = 0; i < dateLength; ++i)
        {     
            string dateString = getDate(secPastEpochsData[i], nsecsData[i]);
            dateStrings.push_back(dateString);
        }
    }


    //  Alarm status.
    PVIntArrayPtr statuses = getIntArrayField(responseValues, "status");
    if (!statuses)
    {
        cerr << "Data invalid: No status field in table values." << endl;
        m_ok = false;  
        return;
    }

    int statusesLength = statuses->getLength();
    if (statusesLength != valuesLength)
    {
        cerr << "Data invalid: status and value lengths don't match." << endl;
        m_ok = false;  
        return; 
    }
    if (isPresent(STATUS, m_parameters.outputtedFields))
    {
        PVIntArray::const_svector statusData = statuses->view();
        arrayValuesToStrings(outputFieldValues[STATUS], statusData, FormatParameters::HEX);
    }


    //  Alarm severity.
    PVIntArrayPtr severities = getIntArrayField(responseValues, "severity");
    if (!severities)
    {
        cerr << "Data invalid: No severity field in table values." << endl;
        m_ok = false;  
        return;
    }

    int severitiesLength = severities->getLength();
    if (severitiesLength != valuesLength)
    {
        cerr << "Data invalid: severity and value lengths don't match." << endl;
        m_ok = false;  
        return; 
    }
    if (isPresent(SEVERITY, m_parameters.outputtedFields))
    {        
        PVIntArray::const_svector severityData = severities->view();
        arrayValuesToStrings(outputFieldValues[SEVERITY], severityData, FormatParameters::HEX);
    }


    //  Alarm string.
    int alarmStringsLength = min(secPastEpochsLength, nsecsLength);
    
    if (isPresent(ALARM, m_parameters.outputtedFields))
    {
        vector<string> & alarmStrings = outputFieldValues[ALARM];        
        alarmStrings.reserve(alarmStringsLength); 

        PVIntArray::const_svector statusData = statuses->view();
        PVIntArray::const_svector severityData = severities->view();

        for (int i = 0; i < valuesLength; ++i)
        {     
            string alarmString = makeAlarmString(statusData[i], severityData[i]);
            alarmStrings.push_back(alarmString);
        }
    }
}