Subversion Repositories HomeAutomation

Rev

Rev 983 | Blame | Last modification | View Log | SVN | RSS feed

  1. /***************************************************************************
  2.  *   Copyright (C) December 27, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   candebug.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 "candebug.h"
  23.  
  24. CanDebug* CanDebug::myInstance = NULL;
  25.  
  26. CanDebug& CanDebug::getInstance()
  27. {
  28.     if (myInstance == NULL)
  29.     {
  30.         myInstance = new CanDebug();
  31.     }
  32.  
  33.     return *myInstance;
  34. }
  35.  
  36. void CanDebug::deleteInstance()
  37. {
  38.     if (myInstance != NULL)
  39.     {
  40.         delete myInstance;
  41.         myInstance = NULL;
  42.     }
  43. }
  44.  
  45. CanDebug::CanDebug()
  46. {
  47.     Thread<CanDebug>();
  48. }
  49.  
  50. CanDebug::~CanDebug()
  51. {
  52.     stop();
  53.  
  54.     map<int, AsyncSocket*>::iterator iter;
  55.  
  56.     for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  57.     {
  58.         if (iter->second->isConnected())
  59.         {
  60.             iter->second->stop();
  61.         }
  62.  
  63.         delete iter->second;
  64.     }
  65. }
  66.  
  67. void CanDebug::run()
  68. {
  69.     SyslogStream &slog = SyslogStream::getInstance();
  70.  
  71.     string port = Settings::get("CanDebugPort");
  72.  
  73.     try
  74.     {
  75.         mySocket.setPort(stoi(port));
  76.  
  77.         mySocket.startListen();
  78.  
  79.         slog << "CanDebug is listening for connections on port " + port + ".\n";
  80.  
  81.         while (true)
  82.         {
  83.             AsyncSocket *newSocket = new AsyncSocket();
  84.  
  85.             if (mySocket.accept(newSocket))
  86.             {
  87.                 newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n");
  88.  
  89.                 slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n";
  90.                 myClientSockets[newSocket->getSocket()] = newSocket;
  91.                 myClientSockets[newSocket->getSocket()]->start();
  92.             }
  93.         }
  94.     }
  95.     catch (SocketException* e)
  96.     {
  97.         slog << "Exception was caught:" + e->getDescription() + ".\n";
  98.     }
  99. }
  100.  
  101. void CanDebug::sendData(string data)
  102. {
  103.     SyslogStream &slog = SyslogStream::getInstance();
  104.  
  105.     map<int, AsyncSocket*>::iterator iter;
  106.  
  107.     for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  108.     {
  109.         if (iter->second->isConnected())
  110.         {
  111.             //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n";
  112.            
  113.             try
  114.             {
  115.                 iter->second->sendData(data);
  116.             }
  117.             catch (SocketException* e)
  118.             {
  119.                 slog << "CanDebug caught an exception:" + e->getDescription() + ".\n";
  120.             }
  121.         }
  122.         else
  123.         {
  124.             slog << "CanDebug had a client disconnect.\n";
  125.             delete iter->second;
  126.             myClientSockets.erase(iter);
  127.         }
  128.     }
  129. }
  130.  
  131. void CanDebug::sendCanMessage(CanMessage canMessage)
  132. {
  133.     if (!canMessage.isHeartbeat() && myClientSockets.size() > 0)
  134.     {
  135.         string debugData = "";
  136.  
  137.         if (canMessage.getDirectionFlag() == "To_Owner")
  138.         {
  139.             debugData += "RX";
  140.         }
  141.         else if (canMessage.getDirectionFlag() == "From_Owner")
  142.         {
  143.             debugData += "TX";
  144.         }
  145.         else
  146.         {
  147.             debugData += "??";
  148.         }
  149.  
  150.         debugData += " " + canMessage.getClassName();
  151.         debugData += "_" + canMessage.getModuleName();
  152.         debugData += ":" + itos(canMessage.getModuleId());
  153.         debugData += " CMD=" + canMessage.getCommandName() + " ";
  154.  
  155.         map<string, CanVariable> vars = canMessage.getData();
  156.  
  157.         map<string, CanVariable>::iterator iter;
  158.  
  159.         for (iter = vars.begin(); iter != vars.end(); iter++)
  160.         {
  161.             debugData += " " + iter->second.getName() + "=" + iter->second.getValue();
  162.         }
  163.  
  164.         sendData("[" + niceTime() + "] " + debugData + "\n");
  165.     }
  166. }
  167.