Subversion Repositories HomeAutomation

Rev

Rev 1604 | Rev 1614 | Go to most recent revision | Details | Compare with Previous | 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
    {
1604 runge 88
        //LOG.Debug("Message received");
1598 runge 89
        Message* payload = static_cast<Message*>(message->GetPayload().get());
90
        Node::Pointer node;
91
 
92
        if (payload->GetClassName() == "nmt")
93
        {
94
            if (payload->GetCommandName() == "Bios_Start")
95
            {
96
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 97
                node->ResetTimeout();
98
 
1598 runge 99
                this->RemoveModules(node->GetId());
100
                node->SetState(Node::STATE_ONLINE);
101
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
102
            }
103
            else if (payload->GetCommandName() == "Reset")
104
            {
105
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 106
 
1598 runge 107
                this->RemoveModules(node->GetId());
108
                node->SetState(Node::STATE_OFFLINE);
109
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
110
            }
1601 runge 111
            else if (payload->GetCommandName() == "App_Start")
1598 runge 112
            {
113
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 114
                node->ResetTimeout();
115
 
1598 runge 116
                this->RequestModules(node->GetId());
117
                node->SetState(Node::STATE_PENDING);
118
                LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
119
            }
120
            else if (payload->GetCommandName() == "Heartbeat")
121
            {
122
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 123
                node->ResetTimeout();
1598 runge 124
 
125
                if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
126
                {
127
                    this->RequestModules(node->GetId());
128
                    node->SetState(Node::STATE_PENDING);
129
                    LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
130
                }
131
                else if (node->GetState() == Node::STATE_INITIALIZED)
132
                {
133
                    if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
134
                    {
135
                        this->RequestModules(node->GetId());
136
                        node->SetState(Node::STATE_PENDING);
137
                        LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
138
                    }
139
                }
140
            }
141
        }
1602 runge 142
        else if (payload->GetDirectionName() == "From_Owner")
1598 runge 143
        {
1602 runge 144
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
145
 
146
            if (payload->GetCommandName() == "List")
1598 runge 147
            {
148
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 149
                node->ResetTimeout();
1598 runge 150
 
1602 runge 151
 
1598 runge 152
                module->SetNodeId(node->GetId());
153
 
154
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
155
                {
156
                    node->SetState(Node::STATE_INITIALIZED);
157
                }
158
 
159
                LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
1602 runge 160
                this->signal_on_module_change_(module->GetFullId(), true);
1598 runge 161
            }
1602 runge 162
            else
163
            {
164
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
165
            }
1598 runge 166
        }
167
    }
168
}
169
 
1604 runge 170
void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables)
1602 runge 171
{
172
    ModuleList::iterator it = this->modules_.find(full_id);
173
 
174
    if (it == this->modules_.end())
175
    {
176
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
177
        return;
178
    }
179
 
180
    try
181
    {
182
        Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
183
        payload->SetVariables(variables);
184
 
185
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
1607 runge 186
        //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
1602 runge 187
    }
188
    catch (std::runtime_error& e)
189
    {
190
        LOG.Error("Failed to send message, " + std::string(e.what()));
191
    }
192
}
193
 
1604 runge 194
void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables)
195
{
196
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
197
}
198
 
199
bool Manager::IsModuleAvailable(std::string full_id)
200
{
201
    return this->modules_.find(full_id) != this->modules_.end();
202
}
203
 
1598 runge 204
Node::Pointer Manager::GetNode(Node::Id node_id)
205
{
206
    Node::Pointer node;
207
    NodeList::iterator it = this->nodes_.find(node_id);
208
 
209
    if (it == this->nodes_.end())
210
    {
211
        node = Node::Pointer(new Node(node_id));
212
        this->nodes_[node_id] = node;
213
    }
214
    else
215
    {
216
        node = it->second;
217
    }
218
 
219
    return node;
220
}
221
 
222
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
223
{
224
    Module::Pointer module;
225
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
226
 
227
    if (it == this->modules_.end())
228
    {
229
        module = Module::Pointer(new Module(module_id, module_name, class_name));
230
        this->modules_[module->GetFullId()] = module;
231
    }
232
    else
233
    {
234
        module = it->second;
235
    }
236
 
237
    return module;
238
}
239
 
240
void Manager::RemoveModules(Node::Id node_id)
241
{
242
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
243
    {
244
        if (it->second->GetNodeId() == node_id)
245
        {
246
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
1602 runge 247
            this->signal_on_module_change_(it->second->GetFullId(), false);
1598 runge 248
 
249
            this->modules_.erase(it);
250
        }
251
    }
252
}
253
 
254
void Manager::RequestModules(Node::Id node_id)
255
{
256
    this->RemoveModules(node_id);
257
 
258
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
259
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
260
 
261
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
262
}
263
 
264
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
265
{
266
    unsigned int number_of_modules = 0;
267
 
268
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
269
    {
270
        if (it->second->GetNodeId() == node_id)
271
        {
272
            number_of_modules++;
273
        }
274
    }
275
 
276
    return number_of_modules;
277
}
278
 
279
 
280
}; // namespace can
281
}; // namespace atom