//------------------------------------------------------------------------------
// <copyright file="TelemetryPublisherKey.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 TelemetryPublisherKey, a serializable entity class that
//   represents the key used for telemetry publishers (i.e. processes that send
//   telemetry)
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#include "TelemetryPublisherKey.h"
#include "nsextend.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;
using namespace LiquidRobotics::SMACFramework::XmlDOM;

namespace LiquidRobotics {
namespace PayloadInterface {

TelemetryPublisherKey::TelemetryPublisherKey()
: boardID(0)
, taskID(0)
{
}

TelemetryPublisherKey::TelemetryPublisherKey(BoardID _boardID, TaskID _taskID)
: boardID(_boardID)
, taskID(_taskID)
{

}

TelemetryPublisherKey::TelemetryPublisherKey(BoardAddress boardAddress)
: boardID(::LiquidRobotics::PayloadInterface::GetBoardID(boardAddress))
, taskID(::LiquidRobotics::PayloadInterface::GetTaskID(boardAddress))
{

}

int TelemetryPublisherKey::Compare(const TelemetryPublisherKey& v1, const TelemetryPublisherKey& v2)
{
	int result;

	if (v1.boardID < v2.boardID)
		result = -1;
	else if (v1.boardID > v2.boardID)
		result = 1;
	else if (v1.taskID < v2.taskID)
		result = -1;
	else if (v1.taskID > v2.taskID)
		result = 1;
	else
		result = 0;

	return result;
}

bool TelemetryPublisherKey::operator==(const TelemetryPublisherKey& other) const
{
	return !Compare(*this, other);
}

bool TelemetryPublisherKey::operator<(const TelemetryPublisherKey& other) const
{
	return Compare(*this, other) < 0;
}

bool TelemetryPublisherKey::operator>(const TelemetryPublisherKey& other) const
{
	return Compare(*this, other) > 0;
}

const TelemetryPublisherKey::BoardAddress TelemetryPublisherKey::GetBoardAddress() const
{
	return MakeBoardAddress(boardID, taskID);
}

const TelemetryPublisherKey::BoardID TelemetryPublisherKey::GetBoardID() const
{
	return boardID;
}

const TelemetryPublisherKey::TaskID TelemetryPublisherKey::GetTaskID() const
{
	return taskID;
}

void TelemetryPublisherKey::SetBoardAddress(BoardAddress newValue)
{
	boardID = ::LiquidRobotics::PayloadInterface::GetBoardID(newValue);
	taskID = ::LiquidRobotics::PayloadInterface::GetTaskID(newValue);
}

void TelemetryPublisherKey::SetBoardID(BoardID newValue)
{
	boardID = newValue;
}

void TelemetryPublisherKey::SetTaskID(TaskID newValue)
{
	taskID = newValue;
}


void TelemetryPublisherKey::Load(XmlNode& node, const XmlNVPBase& nvp)
{
	LoadFromNode(node, XmlNVPBase(nvp.Name(), "boardID", XmlNVPBase::HEX), boardID);
	LoadFromNode(node, XmlNVPBase(nvp.Name(), "taskID", XmlNVPBase::HEX), taskID);
}

void TelemetryPublisherKey::Save(XmlNode& node, const XmlNVPBase& nvp) const
{
	SaveToNode(node, XmlNVPBase(nvp.Name(), "boardID", XmlNVPBase::HEX), boardID);
	SaveToNode(node, XmlNVPBase(nvp.Name(), "taskID", XmlNVPBase::HEX), taskID);
}

std::ostream& operator<<(std::ostream& stm, const TelemetryPublisherKey& key)
{
	stm << "0x" << ultos(MakeBoardAddress(key.GetBoardID(), key.GetTaskID()), 16, 2*sizeof(TelemetryPublisherKey::BoardAddress));
	return stm;
}

}
}
