/****************************************************************************/
/*                                                                          */
/*  FILE: cmd_expose.c                                                      */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute a client command to perform an exposure.  When this command */
/*      finishes the image will be in an in-memory array called the "image  */
/*      buffer".                                                            */
/*                                                                          */
/*  HISTORY:                                                                */   
/*      June 1998 -- Chris Albertson Started work                           */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*                  Copyright (C) 1998 Chris Albertson.                     */
/*                                                                          */
/*   This program is free software; you can redistribute it and/or          */
/*   modify it under the terms of the GNU General Public License as         */ 
/*   published by the Free Software Foundation; either version 2, or        */ 
/*   (at your option) any later version.                                    */
/****************************************************************************/


/* Used by RCS and ident */
char cmd_expose_rcsid[] = 
    "$Id: cmd_expose.c,v 1.1 1998/08/04 05:08:47 chris Exp $";


#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "MLog.h"
#include "server_lib.h"
#include "commands.h"
#include "tasks.h"
#include "time_funct.h"

/* Prototypes of local functions */
static int validate_args(char args[]);

/* Arguments.  Decoded by validate_args() */
static double exp_seconds;

#define EXPOSURE_MIN     0.0      /* min exposure time in seconds */
#define EXPOSURE_MAX  3600.0      /* max exposure time in seconds */


/****************************************************************************/
/*                                                                          */
/*  cmd_expose                                                              */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute the "expose" command.                                       */
/*                                                                          */
/*  ARGUMENTS:                                                              */
/*      cmd_point_at like all command functions accepts one string          */
/*      argument.  Inside this are the "real" arguments.  Theses are:       */
/*        1) The exposure time in seconds                                   */
/*                                                                          */
/****************************************************************************/
int cmd_expose(int sd, char args[])
{
    if (CMD_FAIL == validate_args(args))
    {
        return CMD_FAIL;
    } 
    
    ml_printf(ML_DEBUG, ML_INFORMATION,
        "cmd_expose called with valid argument: exp_seconds=%f\n",
        exp_seconds);
        
    /************************************************************************/
    /************************************************************************/
    /************************************************************************/
    /*                                                                      */
    /*                  THIS IS WHERE ALL THE CODE GOES                     */
    /*                                                                      */
    /************************************************************************/
    /************************************************************************/
    /************************************************************************/
        
    return CMD_OK;
}



/****************************************************************************/
/*                                                                          */
/*  validate_args                                                           */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Validate and decode command argument string.                        */
/*                                                                          */
/*  OUTPUT:                                                                 */
/*      Placed in static global track rate.                                 */
/*                                                                          */
/****************************************************************************/      
static int validate_args(char args[])
{ 
    int nargs;
    
    /* Common string used in lots of places */
    static const char    LocMsg[] = "        %s:%d <%s>\n";
    
    

    /* convert the args[] string to values.  First check for sanity */
    if ( !args )
    {
        /* null pointer */
        ml_print (ML_MANDATORY, ML_ERROR, 
            "ERROR no arguments passed\n");
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, "(null pointer)");
        return CMD_FAIL;
    }
    if ( strlen(args) < 1 )
    {
        /* to short to be valid */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR Argument list to short to be valid >%s<\n", args);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

    /* convert string and return count of arguments */
    nargs = sscanf(args, "%lf", &exp_seconds);
  
    if (nargs != 1)
    {
        /* Some error in args string detected */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR Could not parse argment list >%s<\n", args);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

    /* If we get to this point we know the arguments are  */
    /* converted with no errors.  We still need to do a   */
    /* range check                                        */

    if ( (exp_seconds > EXPOSURE_MAX) || (exp_seconds < EXPOSURE_MIN) )
    {
         /* ra_deg is out of it's valid range */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR Exposure time is out of range >%f<\n", exp_seconds);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

  
    /* Finally, we know we have good input and can proceed      */        
    return CMD_OK;
}    
