//----------------------------------------------------------------------------------
// <copyright file="SensorProgram.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>
//   Declarations for SensorProgram, a basic implementation of an ICommandable
//	 service with events and stubs for ISensor; use with CORBA tie classes.  Note
//   that GPS and shutdown CORBA events are handled if the corresponding event names
//   are defined; there is also support for firing command completion events.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#ifndef __EventLib_SensorProgram_h__
#define __EventLib_SensorProgram_h__

#include "EventTransceiver.h"
#include "LqrSMACIdl.h"
#include "LqrSMACLockableConcept.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACSignalSubscriber.h"
#include "PayloadManagerClient.h"
#include "SensorProgramSettings.h"

namespace LiquidRobotics {
namespace EventLib  {

class GPSSampleEvent;

class SHARED_LIB_PUBLIC SensorProgram
: public LiquidRobotics::EventLib::EventTransceiver
, public LiquidRobotics::SMACFramework::ICommandableBase
{
public:

	LOCKABLE_CONCEPT_TYPEDEFS();

	typedef LiquidRobotics::SMACFramework::PayloadDescription Desc;
	typedef LiquidRobotics::EventLib::PayloadManagerClient PayloadManagerClient;

	SensorProgram(IObjectBroker& _broker, const SensorProgramSettings& _settings, Logger& _logger);
	virtual ~SensorProgram();

	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();

protected:

	typedef boost::signals2::scoped_connection ScopedConnection;
	typedef boost::shared_ptr<ScopedConnection> ScopedConnectionPtr;
	typedef boost::signals2::signal<void (const GPSSampleEvent& sample)> GPSSampleEventSignalType;
	typedef boost::signals2::signal<void ()> ShutdownSignalType;
	typedef LiquidRobotics::SMACFramework::SignalSubscriber<GPSSampleEventSignalType> GPSSignalSubscriber;
	typedef boost::shared_ptr<GPSSignalSubscriber> GPSSignalSubscriberPtr;
	typedef LiquidRobotics::SMACFramework::SignalSubscriber<ShutdownSignalType> ShutdownSignalSubscriber;
	typedef boost::shared_ptr<ShutdownSignalSubscriber> ShutdownSignalSubscriberPtr;

	virtual void FireCommandCompleteEvent(const string& result, const string& requester, const string& transactionID, bool complete = true, bool success = true);
	virtual void GPSSampleHandler(EventTransceiver& sender, const IEventBase& receivedEvent);
	virtual void GPSSampleHandlerAsSample(EventTransceiver& sender, const GPSSampleEvent& receivedEvent);
	virtual void ShutdownHandler(EventTransceiver& sender, const IEventBase& receivedEvent);

	IObjectBroker& broker;
	const SensorProgramSettings& settings;
	Logger& logger;
	PayloadManagerClient::Ptr pmClient;
	LockableConceptMutex firingMutex;
	GPSSampleEventSignalType gpsSampleEventSignal;
	ShutdownSignalType shutdownSignal;
	GPSSignalSubscriberPtr gpsSignalSubscriber;
	ShutdownSignalSubscriberPtr shutdownSignalSubscriber;
	ScopedConnectionPtr connectionToGPSEvent;
	ScopedConnectionPtr connectionToShutdownEvent;
};

template<typename TController, typename TSettings>
class TSensorProgram : public SensorProgram
{
public:

	typedef SensorProgram Base;

	TSensorProgram(IObjectBroker& _broker, const TSettings& _settings, Logger& _logger)
	: Base(_broker, _settings.GetProgramSettings(), _logger)
	{
	}
};


}
}



#endif
