diff --git a/telldus.cc b/telldus.cc index 871c511..c9efb72 100644 --- a/telldus.cc +++ b/telldus.cc @@ -34,6 +34,12 @@ namespace telldus_v8 { int dataType; }; + struct RawDeviceEventBatton { + Callback *callback; + int controllerId; + const char *data; + }; + const int SUPPORTED_METHODS = TELLSTICK_TURNON | TELLSTICK_TURNOFF @@ -216,9 +222,9 @@ namespace telldus_v8 { return 0; } - void DeviceEventCallback( int deviceId, int method, const char * data, int callbackId, void* callback_void ) { + void DeviceEventCallback( int deviceId, int method, const char * data, int callbackId, void* callbackVoid ) { DeviceEventBatton *batton = new DeviceEventBatton(); - batton->callback = static_cast(callback_void); + batton->callback = static_cast(callbackVoid); batton->deviceId = deviceId; eio_nop(EIO_PRI_DEFAULT, DeviceEventCallbackAfter, batton); } @@ -255,10 +261,10 @@ namespace telldus_v8 { } void SensorEventCallback( const char *protocol, const char *model, int sensorId, int dataType, const char *value, - int ts, int callbackId, void *callback_void ) { + int ts, int callbackId, void *callbackVoid ) { SensorEventBatton *batton = new SensorEventBatton(); - batton->callback = static_cast(callback_void); + batton->callback = static_cast(callbackVoid); batton->sensorId = sensorId; batton->protocol = protocol; batton->model = model; @@ -281,6 +287,42 @@ namespace telldus_v8 { return scope.Close(num); } + int RawDataEventCallbackAfter(eio_req *req) { + HandleScope scope; + RawDeviceEventBatton *batton = static_cast(req->data); + + Local args[] = { + Number::New(batton->controllerId), + String::New(batton->data), + }; + + batton->callback->func->Call(batton->callback->func, 5, args); + scope.Close(Undefined()); + + delete batton; + return 0; + } + + void RawDataCallback(const char* data, int controllerId, int callbackId, void *callbackVoid) { + RawDeviceEventBatton *batton = new RawDeviceEventBatton(); + batton->callback = static_cast(callbackVoid); + batton->data = data; + batton->controllerId = controllerId; + eio_nop(EIO_PRI_DEFAULT, RawDataEventCallbackAfter, batton); + } + + Handle addRawDeviceEventListener( const Arguments& args ) { + HandleScope scope; + if (!args[0]->IsFunction()) { + return ThrowException(Exception::TypeError(String::New("Expected 1 argument: (function callback)"))); + } + + Callback *callback = new Callback(); + callback->func = Persistent::New(Handle::Cast(args[0])); + Local num = Number::New(tdRegisterRawDeviceEvent(&RawDataCallback, callback)); + return scope.Close(num); + } + Handle removeEventListener( const Arguments &args ) { HandleScope scope; if (!args[0]->IsNumber()) { @@ -312,6 +354,8 @@ void init(Handle target) { FunctionTemplate::New(telldus_v8::addDeviceEventListener)->GetFunction()); target->Set(String::NewSymbol("addSensorEventListener"), FunctionTemplate::New(telldus_v8::addSensorEventListener)->GetFunction()); + target->Set(String::NewSymbol("addRawDeviceEventListener"), + FunctionTemplate::New(telldus_v8::addRawDeviceEventListener)->GetFunction()); target->Set(String::NewSymbol("removeEventListener"), FunctionTemplate::New(telldus_v8::removeEventListener)->GetFunction()); } diff --git a/telldus.js b/telldus.js index 52e84cb..ea9e84b 100644 --- a/telldus.js +++ b/telldus.js @@ -7,5 +7,6 @@ var tellduscore = require('./build/Release/telldus-core-js'); exports.dim = function(id, levl) { return tellduscore.dim(id, levl); }; exports.addDeviceEventListener = function(callback) { return tellduscore.addDeviceEventListener(callback); }; exports.addSensorEventListener = function(callback) { return tellduscore.addSensorEventListener(callback); }; + exports.addRawDeviceEventListener = function(callback) { return tellduscore.addRawDeviceEventListener(callback); }; exports.removeEventListener = function(id) { return tellduscore.removeEventListener(id); }; })('object' === typeof module ? module.exports : (this.telldus = {}), this); \ No newline at end of file