Subversion Repositories HomeAutomation

Rev

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

Rev 1646 Rev 1657
Line 37... Line 37...
37
 
37
 
38
Manager::Manager() : broker::Subscriber(false), LOG("control::Manager")
38
Manager::Manager() : broker::Subscriber(false), LOG("control::Manager")
39
{
39
{
40
    Node::SetupStateMachine();
40
    Node::SetupStateMachine();
41
   
41
   
42
    this->active_programming_node_id_ = 0;
42
    this->active_programming_node_id_ = "";
43
   
43
   
44
    // 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
45
    this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true);
45
    this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true);
46
}
46
}
47
 
47
 
Line 62... Line 62...
62
void Manager::Delete()
62
void Manager::Delete()
63
{
63
{
64
    Manager::instance_.reset();
64
    Manager::instance_.reset();
65
}
65
}
66
 
66
 
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)
67
void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_)
68
{
68
{
69
    this->signal_on_node_change_.connect(signal_on_node_change_);
69
    this->signal_on_node_change_.connect(signal_on_node_change_);
-
 
70
}
-
 
71
 
-
 
72
void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change)
-
 
73
{
70
    this->signal_on_module_change_.connect(slot_on_module_change);
74
    this->signal_on_module_change_.connect(slot_on_module_change);
-
 
75
}
-
 
76
 
-
 
77
void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message)
-
 
78
{
71
    this->signal_on_module_message_.connect(slot_on_module_message);
79
    this->signal_on_module_message_.connect(slot_on_module_message);
72
}
80
}
73
 
81
 
74
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
82
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
75
{
83
{
Line 80... Line 88...
80
   
88
   
81
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
89
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
82
    {
90
    {
83
        if (!it->second->CheckTimeout())
91
        if (!it->second->CheckTimeout())
84
        {
92
        {
85
            LOG.Info("Node " + common::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
93
            LOG.Info("Node " +it->second->GetId() + " has not sent anything in a long time, setting offline.");
86
            this->RemoveModules(it->second->GetId());
94
            this->RemoveModules(it->second->GetId());
87
           
95
           
88
            storage::Manager::Instance()->FlushStore("NodeList");
96
            storage::Manager::Instance()->FlushStore("NodeList");
89
            storage::Manager::Instance()->FlushStore("ModuleList");
97
            storage::Manager::Instance()->FlushStore("ModuleList");
90
        }
98
        }
Line 100... Line 108...
100
        Node::Pointer node;
108
        Node::Pointer node;
101
       
109
       
102
        if (payload->GetClassName() == "nmt")
110
        if (payload->GetClassName() == "nmt")
103
        {
111
        {
104
            if (payload->GetCommandName() == "Bios_Start")
112
            if (payload->GetCommandName() == "Bios_Start")
105
            {
113
            {
106
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
114
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
107
                node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
115
                node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
108
            }
116
            }
109
            else if (payload->GetCommandName() == "App_Start")
117
            else if (payload->GetCommandName() == "App_Start")
110
            {
118
            {
111
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
119
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
112
                node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
120
                node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
113
            }
121
            }
114
            else if (payload->GetCommandName() == "Heartbeat")
122
            else if (payload->GetCommandName() == "Heartbeat")
115
            {
123
            {
116
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
124
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
117
                node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
125
                node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
118
            }
126
            }
119
            else if (payload->GetCommandName() == "Pgm_Ack")
127
            else if (payload->GetCommandName() == "Pgm_Ack")
120
            {
128
            {
121
                node = this->GetNode(this->active_programming_node_id_);
129
                node = this->GetNode(this->active_programming_node_id_);
Line 131... Line 139...
131
        {
139
        {
132
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
140
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
133
           
141
           
134
            if (payload->GetCommandName() == "List")
142
            if (payload->GetCommandName() == "List")
135
            {
143
            {
136
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
144
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
137
                node->ResetTimeout();
145
                node->ResetTimeout();
138
               
146
               
139
                module->SetNodeId(node->GetId());
147
                module->SetNodeId(node->GetId());
140
               
148
               
141
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
149
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
142
                {
150
                {
143
                    node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
151
                    node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
144
                }
152
                }
145
               
153
               
146
                LOG.Info("Module " + module->GetFullId() + " is available on node " + common::ToHex(node->GetId()) + ".");
154
                LOG.Info("Module " + module->GetFullId() + " is available on node " + node->GetId() + ".");
147
                this->signal_on_module_change_(module->GetFullId(), true);
155
                this->signal_on_module_change_(module->GetFullId(), true);
148
            }
156
            }
149
            else
157
            else if (module->GetNodeId() != "")
150
            {
158
            {
151
                node = this->GetNode(module->GetNodeId());
159
                node = this->GetNode(module->GetNodeId());
152
                node->ResetTimeout();
160
                node->ResetTimeout();
153
               
161
               
154
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
162
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
Line 160... Line 168...
160
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
168
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
161
{
169
{
162
    if (target_state != Node::STATE_NORM_INITIALIZED)
170
    if (target_state != Node::STATE_NORM_INITIALIZED)
163
    {
171
    {
164
        this->RemoveModules(node_id);
172
        this->RemoveModules(node_id);
-
 
173
 
-
 
174
        if (current_state != target_state)
-
 
175
        {
-
 
176
            this->signal_on_node_change_(node_id, false);
-
 
177
        }
-
 
178
    }
-
 
179
    else
-
 
180
    {
-
 
181
        if (current_state != target_state)
-
 
182
        {
-
 
183
            this->signal_on_node_change_(node_id, true);
-
 
184
        }
165
    }
185
    }
166
   
186
   
167
    if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
187
    if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
168
    {
188
    {
169
        this->active_programming_node_id_ = node_id;
189
        this->active_programming_node_id_ = node_id;
170
    }
190
    }
171
    else if (target_state == Node::STATE_NORM_OFFLINE)
191
    else if (target_state == Node::STATE_NORM_OFFLINE)
172
    {
192
    {
173
        this->active_programming_node_id_ = 0;
193
        this->active_programming_node_id_ = "";
174
    }
194
    }
175
}
195
}
176
 
196
 
177
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
197
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
178
{
198
{
Line 194... Line 214...
194
    }
214
    }
195
    catch (std::runtime_error& e)
215
    catch (std::runtime_error& e)
196
    {
216
    {
197
        LOG.Error("Failed to send message, " + std::string(e.what()));
217
        LOG.Error("Failed to send message, " + std::string(e.what()));
198
    }
218
    }
199
}
219
}
200
 
220
 
201
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
221
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
202
{
222
{
203
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
223
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
204
}
224
}
205
 
225
 
206
common::StringList Manager::GetAvailableModules()
226
common::StringList Manager::GetAvailableModules()
207
{
227
{
208
    common::StringList available_modules;
228
    common::StringList available_modules;
209
   
229
   
210
    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++)
211
    {
231
    {
212
        available_modules.push_back(it->first);
232
        available_modules.push_back(it->first);
213
    }
233
    }
214
   
234
   
215
    return available_modules;
235
    return available_modules;
-
 
236
}
-
 
237
 
-
 
238
common::StringList Manager::GetAvailableNodes()
-
 
239
{
-
 
240
    common::StringList available_nodes;
-
 
241
   
-
 
242
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
-
 
243
    {
-
 
244
        std::string node = it->first;
-
 
245
 
-
 
246
        for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++)
-
 
247
        {
-
 
248
            if (it2->second->GetNodeId() == it->first)
-
 
249
            {
-
 
250
                node += "," + it2->first;
-
 
251
            }
-
 
252
        }
-
 
253
       
-
 
254
        available_nodes.push_back(node);
-
 
255
    }
-
 
256
   
-
 
257
    return available_nodes;
-
 
258
}
-
 
259
 
-
 
260
bool Manager::ResetNode(Node::Id node_id)
-
 
261
{
-
 
262
    NodeList::iterator it = this->nodes_.find(node_id);
-
 
263
   
-
 
264
    if (it == this->nodes_.end())
-
 
265
    {
-
 
266
        LOG.Error("Could not find node " +  node_id);
-
 
267
        return false;
-
 
268
    }
-
 
269
   
-
 
270
    it->second->Reset();
-
 
271
    return true;
216
}
272
}
217
 
273
 
218
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
274
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
219
{
275
{
220
    NodeList::iterator it = this->nodes_.find(node_id);
276
    NodeList::iterator it = this->nodes_.find(node_id);
221
   
277
   
222
    if (it == this->nodes_.end())
278
    if (it == this->nodes_.end())
223
    {
279
    {
224
        LOG.Error("Could not find node " +  common::ToHex(node_id));
280
        LOG.Error("Could not find node " +  node_id);
225
        return false;
281
        return false;
226
    }
282
    }
227
   
283
   
228
    Code::Pointer code = Code::Pointer(new Code());
284
    Code::Pointer code = Code::Pointer(new Code());
229
    if (!code->LoadIntelHexFile(filename))
285
    if (!code->LoadIntelHexFile(filename))