//----------------------------------------------------------------------------------
// <copyright file="SSQStopCmd.cpp" 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>
// 	Implementation of SSQStopCmd, a queue-able item that executes the Stop method of
//  the controller.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#include "SSQStopCmd.h"
//#include "MicrostrainControllerPersistSettings.h"
#include "LqrSMACCmdLineParser.h"
#include "LqrSMACBooleanParser.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace SimpleSBDQueue {

SSQStopCmd::SSQStopCmd(TCon& _ctrl, const string& _cmd, Logger& _logger)
: SSQConcreteCmd(_ctrl)
, ctrl(_ctrl)
, cmd(_cmd)
, logger(_logger)
{
}

SSQStopCmd::~SSQStopCmd()
{
}

// this method is called by the background thread to execute the command
void SSQStopCmd::Execute()
{
	SignalStarted();

	try
	{
        ctrl.Stop();
	}
	catch(exception& ex)
	{
		stringstream msg;
		msg << "Caught exception while stopping: " << typeid(ex).name() << " '" << ex.what() << "'";

		TEx::NotReady notReady("");
		const int exNum = TEx::ExceptionToNumber(notReady);
		SetException(exNum, msg.str());
	}
}

// parse the command
SSQStopCmd::CmdPtr SSQStopCmd::Parse(const string& arg1, const string& arg2, TCon& ctrl, const CmdLineParser& parser, ParsingContext& context, Logger& logger)
{
	CmdPtr result;
	const CmdLineParser::StringArray& tokens = parser.GetTokens();

	if ((tokens.size() >= 2)
	 && boost::iequals(tokens[0], arg1)
	 && boost::iequals(tokens[1], arg2))
	{
		if (tokens.size() > 2)
			throw invalid_argument("Too many arguments");

		result.reset(new SSQStopCmd(ctrl, parser.GetOriginalCmdLine(), logger));
	}

	return result;
}

}
}
