Subversion Repositories HomeAutomation

Rev

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

Rev 1601 Rev 1602
Line 54... Line 54...
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_)
Line 90... Line 96...
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...");
Line 130... Line 136...
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
{
Line 195... Line 230...
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
}