Subversion Repositories HomeAutomation

Rev

Rev 1614 | Rev 1627 | 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
{
1619 runge 39
    // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds
40
    this->timer_id_ = timer::Manager::Instance()->Set(20000, true);
1598 runge 41
}
42
 
43
Manager::~Manager()
44
{
45
}
46
 
47
Manager::Pointer Manager::Instance()
48
{
49
    return Manager::instance_;
50
}
51
 
1599 runge 52
void Manager::Create()
53
{
54
    Manager::instance_ = Manager::Pointer(new Manager());
55
}
56
 
1598 runge 57
void Manager::Delete()
58
{
59
    Manager::instance_.reset();
60
}
61
 
1614 runge 62
void Manager::ConnectSlots(const SignalOnNodeChange::slot_type& signal_on_node_change_, const SignalOnModuleChange::slot_type& slot_on_module_change, const SignalOnModuleMessage::slot_type& slot_on_module_message)
1602 runge 63
{
1614 runge 64
    this->signal_on_node_change_.connect(signal_on_node_change_);
1602 runge 65
    this->signal_on_module_change_.connect(slot_on_module_change);
66
    this->signal_on_module_message_.connect(slot_on_module_message);
67
}
68
 
1601 runge 69
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
70
{
71
    if (timer_id != this->timer_id_)
72
    {
73
        return;
74
    }
75
 
76
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
77
    {
78
        if (!it->second->CheckTimeout())
79
        {
80
            LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
81
            this->RemoveModules(it->second->GetId());
82
        }
83
    }
84
}
85
 
1598 runge 86
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
87
{
88
    if (message->GetType() == broker::Message::CAN_MESSAGE)
89
    {
1604 runge 90
        //LOG.Debug("Message received");
1598 runge 91
        Message* payload = static_cast<Message*>(message->GetPayload().get());
92
        Node::Pointer node;
93
 
94
        if (payload->GetClassName() == "nmt")
95
        {
96
            if (payload->GetCommandName() == "Bios_Start")
97
            {
98
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 99
                node->ResetTimeout();
100
 
1598 runge 101
                this->RemoveModules(node->GetId());
102
                node->SetState(Node::STATE_ONLINE);
103
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
104
            }
105
            else if (payload->GetCommandName() == "Reset")
106
            {
107
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 108
 
1598 runge 109
                this->RemoveModules(node->GetId());
110
                node->SetState(Node::STATE_OFFLINE);
111
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
112
            }
1601 runge 113
            else if (payload->GetCommandName() == "App_Start")
1598 runge 114
            {
115
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 116
                node->ResetTimeout();
117
 
1598 runge 118
                this->RequestModules(node->GetId());
119
                node->SetState(Node::STATE_PENDING);
120
                LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
121
            }
122
            else if (payload->GetCommandName() == "Heartbeat")
123
            {
124
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 125
                node->ResetTimeout();
1598 runge 126
 
127
                if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
128
                {
129
                    this->RequestModules(node->GetId());
130
                    node->SetState(Node::STATE_PENDING);
131
                    LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
132
                }
133
                else if (node->GetState() == Node::STATE_INITIALIZED)
134
                {
135
                    if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
136
                    {
137
                        this->RequestModules(node->GetId());
138
                        node->SetState(Node::STATE_PENDING);
139
                        LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
140
                    }
141
                }
142
            }
143
        }
1602 runge 144
        else if (payload->GetDirectionName() == "From_Owner")
1598 runge 145
        {
1602 runge 146
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
147
 
148
            if (payload->GetCommandName() == "List")
1598 runge 149
            {
150
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
1601 runge 151
                node->ResetTimeout();
1598 runge 152
 
1602 runge 153
 
1598 runge 154
                module->SetNodeId(node->GetId());
155
 
156
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
157
                {
158
                    node->SetState(Node::STATE_INITIALIZED);
159
                }
160
 
161
                LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
1602 runge 162
                this->signal_on_module_change_(module->GetFullId(), true);
1598 runge 163
            }
1602 runge 164
            else
165
            {
1619 runge 166
                node = this->GetNode(module->GetNodeId());
167
                node->ResetTimeout();
168
 
1602 runge 169
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
170
            }
1598 runge 171
        }
172
    }
173
}
174
 
1604 runge 175
void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables)
1602 runge 176
{
177
    ModuleList::iterator it = this->modules_.find(full_id);
178
 
179
    if (it == this->modules_.end())
180
    {
181
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
182
        return;
183
    }
184
 
185
    try
186
    {
187
        Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
188
        payload->SetVariables(variables);
189
 
190
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
1607 runge 191
        //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
1602 runge 192
    }
193
    catch (std::runtime_error& e)
194
    {
195
        LOG.Error("Failed to send message, " + std::string(e.what()));
196
    }
197
}
198
 
1604 runge 199
void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables)
200
{
201
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
202
}
203
 
204
bool Manager::IsModuleAvailable(std::string full_id)
205
{
206
    return this->modules_.find(full_id) != this->modules_.end();
207
}
208
 
1598 runge 209
Node::Pointer Manager::GetNode(Node::Id node_id)
210
{
211
    Node::Pointer node;
212
    NodeList::iterator it = this->nodes_.find(node_id);
213
 
214
    if (it == this->nodes_.end())
215
    {
216
        node = Node::Pointer(new Node(node_id));
217
        this->nodes_[node_id] = node;
218
    }
219
    else
220
    {
221
        node = it->second;
222
    }
223
 
224
    return node;
225
}
226
 
227
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
228
{
229
    Module::Pointer module;
230
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
231
 
232
    if (it == this->modules_.end())
233
    {
234
        module = Module::Pointer(new Module(module_id, module_name, class_name));
235
        this->modules_[module->GetFullId()] = module;
236
    }
237
    else
238
    {
239
        module = it->second;
240
    }
241
 
242
    return module;
243
}
244
 
245
void Manager::RemoveModules(Node::Id node_id)
246
{
247
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
248
    {
249
        if (it->second->GetNodeId() == node_id)
250
        {
251
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
1602 runge 252
            this->signal_on_module_change_(it->second->GetFullId(), false);
1598 runge 253
 
254
            this->modules_.erase(it);
255
        }
256
    }
257
}
258
 
259
void Manager::RequestModules(Node::Id node_id)
260
{
261
    this->RemoveModules(node_id);
262
 
263
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
264
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
265
 
266
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
267
}
268
 
269
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
270
{
271
    unsigned int number_of_modules = 0;
272
 
273
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
274
    {
275
        if (it->second->GetNodeId() == node_id)
276
        {
277
            number_of_modules++;
278
        }
279
    }
280
 
281
    return number_of_modules;
282
}
283
 
284
 
285
}; // namespace can
286
}; // namespace atom