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

org.diirt.datasource.sample.jca.JCAClientExample Maven / Gradle / Ivy

There is a newer version: 3.1.7
Show newest version
/**
 * Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.datasource.sample.jca;

import gov.aps.jca.JCALibrary;
import org.diirt.vtype.VEnum;
import org.diirt.vtype.VString;
import org.diirt.vtype.VDouble;
import org.diirt.vtype.VInt;
import org.diirt.datasource.PVReader;
import org.diirt.datasource.PVManager;
import org.diirt.datasource.PVReaderListener;
import org.diirt.vtype.VByteArray;
import org.diirt.vtype.VDoubleArray;
import org.diirt.vtype.VFloatArray;
import org.diirt.vtype.VIntArray;
import org.diirt.vtype.VShortArray;
import org.diirt.vtype.VStringArray;
import static org.diirt.datasource.ExpressionLanguage.*;
import org.diirt.datasource.PVReaderEvent;
import static org.diirt.datasource.vtype.ExpressionLanguage.*;
import org.diirt.support.ca.JCADataSourceConfiguration;
import org.diirt.util.time.TimeDuration;
import static org.diirt.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(Double.NEGATIVE_INFINITY);
        System.out.println(System.getProperty("java.library.path"));
        
        System.loadLibrary("jca");
        PVManager.setDefaultDataSource(new JCADataSourceConfiguration()
                .jcaContextClass(JCALibrary.JNI_THREAD_SAFE).create());
        PVReader pvReader = PVManager.read(channel("counter1"))
                .readListener(new PVReaderListener() {

            @Override
            public void pvChanged(PVReaderEvent event) {
                System.out.println(event.getPvReader().getValue());
            }
        })
                .maxRate(TimeDuration.ofMillis(100));
        
        Thread.sleep(5000);
        pvReader.close();
        
        //        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();
    }
}