Subversion Repositories HomeAutomation

Rev

Rev 985 | Rev 991 | Go to most recent revision | Blame | Compare with Previous | 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.     if (port == "")
  74.     {
  75.         slog << "CanDebugPort is not defined in the config file, will not start debug interface\n";
  76.         return;
  77.     }
  78.  
  79.     try
  80.     {
  81.         mySocket.setPort(stoi(port));
  82.  
  83.         mySocket.startListen();
  84.  
  85.         slog << "CanDebug is listening for connections on port " + port + ".\n";
  86.  
  87.         while (true)
  88.         {
  89.             AsyncSocket *newSocket = new AsyncSocket();
  90.  
  91.             if (mySocket.accept(newSocket))
  92.             {
  93.                 newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n");
  94.  
  95.                 slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n";
  96.                 myClientSockets[newSocket->getSocket()] = newSocket;
  97.                 myClientSockets[newSocket->getSocket()]->start();
  98.             }
  99.         }
  100.     }
  101.     catch (SocketException* e)
  102.     {
  103.         slog << "Exception was caught:" + e->getDescription() + ".\n";
  104.     }
  105. }
  106.  
  107. void CanDebug::sendData(string data)
  108. {
  109.     SyslogStream &slog = SyslogStream::getInstance();
  110.  
  111.     map<int, AsyncSocket*>::iterator iter;
  112.  
  113.     for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  114.     {
  115.         if (iter->second->isConnected())
  116.         {
  117.             //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n";
  118.            
  119.             try
  120.             {
  121.                 iter->second->sendData(data);
  122.             }
  123.             catch (SocketException* e)
  124.             {
  125.                 slog << "CanDebug caught an exception:" + e->getDescription() + ".\n";
  126.             }
  127.         }
  128.         else
  129.         {
  130.             slog << "CanDebug had a client disconnect.\n";
  131.             delete iter->second;
  132.             myClientSockets.erase(iter);
  133.         }
  134.     }
  135. }
  136.  
  137. void CanDebug::sendCanMessage(CanMessage canMessage)
  138. {
  139.     if (myClientSockets.size() > 0)
  140.     {
  141.         string debugData = "";
  142.  
  143.         if (canMessage.getClassName() == "nmt")
  144.         {
  145.             debugData += "NMT";
  146.         }
  147.         else
  148.         {
  149.             if (canMessage.getDirectionFlag() == "To_Owner")
  150.             {
  151.                 debugData += "RX";
  152.             }
  153.             else if (canMessage.getDirectionFlag() == "From_Owner")
  154.             {
  155.                 debugData += "TX";
  156.             }
  157.             else
  158.             {
  159.                 debugData += "??";
  160.             }
  161.  
  162.             debugData += " " + canMessage.getClassName();
  163.             debugData += "_" + canMessage.getModuleName();
  164.             debugData += ":" + itos(canMessage.getModuleId());
  165.         }
  166.  
  167.         debugData += " CMD=" + canMessage.getCommandName() + " ";
  168.  
  169.         map<string, CanVariable> vars = canMessage.getData();
  170.  
  171.         map<string, CanVariable>::iterator iter;
  172.  
  173.         for (iter = vars.begin(); iter != vars.end(); iter++)
  174.         {
  175.             debugData += " " + iter->second.getName() + "=" + iter->second.getValue();
  176.         }
  177.  
  178.         sendData("[" + niceTime() + "] " + debugData + "\n");
  179.     }
  180. }
  181.