//--------------------------------------------------------------------------------
// <copyright file="SV3NetChannel.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 SV3NetChannel, which implements a web services interface client
//  for a single sensor.
// </summary>
//
// <owner>Mike Cookson</owner>
//--------------------------------------------------------------------------------

#ifndef __PAYLOADINTERFACELIB_SV3NetChannel_H__
#define __PAYLOADINTERFACELIB_SV3NetChannel_H__

#include "LqrSMACByteArray.h"
#include "LqrSMACLogger.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACLockableConcept.h"
#include "LqrSMACLogger.h"
#include "LqrSMACSemaphore.h"
#include "LqrSMACScopedConnection.h"
#include "LqrSMACSignalSubscriber.h"
#include "LqrSMACWaitableThreadSafeQueue.h"
#include "RegulusREST.h"
#include <stdint.h>
#include <string>
#include <boost/thread.hpp>
#include <boost/utility.hpp>

namespace LiquidRobotics {
namespace PayloadInterface {

class SV3NetInterfaceSettings;

class SHARED_LIB_PUBLIC SV3NetChannel : public boost::noncopyable
{
public:

	LOCKABLE_CONCEPT_TYPEDEFS();
	SCOPED_CONNECTION_TYPEDEFS();
	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::SV3RemotingLib::RegulusREST RegulusREST;
	typedef LiquidRobotics::SMACFramework::Semaphore Semaphore;
	typedef std::string string;
	typedef boost::shared_ptr<SV3NetChannel> Ptr;
	typedef std::vector<Ptr> PtrArray;

	SV3NetChannel(uint16_t _boardAddress, const string& _serverName, const string& _description, const string& _descriptionJSON,
			const SV3NetInterfaceSettings& _settings, Logger& _logger);
	virtual ~SV3NetChannel();

	void Initialize();
	void Shutdown();

	uint16_t GetBoardAddress() const;
	const string& GetServerName() const;
	const string& GetDescription() const;
	const string& GetDescriptionJSON() const;
	bool GetRegistered() const;
	void SetRegistered(bool newStatus);
	static string GetHostID();
	static string IDForBoardID(uint16_t boardAddress);

	void DelayShutdown(long seconds);
	void InstallJAR(const string& fileName);
	string InvokeOnServer(const string& command);
	void NoteCommandOutput(const string& transactionReference, const string& outputData, bool commandComplete = true, bool succeeded = true);
	static void PollStatusFireEvents(PtrArray& pollList, bool requestGPS = false, bool requestShutdownNotification = false, long maxCommands = 1, long timeoutMS = 15000);
	void RegisterPayload(bool last, bool purge = false);
	void SendFile(const string& fileName, const string& remoteName, bool legacy = false);
	void SendOutputMessage(const ByteArray& messageContent);
	void SetPayloadAMPSSwitch(bool on = true);
	void SetPayloadAMPSSwitch(const string& switchName, bool on = true);
	void TurnPayloadOff();

	typedef boost::signals2::signal<void (SV3NetChannel& sender, time_t timestamp, double latitude, double longitude)> GPSSignal;
	typedef boost::signals2::signal<void (SV3NetChannel& sender, uint16_t boardAddress, const string& transactionReference, bool returnFile, const string& cmdLine)> RemoteCommandSignal;
	typedef boost::signals2::signal<void (SV3NetChannel& sender)> ShutdownSignal;
private:
	LockableConceptMutex firingMutex;
	GPSSignal gpsSignal;
	RemoteCommandSignal remoteCommandSignal;
	ShutdownSignal shutdownSignal;
public:
	LiquidRobotics::SMACFramework::SignalSubscriber<GPSSignal> gpsSignalSubscriber;
	LiquidRobotics::SMACFramework::SignalSubscriber<RemoteCommandSignal> remoteCommandSubscriber;
	LiquidRobotics::SMACFramework::SignalSubscriber<ShutdownSignal> shutdownSignalSubscriber;

private:

	typedef boost::thread Thread;
	typedef boost::shared_ptr<Thread> ThreadPtr;

	string GetHostAddress();

	LockableConceptMutex mutex;
	Semaphore shutdownSem;
	const uint16_t boardAddress;
	const string serverName;
	const string description;
	const string descriptionJSON;
	const SV3NetInterfaceSettings& settings;
	Logger& logger;
	bool isRegistered;
	string cachedHostAddress;
	double cacheTime;
	ThreadPtr commandListenerThread;
	ThreadPtr queueHandlerThread;
};

}
}


#endif
