Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1598 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Manager.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
 
25
#include "broker/Manager.h"
26
#include "broker/Message.h"
1601 runge 27
#include "type/common.h"
28
#include "timer/Manager.h"
29
 
1598 runge 30
#include "Message.h"
31
 
32
namespace atom {
33
namespace can {
34
 
1599 runge 35
Manager::Pointer Manager::instance_;
1598 runge 36
 
37
Manager::Manager() : broker::Subscriber(false), LOG("can::Manager")
38
{
1601 runge 39
    this->timer_id_ = timer::Manager::Instance()->Set(10000, true);
1598 runge 40
}
41
 
42
Manager::~Manager()
43
{
44
}
45
 
46
Manager::Pointer Manager::Instance()
47
{
48
    return Manager::instance_;
49
}
50
 
1599 runge 51
void Manager::Create()
52
{
53
    Manager::instance_ = Manager::Pointer(new Manager());
54
}
55
 
1598 runge 56
void Manager::Delete()
57
{
58
    Manager::instance_.reset();
59
}
60
 
1602 runge 61
void Manager::ConnectSlots(const SignalOnModuleChange::slot_type& slot_on_module_change, const SignalOnModuleMessage::slot_type& slot_on_module_message)
62
{
63
    this->signal_on_module_change_.connect(slot_on_module_change);
64
    this->signal_on_module_message_.connect(slot_on_module_message);
65
}
66
 
1601 runge 67
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
68
{
69
    if (timer_id != this->timer_id_)
70
    {
71
        return;
72
    }
73
 
74
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
75
    {
76
        if (!it->second->CheckTimeout())
77
        {
78
            LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
79
            this->RemoveModules(it->second->GetId());
80
        }
81
    }
82
}
83
 
1598 runge 84
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
85
{
86
    if (message->GetType() == broker::Message::CAN_MESSAGE)
87
    {
88
        Message* payload = static_cast<Message*>(message->GetPayload().get());
89
        Node::Pointer node;
90
 
91
        if (payload->GetClassName() == "nmt")
92
        {
93
            if (payload->GetCommandName() == "Bios_Start")
94
            {
95
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 96
                node->ResetTimeout();
97
 
1598 runge 98
                this->RemoveModules(node->GetId());
99
                node->SetState(Node::STATE_ONLINE);
100
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
101
            }
102
            else if (payload->GetCommandName() == "Reset")
103
            {
104
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 105
 
1598 runge 106
                this->RemoveModules(node->GetId());
107
                node->SetState(Node::STATE_OFFLINE);
108
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
109
            }
1601 runge 110
            else if (payload->GetCommandName() == "App_Start")
1598 runge 111
            {
112
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 113
                node->ResetTimeout();
114
 
1598 runge 115
                this->RequestModules(node->GetId());
116
                node->SetState(Node::STATE_PENDING);
117
                LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
118
            }
119
            else if (payload->GetCommandName() == "Heartbeat")
120
            {
121
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 122
                node->ResetTimeout();
1598 runge 123
 
124
                if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
125
                {
126
                    this->RequestModules(node->GetId());
127
                    node->SetState(Node::STATE_PENDING);
128
                    LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
129
                }
130
                else if (node->GetState() == Node::STATE_INITIALIZED)
131
                {
132
                    if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
133
                    {
134
                        this->RequestModules(node->GetId());
135
                        node->SetState(Node::STATE_PENDING);
136
                        LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
137
                    }
138
                }
139
            }
140
        }
1602 runge 141
        else if (payload->GetDirectionName() == "From_Owner")
1598 runge 142
        {
1602 runge 143
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
144
 
145
            if (payload->GetCommandName() == "List")
1598 runge 146
            {
147
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 148
                node->ResetTimeout();
1598 runge 149
 
1602 runge 150
 
1598 runge 151
                module->SetNodeId(node->GetId());
152
 
153
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
154
                {
155
                    node->SetState(Node::STATE_INITIALIZED);
156
                }
157
 
158
                LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
1602 runge 159
                this->signal_on_module_change_(module->GetFullId(), true);
1598 runge 160
            }
1602 runge 161
            else
162
            {
163
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
164
            }
1598 runge 165
        }
166
    }
167
}
168
 
1602 runge 169
void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables)
170
{
171
    ModuleList::iterator it = this->modules_.find(full_id);
172
 
173
    if (it == this->modules_.end())
174
    {
175
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
176
        return;
177
    }
178
 
179
    try
180
    {
181
        Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
182
        payload->SetVariables(variables);
183
 
184
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
185
    }
186
    catch (std::runtime_error& e)
187
    {
188
        LOG.Error("Failed to send message, " + std::string(e.what()));
189
    }
190
}
191
 
1598 runge 192
Node::Pointer Manager::GetNode(Node::Id node_id)
193
{
194
    Node::Pointer node;
195
    NodeList::iterator it = this->nodes_.find(node_id);
196
 
197
    if (it == this->nodes_.end())
198
    {
199
        node = Node::Pointer(new Node(node_id));
200
        this->nodes_[node_id] = node;
201
    }
202
    else
203
    {
204
        node = it->second;
205
    }
206
 
207
    return node;
208
}
209
 
210
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
211
{
212
    Module::Pointer module;
213
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
214
 
215
    if (it == this->modules_.end())
216
    {
217
        module = Module::Pointer(new Module(module_id, module_name, class_name));
218
        this->modules_[module->GetFullId()] = module;
219
    }
220
    else
221
    {
222
        module = it->second;
223
    }
224
 
225
    return module;
226
}
227
 
228
void Manager::RemoveModules(Node::Id node_id)
229
{
230
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
231
    {
232
        if (it->second->GetNodeId() == node_id)
233
        {
234
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
1602 runge 235
            this->signal_on_module_change_(it->second->GetFullId(), false);
1598 runge 236
 
237
            this->modules_.erase(it);
238
        }
239
    }
240
}
241
 
242
void Manager::RequestModules(Node::Id node_id)
243
{
244
    this->RemoveModules(node_id);
245
 
246
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
247
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
248
 
249
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
250
}
251
 
252
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
253
{
254
    unsigned int number_of_modules = 0;
255
 
256
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
257
    {
258
        if (it->second->GetNodeId() == node_id)
259
        {
260
            number_of_modules++;
261
        }
262
    }
263
 
264
    return number_of_modules;
265
}
266
 
267
 
268
}; // namespace can
269
}; // namespace atom