Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
969 runge 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
 
974 runge 72
    bool waitingForPong = false;
73
 
969 runge 74
    while (1)
75
    {
76
        myChannel->waitForEvent();
77
 
78
        int event = myChannel->getEvent();
79
 
80
        if (event == ASYNCSOCKET_EVENT_DATA)
81
        {
82
            while (myChannel->availableData())
83
            {
974 runge 84
                waitingForPong = false;
85
 
969 runge 86
                string data = myChannel->getData();
87
 
88
                try
89
                {
90
                    vector<string> dataLines = explode("\n", data);
91
 
92
                    for (int n = 0; n < dataLines.size(); n++)
93
                    {
974 runge 94
                        data = trim(data, '\n');
95
 
96
                        if (data == "PONG")
97
                        {
98
                            slog << "Received pong.\n";
99
                            continue;
100
                        }
101
 
969 runge 102
                        CanMessage canMessage;
103
                        canMessage.setRaw(data);
104
 
105
                        //slog << "Received: " << data;
106
 
972 runge 107
                        if (!canMessage.isUnknown())
969 runge 108
                        {
972 runge 109
                            vm.queueCanMessage(canMessage);
969 runge 110
                        }
972 runge 111
                        /*else
969 runge 112
                        {
972 runge 113
                            slog << "Received unknown message. Skipping...\n";
114
                        }*/
969 runge 115
                    }
116
                }
117
                catch (CanMessageException* e)
118
                {
119
                    slog << "CanMessageException was caught:\n";
120
                    slog << e->getDescription() << "\n";
121
                }
122
            }
123
        }
974 runge 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
 
141
        }
969 runge 142
    }
143
 
144
    myChannel->stopEvent();
145
}
146
 
147
void CanNetManager::sendMessage(CanMessage canMessage)
148
{
149
    myChannel->sendData(canMessage.getRaw());
150
}
151