Subversion Repositories HomeAutomation

Rev

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