All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.epics.pvmanager.sample.JCAClientExample Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager.sample;

import gov.aps.jca.JCALibrary;
import java.util.Arrays;
import org.epics.vtype.VEnum;
import org.epics.vtype.VString;
import org.epics.vtype.VDouble;
import org.epics.vtype.VInt;
import org.epics.pvmanager.PVReader;
import org.epics.pvmanager.PVManager;
import org.epics.pvmanager.PVReaderListener;
import org.epics.vtype.VByteArray;
import org.epics.vtype.VDoubleArray;
import org.epics.vtype.VFloatArray;
import org.epics.vtype.VIntArray;
import org.epics.vtype.VShortArray;
import org.epics.vtype.VStringArray;
import static org.epics.pvmanager.ExpressionLanguage.*;
import org.epics.pvmanager.PVReaderEvent;
import static org.epics.pvmanager.vtype.ExpressionLanguage.*;
import org.epics.pvmanager.jca.JCADataSourceBuilder;
import static org.epics.util.time.TimeDuration.*;

/**
 *
 * @author carcassi
 */
public class JCAClientExample {

    private static final String doublePV = "counter1";
    private static final String enumPV = doublePV + ".SCAN";
    private static final String intPV = doublePV + ".RVAL";
    private static final String stringPV = doublePV + ".EGU";
    private static final String doubleArrayPV = "SR:C00-Glb:G00RB-X";

    public static void main(String[] args) throws Exception {
        System.out.println("Test");
        System.out.println(System.getProperty("java.library.path"));
        
        PVManager.setDefaultDataSource(new JCADataSourceBuilder().jcaContextClass(JCALibrary.JNI_THREAD_SAFE).build());

        testNativeTypeSupport();
        testVDoubleSupport();
        testVIntSupport();
        testVStringSupport();
        testVEnumSupport();
        testVDoubleArraySupport();
//        testVFloatArraySupport();
//        testVByteArraySupport();
//        testVShortArraySupport();
//        testVIntArraySupport();
//        testVStringArraySupport();

    }

    private static void logException(Exception ex) {
        if (ex != null)
            ex.printStackTrace(System.out);
    }

    private static void testNativeTypeSupport() throws Exception {
        {
            final PVReader pv = PVManager.read(channel(doublePV)).maxRate(ofHertz(10));
            Thread.sleep(250);
            logException(pv.lastException());
            VDouble value = (VDouble) pv.getValue();
            System.out.println(value.getClass());
            pv.close();
        }
        {
            final PVReader pv = PVManager.read(channel(stringPV)).maxRate(ofHertz(10));
            Thread.sleep(250);
            logException(pv.lastException());
            VString value = (VString) pv.getValue();
            System.out.println(value.getClass());
            pv.close();
        }
        {
            final PVReader pv = PVManager.read(channel(enumPV)).maxRate(ofHertz(10));
            Thread.sleep(250);
            logException(pv.lastException());
            VEnum value = (VEnum) pv.getValue();
            System.out.println(value.getClass());
            pv.close();
        }
        {
            final PVReader pv = PVManager.read(channel(intPV)).maxRate(ofHertz(10));
            Thread.sleep(250);
            logException(pv.lastException());
            VInt value = (VInt) pv.getValue();
            System.out.println(value.getClass());
            pv.close();
        }
        {
            final PVReader pv = PVManager.read(channel(doubleArrayPV)).maxRate(ofHertz(10));
            Thread.sleep(250);
            logException(pv.lastException());
            VDoubleArray value = (VDoubleArray) pv.getValue();
            System.out.println(value.getClass());
            pv.close();
        }
    }

    private static void testVFloatArraySupport() throws Exception {
        PVReader pv = PVManager.read(vFloatArray(doublePV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        System.out.println(pv.getValue().getData() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));
        
        Thread.sleep(10000);

        pv.close();
    }

    private static void testVDoubleArraySupport() throws Exception {
        PVReader pv = PVManager.read(vDoubleArray(doubleArrayPV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        logException(pv.lastException());
                        if (pv.getValue() != null) {
                            System.out.println(pv.getValue().getData() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                        }
                    }
                })
                .maxRate(ofHertz(10));

        Thread.sleep(10000);

        pv.close();
    }

    private static void testVByteArraySupport() throws Exception {
        PVReader pv = PVManager.read(vByteArray(doublePV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                            System.out.println(pv.getValue().getData() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));

        Thread.sleep(10000);

        pv.close();
    }

    private static void testVShortArraySupport() throws Exception {
        final PVReader pv = PVManager.read(vShortArray(doublePV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                            System.out.println(pv.getValue().getData() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));
        
        Thread.sleep(10000);

        pv.close();
    }

    private static void testVIntArraySupport() throws Exception {
        final PVReader pv = PVManager.read(vIntArray(doublePV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        logException(pv.lastException());
                        System.out.println(pv.getValue().getData() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));
        
        Thread.sleep(10000);

        pv.close();
    }

    private static void testVStringArraySupport() throws Exception {
        final PVReader pv = PVManager.read(vStringArray(doublePV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        System.out.println(pv.getValue().getData() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));

        Thread.sleep(10000);

        pv.close();
    }

    private static void testVDoubleSupport() throws Exception {
        final PVReader pv = PVManager.read(vDouble(doublePV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        System.out.println(pv.getValue().getValue() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));

        Thread.sleep(10000);

        pv.close();
    }

    private static void testVIntSupport() throws Exception {
            final PVReader pv = PVManager.read(vInt(intPV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                            System.out.println(pv.getValue().getValue() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                    }
                })
                .maxRate(ofHertz(10));

            Thread.sleep(10000);

            pv.close();
    }

    private static void testVStringSupport() throws Exception {
        final PVReader pv = PVManager.read(vString(stringPV))
                .readListener(new PVReaderListener() {

                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        logException(pv.lastException());
                        if (pv.getValue() != null) {
                            System.out.println(pv.getValue().getValue() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                        }
                    }
                })
                .maxRate(ofHertz(10));

        Thread.sleep(10000);

        pv.close();
    }

    private static void testVEnumSupport() throws Exception {
        final PVReader pv = PVManager.read(vEnum(enumPV))
                .readListener(new PVReaderListener() {
                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        PVReader pv = event.getPvReader();
                        logException(pv.lastException());
                        if (pv.getValue() != null) {
                            System.out.println(pv.getValue().getValue() + " " + pv.getValue().getTimestamp().toDate() + " " + pv.getValue().getAlarmSeverity());
                        }
                    }
                })
                .maxRate(ofHertz(10));

        Thread.sleep(10000);

        pv.close();
    }
}