Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Manager.h"
  22.  
  23. #include <boost/lexical_cast.hpp>
  24.  
  25. #include "broker/Manager.h"
  26. #include "broker/Message.h"
  27. #include "Message.h"
  28. #include <type/common.h>
  29.  
  30. namespace atom {
  31. namespace can {
  32.  
  33.    
  34. Manager::Pointer Manager::instance_ = Manager::Pointer(new Manager());
  35.  
  36. Manager::Manager() : broker::Subscriber(false), LOG("can::Manager")
  37. {
  38.     // TODO: Set offline timer
  39. }
  40.  
  41. Manager::~Manager()
  42. {
  43. }
  44.  
  45. Manager::Pointer Manager::Instance()
  46. {
  47.     return Manager::instance_;
  48. }
  49.  
  50. void Manager::Delete()
  51. {
  52.     Manager::instance_.reset();
  53. }
  54.  
  55. void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
  56. {
  57.     if (message->GetType() == broker::Message::CAN_MESSAGE)
  58.     {
  59.         Message* payload = static_cast<Message*>(message->GetPayload().get());
  60.         Node::Pointer node;
  61.        
  62.         if (payload->GetClassName() == "nmt")
  63.         {
  64.             if (payload->GetCommandName() == "Bios_Start")
  65.             {
  66.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  67.                 this->RemoveModules(node->GetId());
  68.                 node->SetState(Node::STATE_ONLINE);
  69.                 LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
  70.             }
  71.             else if (payload->GetCommandName() == "Reset")
  72.             {
  73.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  74.                 this->RemoveModules(node->GetId());
  75.                 node->SetState(Node::STATE_OFFLINE);
  76.                 LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
  77.             }
  78.             else if (payload->GetCommandName() == "App_start")
  79.             {
  80.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  81.                 this->RequestModules(node->GetId());
  82.                 node->SetState(Node::STATE_PENDING);
  83.                 LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
  84.             }
  85.             else if (payload->GetCommandName() == "Heartbeat")
  86.             {
  87.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  88.                
  89.                 if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
  90.                 {
  91.                     this->RequestModules(node->GetId());
  92.                     node->SetState(Node::STATE_PENDING);
  93.                     LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
  94.                 }
  95.                 else if (node->GetState() == Node::STATE_INITIALIZED)
  96.                 {
  97.                     if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
  98.                     {
  99.                         this->RequestModules(node->GetId());
  100.                         node->SetState(Node::STATE_PENDING);
  101.                         LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.         else
  107.         {
  108.             if (payload->GetCommandName() == "List" && payload->GetDirectionName() == "From_Owner")
  109.             {
  110.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  111.                
  112.                 Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
  113.                 module->SetNodeId(node->GetId());
  114.                
  115.                 if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
  116.                 {
  117.                     node->SetState(Node::STATE_INITIALIZED);
  118.                 }
  119.                
  120.                 LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
  121.                 // TODO: Notify that module is available
  122.             }
  123.         }
  124.     }
  125. }
  126.  
  127. Node::Pointer Manager::GetNode(Node::Id node_id)
  128. {
  129.     Node::Pointer node;
  130.     NodeList::iterator it = this->nodes_.find(node_id);
  131.    
  132.     if (it == this->nodes_.end())
  133.     {
  134.         node = Node::Pointer(new Node(node_id));
  135.         this->nodes_[node_id] = node;
  136.     }
  137.     else
  138.     {
  139.         node = it->second;
  140.     }
  141.    
  142.     return node;
  143. }
  144.  
  145. Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
  146. {
  147.     Module::Pointer module;
  148.     ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
  149.    
  150.     if (it == this->modules_.end())
  151.     {
  152.         module = Module::Pointer(new Module(module_id, module_name, class_name));
  153.         this->modules_[module->GetFullId()] = module;
  154.     }
  155.     else
  156.     {
  157.         module = it->second;
  158.     }
  159.    
  160.     return module;
  161. }
  162.  
  163. void Manager::RemoveModules(Node::Id node_id)
  164. {
  165.     for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
  166.     {
  167.         if (it->second->GetNodeId() == node_id)
  168.         {
  169.             LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
  170.             // TODO: Notify that module is unavailable
  171.            
  172.             this->modules_.erase(it);
  173.         }
  174.     }
  175. }
  176.  
  177. void Manager::RequestModules(Node::Id node_id)
  178. {
  179.     this->RemoveModules(node_id);
  180.    
  181.     Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
  182.     payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
  183.    
  184.     broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
  185.    
  186.     // TODO: Set timeout if no response
  187. }
  188.  
  189. unsigned int Manager::GetNumberOfModules(Node::Id node_id)
  190. {
  191.     unsigned int number_of_modules = 0;
  192.    
  193.     for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
  194.     {
  195.         if (it->second->GetNodeId() == node_id)
  196.         {
  197.             number_of_modules++;
  198.         }
  199.     }
  200.    
  201.     return number_of_modules;
  202. }
  203.  
  204.  
  205. }; // namespace can
  206. }; // namespace atom
  207.