Subversion Repositories HomeAutomation

Rev

Rev 1619 | Rev 1633 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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