Subversion Repositories HomeAutomation

Rev

Rev 972 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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