Subversion Repositories HomeAutomation

Rev

Rev 1642 | Rev 1646 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  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.
  9.  *
  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.
  14.  *
  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.
  18.  *
  19.  */
  20.  
  21. #include "Timer.h"
  22.  
  23. #include <v8-debug.h>
  24. #include <stdio.h>
  25.  
  26. #include <boost/concept_check.hpp>
  27. #include <boost/lexical_cast.hpp>
  28.  
  29. #include "vm/Manager.h"
  30. #include "timer/Manager.h"
  31.  
  32. namespace atom {
  33. namespace vm {
  34. namespace plugin {
  35.  
  36. logging::Logger Timer::LOG("vm::plugin::Timer");
  37.  
  38. Timer::Timers Timer::timers_;
  39.  
  40. Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service)
  41. {
  42.     this->name_ = "timer";
  43.    
  44.     this->ExportFunction("TimerExport_StartTimer",      Timer::Export_StartTimer);
  45.     this->ExportFunction("TimerExport_ClearTimer",      Timer::Export_ClearTimer);
  46.     this->ExportFunction("TimerExport_Sleep",           Timer::Export_Sleep);
  47. }
  48.  
  49. Timer::~Timer()
  50. {
  51.    
  52. }
  53.  
  54. void Timer::InitializeDone()
  55. {
  56.     Plugin::InitializeDone();
  57.    
  58.     this->ImportFunction("Timer_OnTimeout");
  59.    
  60.     timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_));
  61. }
  62.  
  63. void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat)
  64. {
  65.     this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat));
  66. }
  67.  
  68. void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
  69. {
  70.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  71.  
  72.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  73.    
  74.     if (!repeat)
  75.     {
  76.         Timers::iterator it = Timer::timers_.find(timer_id);
  77.        
  78.         if (it != Timer::timers_.end())
  79.         {
  80.             Timer::timers_.erase(it);
  81.         }
  82.     }
  83.    
  84.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  85.     arguments->push_back(v8::Integer::New(timer_id));
  86.     arguments->push_back(v8::Boolean::New(repeat));
  87.    
  88.     this->Call( timer_id, "Timer_OnTimeout",arguments);
  89. }
  90.  
  91. Value Timer::Export_StartTimer(const v8::Arguments& args)
  92. {
  93.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  94.    
  95.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  96.    
  97.     if (args.Length() < 2)
  98.     {
  99.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  100.     }
  101.    
  102.     timer::TimerId timer_id = timer::Manager::Instance()->Set(args[0]->Uint32Value(), args[1]->BooleanValue());
  103.    
  104.     Timer::timers_.insert(timer_id);
  105.    
  106.     return v8::Integer::New(timer_id);
  107. }
  108.  
  109. Value Timer::Export_ClearTimer(const v8::Arguments& args)
  110. {
  111.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  112.    
  113.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  114.    
  115.     if (args.Length() < 1)
  116.     {
  117.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  118.     }
  119.    
  120.     Timers::iterator it = Timer::timers_.find(args[0]->IsUint32());
  121.    
  122.     if (it != Timer::timers_.end())
  123.     {
  124.         Timer::timers_.erase(it);
  125.     }
  126.    
  127.     timer::Manager::Instance()->Cancel(args[0]->IsUint32());
  128.     return v8::Undefined();
  129. }
  130.  
  131. Value Timer::Export_Sleep(const v8::Arguments& args)
  132. {
  133.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  134.    
  135.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  136.    
  137.     if (args.Length() < 1)
  138.     {
  139.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  140.     }
  141.    
  142.     boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value()));
  143.    
  144.     return v8::Undefined();
  145. }
  146.  
  147. }; // namespace plugin
  148. }; // namespace vm
  149. }; // namespace atom
  150.