Subversion Repositories HomeAutomation

Rev

Rev 2082 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1598 runge 1
/*
2081 runge 2
 *
1598 runge 3
 *  Copyright (C) 2010  Mattias Runge
2081 runge 4
 *
1598 runge 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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
2081 runge 9
 *
1598 runge 10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
2081 runge 14
 *
1598 runge 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.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2081 runge 18
 *
1598 runge 19
 */
20
 
21
#include "Manager.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
 
25
#include "broker/Manager.h"
26
#include "broker/Message.h"
1642 runge 27
#include "common/common.h"
1601 runge 28
#include "timer/Manager.h"
1627 runge 29
#include "storage/Manager.h"
1971 runge 30
#include "common/log.h"
1601 runge 31
 
1642 runge 32
#include "can/Message.h"
1598 runge 33
 
34
namespace atom {
1642 runge 35
namespace control {
2081 runge 36
 
1599 runge 37
Manager::Pointer Manager::instance_;
1598 runge 38
 
1971 runge 39
static const std::string log_module_ = "control::manager";
40
 
41
Manager::Manager() : broker::Subscriber(false)
1598 runge 42
{
1971 runge 43
  LOG_DEBUG_ENTER;
2081 runge 44
 
1971 runge 45
  Node::SetupStateMachine();
2081 runge 46
 
1971 runge 47
  this->active_programming_node_id_ = "";
2081 runge 48
 
1971 runge 49
  // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds
50
  this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true);
2081 runge 51
 
1971 runge 52
  LOG_DEBUG_EXIT;
1598 runge 53
}
54
 
55
Manager::~Manager()
56
{
1971 runge 57
  LOG_DEBUG_ENTER;
58
  LOG_DEBUG_EXIT;
1598 runge 59
}
60
 
61
Manager::Pointer Manager::Instance()
62
{
1971 runge 63
  LOG_DEBUG_ENTER;
64
  LOG_DEBUG_EXIT;
2081 runge 65
 
1971 runge 66
  return Manager::instance_;
1598 runge 67
}
68
 
1599 runge 69
void Manager::Create()
70
{
1971 runge 71
  LOG_DEBUG_ENTER;
2081 runge 72
 
1971 runge 73
  Manager::instance_ = Manager::Pointer(new Manager());
2081 runge 74
 
1971 runge 75
  LOG_DEBUG_EXIT;
1599 runge 76
}
77
 
1598 runge 78
void Manager::Delete()
79
{
1971 runge 80
  LOG_DEBUG_ENTER;
2081 runge 81
 
1971 runge 82
  Manager::instance_.reset();
2081 runge 83
 
1971 runge 84
  LOG_DEBUG_EXIT;
1598 runge 85
}
86
 
1657 runge 87
void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_)
1602 runge 88
{
1971 runge 89
  LOG_DEBUG_ENTER;
2081 runge 90
 
1971 runge 91
  this->signal_on_node_change_.connect(signal_on_node_change_);
2081 runge 92
 
1971 runge 93
  LOG_DEBUG_EXIT;
1657 runge 94
}
95
 
96
void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change)
97
{
1971 runge 98
  LOG_DEBUG_ENTER;
2081 runge 99
 
1971 runge 100
  this->signal_on_module_change_.connect(slot_on_module_change);
2081 runge 101
 
1971 runge 102
  LOG_DEBUG_EXIT;
1657 runge 103
}
104
 
105
void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message)
106
{
1971 runge 107
  LOG_DEBUG_ENTER;
2081 runge 108
 
1971 runge 109
  this->signal_on_module_message_.connect(slot_on_module_message);
2081 runge 110
 
1971 runge 111
  LOG_DEBUG_EXIT;
1602 runge 112
}
113
 
1601 runge 114
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
115
{
1971 runge 116
  LOG_DEBUG_ENTER;
2081 runge 117
 
1971 runge 118
  if (timer_id != this->timer_id_)
119
  {
120
    return;
121
  }
2081 runge 122
 
1971 runge 123
  for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
124
  {
125
    if (!it->second->CheckTimeout())
1601 runge 126
    {
1971 runge 127
      log::Info(log_module_, "Node %s has not sent anything in a long time, setting offline.", it->second->GetId().c_str());
2081 runge 128
 
1971 runge 129
      this->RemoveModules(it->second->GetId());
2081 runge 130
 
1971 runge 131
      storage::Manager::Instance()->FlushStore("NodeList");
132
      storage::Manager::Instance()->FlushStore("ModuleList");
1601 runge 133
    }
1971 runge 134
  }
2081 runge 135
 
1971 runge 136
  LOG_DEBUG_EXIT;
1601 runge 137
}
138
 
1598 runge 139
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
140
{
1971 runge 141
  LOG_DEBUG_ENTER;
142
 
143
  if (message->GetType() == broker::Message::CAN_MESSAGE)
144
  {
145
    //LOG.Debug("Message received");
146
    can::Message* payload = static_cast<can::Message*>(message->GetPayload().get());
147
    Node::Pointer node;
2081 runge 148
 
1971 runge 149
    if (payload->GetClassName() == "nmt")
1598 runge 150
    {
1971 runge 151
      if (payload->GetCommandName() == "Bios_Start")
152
      {
153
        node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
154
        node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
155
      }
156
      else if (payload->GetCommandName() == "App_Start")
157
      {
158
        node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
159
        node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
160
      }
161
      else if (payload->GetCommandName() == "Heartbeat")
162
      {
163
        node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
164
        node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
165
      }
166
      else if (payload->GetCommandName() == "Pgm_Ack")
167
      {
168
        node = this->GetNode(this->active_programming_node_id_);
169
        node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables());
170
      }
171
      else if (payload->GetCommandName() == "Pgm_Nack")
172
      {
173
        node = this->GetNode(this->active_programming_node_id_);
174
        node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables());
175
      }
176
    }
177
    else if (payload->GetDirectionName() == "From_Owner")
178
    {
179
      Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
2081 runge 180
 
1971 runge 181
      if (payload->GetCommandName() == "List")
182
      {
183
        node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
184
        node->ResetTimeout();
2081 runge 185
 
1971 runge 186
        module->SetNodeId(node->GetId());
2081 runge 187
 
1971 runge 188
        if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
1598 runge 189
        {
1971 runge 190
          node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
1598 runge 191
        }
2081 runge 192
 
1971 runge 193
        log::Info(log_module_, "Module %s is available on node %s.", module->GetFullId().c_str(), node->GetId().c_str());
194
        this->signal_on_module_change_(module->GetFullId(), true);
195
      }
196
      else if (module->GetNodeId() != "")
197
      {
198
        node = this->GetNode(module->GetNodeId());
199
        node->ResetTimeout();
2081 runge 200
 
1971 runge 201
        this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
202
      }
2081 runge 203
      else
204
      {
2082 runge 205
        log::Info(log_module_, "Nothing found, id=%d, modulename=%s, classname=%s", payload->GetId(), payload->GetModuleName().data(), payload->GetClassName().data());
2081 runge 206
      }
1598 runge 207
    }
1971 runge 208
  }
2081 runge 209
 
1971 runge 210
  LOG_DEBUG_EXIT;
1598 runge 211
}
212
 
1627 runge 213
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
214
{
1971 runge 215
  LOG_DEBUG_ENTER;
2081 runge 216
 
1971 runge 217
  if (target_state != Node::STATE_NORM_INITIALIZED)
218
  {
219
    this->RemoveModules(node_id);
220
  }
2081 runge 221
 
1971 runge 222
  if (current_state != target_state)
223
  {
224
    this->signal_on_node_change_(node_id, (target_state == Node::STATE_NORM_INITIALIZED || target_state == Node::STATE_NORM_LIST || target_state == Node::STATE_NORM_ONLINE));
225
  }
2081 runge 226
 
1971 runge 227
  if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
228
  {
229
    this->active_programming_node_id_ = node_id;
230
  }
231
  else if (target_state == Node::STATE_NORM_OFFLINE)
232
  {
233
    this->active_programming_node_id_ = "";
234
  }
2081 runge 235
 
1971 runge 236
  LOG_DEBUG_EXIT;
1627 runge 237
}
238
 
1642 runge 239
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
1602 runge 240
{
1971 runge 241
  LOG_DEBUG_ENTER;
2081 runge 242
 
1971 runge 243
  ModuleList::iterator it = this->modules_.find(full_id);
2081 runge 244
 
1971 runge 245
  if (it == this->modules_.end())
246
  {
247
    log::Warning(log_module_, "Can not send message to %s , it is not available.", full_id.c_str());
248
    return;
249
  }
2081 runge 250
 
1971 runge 251
  try
252
  {
253
    can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
254
    payload->SetVariables(variables);
2081 runge 255
 
1971 runge 256
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
257
    //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
258
  }
259
  catch (std::runtime_error& e)
260
  {
261
    log::Error(log_module_, "Failed to send message, %s", e.what());
262
  }
2081 runge 263
 
1971 runge 264
  LOG_DEBUG_EXIT;
1602 runge 265
}
266
 
1642 runge 267
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
1604 runge 268
{
1971 runge 269
  LOG_DEBUG_ENTER;
2081 runge 270
 
1971 runge 271
  this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
2081 runge 272
 
1971 runge 273
  LOG_DEBUG_EXIT;
1604 runge 274
}
275
 
1644 runge 276
common::StringList Manager::GetAvailableModules()
1604 runge 277
{
1971 runge 278
  LOG_DEBUG_ENTER;
2081 runge 279
 
1971 runge 280
  common::StringList available_modules;
2081 runge 281
 
1971 runge 282
  for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
283
  {
284
    available_modules.push_back(it->first);
285
  }
2081 runge 286
 
1971 runge 287
  LOG_DEBUG_EXIT;
2081 runge 288
 
1971 runge 289
  return available_modules;
1604 runge 290
}
291
 
1657 runge 292
common::StringList Manager::GetAvailableNodes()
293
{
1971 runge 294
  LOG_DEBUG_ENTER;
2081 runge 295
 
1971 runge 296
  common::StringList available_nodes;
2081 runge 297
 
1971 runge 298
  for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
299
  {
300
    std::string node = it->first;
301
 
302
    for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++)
1657 runge 303
    {
1971 runge 304
      if (it2->second->GetNodeId() == it->first)
305
      {
306
        node += "," + it2->first;
307
      }
1657 runge 308
    }
2081 runge 309
 
1971 runge 310
    available_nodes.push_back(node);
311
  }
2081 runge 312
 
1971 runge 313
  LOG_DEBUG_EXIT;
2081 runge 314
 
1971 runge 315
  return available_nodes;
1657 runge 316
}
317
 
318
bool Manager::ResetNode(Node::Id node_id)
319
{
1971 runge 320
  LOG_DEBUG_ENTER;
2081 runge 321
 
1971 runge 322
  NodeList::iterator it = this->nodes_.find(node_id);
2081 runge 323
 
1971 runge 324
  if (it == this->nodes_.end())
325
  {
326
      log::Error(log_module_, "Could not find node %s.", node_id.c_str());
327
      return false;
328
  }
2081 runge 329
 
1971 runge 330
  it->second->Reset();
2081 runge 331
 
1971 runge 332
  LOG_DEBUG_EXIT;
2081 runge 333
 
1971 runge 334
  return true;
1657 runge 335
}
336
 
2164 arune 337
unsigned int Manager::GetProgramProgress(Node::Id node_id)
338
{
339
  LOG_DEBUG_ENTER;
340
 
341
  NodeList::iterator it = this->nodes_.find(node_id);
342
 
343
  if (it == this->nodes_.end())
344
  {
345
    log::Error(log_module_, "Could not find node %s.", node_id.c_str());
346
    return 0;
347
  }
348
 
349
  return it->second->GetProgramProgress();
350
 
351
  LOG_DEBUG_EXIT;
352
}
353
 
1642 runge 354
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
355
{
1971 runge 356
  LOG_DEBUG_ENTER;
357
 
358
  NodeList::iterator it = this->nodes_.find(node_id);
2081 runge 359
 
1971 runge 360
  if (it == this->nodes_.end())
361
  {
362
    log::Error(log_module_, "Could not find node %s.", node_id.c_str());
363
    return false;
364
  }
2081 runge 365
 
1971 runge 366
  Code::Pointer code = Code::Pointer(new Code());
367
  if (!code->LoadIntelHexFile(filename))
368
  {
369
    log::Error(log_module_, "Could not parse %s.", filename.c_str());
370
    return false;
371
  }
2081 runge 372
 
1971 runge 373
  if (is_bios)
374
  {
375
    log::Debug(log_module_, "is_bios true");
376
    it->second->ProgramBios(code);
377
  }
378
  else
379
  {
380
    log::Debug(log_module_, "is_bios false");
381
    it->second->ProgramApplication(code);
382
  }
2081 runge 383
 
1971 runge 384
  LOG_DEBUG_EXIT;
2081 runge 385
 
1971 runge 386
  return true;
1642 runge 387
}
388
 
1887 arune 389
/* Program node with hexdata as argument */
390
bool Manager::ProgramNodeHex(Node::Id node_id, bool is_bios, std::string hex_data)
391
{
1971 runge 392
  LOG_DEBUG_ENTER;
2081 runge 393
 
1971 runge 394
  NodeList::iterator it = this->nodes_.find(node_id);
2081 runge 395
 
1971 runge 396
  if (it == this->nodes_.end())
397
  {
398
    log::Error(log_module_, "Could not find node %s.", node_id.c_str());
399
    return false;
400
  }
2081 runge 401
 
1971 runge 402
  Code::Pointer code = Code::Pointer(new Code());
2081 runge 403
 
1971 runge 404
  if (!code->ParseIntelHex(hex_data))
405
  {
406
    log::Error(log_module_, "Could not parse hexdata %s.", hex_data.c_str());
407
    return false;
408
  }
2081 runge 409
 
1971 runge 410
  if (is_bios)
411
  {
412
    log::Debug(log_module_, "is_bios true");
413
    it->second->ProgramBios(code);
414
  }
415
  else
416
  {
417
    log::Debug(log_module_, "is_bios false");
418
    it->second->ProgramApplication(code);
419
  }
2081 runge 420
 
1971 runge 421
  LOG_DEBUG_EXIT;
2081 runge 422
 
1971 runge 423
  return true;
1887 arune 424
}
425
 
1665 runge 426
Node::Information Manager::GetNodeInformation(Node::Id node_id)
427
{
1971 runge 428
  LOG_DEBUG_ENTER;
2081 runge 429
 
1971 runge 430
  NodeList::iterator it = this->nodes_.find(node_id);
2081 runge 431
 
1971 runge 432
  if (it == this->nodes_.end())
433
  {
434
    throw std::runtime_error("No such node exists, " + node_id);
435
  }
2081 runge 436
 
1971 runge 437
  LOG_DEBUG_EXIT;
2081 runge 438
 
1971 runge 439
  return it->second->GetInformation();
1665 runge 440
}
441
 
1598 runge 442
Node::Pointer Manager::GetNode(Node::Id node_id)
443
{
1971 runge 444
  LOG_DEBUG_ENTER;
2081 runge 445
 
1971 runge 446
  Node::Pointer node;
447
  NodeList::iterator it = this->nodes_.find(node_id);
2081 runge 448
 
1971 runge 449
  if (it == this->nodes_.end())
450
  {
451
    node = Node::Pointer(new Node(node_id));
452
    node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_));
453
    this->nodes_[node_id] = node;
454
  }
455
  else
456
  {
457
    node = it->second;
458
  }
2081 runge 459
 
1971 runge 460
  LOG_DEBUG_EXIT;
2081 runge 461
 
1971 runge 462
  return node;
1598 runge 463
}
464
 
465
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
466
{
1971 runge 467
  LOG_DEBUG_ENTER;
2081 runge 468
 
1971 runge 469
  Module::Pointer module;
470
  ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
2081 runge 471
 
1971 runge 472
  if (it == this->modules_.end())
473
  {
474
    module = Module::Pointer(new Module(module_id, module_name, class_name));
475
    this->modules_[module->GetFullId()] = module;
476
  }
477
  else
478
  {
479
    module = it->second;
480
  }
2081 runge 481
 
1971 runge 482
  LOG_DEBUG_EXIT;
2081 runge 483
 
1971 runge 484
  return module;
1598 runge 485
}
486
 
487
void Manager::RemoveModules(Node::Id node_id)
488
{
1971 runge 489
  LOG_DEBUG_ENTER;
2081 runge 490
 
1971 runge 491
  for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
492
  {
493
    if (it->second->GetNodeId() == node_id)
1598 runge 494
    {
1971 runge 495
      log::Info(log_module_, "Module %s is unavailable.", it->second->GetFullId().c_str());
496
      this->signal_on_module_change_(it->second->GetFullId(), false);
2081 runge 497
 
1971 runge 498
      this->modules_.erase(it);
499
      break;
1598 runge 500
    }
1971 runge 501
  }
2081 runge 502
 
1971 runge 503
  LOG_DEBUG_EXIT;
1598 runge 504
}
505
 
506
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
507
{
1971 runge 508
  LOG_DEBUG_ENTER;
2081 runge 509
 
1971 runge 510
  unsigned int number_of_modules = 0;
2081 runge 511
 
1971 runge 512
  for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
513
  {
514
    if (it->second->GetNodeId() == node_id)
1598 runge 515
    {
1971 runge 516
      number_of_modules++;
1598 runge 517
    }
1971 runge 518
  }
2081 runge 519
 
1971 runge 520
  LOG_DEBUG_EXIT;
2081 runge 521
 
1971 runge 522
  return number_of_modules;
1598 runge 523
}
524
 
1642 runge 525
}; // namespace control
1598 runge 526
}; // namespace atom