Subversion Repositories HomeAutomation

Rev

Rev 1619 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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