Subversion Repositories HomeAutomation

Rev

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

  1. /***************************************************************************
  2.  *   Copyright (C) November 29, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   cannetmanager.cpp                                            *
  5.  *                                                                         *
  6.  *   This program is free software; you can redistribute it and/or modify  *
  7.  *   it under the terms of the GNU General Public License as published by  *
  8.  *   the Free Software Foundation; either version 2 of the License, or     *
  9.  *   (at your option) any later version.                                   *
  10.  *                                                                         *
  11.  *   This program is distributed in the hope that it will be useful,       *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  14.  *   GNU General Public License for more details.                          *
  15.  *                                                                         *
  16.  *   You should have received a copy of the GNU General Public License     *
  17.  *   along with this program; if not, write to the                         *
  18.  *   Free Software Foundation, Inc.,                                       *
  19.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  20.  ***************************************************************************/
  21.  
  22. #include "canmessage.h"
  23. #include "cannetmanager.h"
  24.  
  25. CanNetManager* CanNetManager::myInstance = NULL;
  26.  
  27. CanNetManager& CanNetManager::getInstance()
  28. {
  29.     if (myInstance == NULL)
  30.     {
  31.         myInstance = new CanNetManager();
  32.     }
  33.  
  34.     return *myInstance;
  35. }
  36.  
  37. void CanNetManager::deleteInstance()
  38. {
  39.     if (myInstance != NULL)
  40.     {
  41.         delete myInstance;
  42.         myInstance = NULL;
  43.     }
  44. }
  45.  
  46. CanNetManager::CanNetManager()
  47. {
  48.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  49.     myChannel = new AsyncSocket();
  50. }
  51.  
  52. CanNetManager::~CanNetManager()
  53. {
  54.     CanIdTranslator::deleteInstance();
  55.     delete myChannel;
  56. }
  57.  
  58. void CanNetManager::openChannel()
  59. {
  60.     SyslogStream &slog = SyslogStream::getInstance();
  61.     VirtualMachine &vm = VirtualMachine::getInstance();
  62.  
  63.     string address = Settings::get("CanNetAddress");
  64.     string port = Settings::get("CanNetPort");
  65.  
  66.     myChannel->setAddress(address, stoi(port));
  67.  
  68.     myChannel->start();
  69.  
  70.     myChannel->startEvent();
  71.  
  72.     while (1)
  73.     {
  74.         myChannel->waitForEvent();
  75.  
  76.         int event = myChannel->getEvent();
  77.  
  78.         if (event == ASYNCSOCKET_EVENT_DATA)
  79.         {
  80.             while (myChannel->availableData())
  81.             {
  82.                 string data = myChannel->getData();
  83.  
  84.                 try
  85.                 {
  86.                     vector<string> dataLines = explode("\n", data);
  87.  
  88.                     for (int n = 0; n < dataLines.size(); n++)
  89.                     {
  90.                         CanMessage canMessage;
  91.                         canMessage.setRaw(data);
  92.  
  93.                         //slog << "Received: " << data;
  94.  
  95.                         if (!canMessage.isUnknown())
  96.                         {
  97.                             vm.queueCanMessage(canMessage);
  98.                         }
  99.                         /*else
  100.                         {
  101.                             slog << "Received unknown message. Skipping...\n";
  102.                         }*/
  103.                     }
  104.                 }
  105.                 catch (CanMessageException* e)
  106.                 {
  107.                     slog << "CanMessageException was caught:\n";
  108.                     slog << e->getDescription() << "\n";
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.     myChannel->stopEvent();
  115. }
  116.  
  117. void CanNetManager::sendMessage(CanMessage canMessage)
  118. {
  119.     myChannel->sendData(canMessage.getRaw());
  120. }
  121.  
  122.