//--------------------------------------------------------------------------------
// <copyright file="TelemetrySubsystem.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>
// 	Declaration of TelemetrySubsystem, which factors out the telemetry composition
//  objects for PayloadInterfaceController and SV3RemoteController
// </summary>
//
// <owner>Mike Cookson</owner>
//--------------------------------------------------------------------------------

#ifndef TELEMETRYSUBSYSTEM_H_
#define TELEMETRYSUBSYSTEM_H_

#include <string>
#include "EventTransceiver.h"
#include "LqrSMACScopedConnection.h"
#include "TelemetryBuffer.h"

namespace LiquidRobotics {
namespace PayloadInterface {

class PayloadInterfaceController_Config;

class SHARED_LIB_PUBLIC TelemetrySubsystem
{
public:

	TelemetrySubsystem(
			const std::string& _telemConfigPath,
			LiquidRobotics::EventLib::EventTransceiver& _evtTrans,
			LiquidRobotics::SMACFramework::Logger& _logger,
			const PayloadInterfaceController_Config& _config,
			LiquidRobotics::SMACFramework::IObjectBroker& _broker,
			LiquidRobotics::EventLib::PayloadManagerClient& _pmClient);

	void Initialize(bool startTimeoutThread = false);
	void Shutdown();

	void CheckForTelemetryPublicationTimeout();
	void SavePosition(double latitude, double longitude);

protected:

	virtual void EventToTelemetry(LiquidRobotics::EventLib::EventTransceiver& sender, const LiquidRobotics::EventLib::IEventBase& receivedEvent);
	virtual bool IsSpecialPurposeEvent(const LiquidRobotics::EventLib::EventSettings& evtSettings) const;
	virtual void TelemetryChangedHandler(LiquidRobotics::EventLib::EventTransceiver& sender, const LiquidRobotics::EventLib::IEventBase& receivedEvent);
	virtual void TelemetryReportComplete(TelemetryBuffer& sender, TelemetryReportKey reportKey,
			TelemetryBuffer::BoardAddress targetAddress, const LiquidRobotics::SMACFramework::ByteArray& content) = 0;

private:

	const std::string telemConfigPath;
	LiquidRobotics::EventLib::EventTransceiver& evtTrans;
	LiquidRobotics::SMACFramework::Logger& logger;
	const PayloadInterfaceController_Config& config;
	LiquidRobotics::SMACFramework::IObjectBroker& broker;
	LiquidRobotics::EventLib::PayloadManagerClient& pmClient;
	TelemetryBuffer::Config::Ptr telemConfigPtr;
	TelemetryBuffer::Ptr telemBufPtr;
	LiquidRobotics::SMACFramework::ScopedConnectionPtr connectionToEventReceive;
	LiquidRobotics::SMACFramework::ScopedConnectionPtr connectionToTelemetryChanged;
	LiquidRobotics::SMACFramework::ScopedConnectionPtr connectionToTelemetryComplete;
};

}
}

#endif /* TELEMETRYSUBSYSTEM_H_ */
