/******************************************************************************/
/* Copyright 1996 MBARI                                                       */
/******************************************************************************/
/* Summary  : Video overlay functions for Condition class                     */
/* Filename : Condition.c                                                     */
/* Author   : Janice Tarrant                                                  */
/* Project  : Tiburon                                                         */
/* Version  : Version 1.0                                                     */
/* Created  : 08/27/96                                                        */
/* Modified :                                                                 */
/* Archived :                                                                 */
/******************************************************************************/
/* Modification History :                                                     */
/* $Header$
 * $Log$
 *
 */
/******************************************************************************/

#include "dmvideo.h"			/* definitions for Data Manager       */
#include "OVtypes.h"                    /* video overlay type definitions     */
#include <powerTypes.h>			/* definitions for power types        */
#include "Condition.h" 			/* definitions for Condition class    */


/******************************************************************************/
/* Function : Condition_New                                                   */
/* Purpose  : Constructor for Condition class.                                */
/* Inputs   : None.                                                           */
/* Outputs  : Returns pointer to condition structure.                         */
/******************************************************************************/
    OV_Condition*
Condition_New(OV_char *name, OV_char *dm_name)
{
    OV_Condition *p_condition;			/* condition structure pointer*/

    p_condition = (OV_Condition *) malloc(sizeof(OV_Condition));
    p_condition->name = name;
    p_condition->dm_name = dm_name;

    return(p_condition);

}  /* Condition_New */


/******************************************************************************/
/* Function : Condition_Delete                                                */
/* Purpose  : Destructor for Condition class.                                 */
/* Inputs   : Pointer to condition structure.                                 */
/* Outputs  : None.                                                           */
/******************************************************************************/
    Void
Condition_Delete(OV_Condition *p_condition)
{
    if (p_condition->name != NULL)
	free(p_condition->name);

    if (p_condition->dm_name != NULL)
	free(p_condition->dm_name);

    free(p_condition);

    return;

}  /* Condition_Delete */


/******************************************************************************/
/* Function : Condition_Initialize                                            */
/* Purpose  : Initializes Condition class.                                    */
/* Inputs   : Pointer to condition structure.                                 */
/* Outputs  : None.                                                           */
/******************************************************************************/
    Void
Condition_Initialize(OV_Condition *p_condition)
{
    p_condition->dm_item = NO_ITEM;
    p_condition->value = FALSE;
    p_condition->idBit = NULL;

}  /* Condition_Initialize */


/******************************************************************************/
/* Function : Condition_Read                                                  */
/* Purpose  : Reads a Condition value.                                        */
/* Inputs   : Pointer to condition structure.                                 */
/* Outputs  : None.                                                           */
/******************************************************************************/
    Void
Condition_Read(OV_Condition *p_condition)
{
    MBool        condition;			/* condition value            */
    switchStatus switchState;			/* power switch status        */
    Errno        status;			/* data manager error code    */

    if (p_condition->dm_type == DM_MBOOL)
    {
	status = dm_read(p_condition->dm_item, (Void *) &condition,
                         sizeof(condition), (DM_Time *) NULL);
/*
        if (checkRead(status, p_condition->dm_name, FALSE) == ERROR)
*/
	if (status != SUCCESS)
            p_condition->value = TRUE;
        else
            p_condition->value = condition;
    }
    else if (p_condition->dm_type == DM_ENUM)
    {
	if (strcmp(p_condition->name, "vbmotor") == 0)
	{
	    status = dm_read(p_condition->dm_item, (Void *) &switchState,
                             sizeof(switchState), (DM_Time *) NULL);
/*
            if (checkRead(status, p_condition->dm_name, FALSE) == ERROR)
*/
	    if (status != SUCCESS)
                p_condition->value = TRUE;
            else
	    {
		if (switchState == SWITCH_ON)
                    p_condition->value = TRUE;
		else
		    p_condition->value = FALSE;
	    }
	}
    }

}  /* Condition_Read */
