Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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