//------------------------------------------------------------------------------------------------------
// <copyright file="DataRecorderHandler.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. This file has the serial data receive and process.
// </summary>
//
// <owner>Raj Jha</owner>
//------------------------------------------------------------------------------------------------------

#include "EventTransceiver.h"
#include "LqrSMACSemaphore.h"
#include "LqrSMACTimerCollection.h"
#include "LqrSMACDeclareException.h"
#include "LqrSMACIObjectBroker.h"
#include "LqrSMACSerialPort.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACLogger.h"
#include "LqrSMACSerialListener.h"
#include "LqrSMACLineMachine.h"
#include "LqrSMACCmdLineParser.h"
#include <map>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include "DataRecorder.h"
#include "DataRecorderSettings.h"

#ifndef DATARECORDERHANDLER_H_
#define DATARECORDERHANDLER_H_

namespace LiquidRobotics {
namespace DataRecorder {

class DataRecorderHandler
{
public:

	typedef std::string string;
	typedef LiquidRobotics::EventLib::IEventBase IEventBase;
	typedef LiquidRobotics::SMACFramework::CmdLineParser CmdLineParser;
	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef LiquidRobotics::SMACFramework::ISerialPort ISerialPort;
	typedef boost::shared_ptr<ISerialPort> ISerialPortPtr;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::SMACFramework::PayloadDescription Desc;
	typedef boost::signals2::scoped_connection ScopedConnection;
	typedef boost::shared_ptr<ScopedConnection> ScopedConnectionPtr;
	typedef LiquidRobotics::SMACFramework::SerialListener SerialListener;
	typedef boost::shared_ptr<SerialListener> SerialListenerPtr;

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

	void ReceivedSerialData(SerialListener& sender, const ByteArray& receivedData);

	virtual void Initialize();

	virtual int PowerOn();
	virtual int PowerOff();
	virtual void Pause();
	virtual void Resume();
	virtual bool IsPaused();
	virtual bool SetRecordingFilename(const string& fname);
	virtual int Start();
	virtual int Stop();
	virtual string ChangePortSettings(const CmdLineParser::StringArray& tokens);

	void ProcessGPS(::CORBA::Long dateTimeUTC, ::CORBA::Double latitude, ::CORBA::Double longitude);

	virtual bool SerialStartup();
	virtual void SerialShutdown();
	virtual void Shutdown();

private:

	void WriteToFile(const ByteArray& receivedData);
	void WriteToFile_Format2(const ByteArray& receivedData);

    typedef boost::mutex Mutex;
    typedef boost::recursive_mutex RMutex;
    typedef boost::unique_lock<Mutex> Lock;
    typedef boost::unique_lock<RMutex> RLock;

    RMutex mtx_paused;					 // recursive mutex for generic state protection

	ISerialPortPtr serialPort;
	DataRecorderSettings settings;
	IObjectBroker& broker;
	Logger& logger;
	string storageFileName;
	bool initialized;
	bool recordingEnabled;
	SerialListenerPtr serialListener;
	ScopedConnectionPtr serialListenerConnection;

	long GPSdateTime;
	double lat, lon;

};

} // namespace LiquidRobotics
} // namespace DataRecorder

#endif /* DATARECORDERHANDLER_H_ */
