Subversion Repositories HomeAutomation

Rev

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

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