/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : lcSubscriber2.cc                                              */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include "Task.h"
#include "LayeredControlIF.h"
#include "Clock.h"
#include "EventService.h"

class MyTask : public Task {

public:

  MyTask(const char *name);
  ~MyTask();
  void run();

  LayeredControlIF *layeredControl;
  Clock *clock;

  // Callback for LayeredControl output 
  void lcCallback(TaskInterface *client, EventCode eventCode);

  // Callback for clock tick
  void tickCallback(TaskInterface *client, EventCode eventCode);

protected:

  virtual void execServer();

};


MyTask::MyTask(const char *name)
  : Task(name)
{
}


MyTask::~MyTask()
{
}


void MyTask::run()
{
  EventService *eventService = new EventService("eventService", this);

  layeredControl = 
    new LayeredControlIF("layeredControl");

  clock = new Clock("clock");

  // Subscribe to LayeredControl events
  eventService->subscribe(layeredControl,
			  layeredControl->OutputGenerated,
			  (EventService::Callback)MyTask::lcCallback);

  // Subscribe to Clock ticks
  eventService->subscribe(clock,
			  clock->TickEvent,
			  (EventService::Callback)MyTask::tickCallback);

  while (True) {
    eventService->processEvent();
    printf("processed event\n");
  }
}



void MyTask::lcCallback(TaskInterface *client, EventCode eventCode)
{
  LayeredControlIF::OutputCommand command;


  layeredControl->outputCommand(&command);

  printf("Depth mode: %d\n", command.depthMode);
  printf("Depth: %.3f\n", command.depth);
  printf("Heading mode: %d\n", command.headingMode);
  printf("Heading: %.3f\n", command.heading);
  printf("Speed mode: %d\n", command.speedMode);
  printf("Speed: %.3f\n", command.speed);
}



void MyTask::tickCallback(TaskInterface *client, EventCode eventCode)
{
  printf("Clock ticked!\n");
}


int main(int argc, char **argv)
{
  MyTask *myTask = new MyTask("myTask");
  myTask->run();

  return 0;
}


void MyTask::execServer()
{
  // Don't have a server
  exit(0);
}
