Subversion Repositories HomeAutomation

Rev

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

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