Subversion Repositories HomeAutomation

Rev

Rev 1647 | Rev 1741 | 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/lexical_cast.hpp>
  27.  
  28. #include "vm/Manager.h"
  29. #include "timer/Manager.h"
  30.  
  31. namespace atom {
  32. namespace vm {
  33. namespace plugin {
  34.  
  35. logging::Logger Timer::LOG("vm::plugin::Timer");
  36.  
  37. Timer::Timers Timer::timers_;
  38.  
  39. Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service)
  40. {
  41.     this->name_ = "timer";
  42.    
  43.     this->ExportFunction("TimerExport_SetTimer",      Timer::Export_SetTimer);
  44.     this->ExportFunction("TimerExport_SetAlarm",      Timer::Export_SetAlarm);
  45.     this->ExportFunction("TimerExport_Cancel",        Timer::Export_Cancel);
  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::CallOutput(unsigned int request_id, std::string output)
  64. {
  65.     LOG.Info(output);
  66. }
  67.  
  68. void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat)
  69. {
  70.     this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat));
  71. }
  72.  
  73. void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
  74. {
  75.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  76.  
  77.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  78.    
  79.     if (!repeat)
  80.     {
  81.         Timers::iterator it = Timer::timers_.find(timer_id);
  82.        
  83.         if (it != Timer::timers_.end())
  84.         {
  85.             Timer::timers_.erase(it);
  86.         }
  87.     }
  88.    
  89.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  90.     arguments->push_back(v8::Integer::New(timer_id));
  91.     arguments->push_back(v8::Boolean::New(repeat));
  92.    
  93.     this->Call( timer_id, "Timer_OnTimeout",arguments);
  94. }
  95.  
  96. Value Timer::Export_SetAlarm(const v8::Arguments& args)
  97. {
  98.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  99.    
  100.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  101.    
  102.     if (args.Length() < 1)
  103.     {
  104.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  105.         return v8::Boolean::New(false);
  106.     }
  107.    
  108.     v8::String::AsciiValue time(args[0]);
  109.    
  110.     timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time);
  111.    
  112.     Timer::timers_.insert(timer_id);
  113.    
  114.     return v8::Integer::New(timer_id);
  115. }
  116.  
  117. Value Timer::Export_SetTimer(const v8::Arguments& args)
  118. {
  119.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  120.    
  121.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  122.    
  123.     if (args.Length() < 2)
  124.     {
  125.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  126.         return v8::Boolean::New(false);
  127.     }
  128.    
  129.     timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue());
  130.    
  131.     Timer::timers_.insert(timer_id);
  132.    
  133.     return v8::Integer::New(timer_id);
  134. }
  135.  
  136. Value Timer::Export_Cancel(const v8::Arguments& args)
  137. {
  138.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  139.    
  140.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  141.    
  142.     if (args.Length() < 1)
  143.     {
  144.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  145.         return v8::Boolean::New(false);
  146.     }
  147.    
  148.     Timers::iterator it = Timer::timers_.find(args[0]->Uint32Value());
  149.    
  150.     if (it != Timer::timers_.end())
  151.     {
  152.         Timer::timers_.erase(it);
  153.     }
  154.    
  155.     timer::Manager::Instance()->Cancel(args[0]->Uint32Value());
  156.     return v8::Undefined();
  157. }
  158.  
  159. Value Timer::Export_Sleep(const v8::Arguments& args)
  160. {
  161.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  162.    
  163.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  164.    
  165.     if (args.Length() < 1)
  166.     {
  167.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  168.         return v8::Boolean::New(false);
  169.     }
  170.    
  171.     boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value()));
  172.    
  173.     return v8::Undefined();
  174. }
  175.  
  176. }; // namespace plugin
  177. }; // namespace vm
  178. }; // namespace atom
  179.