Subversion Repositories HomeAutomation

Rev

Rev 1323 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * Header.cpp
  3.  *
  4.  *  Created on: Jul 23, 2009
  5.  *      Author: Mattias Runge
  6.  */
  7.  
  8. #include "Header.h"
  9.  
  10. #include "message/types/UnsignedInteger.hpp"
  11.  
  12. namespace atom {
  13. namespace message {
  14.  
  15. Header::Header()
  16. {
  17.     LOG.setName("Header");
  18.     this->loadVariables();
  19. }
  20.  
  21. Header::Header(BitBuffer & buffer)
  22. {
  23.     LOG.setName("Header");
  24.     this->loadVariables();
  25.     this->readBits(buffer);
  26. }
  27.  
  28. Header::Header(unsigned long moduleId, string moduleType, string messageType)
  29. {
  30.     LOG.setName("Header");
  31.  
  32.     LOG.debug("Start of constructor...");
  33.  
  34.     this->loadVariables();
  35.  
  36.     LOG.debug("Done loading variables...");
  37.  
  38.     if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get()))
  39.     {
  40.         moduleIdDatatype->setValue(moduleId);
  41.     }
  42.     else
  43.     {
  44.         throw new Exception("moduleId is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
  45.     }
  46.  
  47.     LOG.debug("checkpoint 1...");
  48.  
  49.     if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
  50.     {
  51.         unsigned long moduleTypeId = convert::string2uint(Protocol::getInstance()->getRootCacheNode().selectFirst("module", xml::Node::attributePair("name", moduleType))["id"]);
  52.  
  53.         moduleTypeDatatype->setValue(moduleTypeId);
  54.     }
  55.     else
  56.     {
  57.         throw new Exception("moduletype is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
  58.     }
  59.  
  60.     LOG.debug("checkpoint 2...");
  61.  
  62.     if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get()))
  63.     {
  64.         unsigned long messageTypeId = convert::string2uint(Protocol::getInstance()->getRootCacheNode().selectFirst("messagetype", xml::Node::attributePair("name", messageType))["id"]);
  65.  
  66.         messageTypeDatatype->setValue(messageTypeId);
  67.     }
  68.     else
  69.     {
  70.         throw new Exception("messageType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
  71.     }
  72.  
  73.     LOG.debug("End of contructor...");
  74. }
  75.  
  76. Header::~Header()
  77. {
  78. }
  79.  
  80. void Header::loadVariables()
  81. {
  82.     xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable");
  83.  
  84.     for (unsigned int n = 0; n < nodes.size(); n++)
  85.     {
  86.         Variable variable(nodes[n]);
  87.         this->myVariables[nodes[n]["name"]] = variable;
  88.     }
  89.  
  90.     // TODO check that moduleType, moduleId and messageId are present
  91. }
  92.  
  93. unsigned long Header::getModuleId()
  94. {
  95.     if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get()))
  96.     {
  97.         return moduleIdDatatype->getValue();
  98.     }
  99.  
  100.     throw new Exception("moduleIdDatatype is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
  101. }
  102.  
  103. string Header::getModuleType()
  104. {
  105.     if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
  106.     {
  107.         unsigned long moduleTypeId = moduleTypeDatatype->getValue();
  108.  
  109.         string moduleType = Protocol::getInstance()->getRootCacheNode().selectFirst("module", xml::Node::attributePair("id", convert::uint2string(moduleTypeId)))["name"];
  110.  
  111.         return moduleType;
  112.     }
  113.  
  114.     throw new Exception("moduleType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
  115. }
  116.  
  117. string Header::getMessageType()
  118. {
  119.     if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get()))
  120.     {
  121.         unsigned long messageTypeId = messageTypeDatatype->getValue();
  122.  
  123.         string messageType = Protocol::getInstance()->getRootCacheNode().selectFirst("messagetype", xml::Node::attributePair("id", convert::uint2string(messageTypeId)))["name"];
  124.  
  125.         return messageType;
  126.     }
  127.  
  128.     throw new Exception("messageType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
  129. }
  130.  
  131. void Header::readBits(BitBuffer & buffer)
  132. {
  133.     // Header is only 29 bits but our buffer is whole bytes, padded in front
  134.     buffer.incrementPointer(3); // Increment 3 bits
  135.  
  136.     for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
  137.     {
  138.         iter->second.readBits(buffer);
  139.     }
  140. }
  141.  
  142. void Header::writeBits(BitBuffer & buffer)
  143. {
  144.     // Header is only 29 bits but our buffer is whole bytes, padded in front
  145.     buffer.write(3, (long) 0); // Write 3 bits
  146.  
  147.     for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
  148.     {
  149.         iter->second.writeBits(buffer);
  150.     }
  151. }
  152.  
  153. string Header::toString()
  154. {
  155.     string buffer = "Header(";
  156.  
  157.     for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
  158.     {
  159.         buffer += iter->second.toString() + ",";
  160.     }
  161.  
  162.     trim_if(buffer, is_any_of(","));
  163.  
  164.     buffer += ")";
  165.  
  166.     return buffer;
  167. }
  168.  
  169. }
  170. }
  171.