Subversion Repositories HomeAutomation

Rev

Rev 1644 | Rev 1657 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1644 Rev 1646
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 "common/common.h"
27
#include "common/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 "can/Message.h"
31
#include "can/Message.h"
32
 
32
 
33
namespace atom {
33
namespace atom {
34
namespace control {
34
namespace control {
35
   
35
   
36
Manager::Pointer Manager::instance_;
36
Manager::Pointer Manager::instance_;
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_ = 0;
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()->Set(20000, true);
45
    this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true);
46
}
46
}
47
 
47
 
48
Manager::~Manager()
48
Manager::~Manager()
49
{
49
{
50
}
50
}
51
 
51
 
52
Manager::Pointer Manager::Instance()
52
Manager::Pointer Manager::Instance()
53
{
53
{
54
    return Manager::instance_;
54
    return Manager::instance_;
55
}
55
}
56
 
56
 
57
void Manager::Create()
57
void Manager::Create()
58
{
58
{
59
    Manager::instance_ = Manager::Pointer(new Manager());
59
    Manager::instance_ = Manager::Pointer(new Manager());
60
}
60
}
61
 
61
 
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::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)
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
    this->signal_on_module_change_.connect(slot_on_module_change);
70
    this->signal_on_module_change_.connect(slot_on_module_change);
71
    this->signal_on_module_message_.connect(slot_on_module_message);
71
    this->signal_on_module_message_.connect(slot_on_module_message);
72
}
72
}
73
 
73
 
74
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
74
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
75
{
75
{
76
    if (timer_id != this->timer_id_)
76
    if (timer_id != this->timer_id_)
77
    {
77
    {
78
        return;
78
        return;
79
    }
79
    }
80
   
80
   
81
    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++)
82
    {
82
    {
83
        if (!it->second->CheckTimeout())
83
        if (!it->second->CheckTimeout())
84
        {
84
        {
85
            LOG.Info("Node " + common::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
85
            LOG.Info("Node " + common::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline.");
86
            this->RemoveModules(it->second->GetId());
86
            this->RemoveModules(it->second->GetId());
87
           
87
           
88
            storage::Manager::Instance()->FlushStore("NodeList");
88
            storage::Manager::Instance()->FlushStore("NodeList");
89
            storage::Manager::Instance()->FlushStore("ModuleList");
89
            storage::Manager::Instance()->FlushStore("ModuleList");
90
        }
90
        }
91
    }
91
    }
92
}
92
}
93
 
93
 
94
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
94
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
95
{
95
{
96
    if (message->GetType() == broker::Message::CAN_MESSAGE)
96
    if (message->GetType() == broker::Message::CAN_MESSAGE)
97
    {
97
    {
98
        //LOG.Debug("Message received");
98
        //LOG.Debug("Message received");
99
        can::Message* payload = static_cast<can::Message*>(message->GetPayload().get());
99
        can::Message* payload = static_cast<can::Message*>(message->GetPayload().get());
100
        Node::Pointer node;
100
        Node::Pointer node;
101
       
101
       
102
        if (payload->GetClassName() == "nmt")
102
        if (payload->GetClassName() == "nmt")
103
        {
103
        {
104
            if (payload->GetCommandName() == "Bios_Start")
104
            if (payload->GetCommandName() == "Bios_Start")
105
            {
105
            {
106
                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->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
107
                node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
108
            }
108
            }
109
            else if (payload->GetCommandName() == "App_Start")
109
            else if (payload->GetCommandName() == "App_Start")
110
            {
110
            {
111
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
111
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
112
                node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
112
                node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
113
            }
113
            }
114
            else if (payload->GetCommandName() == "Heartbeat")
114
            else if (payload->GetCommandName() == "Heartbeat")
115
            {
115
            {
116
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
116
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
117
                node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
117
                node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
118
            }
118
            }
119
            else if (payload->GetCommandName() == "Pgm_Ack")
119
            else if (payload->GetCommandName() == "Pgm_Ack")
120
            {
120
            {
121
                node = this->GetNode(this->active_programming_node_id_);
121
                node = this->GetNode(this->active_programming_node_id_);
122
                node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables());
122
                node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables());
123
            }
123
            }
124
            else if (payload->GetCommandName() == "Pgm_Nack")
124
            else if (payload->GetCommandName() == "Pgm_Nack")
125
            {
125
            {
126
                node = this->GetNode(this->active_programming_node_id_);
126
                node = this->GetNode(this->active_programming_node_id_);
127
                node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables());
127
                node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables());
128
            }
128
            }
129
        }
129
        }
130
        else if (payload->GetDirectionName() == "From_Owner")
130
        else if (payload->GetDirectionName() == "From_Owner")
131
        {
131
        {
132
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
132
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
133
           
133
           
134
            if (payload->GetCommandName() == "List")
134
            if (payload->GetCommandName() == "List")
135
            {
135
            {
136
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
136
                node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]));
137
                node->ResetTimeout();
137
                node->ResetTimeout();
138
               
138
               
139
                module->SetNodeId(node->GetId());
139
                module->SetNodeId(node->GetId());
140
               
140
               
141
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
141
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
142
                {
142
                {
143
                    node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
143
                    node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
144
                }
144
                }
145
               
145
               
146
                LOG.Info("Module " + module->GetFullId() + " is available on node " + common::ToHex(node->GetId()) + ".");
146
                LOG.Info("Module " + module->GetFullId() + " is available on node " + common::ToHex(node->GetId()) + ".");
147
                this->signal_on_module_change_(module->GetFullId(), true);
147
                this->signal_on_module_change_(module->GetFullId(), true);
148
            }
148
            }
149
            else
149
            else
150
            {
150
            {
151
                node = this->GetNode(module->GetNodeId());
151
                node = this->GetNode(module->GetNodeId());
152
                node->ResetTimeout();
152
                node->ResetTimeout();
153
               
153
               
154
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
154
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
155
            }
155
            }
156
        }
156
        }
157
    }
157
    }
158
}
158
}
159
 
159
 
160
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
160
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
161
{
161
{
162
    if (target_state != Node::STATE_NORM_INITIALIZED)
162
    if (target_state != Node::STATE_NORM_INITIALIZED)
163
    {
163
    {
164
        this->RemoveModules(node_id);
164
        this->RemoveModules(node_id);
165
    }
165
    }
166
   
166
   
167
    if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
167
    if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
168
    {
168
    {
169
        this->active_programming_node_id_ = node_id;
169
        this->active_programming_node_id_ = node_id;
170
    }
170
    }
171
    else if (target_state == Node::STATE_NORM_OFFLINE)
171
    else if (target_state == Node::STATE_NORM_OFFLINE)
172
    {
172
    {
173
        this->active_programming_node_id_ = 0;
173
        this->active_programming_node_id_ = 0;
174
    }
174
    }
175
}
175
}
176
 
176
 
177
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
177
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
178
{
178
{
179
    ModuleList::iterator it = this->modules_.find(full_id);
179
    ModuleList::iterator it = this->modules_.find(full_id);
180
   
180
   
181
    if (it == this->modules_.end())
181
    if (it == this->modules_.end())
182
    {
182
    {
183
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
183
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
184
        return;
184
        return;
185
    }
185
    }
186
   
186
   
187
    try
187
    try
188
    {
188
    {
189
        can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
189
        can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
190
        payload->SetVariables(variables);
190
        payload->SetVariables(variables);
191
       
191
       
192
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
192
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
193
        //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
193
        //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
194
    }
194
    }
195
    catch (std::runtime_error& e)
195
    catch (std::runtime_error& e)
196
    {
196
    {
197
        LOG.Error("Failed to send message, " + std::string(e.what()));
197
        LOG.Error("Failed to send message, " + std::string(e.what()));
198
    }
198
    }
199
}
199
}
200
 
200
 
201
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
201
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
202
{
202
{
203
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
203
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
204
}
204
}
205
 
205
 
206
common::StringList Manager::GetAvailableModules()
206
common::StringList Manager::GetAvailableModules()
207
{
207
{
208
    common::StringList available_modules;
208
    common::StringList available_modules;
209
   
209
   
210
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
210
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
211
    {
211
    {
212
        available_modules.push_back(it->first);
212
        available_modules.push_back(it->first);
213
    }
213
    }
214
   
214
   
215
    return available_modules;
215
    return available_modules;
216
}
216
}
217
 
217
 
218
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
218
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
219
{
219
{
220
    NodeList::iterator it = this->nodes_.find(node_id);
220
    NodeList::iterator it = this->nodes_.find(node_id);
221
   
221
   
222
    if (it == this->nodes_.end())
222
    if (it == this->nodes_.end())
223
    {
223
    {
224
        LOG.Error("Could not find node " +  common::ToHex(node_id));
224
        LOG.Error("Could not find node " +  common::ToHex(node_id));
225
        return false;
225
        return false;
226
    }
226
    }
227
   
227
   
228
    Code::Pointer code = Code::Pointer(new Code());
228
    Code::Pointer code = Code::Pointer(new Code());
229
    if (!code->LoadIntelHexFile(filename))
229
    if (!code->LoadIntelHexFile(filename))
230
    {
230
    {
231
        LOG.Error("Failed to parse " + filename);
231
        LOG.Error("Failed to parse " + filename);
232
        return false;
232
        return false;
233
    }
233
    }
234
   
234
   
235
    if (is_bios)
235
    if (is_bios)
236
    {
236
    {
237
        LOG.Debug("is_bios true");
237
        LOG.Debug("is_bios true");
238
        it->second->ProgramBios(code);
238
        it->second->ProgramBios(code);
239
    }
239
    }
240
    else
240
    else
241
    {
241
    {
242
        LOG.Debug("is_bios false");
242
        LOG.Debug("is_bios false");
243
        it->second->ProgramApplication(code);
243
        it->second->ProgramApplication(code);
244
    }
244
    }
245
   
245
   
246
    return true;
246
    return true;
247
}
247
}
248
 
248
 
249
Node::Pointer Manager::GetNode(Node::Id node_id)
249
Node::Pointer Manager::GetNode(Node::Id node_id)
250
{
250
{
251
    Node::Pointer node;
251
    Node::Pointer node;
252
    NodeList::iterator it = this->nodes_.find(node_id);
252
    NodeList::iterator it = this->nodes_.find(node_id);
253
   
253
   
254
    if (it == this->nodes_.end())
254
    if (it == this->nodes_.end())
255
    {
255
    {
256
        node = Node::Pointer(new Node(node_id));
256
        node = Node::Pointer(new Node(node_id));
257
        node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_));
257
        node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_));
258
        this->nodes_[node_id] = node;
258
        this->nodes_[node_id] = node;
259
    }
259
    }
260
    else
260
    else
261
    {
261
    {
262
        node = it->second;
262
        node = it->second;
263
    }
263
    }
264
   
264
   
265
    return node;
265
    return node;
266
}
266
}
267
 
267
 
268
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
268
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
269
{
269
{
270
    Module::Pointer module;
270
    Module::Pointer module;
271
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
271
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
272
   
272
   
273
    if (it == this->modules_.end())
273
    if (it == this->modules_.end())
274
    {
274
    {
275
        module = Module::Pointer(new Module(module_id, module_name, class_name));
275
        module = Module::Pointer(new Module(module_id, module_name, class_name));
276
        this->modules_[module->GetFullId()] = module;
276
        this->modules_[module->GetFullId()] = module;
277
    }
277
    }
278
    else
278
    else
279
    {
279
    {
280
        module = it->second;
280
        module = it->second;
281
    }
281
    }
282
   
282
   
283
    return module;
283
    return module;
284
}
284
}
285
 
285
 
286
void Manager::RemoveModules(Node::Id node_id)
286
void Manager::RemoveModules(Node::Id node_id)
287
{
287
{
288
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
288
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
289
    {
289
    {
290
        if (it->second->GetNodeId() == node_id)
290
        if (it->second->GetNodeId() == node_id)
291
        {
291
        {
292
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
292
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
293
            this->signal_on_module_change_(it->second->GetFullId(), false);
293
            this->signal_on_module_change_(it->second->GetFullId(), false);
294
           
294
           
295
            this->modules_.erase(it);
295
            this->modules_.erase(it);
296
        }
296
        }
297
    }
297
    }
298
}
298
}
299
 
299
 
300
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
300
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
301
{
301
{
302
    unsigned int number_of_modules = 0;
302
    unsigned int number_of_modules = 0;
303
   
303
   
304
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
304
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
305
    {
305
    {
306
        if (it->second->GetNodeId() == node_id)
306
        if (it->second->GetNodeId() == node_id)
307
        {
307
        {
308
            number_of_modules++;
308
            number_of_modules++;
309
        }
309
        }
310
    }
310
    }
311
   
311
   
312
    return number_of_modules;
312
    return number_of_modules;
313
}
313
}
314
 
314
 
315
}; // namespace control
315
}; // namespace control
316
}; // namespace atom
316
}; // namespace atom
317
 
317