Subversion Repositories HomeAutomation

Rev

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

  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <boost/bind.hpp>
  9. #include <boost/program_options.hpp>
  10.  
  11. #include "net/Manager.h"
  12. #include "net/types.h"
  13.  
  14. #include "timer/Manager.h"
  15. #include "timer/types.h"
  16.  
  17. #include "config/Manager.h"
  18.  
  19. #include "logging/Logger.h"
  20.  
  21. #include "can/Network.h"
  22. #include "can/Monitor.h"
  23. #include "can/Protocol.h"
  24.  
  25. using namespace atom;
  26.  
  27. logging::Logger LOG("Main");
  28.  
  29. net::Manager::Pointer NM = net::Manager::Instance();
  30.  
  31. net::ServerId server_id = 0;
  32. timer::TimerId timer_id;
  33.  
  34. void Main_SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  35. {
  36.     std::cout << "client:" << client_id << " - New state: " << client_state << std::endl;
  37. }
  38.  
  39. void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
  40. {
  41.     char* t = new char[2048];
  42.    
  43.     memset(t, 0, sizeof(t));
  44.    
  45.     //LOG.Info("size:" + boost::lexical_cast<std::string>(data.GetSize()*3+2));
  46.    
  47.     for (unsigned int n = 0; n < data.GetSize(); n++)
  48.     {
  49.         unsigned char c = data[n];
  50.    
  51.         //LOG.Info(boost::lexical_cast<std::string>(n) + ":" + boost::lexical_cast<std::string>((unsigned int)c));
  52.        
  53.         //std::cout << std::dec << n << ":(dec)" << std::dec << (unsigned int)c << std::endl;
  54.         std::cout << std::dec << n << ":(hex)" << std::hex << (unsigned int)c << std::endl;
  55.        
  56.         sprintf(t + n*2, "%02X", c);
  57.     }
  58.    
  59.    
  60.     std::cout << std::dec << "client:" << client_id << " - New data: " << t << std::endl;
  61.    
  62.     delete [] t;
  63. }
  64.  
  65. void Main_SlotOnTimeout(timer::TimerId timer_id)
  66. {
  67.  /*   std::cout << "timer:" << timer_id << " - expired" << std::endl;
  68.    
  69.     std::string out_string = "Timeout!";
  70.    
  71.     type::Byteset out_data;
  72.    
  73.     strncpy(out_data.GetRaw(), out_string.data(), out_data.size());
  74.    
  75.    // NM->SendToAll(server_id, out_data);*/
  76. }
  77.  
  78. int main(int argc, char **argv)
  79. {
  80.     std::vector<broker::Subscriber::Pointer> subscribers;
  81.  
  82.     config::Manager::Pointer CM = config::Manager::Instance();
  83.    
  84.     if (!CM->Set(argc, argv))
  85.     {
  86.         NM->Delete();
  87.         return 0;
  88.     }
  89.    
  90.     if (CM->Exist("ProtocolFile"))
  91.     {
  92.         LOG.Info("ProtocolFile: " + CM->GetAsString("ProtocolFile"));
  93.        
  94.         can::Protocol::Instance()->Load(CM->GetAsString("ProtocolFile"));
  95.     }
  96.    
  97.     if (CM->Exist("MonitorPort"))
  98.     {
  99.         LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(CM->GetAsInt("MonitorPort")));
  100.        
  101.         subscribers.push_back(can::Monitor::Pointer(new can::Monitor(CM->GetAsInt("MonitorPort"))));  
  102.     }
  103.    
  104.     if (CM->Exist("CanNet"))
  105.     {
  106.         type::StringList cannets = CM->GetAsStringVector("CanNet");
  107.        
  108.         for (int n = 0; n < cannets.size(); n++)
  109.         {
  110.             LOG.Info("CanNet[" + boost::lexical_cast<std::string>(n) + "]=" + cannets[n]);
  111.            
  112.             subscribers.push_back(can::Network::Pointer(new can::Network(cannets[n])));            
  113.         }
  114.        
  115.     }
  116.    
  117.    
  118.    
  119.    /* timer::Manager::Pointer TM = timer::Manager::Instance();
  120.    
  121.    
  122.     TM->Set(10000, true);
  123.    
  124.     TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1));
  125.     */
  126.    
  127.    // std::cout << "connect slots2" << std::endl;
  128.    // NM->ConnectSlots(boost::bind(&Main_SlotOnNewState, _1, _2, _3), boost::bind(&Main_SlotOnNewData, _1, _2, _3));
  129.    
  130.    
  131.     try
  132.     {
  133.        // server_id = NM->StartServer(net::PROTOCOL_SERIAL, 2000);
  134.    
  135.        // timer_id = TM->Set(10000, true);
  136.        
  137.        
  138.         //net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_UDP, "192.168.1.250", 1100);
  139.         //net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_SERIAL, "/dev/ttyUSB0", 57600);
  140.        
  141.         //std::cout << "connected" << std::endl;
  142.        
  143.         /*char c[2] = { 251, 0 };
  144.        
  145.         std::cout << "before send " << std::endl;
  146.        
  147.         NM->SendTo(client_id_udp, c);*/
  148.     }
  149.     catch (std::exception e)
  150.     {
  151.         std::cerr << e.what() << std::endl;
  152.     }
  153.    
  154.     std::cout << "sleep" << std::endl;
  155.    
  156.     sleep(100000000);
  157.    
  158.     return 0;
  159. }
  160.