Browse files

Added support for tdRegisterRawDeviceEvent()

Added support for tdRegisterRawDeviceEvent()
  • Loading branch information...
1 parent e5631b1 commit a549404c263b1c0b5e378b9488069be230d44dc9 @marchaos marchaos committed Dec 14, 2012
Showing with 49 additions and 4 deletions.
  1. +48 −4 telldus.cc
  2. +1 −0 telldus.js
View
52 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 *>(callback_void);
+ batton->callback = static_cast<Callback *>(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 *>(callback_void);
+ batton->callback = static_cast<Callback *>(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<RawDeviceEventBatton *>(req->data);
+
+ Local<Value> 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<Callback *>(callbackVoid);
+ batton->data = data;
+ batton->controllerId = controllerId;
+ eio_nop(EIO_PRI_DEFAULT, RawDataEventCallbackAfter, batton);
@Hexagon
Hexagon added a note Nov 21, 2013

What's these eio_nop calls used for? Eio is no longer supported by node so these breaks the builds. @marchaos @evilmachina

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ Handle<Value> 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<Function>::New(Handle<Function>::Cast(args[0]));
+ Local<Number> num = Number::New(tdRegisterRawDeviceEvent(&RawDataCallback, callback));
+ return scope.Close(num);
+ }
+
Handle<Value> removeEventListener( const Arguments &args ) {
HandleScope scope;
if (!args[0]->IsNumber()) {
@@ -312,6 +354,8 @@ void init(Handle<Object> 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());
}
View
1 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);

0 comments on commit a549404

Please sign in to comment.