/*
  sensor.cpp

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  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.
*/

#include "sensor.h"
#include "rdm.h"

namespace RDM {

/**
 * @brief Sensor::Sensor
 */
Sensor::Sensor()
  : type(SENS_OTHER)
  , unit(UNITS_NONE)
  , unitPrefix(PREFIX_NONE)
  , minLimit(-32768)
  , maxLimit(32767)
  , minNominal(-32768)
  , maxNominal(32767)
  , recordedValue(true)
  , minMaxDetectedValue(true)
  , reserved(0)
  , description("Null Sensor")
  , min_(32767)
  , max_(-32768)
  , memory_(0)
  , value_(0)
{
}


/**
 * @brief Sensor::setValue
 * @param val
 */
void Sensor::setValue(int16_t &val)
{
  if (val < min_)
    min_ = val;
  if (val > max_)
    max_ = val;
  value_ = val;
}


/**
 * @brief Sensor::save
 */
void Sensor::save()
{
  memory_ = value_;
}


/**
 * @brief Sensor::minimum
 * @return
 */
int16_t Sensor::minimum() const
{
  if (!minMaxDetectedValue)
    return 0;
  return min_;
}


/**
 * @brief Sensor::maximum
 * @return
 */
int16_t Sensor::maximum() const
{
  if (!minMaxDetectedValue)
    return 0;
  return max_;
}


/**
 * @brief Sensor::value
 * @return
 */
int16_t Sensor::value() const
{
  return value_;
}


/**
 * @brief Sensor::saved
 * @return
 */
int16_t Sensor::saved() const
{
  if (!recordedValue)
    return 0;
  return memory_;
}


/**
 * @brief Sensor::resetMinMax
 */
void Sensor::clearMemory()
{
  min_ = value_;
  max_ = value_;
  memory_ = value_;
}



/**
 * @brief Sensor::actionGetSensorDefinition
 * @param index
 * @param response
 */
void Sensor::actionGetSensorDefinition(uint8_t index, MsgPtr response)
{
  response->responseType = RESPONSE_TYPE_ACK;
  response->appendData(index);
  response->appendData(type);
  response->appendData(unit);
  response->appendData(unitPrefix);
  response->appendData(minLimit);
  response->appendData(maxLimit);
  response->appendData(minNominal);
  response->appendData(maxNominal);
  response->appendData(recordedValueSupport);
  for (size_t i = 0; i < description.size(); i++)
    {
      if (i > 32)
        break;
      response->appendData(description.at(i));
    }
}


/**
 * @brief Sensor::actionGetSensorValue
 * @param index
 * @param response
 */
void Sensor::actionGetSensorValue(uint8_t index, MsgPtr response)
{
  response->responseType = RESPONSE_TYPE_ACK;
  response->appendData(index);
  response->appendData(value());
  response->appendData(minimum());
  response->appendData(maximum());
  response->appendData(saved());
}


/**
 * @brief Sensor::actionSetSensorValue
 * @param index
 * @param response
 */
void Sensor::actionSetSensorValue(uint8_t index, MsgPtr response)
{
  clearMemory();

  if (response->length() != 0)
    return;

  int16_t val=0, min=0, max=0, mem=0;
  if (index != 0xff)
    {
      val = value();
      min = minimum();
      max = maximum();
      mem = saved();
    }

  response->responseType = RESPONSE_TYPE_ACK;
  response->appendData(index);
  response->appendData(val);
  response->appendData(min);
  response->appendData(max);
  response->appendData(mem);
}


/**
 * @brief Sensor::actionSetRecordSensors
 * @param response
 */
void Sensor::actionSetRecordSensors(MsgPtr response)
{
  save();
  response->responseType = RESPONSE_TYPE_ACK;
}


} // namespace RDM
