Ejemplo n.º 1
0
bool NTMatrix::isValid()
{
    int valueLength = getValue()->getLength();
    if (valueLength == 0)
        return false;

    PVIntArrayPtr pvDim = getDim();
    if (pvDim.get())
    {
        int length = pvDim->getLength();
        if (length != 1 && length !=2)
            return false;

        PVIntArray::const_svector data = pvDim->view();
        int expectedLength = 1;
        for (PVIntArray::const_svector::const_iterator it = data.begin();
                 it != data.end(); ++it)
        {
             expectedLength *= *it;
        }
        if (expectedLength != valueLength)
        return false;
    }
    return true;
}
Ejemplo n.º 2
0
void testGet(bool debug,GatherV3DataPtr gather)
{
    String builder;
    NTTablePtr nttable = gather->getNTTable();
    bool result = gather->get();
    if(!result) printf("get failed\n%s\n",gather->getMessage().c_str());
    if(debug) {
        builder.clear();
        nttable->getPVStructure()->toString(&builder);
        printf("nttable\n%s\n",builder.c_str());
    }
    PVDoubleArrayPtr values = gather->getDoubleValue();
    PVIntArrayPtr severitys = gather->getAlarmSeverity();
    PVBooleanArrayPtr isConnecteds = gather->getIsConnected();
    PVStringArrayPtr channelNames = gather->getChannelName();
    if(debug) {
        builder.clear();
        values->toString(&builder);
        printf("value: %s\n",builder.c_str());
        builder.clear();
        severitys->toString(&builder);
        printf("severity: %s\n",builder.c_str());
        builder.clear();
        isConnecteds->toString(&builder);
        printf("isConnected: %s\n",builder.c_str());
        builder.clear();
        channelNames->toString(&builder);
        printf("channelName: %s\n",builder.c_str());
    }
}
void test_ntscalarArray()
{
    testDiag("test_ntscalarArray");

    NTScalarArrayBuilderPtr builder = NTScalarArray::createBuilder();
    testOk(builder.get() != 0, "Got builder");

    NTScalarArrayPtr ntScalarArray = builder->
            value(pvInt)->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            addDisplay()->
            addControl()->
            create();
    testOk1(ntScalarArray.get() != 0);

    testOk1(ntScalarArray->getPVStructure().get() != 0);
    testOk1(ntScalarArray->getValue().get() != 0);
    testOk1(ntScalarArray->getDescriptor().get() != 0);
    testOk1(ntScalarArray->getAlarm().get() != 0);
    testOk1(ntScalarArray->getTimeStamp().get() != 0);
    testOk1(ntScalarArray->getDisplay().get() != 0);
    testOk1(ntScalarArray->getControl().get() != 0);

    //
    // example how to set values
    //
    PVIntArray::svector newValues;
    newValues.push_back(1);
    newValues.push_back(2);
    newValues.push_back(8);

    PVIntArrayPtr pvValueField = ntScalarArray->getValue<PVIntArray>();
    pvValueField->replace(freeze(newValues));

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

    testOk1(values.size() == 3);
    testOk1(values[0] == 1);
    testOk1(values[1] == 2);
    testOk1(values[2] == 8);

    //
    // timeStamp ops
    //
    PVTimeStamp pvTimeStamp;
    if (ntScalarArray->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 (ntScalarArray->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");

    //
    // display ops
    //
    PVDisplay pvDisplay;
    if (ntScalarArray->attachDisplay(pvDisplay))
    {
        testPass("display attach");

        // example how to set an display
        Display display;
        display.setLow(-15);
        display.setHigh(15);
        display.setDescription("This is a test scalar array");
        display.setFormat("%d");
        display.setUnits("A");
        pvDisplay.set(display);
    }
    else
        testFail("display attach fail");

    //
    // control ops
    //
    PVControl pvControl;
    if (ntScalarArray->attachControl(pvControl))
    {
        testPass("control attach");

        // example how to set an control
        Control control;
        control.setLow(-10);
        control.setHigh(10);
        control.setMinStep(1);
        pvControl.set(control);
    }
    else
        testFail("control attach fail");

    //
    // set descriptor
    //
    ntScalarArray->getDescriptor()->put("This is a test NTScalarArray");

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

}
Ejemplo n.º 4
0
void test_nttable()
{
    testDiag("test_nttable");

    NTTableBuilderPtr builder = NTTable::createBuilder();
    testOk(builder.get() != 0, "Got builder");

    NTTablePtr ntTable = builder->
            addColumn("column0", pvDouble)->
            addColumn("column1", pvString)->
            addColumn("column2", pvInt)->
            addDescriptor()->
            addAlarm()->
            addTimeStamp()->
            create();
    testOk1(ntTable.get() != 0);

    testOk1(ntTable->getPVStructure().get() != 0);
    testOk1(ntTable->getDescriptor().get() != 0);
    testOk1(ntTable->getAlarm().get() != 0);
    testOk1(ntTable->getTimeStamp().get() != 0);
    testOk1(ntTable->getLabels().get() != 0);

    testOk1(ntTable->getColumn<PVDoubleArray>("column0").get() != 0);
    testOk1(ntTable->getColumn<PVStringArray>("column1").get() != 0);
    testOk1(ntTable->getColumn<PVIntArray>("column2").get() != 0);

    testOk1(ntTable->getColumn("invalid").get() == 0);

    //
    // example how to set column values
    //
    PVIntArray::svector newValues;
    newValues.push_back(1);
    newValues.push_back(2);
    newValues.push_back(8);

    PVIntArrayPtr intColumn = ntTable->getColumn<PVIntArray>("column2");
    intColumn->replace(freeze(newValues));

    //
    // example how to get column values
    //
    PVIntArray::const_svector values(intColumn->view());

    testOk1(values.size() == 3);
    testOk1(values[0] == 1);
    testOk1(values[1] == 2);
    testOk1(values[2] == 8);

    //
    // timeStamp ops
    //
    PVTimeStamp pvTimeStamp;
    if (ntTable->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 (ntTable->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
    //
    ntTable->getDescriptor()->put("This is a test NTTable");

    // dump NTTable
    std::cout << *ntTable->getPVStructure() << std::endl;

}
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);
}
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;

}
Ejemplo n.º 7
0
void test()
{
    String builder;
    int n = 9;
    StringArray channelName(n);
    channelName[0] = "masarExample0000";
    channelName[1] = "masarExample0001";
    channelName[2] = "masarExample0002";
    channelName[3] = "masarExample0003";
    channelName[4] = "masarExample0004";
    channelName[5] = "masarExampleCharArray";
    channelName[6] = "masarExampleStringArray";
    channelName[7] = "masarExampleLongArray";
    channelName[8] = "masarExampleDoubleArray";
    GatherV3DataPtr gather(new GatherV3Data(channelName,n));
    bool result = gather->connect(5.0);
    if(!result) {
        printf("connect failed\n%s\n",gather->getMessage().c_str());
        printf("This test requires iocBoot/iocAll ");
        printf("It must be started before running this test\n");
        exit(1);
    }
    NTTablePtr nttable = gather->getNTTable();
    BooleanArrayData booldata;
    PVBooleanArrayPtr pvIsArray = static_pointer_cast<PVBooleanArray>
            (nttable->getPVStructure()->getScalarArrayField("isArray",pvBoolean));
    pvIsArray->get(0,n,booldata);
    BooleanArray & isArray = booldata.data;
    DoubleArrayData ddata;
    gather->getDoubleValue()->get(0,n,ddata);
    DoubleArray & dvalue = ddata.data;
    StringArrayData sdata;
    gather->getStringValue()->get(0,n,sdata);
    StringArray & svalue = sdata.data;
    LongArrayData ldata;
    gather->getLongValue()->get(0,n,ldata);
    LongArray & lvalue = ldata.data;
    IntArrayData idata;
    StructureArrayData structdata;
    gather->getArrayValue()->get(0,n,structdata);
    PVStructurePtrArray & structvalue = structdata.data;
    gather->getDBRType()->get(0,n,idata);
    IntArray & dbrType = idata.data;
    for(int i=0; i<n; i++) {
        if(isArray[i]) {
            PVStructurePtr pvStructure = structvalue[i];
            switch(dbrType[i]) {
            case DBF_STRING: {
                PVStringArrayPtr pvValue = static_pointer_cast<PVStringArray>(
                    pvStructure->getScalarArrayField("stringValue",pvString));
                int num = 4;
                String value[4];
                value[0] = "aaa";
                value[1] = "bbb";
                value[2] = "ccc";
                value[3] = "ddd";
                pvValue->put(0,num,value,0);
                break;
            }
            case DBF_CHAR:
            case DBF_INT:
            case DBF_LONG: {
                PVIntArrayPtr pvValue = static_pointer_cast<PVIntArray>(
                    pvStructure->getScalarArrayField("intValue",pvInt));
                int num = 4;
                int32 value[4] = {1,2,3,4};
                pvValue->put(0,num,value,0);
                 break;
            }
            case DBF_FLOAT:
            case DBF_DOUBLE: {
                PVDoubleArrayPtr pvValue = static_pointer_cast<PVDoubleArray>(
                    pvStructure->getScalarArrayField("doubleValue",pvDouble));
                int num = 4;
                double value[4] = {1e1,1e2,1e3,1e4};
                pvValue->put(0,num,value,0);
                 break;
            }
            default:
                printf("got an unexpected DBF type. Logic error\n");
                exit(1);
            }
            continue;
        }
        switch(dbrType[i]) {
        case DBF_STRING:
             svalue[i] = String("this is set by gatherV3DataPut"); break;
        case DBF_ENUM:
             svalue[i] = String("one"); break;
        case DBF_CHAR:
        case DBF_INT:
        case DBF_LONG:
             lvalue[i] = i; break;
        case DBF_FLOAT:
        case DBF_DOUBLE:
             dvalue[i] = i; break;
        default:
            printf("got an unexpected DBF type. Logic error\n");
            exit(1);
        }
    }
    result = gather->put();
    if(!result) {printf("put failed\n%s\n",gather->getMessage().c_str()); exit(1);}
    result = gather->get();
    if(!result) {printf("get failed\n%s\n",gather->getMessage().c_str()); exit(1);}
    builder.clear();
    nttable->getPVStructure()->toString(&builder);
    printf("nttable\n%s\n",builder.c_str());
}
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);
}
Ejemplo n.º 9
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;
}
Ejemplo n.º 10
0
void test_ntnameValue()
{
    testDiag("test_ntnameValue");

    NTNameValueBuilderPtr builder = NTNameValue::createBuilder();
    testOk(builder.get() != 0, "Got builder");

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

    testOk1(NTNameValue::is_a(ntNameValue->getPVStructure()));
    testOk1(NTNameValue::isCompatible(ntNameValue->getPVStructure()));

    testOk1(ntNameValue->getPVStructure().get() != 0);
    testOk1(ntNameValue->getDescriptor().get() != 0);
    testOk1(ntNameValue->getAlarm().get() != 0);
    testOk1(ntNameValue->getTimeStamp().get() != 0);
    testOk1(ntNameValue->getName().get() != 0);
    testOk1(ntNameValue->getValue().get() != 0);

    //
    // example how to set name
    //
    PVStringArray::svector newName;
    newName.push_back("name1");
    newName.push_back("name2");
    newName.push_back("name3");

    PVStringArrayPtr pvNameField = ntNameValue->getName();
    pvNameField->replace(freeze(newName));

    //
    // example how to get name
    //
    PVStringArray::const_svector name(pvNameField->view());

    testOk1(name.size() == 3);
    testOk1(name[0] == "name1");
    testOk1(name[1] == "name2");
    testOk1(name[2] == "name3");

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

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

    //
    // example how to get column value
    //
    PVIntArray::const_svector value(pvValueField->view());

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

    //
    // timeStamp ops
    //
    PVTimeStamp pvTimeStamp;
    if (ntNameValue->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 (ntNameValue->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
    //
    ntNameValue->getDescriptor()->put("This is a test NTNameValue");

    // dump NTNameValue
    std::cout << *ntNameValue->getPVStructure() << std::endl;

}
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);
        }
    }
}