<commit_before>/*******************************************************************************
 * Copyright (c) 2010 O<NAME> National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.utility.pv.epics;

import gov.aps.jca.dbr.DBR;
import gov.aps.jca.dbr.DBRType;
import gov.aps.jca.dbr.DBR_Byte;
import gov.aps.jca.dbr.DBR_CTRL_Double;
import gov.aps.jca.dbr.DBR_CTRL_Int;
import gov.aps.jca.dbr.DBR_Double;
import gov.aps.jca.dbr.DBR_Float;
import gov.aps.jca.dbr.DBR_Int;
import gov.aps.jca.dbr.DBR_LABELS_Enum;
import gov.aps.jca.dbr.DBR_Short;
import gov.aps.jca.dbr.DBR_String;
import gov.aps.jca.dbr.DBR_TIME_Byte;
import gov.aps.jca.dbr.DBR_TIME_Double;
import gov.aps.jca.dbr.DBR_TIME_Enum;
import gov.aps.jca.dbr.DBR_TIME_Float;
import gov.aps.jca.dbr.DBR_TIME_Int;
import gov.aps.jca.dbr.DBR_TIME_Short;
import gov.aps.jca.dbr.DBR_TIME_String;
import gov.aps.jca.dbr.Status;
import gov.aps.jca.dbr.TimeStamp;

import org.csstudio.data.values.IEnumeratedMetaData;
import org.csstudio.data.values.IMetaData;
import org.csstudio.data.values.INumericMetaData;
import org.csstudio.data.values.ISeverity;
import org.csstudio.data.values.ITimestamp;
import org.csstudio.data.values.IValue;
import org.csstudio.data.values.TimestampFactory;
import org.csstudio.data.values.ValueFactory;

/** Helper for dealing with DBR.. types.
 *  <p>
 *  JCA provides up to "...Int", returning an int/Integer.
 *  IValue uses long for future protocol support.
 *
 *  @author <NAME>
 */
public class DBR_Helper
{
	/** All live samples are 'original' */
	final private static IValue.Quality quality = IValue.Quality.Original;

	/** Convert the EPICS time stamp (based on 1990) into the usual 1970 epoch.
     *  <p>
     *  In case this is called with data from a CTRL_... request,
     *  the null timestamp is replaced with the current host time.
     *  <p>
     *  The EPICS start of epoch as returned by IOCs for records that have never
     *  processed is mapped to 0 in the 1970 epoch so that ITimestamp.isValid()
     *  can be used to catch it.
     *  This "works", bad time stamps are identified. But it's not ideal because the
     *  original time stamp is lost. Better would be an implementation that passes 1990+0secs on,
     *  yet makes isValid() return false by using an EPICS-specific ITimestamp override of isValid().
     *  Not implemented at this time.
     */
    private static ITimestamp createTimeFromEPICS(final TimeStamp t)
    {
        if (t == null)
            return TimestampFactory.now();
        if (t.secPastEpoch() == 0  &&  t.nsec() == 0)
            return TimestampFactory.createTimestamp(0, 0);
        return TimestampFactory.createTimestamp(
                        t.secPastEpoch() + 631152000L, t.nsec());
    }

    /** @return CTRL_... type for this channel. */
    public static DBRType getCtrlType(final boolean plain, final DBRType type)
    {
        if (type.isDOUBLE())
            return plain ? DBRType.DOUBLE : DBRType.CTRL_DOUBLE;
        else if (type.isFLOAT())
            return plain ? DBRType.FLOAT : DBRType.CTRL_DOUBLE;
        else if (type.isINT())
            return plain ? DBRType.INT : DBRType.CTRL_INT;
        else if (type.isSHORT())
            return plain ? DBRType.SHORT : DBRType.CTRL_INT;
        else if (type.isBYTE())
            return plain ? DBRType.BYTE : DBRType.CTRL_BYTE;
        else if (type.isENUM())
            return plain ? DBRType.SHORT : DBRType.CTRL_ENUM;
        // default: get as string
        return plain ? DBRType.STRING : DBRType.CTRL_STRING;
    }

    /** @return Meta data extracted from dbr */
    public static IMetaData decodeMetaData(final DBR dbr)
    {
        if (dbr.isLABELS())
        {
            final DBR_LABELS_Enum labels = (DBR_LABELS_Enum)dbr;
            return ValueFactory.createEnumeratedMetaData(labels.getLabels());
        }
        else if (dbr instanceof DBR_CTRL_Double)
        {
            final DBR_CTRL_Double ctrl = (DBR_CTRL_Double)dbr;
            return ValueFactory.createNumericMetaData(
                            ctrl.getLowerDispLimit().doubleValue(),
                            ctrl.getUpperDispLimit().doubleValue(),
                            ctrl.getLowerWarningLimit().doubleValue(),
                            ctrl.getUpperWarningLimit().doubleValue(),
                            ctrl.getLowerAlarmLimit().doubleValue(),
                            ctrl.getUpperAlarmLimit().doubleValue(),
                            ctrl.getPrecision(),
                            ctrl.getUnits());
        }
        else if (dbr instanceof DBR_CTRL_Int)
        {
            final DBR_CTRL_Int ctrl = (DBR_CTRL_Int)dbr;
            return ValueFactory.createNumericMetaData(
                            ctrl.getLowerDispLimit().doubleValue(),
                            ctrl.getUpperDispLimit().doubleValue(),
                            ctrl.getLowerWarningLimit().doubleValue(),
                            ctrl.getUpperWarningLimit().doubleValue(),
                            ctrl.getLowerAlarmLimit().doubleValue(),
                            ctrl.getUpperAlarmLimit().doubleValue(),
                            0, // no precision
                            ctrl.getUnits());
        }
        return null;
    }

    /** @return TIME_... type for this channel. */
    public static DBRType getTimeType(final boolean plain, final DBRType type)
    {
        if (type.isDOUBLE())
            return plain ? DBRType.DOUBLE : DBRType.TIME_DOUBLE;
        else if (type.isFLOAT())
            return plain ? DBRType.FLOAT : DBRType.TIME_FLOAT;
        else if (type.isINT())
            return plain ? DBRType.INT : DBRType.TIME_INT;
        else if (type.isSHORT())
            return plain ? DBRType.SHORT : DBRType.TIME_SHORT;<commit_msg>o.c.utility.pv: fix a bug that results in null meta data on some data types<commit_after>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.utility.pv.epics;

import gov.aps.jca.dbr.DBR;
import gov.aps.jca.dbr.DBRType;
import gov.aps.jca.dbr.DBR_Byte;
import gov.aps.jca.dbr.DBR_CTRL_Double;
import gov.aps.jca.dbr.DBR_CTRL_Int;
import gov.aps.jca.dbr.DBR_Double;
import gov.aps.jca.dbr.DBR_Float;
import gov.aps.jca.dbr.DBR_Int;
import gov.aps.jca.dbr.DBR_LABELS_Enum;
import gov.aps.jca.dbr.DBR_Short;
import gov.aps.jca.dbr.DBR_String;
import gov.aps.jca.dbr.DBR_TIME_Byte;
import gov.aps.jca.dbr.DBR_TIME_Double;
import gov.aps.jca.dbr.DBR_TIME_Enum;
import gov.aps.jca.dbr.DBR_TIME_Float;
import gov.aps.jca.dbr.DBR_TIME_Int;
import gov.aps.jca.dbr.DBR_TIME_Short;
import gov.aps.jca.dbr.DBR_TIME_String;
import gov.aps.jca.dbr.GR;
import gov.aps.jca.dbr.PRECISION;
import gov.aps.jca.dbr.Status;
import gov.aps.jca.dbr.TimeStamp;

import org.csstudio.data.values.IEnumeratedMetaData;
import org.csstudio.data.values.IMetaData;
import org.csstudio.data.values.INumericMetaData;
import org.csstudio.data.values.ISeverity;
import org.csstudio.data.values.ITimestamp;
import org.csstudio.data.values.IValue;
import org.csstudio.data.values.TimestampFactory;
import org.csstudio.data.values.ValueFactory;

/** Helper for dealing with DBR.. types.
 *  <p>
 *  JCA provides up to "...Int", returning an int/Integer.
 *  IValue uses long for future protocol support.
 *
 *  @author <NAME>
 */
public class DBR_Helper
{
	/** All live samples are 'original' */
	final private static IValue.Quality quality = IValue.Quality.Original;

	/** Convert the EPICS time stamp (based on 1990) into the usual 1970 epoch.
     *  <p>
     *  In case this is called with data from a CTRL_... request,
     *  the null timestamp is replaced with the current host time.
     *  <p>
     *  The EPICS start of epoch as returned by IOCs for records that have never
     *  processed is mapped to 0 in the 1970 epoch so that ITimestamp.isValid()
     *  can be used to catch it.
     *  This "works", bad time stamps are identified. But it's not ideal because the
     *  original time stamp is lost. Better would be an implementation that passes 1990+0secs on,
     *  yet makes isValid() return false by using an EPICS-specific ITimestamp override of isValid().
     *  Not implemented at this time.
     */
    private static ITimestamp createTimeFromEPICS(final TimeStamp t)
    {
        if (t == null)
            return TimestampFactory.now();
        if (t.secPastEpoch() == 0  &&  t.nsec() == 0)
            return TimestampFactory.createTimestamp(0, 0);
        return TimestampFactory.createTimestamp(
                        t.secPastEpoch() + 631152000L, t.nsec());
    }

    /** @return CTRL_... type for this channel. */
    public static DBRType getCtrlType(final boolean plain, final DBRType type)
    {
        if (type.isDOUBLE())
            return plain ? DBRType.DOUBLE : DBRType.CTRL_DOUBLE;
        else if (type.isFLOAT())
            return plain ? DBRType.FLOAT : DBRType.CTRL_DOUBLE;
        else if (type.isINT())
            return plain ? DBRType.INT : DBRType.CTRL_INT;
        else if (type.isSHORT())
            return plain ? DBRType.SHORT : DBRType.CTRL_INT;
        else if (type.isBYTE())
            return plain ? DBRType.BYTE : DBRType.CTRL_BYTE;
        else if (type.isENUM())
            return plain ? DBRType.SHORT : DBRType.CTRL_ENUM;
        // default: get as string
        return plain ? DBRType.STRING : DBRType.CTRL_STRING;
    }

    /** @return Meta data extracted from dbr */
    public static IMetaData decodeMetaData(final DBR dbr)
    {
        if (dbr.isLABELS())
        {
            final DBR_LABELS_Enum labels = (DBR_LABELS_Enum)dbr;
            return ValueFactory.createEnumeratedMetaData(labels.getLabels());
        }
        else if (dbr instanceof DBR_CTRL_Double)
        {
            final DBR_CTRL_Double ctrl = (DBR_CTRL_Double)dbr;
            return ValueFactory.createNumericMetaData(
                            ctrl.getLowerDispLimit().doubleValue(),
                            ctrl.getUpperDispLimit().doubleValue(),
                            ctrl.getLowerWarningLimit().doubleValue(),
                            ctrl.getUpperWarningLimit().doubleValue(),
                            ctrl.getLowerAlarmLimit().doubleValue(),
                            ctrl.getUpperAlarmLimit().doubleValue(),
                            ctrl.getPrecision(),
                            ctrl.getUnits());
        }
        else if (dbr instanceof DBR_CTRL_Int)
        {
            final DBR_CTRL_Int ctrl = (DBR_CTRL_Int)dbr;
            return ValueFactory.createNumericMetaData(
                            ctrl.getLowerDispLimit().doubleValue(),
                            ctrl.getUpperDispLimit().doubleValue(),
                            ctrl.getLowerWarningLimit().doubleValue(),
                            ctrl.getUpperWarningLimit().doubleValue(),
                            ctrl.getLowerAlarmLimit().doubleValue(),
                            ctrl.getUpperAlarmLimit().doubleValue(),
                            0, // no precision
                            ctrl.getUnits());
        }else if (dbr instanceof GR)
        {
            final GR ctrl = (GR)dbr;
            return ValueFactory.createNumericMetaData(
                            ctrl.getLowerDispLimit().doubleValue(),
                            ctrl.getUpperDispLimit().doubleValue(),
                            ctrl.getLowerWarningLimit().doubleValue(),
                            ctrl.getUpperWarningLimit().doubleValue(),
                            ctrl.getLowerAlarmLimit().doubleValue(),
                            ctrl.getUpperAlarmLimit().doubleValue(),
                            (dbr instanceof PRECISION)? ((PRECISION)dbr).getPrecision() : 0,
                            ctrl.getUnits());
        }
        return null;
    }

    /** @return TIME_... type for this channel. */
    public static DBRType getTimeType(final boolean plain, final DBRType type)
    {
        if (type.isDOUBLE())
            return plain ? DBRType.DOUBLE : DBRType.TIME_DOUBLE;
        else if (type.isFLOAT())
            return plain ? DBRType.FLOAT : DBRType.TIME_FLOAT;
        else if (type.isINT())
            return plain ? DBRType.INT : DBRType.TIME_INT;
        else if (type.isSHORT())
            return plain ? DBRType.SHORT : DBRType.TIME_SHORT;
{'fraction_non_alphanumeric': 0.12086054552439493, 'fraction_numerical': 0.006223588167499039, 'mean_word_length': 2.2926890968884392, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '539545', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3887, 'n_words': 916}