Subversion Repositories HomeAutomation

Rev

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

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