/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : GncServer.cc                                           */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <time.h>
#include <math.h>
#include <process.h>
#include "GncServer.h"
#include "Syslog.h"
#include "WorkSiteIF.h"
#include "NavUtils.h"

GncServer::GncServer( int nArgc, char **nArgv )
: GncIF_SK()
{
    Boolean debug = True;
    
    dprintf("*** GncServer::GncServer(argc,argv)...[%d]\n",nArgc);
    _argv = nArgv;
    _argc = nArgc;

    try {
        _output = new GncOutput(SharedData::ReadWrite);
    }
    catch (...) {
        Syslog::write("GncServer::GncServer() - "
                      "output constructor failed\n");
        
        return;
    }
    
    try {
        WorkSiteIF workSite("workSite");
        _utmZone = workSite.utmZone();
    }
    catch (...) {
        Syslog::write("GncServer::GncServer() - "
                      "couldn't connect to WorkSiteIF\n");
        
        return;
    }
    try
    {
        _input = new GncInput( MessageQueue::ReadWrite );
    }
    catch(...)
    {
        Syslog::write("GncServer::GncServer() - "
                      "couldn't open message queue to the driver.\n");
    }
    
    dprintf("*** GncServer::GncServer(argc,argv) - DONE\n");
}


GncServer::~GncServer()
{
    if (_output) delete _output;
    if (_input) delete _input;
}



int GncServer::spawnAuxTasks()
{
    Boolean debug = True;
    dprintf("GncServer::spawnAuxTasks - starting Gnc\n");
    
        char *program = "gnc";
        char errorBuf[256];
    
        pid_t pid;
    
        strcpy( _argv[0], program );
        _argv[_argc] = NULL;
    
        switch ((pid = fork())) {
    
            case 0:
                // In child
                // Execute server program
                execvp(program, (char const * const *)&_argv[0] );
    
                sprintf(errorBuf,
                        "Task::spawnServer() - execlp() of \"%s\" failed", program);
    
                perror(errorBuf);
    
                break;
    
            case -1:
                perror("Task::spawnServer() - fork() failed");
                return -1;
        }
    
    return 0;
}

void GncServer::deviceStatusCallback(TaskInterface *taskInterface,
                                            EventCode eventCode)
{
    Boolean debug = True;
    dprintf("GncServer::deviceStatusCallback - Got event %d from device\n", eventCode);
}

///////////////////////////////////////////////////////////////////
// DynamicControlIF methods

void GncServer::dcGetWallData(double *lastWallTime, double *lastWallBearing)
{
    Boolean debug = True;
//    dprintf("GncServer::dcGetWallData - GOT HERE\n");
    
    _output->read(&_state);
    *lastWallTime = _state.dc.lastWallTime;
    *lastWallBearing = _state.dc.lastWallBearing;
    dprintf("GncServer::dcGetWallData - GOT HERE lwt[%.2f] lwb[%.2f]\n",*lastWallTime,*lastWallBearing);

 //   _dyncon->getWallData(lastWallTime, wallBearing);
}

void GncServer::dcSetGains(GncIF::Gains *gains)
{
    Boolean debug = True;
    dprintf("GncServer::dcSetGains - GOT HERE\n");
//    _dyncon->setGains(gains);
}

void GncServer::dcRevertGains(void)
{
    Boolean debug = True;
    dprintf("GncServer::dcRevertGains - GOT HERE\n");
//    _dyncon->revertGains();
}


