Subversion Repositories HomeAutomation

Rev

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

Rev 1599 Rev 1601
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"
-
 
28
#include "timer/Manager.h"
-
 
29
 
27
#include "Message.h"
30
#include "Message.h"
28
#include <type/common.h>
-
 
29
 
31
 
30
namespace atom {
32
namespace atom {
31
namespace can {
33
namespace can {
32
 
-
 
33
   
34
   
34
Manager::Pointer Manager::instance_;
35
Manager::Pointer Manager::instance_;
35
 
36
 
36
Manager::Manager() : broker::Subscriber(false), LOG("can::Manager")
37
Manager::Manager() : broker::Subscriber(false), LOG("can::Manager")
37
{
38
{
38
    // TODO: Set offline timer
39
    this->timer_id_ = timer::Manager::Instance()->Set(10000, true);
39
}
40
}
40
 
41
 
41
Manager::~Manager()
42
Manager::~Manager()
42
{
43
{
43
}
44
}
44
 
45
 
45
Manager::Pointer Manager::Instance()
46
Manager::Pointer Manager::Instance()
46
{
47
{
47
    return Manager::instance_;
48
    return Manager::instance_;
48
}
49
}
49
 
50
 
50
void Manager::Create()
51
void Manager::Create()
51
{
52
{
52
    Manager::instance_ = Manager::Pointer(new Manager());
53
    Manager::instance_ = Manager::Pointer(new Manager());
53
}
54
}
54
 
55
 
55
void Manager::Delete()
56
void Manager::Delete()
56
{
57
{
57
    Manager::instance_.reset();
58
    Manager::instance_.reset();
-
 
59
}
-
 
60
 
-
 
61
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
-
 
62
{
-
 
63
    if (timer_id != this->timer_id_)
-
 
64
    {
-
 
65
        return;
-
 
66
    }
-
 
67
   
-
 
68
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
-
 
69
    {
-
 
70
        if (!it->second->CheckTimeout())
-
 
71
        {
-
 
72
            LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
-
 
73
            this->RemoveModules(it->second->GetId());
-
 
74
        }
-
 
75
    }
58
}
76
}
59
 
77
 
60
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
78
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
61
{
79
{
62
    if (message->GetType() == broker::Message::CAN_MESSAGE)
80
    if (message->GetType() == broker::Message::CAN_MESSAGE)
63
    {
81
    {
64
        Message* payload = static_cast<Message*>(message->GetPayload().get());
82
        Message* payload = static_cast<Message*>(message->GetPayload().get());
65
        Node::Pointer node;
83
        Node::Pointer node;
66
       
84
       
67
        if (payload->GetClassName() == "nmt")
85
        if (payload->GetClassName() == "nmt")
68
        {
86
        {
69
            if (payload->GetCommandName() == "Bios_Start")
87
            if (payload->GetCommandName() == "Bios_Start")
70
            {
88
            {
71
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
89
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
-
 
90
                node->ResetTimeout();
-
 
91
               
72
                this->RemoveModules(node->GetId());
92
                this->RemoveModules(node->GetId());
73
                node->SetState(Node::STATE_ONLINE);
93
                node->SetState(Node::STATE_ONLINE);
74
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
94
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went online.");
75
            }
95
            }
76
            else if (payload->GetCommandName() == "Reset")
96
            else if (payload->GetCommandName() == "Reset")
77
            {
97
            {
78
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
98
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
-
 
99
 
79
                this->RemoveModules(node->GetId());
100
                this->RemoveModules(node->GetId());
80
                node->SetState(Node::STATE_OFFLINE);
101
                node->SetState(Node::STATE_OFFLINE);
81
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
102
                LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline.");
82
            }
103
            }
83
            else if (payload->GetCommandName() == "App_start")
104
            else if (payload->GetCommandName() == "App_Start")
84
            {
105
            {
85
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
106
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
-
 
107
                node->ResetTimeout();
-
 
108
               
86
                this->RequestModules(node->GetId());
109
                this->RequestModules(node->GetId());
87
                node->SetState(Node::STATE_PENDING);
110
                node->SetState(Node::STATE_PENDING);
88
                LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
111
                LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules...");
89
            }
112
            }
90
            else if (payload->GetCommandName() == "Heartbeat")
113
            else if (payload->GetCommandName() == "Heartbeat")
91
            {
114
            {
92
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
115
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
-
 
116
                node->ResetTimeout();
93
               
117
               
94
                if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
118
                if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE)
95
                {
119
                {
96
                    this->RequestModules(node->GetId());
120
                    this->RequestModules(node->GetId());
97
                    node->SetState(Node::STATE_PENDING);
121
                    node->SetState(Node::STATE_PENDING);
98
                    LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
122
                    LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules...");
99
                }
123
                }
100
                else if (node->GetState() == Node::STATE_INITIALIZED)
124
                else if (node->GetState() == Node::STATE_INITIALIZED)
101
                {
125
                {
102
                    if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
126
                    if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
103
                    {
127
                    {
104
                        this->RequestModules(node->GetId());
128
                        this->RequestModules(node->GetId());
105
                        node->SetState(Node::STATE_PENDING);
129
                        node->SetState(Node::STATE_PENDING);
106
                        LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
130
                        LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules...");
107
                    }
131
                    }
108
                }
132
                }
109
            }
133
            }
110
        }
134
        }
111
        else
135
        else
112
        {
136
        {
113
            if (payload->GetCommandName() == "List" && payload->GetDirectionName() == "From_Owner")
137
            if (payload->GetCommandName() == "List" && payload->GetDirectionName() == "From_Owner")
114
            {
138
            {
115
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
139
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
-
 
140
                node->ResetTimeout();
116
               
141
               
117
                Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
142
                Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
118
                module->SetNodeId(node->GetId());
143
                module->SetNodeId(node->GetId());
119
               
144
               
120
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
145
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
121
                {
146
                {
122
                    node->SetState(Node::STATE_INITIALIZED);
147
                    node->SetState(Node::STATE_INITIALIZED);
123
                }
148
                }
124
               
149
               
125
                LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
150
                LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + ".");
126
                // TODO: Notify that module is available
151
                // TODO: Notify that module is available
127
            }
152
            }
128
        }
153
        }
129
    }
154
    }
130
}
155
}
131
 
156
 
132
Node::Pointer Manager::GetNode(Node::Id node_id)
157
Node::Pointer Manager::GetNode(Node::Id node_id)
133
{
158
{
134
    Node::Pointer node;
159
    Node::Pointer node;
135
    NodeList::iterator it = this->nodes_.find(node_id);
160
    NodeList::iterator it = this->nodes_.find(node_id);
136
   
161
   
137
    if (it == this->nodes_.end())
162
    if (it == this->nodes_.end())
138
    {
163
    {
139
        node = Node::Pointer(new Node(node_id));
164
        node = Node::Pointer(new Node(node_id));
140
        this->nodes_[node_id] = node;
165
        this->nodes_[node_id] = node;
141
    }
166
    }
142
    else
167
    else
143
    {
168
    {
144
        node = it->second;
169
        node = it->second;
145
    }
170
    }
146
   
171
   
147
    return node;
172
    return node;
148
}
173
}
149
 
174
 
150
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
175
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
151
{
176
{
152
    Module::Pointer module;
177
    Module::Pointer module;
153
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
178
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
154
   
179
   
155
    if (it == this->modules_.end())
180
    if (it == this->modules_.end())
156
    {
181
    {
157
        module = Module::Pointer(new Module(module_id, module_name, class_name));
182
        module = Module::Pointer(new Module(module_id, module_name, class_name));
158
        this->modules_[module->GetFullId()] = module;
183
        this->modules_[module->GetFullId()] = module;
159
    }
184
    }
160
    else
185
    else
161
    {
186
    {
162
        module = it->second;
187
        module = it->second;
163
    }
188
    }
164
   
189
   
165
    return module;
190
    return module;
166
}
191
}
167
 
192
 
168
void Manager::RemoveModules(Node::Id node_id)
193
void Manager::RemoveModules(Node::Id node_id)
169
{
194
{
170
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
195
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
171
    {
196
    {
172
        if (it->second->GetNodeId() == node_id)
197
        if (it->second->GetNodeId() == node_id)
173
        {
198
        {
174
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
199
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
175
            // TODO: Notify that module is unavailable
200
            // TODO: Notify that module is unavailable
176
           
201
           
177
            this->modules_.erase(it);
202
            this->modules_.erase(it);
178
        }
203
        }
179
    }
204
    }
180
}
205
}
181
 
206
 
182
void Manager::RequestModules(Node::Id node_id)
207
void Manager::RequestModules(Node::Id node_id)
183
{
208
{
184
    this->RemoveModules(node_id);
209
    this->RemoveModules(node_id);
185
   
210
   
186
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
211
    Message* payload = new Message("mnmt", "To_Owner", "", 0, "List");
187
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
212
    payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id));
188
   
213
   
189
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
214
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
190
   
-
 
191
    // TODO: Set timeout if no response
-
 
192
}
215
}
193
 
216
 
194
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
217
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
195
{
218
{
196
    unsigned int number_of_modules = 0;
219
    unsigned int number_of_modules = 0;
197
   
220
   
198
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
221
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
199
    {
222
    {
200
        if (it->second->GetNodeId() == node_id)
223
        if (it->second->GetNodeId() == node_id)
201
        {
224
        {
202
            number_of_modules++;
225
            number_of_modules++;
203
        }
226
        }
204
    }
227
    }
205
   
228
   
206
    return number_of_modules;
229
    return number_of_modules;
207
}
230
}
208
 
231
 
209
 
232
 
210
}; // namespace can
233
}; // namespace can
211
}; // namespace atom
234
}; // namespace atom
212
 
235