예제 #1
0
shared_vector<const double> PvaClientPutData::getDoubleArray()
{
    checkValue();
    PVDoubleArrayPtr pv = pvStructure->getSubField<PVDoubleArray>("value");
    if(!pv) {
        throw std::runtime_error(messagePrefix + notDoubleArray);
    }
    return pv->view();   
}
void ExampleMonitorLinkRecord::event(PvaClientMonitorPtr const & monitor)
{
    while(monitor->poll()) {
        PVStructurePtr pvStructure = monitor->getData()->getPVStructure();
        PVDoubleArrayPtr pvDoubleArray = pvStructure->getSubField<PVDoubleArray>("value");
        if(!pvDoubleArray) throw std::runtime_error("value is not a double array");
        lock();
        try {
            beginGroupPut();
            pvValue->replace(pvDoubleArray->view());
            process();
            endGroupPut();
        } catch(...) {
           unlock();
           throw;
        }
        unlock();
        monitor->releaseEvent();
    }
}
예제 #3
0
void ExampleLink::monitorEvent(epics::pvData::MonitorPtr const & monitor)
{
    while(true) {
        MonitorElementPtr monitorElement = monitor->poll();
        if(!monitorElement) break;
        PVStructurePtr pvStructurePtr = monitorElement->pvStructurePtr;
        PVDoubleArrayPtr pvDoubleArray = pvStructurePtr->getSubField<PVDoubleArray>("value");
        if(!pvDoubleArray) throw std::runtime_error("value is not a double array");

        lock();
        try {
            beginGroupPut();
            pvValue->replace(pvDoubleArray->view());
            process();
            endGroupPut();
        } catch(...) {
           unlock();
           throw;
        }
        unlock();
        monitor->release(monitorElement);
    }
}
void test_ntcontinuum()
{
    testDiag("test_ntcontinuum");

    NTContinuumBuilderPtr builder = NTContinuum::createBuilder();
    testOk(builder.get() != 0, "Got builder");

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

    testOk1(ntContinuum->getPVStructure().get() != 0);
    testOk1(ntContinuum->getDescriptor().get() != 0);
    testOk1(ntContinuum->getAlarm().get() != 0);
    testOk1(ntContinuum->getTimeStamp().get() != 0);
    testOk1(ntContinuum->getBase().get() != 0);
    testOk1(ntContinuum->getValue().get() != 0);
    //
    // example how to set base
    //
    PVDoubleArray::svector newBase;
    newBase.push_back(1.0);
    newBase.push_back(2.0);

    PVDoubleArrayPtr pvBaseField = ntContinuum->getBase();
    pvBaseField->replace(freeze(newBase));

    //
    // example how to get bases
    //
    PVDoubleArray::const_svector base(pvBaseField->view());

    testOk1(base.size() == 2);
    testOk1(base[0] == 1.0);
    testOk1(base[1] == 2.0);

    //
    // example how to set values
    //
    PVDoubleArray::svector newValue;
    newValue.push_back(1.0);
    newValue.push_back(2.0);
    newValue.push_back(10.0);
    newValue.push_back(20.0);
    newValue.push_back(100.0);
    newValue.push_back(200.0);

    PVDoubleArrayPtr pvValueField = ntContinuum->getValue();
    pvValueField->replace(freeze(newValue));

    //
    // example how to get values
    //
    PVDoubleArray::const_svector value(pvValueField->view());

    testOk1(value.size() == 6);
    testOk1(value[0] == 1.0);
    testOk1(value[1] == 2.0);
    testOk1(value[2] == 10.0);
    testOk1(value[3] == 20.0);
    testOk1(value[4] == 100.0);
    testOk1(value[5] == 200.0);

    //
    // example how to set units
    //
    PVStringArray::svector newUnits;
    newUnits.push_back("s");
    newUnits.push_back("ms");
    newUnits.push_back("us");
    newUnits.push_back("s");

    PVStringArrayPtr pvUnitsField = ntContinuum->getUnits();
    pvUnitsField->replace(freeze(newUnits));

    //
    // example how to get units
    //
    PVStringArray::const_svector units(pvUnitsField->view());

    testOk1(units.size() == 4);
    testOk1(units[0] == "s");
    testOk1(units[1] == "ms");
    testOk1(units[2] == "us");
    testOk1(units[3] == "s");

    //
    // test isValid
    //
    testOk1(ntContinuum->isValid());
    //
    // timeStamp ops
    //
    PVTimeStamp pvTimeStamp;
    if (ntContinuum->attachTimeStamp(pvTimeStamp))
    {
        testPass("timeStamp attach");

        // example how to set current time
        TimeStamp ts;
        ts.getCurrent();
        pvTimeStamp.set(ts);

        // example how to get EPICS time
        TimeStamp ts2;
        pvTimeStamp.get(ts2);
        testOk1(ts2.getEpicsSecondsPastEpoch() != 0);
    }
    else
        testFail("timeStamp attach fail");

    //
    // alarm ops
    //
    PVAlarm pvAlarm;
    if (ntContinuum->attachAlarm(pvAlarm))
    {
        testPass("alarm attach");

        // example how to set an alarm
        Alarm alarm;
        alarm.setStatus(deviceStatus);
        alarm.setSeverity(minorAlarm);
        alarm.setMessage("simulation alarm");
        pvAlarm.set(alarm);
    }
    else
        testFail("alarm attach fail");

    //
    // set descriptor
    //
    ntContinuum->getDescriptor()->put("This is a test NTContinuum");

    // dump NTContinuum
    std::cout << *ntContinuum->getPVStructure() << std::endl;

}
void test_nthistogram()
{
    testDiag("test_nthistogram");

    NTHistogramBuilderPtr builder = NTHistogram::createBuilder();
    testOk(builder.get() != 0, "Got builder");

    NTHistogramPtr ntHistogram = builder->
            value(pvInt)->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            add("extra1",fieldCreate->createScalar(pvString)) ->
            add("extra2",fieldCreate->createScalarArray(pvString)) ->
            create();
    testOk1(ntHistogram.get() != 0);

    testOk1(ntHistogram->getPVStructure().get() != 0);
    testOk1(ntHistogram->getDescriptor().get() != 0);
    testOk1(ntHistogram->getAlarm().get() != 0);
    testOk1(ntHistogram->getTimeStamp().get() != 0);
    testOk1(ntHistogram->getRanges().get() != 0);
    testOk1(ntHistogram->getValue().get() != 0);
    testOk1(ntHistogram->getValue<PVIntArray>().get() != 0);
    //
    // example how to set ranges
    //
    PVDoubleArray::svector newRanges;
    newRanges.push_back(-100.0);
    newRanges.push_back(0.0);
    newRanges.push_back(100.0);

    PVDoubleArrayPtr pvRangesField = ntHistogram->getRanges();
    pvRangesField->replace(freeze(newRanges));

    //
    // example how to get ranges
    //
    PVDoubleArray::const_svector ranges(pvRangesField->view());

    testOk1(ranges.size() == 3);
    testOk1(ranges[0] == -100.0);
    testOk1(ranges[1] == 0.0);
    testOk1(ranges[2] == 100.0);

    //
    // example how to set value
    //
    PVIntArray::svector newValue;
    newValue.push_back(1);
    newValue.push_back(2);

    PVIntArrayPtr pvValueField = ntHistogram->getValue<PVIntArray>();
    pvValueField->replace(freeze(newValue));

    //
    // example how to get values for each bin
    //
    PVIntArray::const_svector value(pvValueField->view());

    testOk1(value.size() == 2);
    testOk1(value[0] == 1);
    testOk1(value[1] == 2);

    //
    // test isValid
    //
    testOk1(ntHistogram->isValid());
    //
    // timeStamp ops
    //
    PVTimeStamp pvTimeStamp;
    if (ntHistogram->attachTimeStamp(pvTimeStamp))
    {
        testPass("timeStamp attach");

        // example how to set current time
        TimeStamp ts;
        ts.getCurrent();
        pvTimeStamp.set(ts);

        // example how to get EPICS time
        TimeStamp ts2;
        pvTimeStamp.get(ts2);
        testOk1(ts2.getEpicsSecondsPastEpoch() != 0);
    }
    else
        testFail("timeStamp attach fail");

    //
    // alarm ops
    //
    PVAlarm pvAlarm;
    if (ntHistogram->attachAlarm(pvAlarm))
    {
        testPass("alarm attach");

        // example how to set an alarm
        Alarm alarm;
        alarm.setStatus(deviceStatus);
        alarm.setSeverity(minorAlarm);
        alarm.setMessage("simulation alarm");
        pvAlarm.set(alarm);
    }
    else
        testFail("alarm attach fail");

    //
    // set descriptor
    //
    ntHistogram->getDescriptor()->put("This is a test NTHistogram");

    // dump NTHistogram
    std::cout << *ntHistogram->getPVStructure() << std::endl;

}
예제 #6
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);
        }
    }
}