//------------------------------------------------------------------------------
// <copyright file="DataRecorder.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// 	Data Recorder from serial port to an output file
// </summary>
//
// <owner>Raj Jha</owner>
//------------------------------------------------------------------------------

#ifndef DATARECORDER_H_
#define DATARECORDER_H_

#include "IEventBase.h"
#include "EventTransceiver.h"
#include "GPSSampleEvent.h"
#include "LqrSMACLogger.h"
#include "LqrSMACIdl.h"
#include "LqrSMACIObjectBroker.h"
#include "LqrSMACSerialListener.h"
#include "DataRecorderHandler.h"
#include "DataRecorderSettings.h"
#include "PayloadManagerClient.h"

namespace LiquidRobotics {
namespace DataRecorder {

class DataRecorder
: public LiquidRobotics::EventLib::EventTransceiver
, public LiquidRobotics::SMACFramework::ICommandableBase
{
public:

	typedef LiquidRobotics::EventLib::IEventBase IEventBase;
	typedef LiquidRobotics::EventLib::EventTransceiver EventTransceiver;
	typedef LiquidRobotics::EventLib::EventType EventType;
	typedef LiquidRobotics::EventLib::GPSSampleEvent GPSSampleEvent;
	typedef LiquidRobotics::EventLib::PayloadManagerClient PayloadManagerClient;
	typedef EventTransceiver::Settings EventTransceiverSettings;
	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::SMACFramework::PayloadDescription Desc;
	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::SMACFramework::ISerialPort ISerialPort;
	typedef boost::shared_ptr<ISerialPort> ISerialPortPtr;
	typedef ISerialPort::TimeSpan TimeSpan;
	typedef LiquidRobotics::SMACFramework::SerialPortSettings SerialPortSettings;
	typedef LiquidRobotics::SMACFramework::SerialListenerSettings SerialListenerSettings;
	typedef LiquidRobotics::SMACFramework::SerialListener SerialListener;
	typedef boost::shared_ptr<SerialListener> SerialListenerPtr;
	typedef boost::signals2::scoped_connection ScopedConnection;
	typedef boost::shared_ptr<ScopedConnection> ScopedConnectionPtr;
	typedef boost::shared_ptr<DataRecorderHandler> DataRecorderHandlerPtr;


	DataRecorder(IObjectBroker& _broker, const DataRecorderSettings& _settings, Logger& _logger);
	virtual ~DataRecorder();

	virtual void Initialize(Object_ptr _pServer, PayloadManagerClient::Ptr _pmClient);

	// IPingable operations
    virtual ::CORBA::Double Ping();

    // IPayloadDevice
    virtual ::LiquidRobotics::SMAC::EnumerationData* GetEnumerationData();
    virtual void PowerOff(::LiquidRobotics::SMAC::PowerOffTimeType offDelay);
    virtual void PowerOn();
    virtual ::LiquidRobotics::SMAC::BoardIDType boardID();
    virtual ::LiquidRobotics::SMAC::PayloadDescription* description();
    virtual ::LiquidRobotics::SMAC::EPayloadEnableState enableState();
    virtual ::LiquidRobotics::SMAC::PowerOffTimeType requestOffTime();
    virtual ::LiquidRobotics::SMAC::PowerOnTimeType requestOnTime();
    virtual ::LiquidRobotics::SMAC::ComponentVersion* softwareVersion();
    virtual ::LiquidRobotics::SMAC::TaskIDType taskID();

    // ICommandable
    virtual void SendCommand(const ::CORBA::Any& command, const char* requester, const char* transactionID, ::CORBA::Any_OUT_arg result);

	// ISensor
    virtual ::LiquidRobotics::SMAC::SensorSample* SampleNow(const ::CORBA::Any& sampleParameter);
    virtual void SetSamplePending(const ::CORBA::Any& sampleParameter);
    virtual ::LiquidRobotics::SMAC::ComponentVersion* hardwareVersion();
    virtual ::LiquidRobotics::SMAC::SensorSampleIntervalType sensorSampleInterval();
    virtual void sensorSampleInterval(::LiquidRobotics::SMAC::SensorSampleIntervalType _v);
    virtual ::CORBA::Boolean samplePending();

	// IDataRecorder
	virtual void NoteGPSSample(::CORBA::Long dateTimeUTC, ::CORBA::Boolean validFix, ::CORBA::Double latitude, ::CORBA::Double longitude);

private:

	void SendCommand_Asynch(const string requester, const string transID, float seconds, int times);
	void GPSSampleHandler(EventTransceiver& sender, const IEventBase& receivedEvent);

	IObjectBroker& broker;
	Logger& logger;
	DataRecorderSettings settings;
	const string gpsEventName;
	EventType gpsEventType;
	ScopedConnectionPtr connectionToGPSEvent;
	double lat, lon;
	bool GPSvalid;
	PayloadManagerClient::Ptr pmClient;
	DataRecorderHandlerPtr dataHandler;
};

} // namespace LiquidRobotics
} // namespace DataRecorder

#endif /* DATARECORDER_H_ */
