Subversion Repositories HomeAutomation

Rev

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