/****************************************************************************/
/* Copyright (c) 2007 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : StatePublisher.h                                              */
/* Author   :                                                               */
/* Project  : Onboard Deliberative Autonomy                                 */
/* Version  : 1.0                                                           */
/* Created  : 09/17/2007                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _StatePublisher_H
#define _StatePublisher_H

#include "PeriodicTask.h"
#include "Attributes.h"
#include "TailConeIF.h"
#include "NavigationIF.h"
#include "BluefinBatteryIF.h"
#include "HydroscatIF.h"
#include "IsusIF.h"
#include "CtdIF.h"
#include "TimeIF.h"
#include "ClusterIF.h"
#include "SmartSamplerIF.h"

#include "StatePacket.h"
#include "StatePacketSender.h"

#include <time.h>

#define DEFAULT_IP "localhost"
#define DEFAULT_PERIOD 1000
#define DEFAULT_PORT 8001

#define DEFAULT_CLUSTER_NAME "plume"
// keep it with the pas name for simplification purpose
#define VcsMessageQueueName "VcsMessages"

/*
CLASS 
StatePublisher

DESCRIPTION
Gather data representing the state of the vehicle, and broadcast data
over socket. We'll implement the publisher as a device driver in order
to use common idioms and framework components we're all familiar with.

*/

#define DEFAULT_CONFIG "statePublisher.cfg"

class VcsMessage;

class StatePublisher : public PeriodicTask {

public:
  
  /////////////////////////////////////////////////////////
  // overloaded Constructor
  StatePublisher(char* configfile = DEFAULT_CONFIG);

  ///////////////////////////////////////////////////////
  //destructor
  virtual  ~StatePublisher();
  int init();
  void setVerbose(const Boolean b) { _verbose = b; }


protected:

  /** Buffer for outgoing data; XDR byte-count (first 2 bytes, 
      network order) followed by XDR state data. */
  char *_outBuf;

  /** Points to out-going state data in XDR format */
  char *_xdrStateBuf;

  /** XDR memory stream */
  XDR _xdrStream;
  double _startTime;
  static double getTime();

  Boolean _verbose;
  int     _amcSocket;

  long   _samplePeriod;
  long   _amcPort;
  char*  _amcIP;
  char*  _config;
  struct sockaddr_in _servaddr;
  Attributes _attributes;

  StatePacket _statePacket;

  TailConeIF     *_tailConeIF;
  NavigationIF   *_navigationIF;
  BluefinBatteryIF *_batteryIF;
  HydroscatIF *_hydroscatIF;
  IsusIF      *_isusIF;

  SmartSamplerIF  *_smartSampler;
  
  char *_ctdServerName;
  CtdIF *_ctdIF;
  
  char *_clusterName;
  ClusterIF *_clusterIF;

  VcsMessage *_msgs;

  StatePacketSender *_statePacketSender;

  //////////////////////////////////////////////////////////////// 
  // initialization function - create interfaces to the servers
  // we're going to need, create the socket, etc.
  //
  int  setupSocket();
  void createCfgAttributes();
  void loadConfigFile(const char* file);
  void reportCfgAttributes();
  
  ////////////////////////////////////////////////////////////////////
  // Here's our main working funtion that will be called periodically
  //
  virtual void callback();

  /** Debug method to print packet contents */
  void printPacket(StatePacket &packet);
  Boolean handleLCMessages();
  void ackAmc(int32_t id, char type);
};

#endif
