Subversion Repositories HomeAutomation

Rev

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

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