Subversion Repositories HomeAutomation

Rev

Rev 1644 | Rev 1647 | 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_SetTimer",      Timer::Export_SetTimer);
  45.     this->ExportFunction("TimerExport_SetAlarm",      Timer::Export_SetAlarm);
  46.     this->ExportFunction("TimerExport_Cancel",        Timer::Export_Cancel);
  47.     this->ExportFunction("TimerExport_Sleep",         Timer::Export_Sleep);
  48. }
  49.  
  50. Timer::~Timer()
  51. {
  52.    
  53. }
  54.  
  55. void Timer::InitializeDone()
  56. {
  57.     Plugin::InitializeDone();
  58.    
  59.     this->ImportFunction("Timer_OnTimeout");
  60.    
  61.     timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_));
  62. }
  63.  
  64. void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat)
  65. {
  66.     this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat));
  67. }
  68.  
  69. void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
  70. {
  71.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  72.  
  73.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  74.    
  75.     if (!repeat)
  76.     {
  77.         Timers::iterator it = Timer::timers_.find(timer_id);
  78.        
  79.         if (it != Timer::timers_.end())
  80.         {
  81.             Timer::timers_.erase(it);
  82.         }
  83.     }
  84.    
  85.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  86.     arguments->push_back(v8::Integer::New(timer_id));
  87.     arguments->push_back(v8::Boolean::New(repeat));
  88.    
  89.     this->Call( timer_id, "Timer_OnTimeout",arguments);
  90. }
  91.  
  92. Value Timer::Export_SetAlarm(const v8::Arguments& args)
  93. {
  94.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  95.    
  96.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  97.    
  98.     if (args.Length() < 1)
  99.     {
  100.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  101.     }
  102.    
  103.     v8::String::AsciiValue time(args[0]);
  104.    
  105.     timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time);
  106.    
  107.     Timer::timers_.insert(timer_id);
  108.    
  109.     return v8::Integer::New(timer_id);
  110. }
  111.  
  112. Value Timer::Export_SetTimer(const v8::Arguments& args)
  113. {
  114.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  115.    
  116.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  117.    
  118.     if (args.Length() < 2)
  119.     {
  120.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  121.     }
  122.    
  123.     timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue());
  124.    
  125.     Timer::timers_.insert(timer_id);
  126.    
  127.     return v8::Integer::New(timer_id);
  128. }
  129.  
  130. Value Timer::Export_Cancel(const v8::Arguments& args)
  131. {
  132.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  133.    
  134.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  135.    
  136.     if (args.Length() < 1)
  137.     {
  138.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  139.     }
  140.    
  141.     Timers::iterator it = Timer::timers_.find(args[0]->IsUint32());
  142.    
  143.     if (it != Timer::timers_.end())
  144.     {
  145.         Timer::timers_.erase(it);
  146.     }
  147.    
  148.     timer::Manager::Instance()->Cancel(args[0]->IsUint32());
  149.     return v8::Undefined();
  150. }
  151.  
  152. Value Timer::Export_Sleep(const v8::Arguments& args)
  153. {
  154.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  155.    
  156.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  157.    
  158.     if (args.Length() < 1)
  159.     {
  160.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  161.     }
  162.    
  163.     boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value()));
  164.    
  165.     return v8::Undefined();
  166. }
  167.  
  168. }; // namespace plugin
  169. }; // namespace vm
  170. }; // namespace atom
  171.