/*
*/

#include <VibSensorDesc.h>
#include <string>
#include <list>
#include <VibChannelDesc.h>


static const std::string RTSMONERROR("error");
static const std::string RTSMONNAME("name");
static const std::string RTSMONUINT("UINT32");
static const std::string RTSMONINT("INT32");
static const std::string RTSMONFLOAT("FLOAT");
static const std::string RTSMONDOUBLE("DOUBLE");


VibSensorDesc::VibSensorDesc(const std::string& name){
   //Init Vibrationsensor
   this->name = name;
   //u_int32_t id, const std::string& name, VALUEFORMAT valueFormat, u_int32_t vByteSpace = 1)
   std::shared_ptr<IChannelDesc> channelDesc = std::make_shared<VibChannelDesc>(1, "vibration1", VALUEFORMAT::UINT32, 1);
   this->mDescriptions.push_back(channelDesc);
   channelDesc = std::make_shared<VibChannelDesc>(2, "vibration2", VALUEFORMAT::UINT32, 1);
   this->mDescriptions.push_back(channelDesc);
   channelDesc = std::make_shared<VibChannelDesc>(3, "vibration3", VALUEFORMAT::UINT32, 1);
   this->mDescriptions.push_back(channelDesc);   
}

int VibSensorDesc::GetDescription(){
   return 0;
}


bool VibSensorDesc::AddHW2Json(json& j){
   json jsensor = json::object();
   jsensor[RTSMONNAME] = this->name;
     
   if (this->mDescriptions.empty()) {
      printf("CA7 : Error empty channel description name:%s.\n", this->name.c_str());               
      j[RTSMONERROR] = "Error empty channel description";
      return false;
   }
      
   json jchannels = json::array();

   for (auto &&chanDesc : this->mDescriptions)
   {
      json jchannel = json::object();
      jchannel[RTSMONCHANNELNAME] = chanDesc->GetName();
      
      switch (chanDesc->GetValueFormat())
      {
      case VALUEFORMAT::UINT32:
         jchannel[RTSMONCHANNELVALUETYPE] = RTSMONUINT;
         break;

      case VALUEFORMAT::INT32:
         jchannel[RTSMONCHANNELVALUETYPE] = RTSMONINT;
         break;

      case VALUEFORMAT::FLOAT:
         jchannel[RTSMONCHANNELVALUETYPE] = RTSMONFLOAT;
         break;

      case VALUEFORMAT::DOUBLE:
         jchannel[RTSMONCHANNELVALUETYPE] = RTSMONDOUBLE;
         break;            
      default:
         break;
      }
      jchannels.push_back(jchannel);
   }

   jsensor[RTSMONCHANNELS] = jchannels;
   j.push_back(jsensor);
   return true;
}

u_int32_t VibSensorDesc::AddValues2Json(json& j, u_int32_t* values, u_int32_t actno, u_int32_t length){
   //sensor Object
   json jsensor = json::object();

   u_int32_t result = actno;
   if (actno <= length) {
      //iterate over the channels
      
      rtsSensorValue value;
      if (this->mDescriptions.empty()) {
         printf("CA7 : Error empty channel description - actno[%d].\n", actno);               
         j[RTSMONERROR] = "Error empty channel description";
         return length;
      }
      
      //json jchannels = json::array();

      for (auto &&chanDesc : this->mDescriptions)
      {
         value.vUINT32 = values[result];
         result = result +1;
         //json jchannel = json::object();

         switch (chanDesc->GetValueFormat())
         {
         case VALUEFORMAT::UINT32:
            //j[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vUINT32);
            j.push_back(json::number_unsigned_t(value.vUINT32));
            break;

         case VALUEFORMAT::INT32:
            //j[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vINT32);
            j.push_back(json::number_unsigned_t(value.vINT32));            
            break;

         case VALUEFORMAT::FLOAT:
            //j[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vFLOAT);
            j.push_back(json::number_unsigned_t(value.vFLOAT));            
            break;

         case VALUEFORMAT::DOUBLE:
            //j[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vDOUBLE);
            j.push_back(json::number_unsigned_t(value.vDOUBLE));
            break;            
         default:
            break;
         }
         //jchannels.push_back(jchannel);
      }

      //jsensor[RTSMONCHANNELS] = jchannels;
      //j.push_back(jsensor);
   }
   //printf("CA7 : add2json-1 return actno %d.\n", actno);                  
   return result;      
}


/*
   json jsensor = json::object();
   jsensor[RTSMONNAME] = this->name;

   u_int32_t result = actno;
   if (actno <= length) {
      //iterate over the channels
      
      rtsSensorValue value;
      if (this->mDescriptions.empty()) {
         printf("CA7 : Error empty channel description - actno[%d].\n", actno);               
         j[RTSMONERROR] = "Error empty channel description";
         return length;
      }
      
      json jchannels = json::array();

      for (auto &&chanDesc : this->mDescriptions)
      {
         value.vUINT32 = values[result];
         result = result +1;
         //printf("CA7 : add2json-1 actno %d.\n", actno);               
         json jchannel = json::object();

         switch (chanDesc->GetValueFormat())
         {
         case VALUEFORMAT::UINT32:
            // j[chanDesc->GetName()] = json::number_unsigned_t(value.vUINT32);
            jchannel[RTSMONCHANNELNAME] = chanDesc->GetName();
            jchannel[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vUINT32);
            break;

         case VALUEFORMAT::INT32:
            //j[chanDesc->GetName()] = json::number_integer_t(value.vINT32);
            jchannel[RTSMONCHANNELNAME] = chanDesc->GetName();
            jchannel[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vINT32);
            break;

         case VALUEFORMAT::FLOAT:
            //j[chanDesc->GetName()] = json::number_float_t(value.vFLOAT);
            jchannel[RTSMONCHANNELNAME] = chanDesc->GetName();
            jchannel[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vFLOAT);
            break;

         case VALUEFORMAT::DOUBLE:
            //j[chanDesc->GetName()] = json::number_float_t(value.vDOUBLE);
            jchannel[RTSMONCHANNELNAME] = chanDesc->GetName();
            jchannel[RTSMONCHANNELVALUE] = json::number_unsigned_t(value.vDOUBLE);
            break;            
         default:
            break;
         }
         jchannels.push_back(jchannel);
      }

      jsensor[RTSMONCHANNELS] = jchannels;
      j.push_back(jsensor);
   }
   //printf("CA7 : add2json-1 return actno %d.\n", actno);                  
   return result;      

*/