Subversion Repositories HomeAutomation

Rev

Rev 1607 | Rev 1619 | 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 "type/common.h"
  28. #include "timer/Manager.h"
  29.  
  30. #include "Message.h"
  31.  
  32. namespace atom {
  33. namespace can {
  34.    
  35. Manager::Pointer Manager::instance_;
  36.  
  37. Manager::Manager() : broker::Subscriber(false), LOG("can::Manager")
  38. {
  39.     this->timer_id_ = timer::Manager::Instance()->Set(10000, true);
  40. }
  41.  
  42. Manager::~Manager()
  43. {
  44. }
  45.  
  46. Manager::Pointer Manager::Instance()
  47. {
  48.     return Manager::instance_;
  49. }
  50.  
  51. void Manager::Create()
  52. {
  53.     Manager::instance_ = Manager::Pointer(new Manager());
  54. }
  55.  
  56. void Manager::Delete()
  57. {
  58.     Manager::instance_.reset();
  59. }
  60.  
  61. void Manager::ConnectSlots(const SignalOnNodeChange::slot_type& signal_on_node_change_, const SignalOnModuleChange::slot_type& slot_on_module_change, const SignalOnModuleMessage::slot_type& slot_on_module_message)
  62. {
  63.     this->signal_on_node_change_.connect(signal_on_node_change_);
  64.     this->signal_on_module_change_.connect(slot_on_module_change);
  65.     this->signal_on_module_message_.connect(slot_on_module_message);
  66. }
  67.  
  68. void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
  69. {
  70.     if (timer_id != this->timer_id_)
  71.     {
  72.         return;
  73.     }
  74.    
  75.     for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
  76.     {
  77.         if (!it->second->CheckTimeout())
  78.         {
  79.             LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
  80.             this->RemoveModules(it->second->GetId());
  81.         }
  82.     }
  83. }
  84.  
  85. void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
  86. {
  87.     if (message->GetType() == broker::Message::CAN_MESSAGE)
  88.     {
  89.         //LOG.Debug("Message received");
  90.         Message* payload = static_cast<Message*>(message->GetPayload().get());
  91.         Node::Pointer node;
  92.        
  93.         if (payload->GetClassName() == "nmt")
  94.         {
  95.             if (payload->GetCommandName() == "Bios_Start")
  96.             {
  97.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  98.                 node->ResetTimeout();
  99.                
  100.                 this->RemoveModules(node->GetId());
  101.                 node->SetState(Node::STATE_ONLINE);
  102.                 LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
  103.             }
  104.             else if (payload->GetCommandName() == "Reset")
  105.             {
  106.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  107.  
  108.                 this->RemoveModules(node->GetId());
  109.                 node->SetState(Node::STATE_OFFLINE);
  110.                 LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
  111.             }
  112.             else if (payload->GetCommandName() == "App_Start")
  113.             {
  114.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  115.                 node->ResetTimeout();
  116.                
  117.                 this->RequestModules(node->GetId());
  118.                 node->SetState(Node::STATE_PENDING);
  119.                 LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
  120.             }
  121.             else if (payload->GetCommandName() == "Heartbeat")
  122.             {
  123.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  124.                 node->ResetTimeout();
  125.                
  126.                 if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
  127.                 {
  128.                     this->RequestModules(node->GetId());
  129.                     node->SetState(Node::STATE_PENDING);
  130.                     LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
  131.                 }
  132.                 else if (node->GetState() == Node::STATE_INITIALIZED)
  133.                 {
  134.                     if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
  135.                     {
  136.                         this->RequestModules(node->GetId());
  137.                         node->SetState(Node::STATE_PENDING);
  138.                         LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.         else if (payload->GetDirectionName() == "From_Owner")
  144.         {
  145.             Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
  146.            
  147.             if (payload->GetCommandName() == "List")
  148.             {
  149.                 node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
  150.                 node->ResetTimeout();
  151.                
  152.                
  153.                 module->SetNodeId(node->GetId());
  154.                
  155.                 if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
  156.                 {
  157.                     node->SetState(Node::STATE_INITIALIZED);
  158.                 }
  159.                
  160.                 LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
  161.                 this->signal_on_module_change_(module->GetFullId(), true);
  162.             }
  163.             else
  164.             {
  165.                 this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
  166.             }
  167.         }
  168.     }
  169. }
  170.  
  171. void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables)
  172. {
  173.     ModuleList::iterator it = this->modules_.find(full_id);
  174.    
  175.     if (it == this->modules_.end())
  176.     {
  177.         LOG.Warning("Can not send message to " + full_id + ", it is not available");
  178.         return;
  179.     }
  180.    
  181.     try
  182.     {
  183.         Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
  184.         payload->SetVariables(variables);
  185.        
  186.         broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
  187.         //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
  188.     }
  189.     catch (std::runtime_error& e)
  190.     {
  191.         LOG.Error("Failed to send message, " + std::string(e.what()));
  192.     }
  193. }
  194.  
  195. void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables)
  196. {
  197.     this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
  198. }
  199.  
  200. bool Manager::IsModuleAvailable(std::string full_id)
  201. {
  202.     return this->modules_.find(full_id) != this->modules_.end();
  203. }
  204.  
  205. Node::Pointer Manager::GetNode(Node::Id node_id)
  206. {
  207.     Node::Pointer node;
  208.     NodeList::iterator it = this->nodes_.find(node_id);
  209.    
  210.     if (it == this->nodes_.end())
  211.     {
  212.         node = Node::Pointer(new Node(node_id));
  213.         this->nodes_[node_id] = node;
  214.     }
  215.     else
  216.     {
  217.         node = it->second;
  218.     }
  219.    
  220.     return node;
  221. }
  222.  
  223. Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
  224. {
  225.     Module::Pointer module;
  226.     ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
  227.    
  228.     if (it == this->modules_.end())
  229.     {
  230.         module = Module::Pointer(new Module(module_id, module_name, class_name));
  231.         this->modules_[module->GetFullId()] = module;
  232.     }
  233.     else
  234.     {
  235.         module = it->second;
  236.     }
  237.    
  238.     return module;
  239. }
  240.  
  241. void Manager::RemoveModules(Node::Id node_id)
  242. {
  243.     for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
  244.     {
  245.         if (it->second->GetNodeId() == node_id)
  246.         {
  247.             LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
  248.             this->signal_on_module_change_(it->second->GetFullId(), false);
  249.            
  250.             this->modules_.erase(it);
  251.         }
  252.     }
  253. }
  254.  
  255. void Manager::RequestModules(Node::Id node_id)
  256. {
  257.     this->RemoveModules(node_id);
  258.    
  259.     Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
  260.     payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
  261.    
  262.     broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
  263. }
  264.  
  265. unsigned int Manager::GetNumberOfModules(Node::Id node_id)
  266. {
  267.     unsigned int number_of_modules = 0;
  268.    
  269.     for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
  270.     {
  271.         if (it->second->GetNodeId() == node_id)
  272.         {
  273.             number_of_modules++;
  274.         }
  275.     }
  276.    
  277.     return number_of_modules;
  278. }
  279.  
  280.  
  281. }; // namespace can
  282. }; // namespace atom
  283.