Subversion Repositories HomeAutomation

Rev

Rev 1594 | Rev 1596 | 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 <boost/bind.hpp>
  7. #include <boost/program_options.hpp>
  8.  
  9. #include "net/Manager.h"
  10. #include "net/types.h"
  11.  
  12. #include "timer/Manager.h"
  13. #include "timer/types.h"
  14.  
  15. #include "config/Manager.h"
  16.  
  17. #include "logging/Logger.h"
  18.  
  19. using namespace atom;
  20.  
  21.  
  22. net::Manager::Pointer NM = net::Manager::Instance();
  23.  
  24. net::ServerId server_id = 0;
  25. timer::TimerId timer_id;
  26.  
  27. void Main_SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  28. {
  29.     std::cout << "client:" << client_id << " - New state: " << client_state << std::endl;
  30. }
  31.  
  32. void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, net::Buffer data)
  33. {
  34.     std::string s(data.data());
  35.    
  36.     std::cout << "client:" << client_id << " - New data: " << s << std::endl;
  37.    
  38.     net::Buffer out_data;
  39.    
  40.     strncpy(out_data.c_array(), data.data(), out_data.size());
  41.    
  42.     NM->Disconnect(client_id);
  43.    
  44.     std::cout << "client after disconnect" << s << std::endl;
  45. }
  46.  
  47. void Main_SlotOnTimeout(timer::TimerId timer_id)
  48. {
  49.     std::cout << "timer:" << timer_id << " - expired" << std::endl;
  50.    
  51.     std::string out_string = "Timeout!";
  52.    
  53.     net::Buffer out_data;
  54.    
  55.     strncpy(out_data.c_array(), out_string.data(), out_data.size());
  56.    
  57.    // NM->SendToAll(server_id, out_data);
  58. }
  59.  
  60. int main(int argc, char **argv)
  61. {
  62.     logging::Logger LOG("Main");
  63.  
  64.     config::Manager::Pointer CM = config::Manager::Instance();
  65.    
  66.     if (!CM->Set(argc, argv))
  67.     {
  68.         return 0;
  69.     }
  70.    
  71.     if (CM->Exist("MonitorPort"))
  72.     {
  73.         LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(CM->GetAsInt("MonitorPort")));
  74.     }
  75.    
  76.     if (CM->Exist("CanNet"))
  77.     {
  78.         std::vector<std::string> cannets = CM->GetAsStringVector("CanNet");
  79.        
  80.         for (int n = 0; n < cannets.size(); n++)
  81.         {
  82.             LOG.Info("CanNet[" + boost::lexical_cast<std::string>(n) + "]=" + cannets[n]);
  83.         }
  84.        
  85.     }
  86.    
  87.    /* timer::Manager::Pointer TM = timer::Manager::Instance();
  88.    
  89.    
  90.     TM->Set(10000, true);
  91.    
  92.     TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1));
  93.     */
  94.    
  95.     std::cout << "connect slots2" << std::endl;
  96.     NM->ConnectSlots(boost::bind(&Main_SlotOnNewState, _1, _2, _3), boost::bind(&Main_SlotOnNewData, _1, _2, _3));
  97.    
  98.    
  99.     try
  100.     {
  101.        // server_id = NM->StartServer(net::PROTOCOL_SERIAL, 2000);
  102.    
  103.        // timer_id = TM->Set(10000, true);
  104.        
  105.        
  106.         net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_SERIAL, "/dev/ttyUSB0", 57600);
  107.        
  108.         std::cout << "connected" << std::endl;
  109.        
  110.         char c[2] = { 251, 0 };
  111.        
  112.         std::cout << "before send " << std::endl;
  113.        
  114.         NM->SendTo(client_id_udp, std::string(c));
  115.     }
  116.     catch (std::exception e)
  117.     {
  118.         std::cerr << e.what() << std::endl;
  119.     }
  120.    
  121.     std::cout << "sleep" << std::endl;
  122.    
  123.     sleep(100000000);
  124.    
  125.     return 0;
  126. }
  127.