Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding support for more telldus features

Added support for DeviceEvents and SensorEvents. Changed build to use
GYP.
Changed some JS key names to make more sense.
  • Loading branch information...
commit ac2d2de6e2be08d15032fe1f80cc47565f6c6840 1 parent 415b5f6
@marchaos marchaos authored
Showing with 179 additions and 51 deletions.
  1. +26 −0 binding.gyp
  2. +3 −3 package.json
  3. +146 −47 telldus.cc
  4. +4 −1 telldus.js
View
26 binding.gyp
@@ -0,0 +1,26 @@
+{
+ "targets": [
+ {
+ "target_name": "telldus-core-js",
+ "sources": [ "telldus.cc" ],
+ "conditions": [
+ ['OS=="mac"', {
+ 'include_dirs': [
+ '/Library/Frameworks/TelldusCore.framework/Headers'
+ ],
+ 'libraries': [
+ '/Library/Frameworks/TelldusCore.framework'
+ ]
+ }],
+ ['OS=="linux"', {
+ 'defines': [
+ 'LIB_TC=telldus-core',
+ 'CCDEFINES_TC=TC'
+ ]
+ }]
+ ],
+ 'libraries': [
+ ],
+ }
+ ]
+}
View
6 package.json
@@ -1,7 +1,7 @@
{ "name" : "telldus-core-js"
-, "version" : "0.0.5"
+, "version" : "0.0.6"
, "description" : "node wrapper for telldus-core"
-, "keywords": ["telldus", "telldus-core","TellStick", "home automation"]
+, "keywords": ["telldus", "telldus-core", "TellStick", "home automation"]
, "homepage" : "http://www.slickstreamer.info"
, "author": "Marcus Olsson <marcus.h.olsson@gmail.com>"
, "repository":{
@@ -11,7 +11,7 @@
, "engines": { "node": ">= 0.6.14" }
, "license": "MIT"
, "main": "./telldus.js"
-, "scripts": { "install": "node-waf configure build" }
+, "scripts": { "install": "node-gyp configure build" }
, "os" : [ "darwin", "linux" ]
, "devDependencies": {
"mocha": "*"
View
193 telldus.cc
@@ -5,37 +5,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <iostream>
#include <telldus-core.h>
using namespace v8;
using namespace node;
+using namespace std;
-#ifdef _WINDOWS
-#define strcasecmp _stricmp
-#define DEGREE " "
-#else
-#define DEGREE "°"
-#endif
+namespace telldus_v8 {
+ struct Callback {
+ Persistent<Function> func;
+ };
+ struct DeviceEventBatton {
+ Callback *callback;
+ int deviceId;
+ };
-namespace telldus_v8 {
+ struct SensorEventBatton {
+ Callback *callback;
+ int sensorId;
+ const char *model;
+ const char *protocol;
+ const char *value;
+ int ts;
+ int dataType;
+ };
const int SUPPORTED_METHODS =
- TELLSTICK_TURNON
- | TELLSTICK_TURNOFF
- | TELLSTICK_BELL
- | TELLSTICK_TOGGLE
- | TELLSTICK_DIM
- | TELLSTICK_LEARN
- | TELLSTICK_EXECUTE
- | TELLSTICK_UP
- | TELLSTICK_DOWN
+ TELLSTICK_TURNON
+ | TELLSTICK_TURNOFF
+ | TELLSTICK_BELL
+ | TELLSTICK_TOGGLE
+ | TELLSTICK_DIM
+ | TELLSTICK_LEARN
+ | TELLSTICK_EXECUTE
+ | TELLSTICK_UP
+ | TELLSTICK_DOWN
| TELLSTICK_STOP;
- const int DATA_LENGTH = 20;
-
Handle<Value> getNumberOfDevices( const Arguments& args ) {
HandleScope scope;
//tdInit();
@@ -44,7 +54,7 @@ namespace telldus_v8 {
}
Local<Object> GetSuportedMethods(int id){
-
+
int methods = tdMethods( id, SUPPORTED_METHODS );
Local<Array> methodsObj = Array::New();
int i = 0;
@@ -108,30 +118,30 @@ namespace telldus_v8 {
char *level = 0;
switch(lastSentCommand) {
case TELLSTICK_TURNON:
- status->Set(String::NewSymbol("name"), String::New("ON"));
+ status->Set(String::NewSymbol("status"), String::New("ON"));
break;
case TELLSTICK_TURNOFF:
- status->Set(String::NewSymbol("name"), String::New("OFF"));
+ status->Set(String::NewSymbol("status"), String::New("OFF"));
break;
case TELLSTICK_DIM:
- status->Set(String::NewSymbol("name"), String::New("DIM"));
+ status->Set(String::NewSymbol("status"), String::New("DIM"));
level = tdLastSentValue(id);
status->Set(String::NewSymbol("level"), String::New(level, strlen(level)));
tdReleaseString(level);
break;
default:
- status->Set(String::NewSymbol("name"), String::New("Unknown"));
+ status->Set(String::NewSymbol("status"), String::New("UNNKOWN"));
}
return status;
}
-
- Local<Object> GetDevice(int index){
+
+ Local<Object> GetDevice(int index) {
int id = tdGetDeviceId( index );
char *name = tdGetName( id );
char *model = tdGetModel(id);
-
+
Local<Object> obj = Object::New();
obj->Set(String::NewSymbol("name"), String::New(name, strlen(name)));
obj->Set(String::NewSymbol("id"), Number::New(id));
@@ -145,29 +155,27 @@ namespace telldus_v8 {
return obj;
}
-
+
Handle<Value> getDevices( const Arguments& args ) {
HandleScope scope;
tdInit();
-
int intNumberOfDevices = tdGetNumberOfDevices();
Local<Array> devices = Array::New(intNumberOfDevices);
for (int i = 0; i < intNumberOfDevices; i++) {
devices->Set(i, GetDevice(i));
}
-
+
return scope.Close(devices);
}
Handle<Value> turnOn( const Arguments& args ) {
HandleScope scope;
if (!args[0]->IsNumber()) {
- ThrowException(Exception::TypeError(String::New("Wrong arguments")));
- return scope.Close(Undefined());
+ return ThrowException(Exception::TypeError(String::New("Wrong arguments")));
}
-
+
Local<Number> num = Number::New(tdTurnOn(args[0]->NumberValue()));
return scope.Close(num);
}
@@ -175,35 +183,120 @@ namespace telldus_v8 {
Handle<Value> turnOff( const Arguments& args ) {
HandleScope scope;
if (!args[0]->IsNumber()) {
- ThrowException(Exception::TypeError(String::New("Wrong arguments")));
- return scope.Close(Undefined());
+ return ThrowException(Exception::TypeError(String::New("Wrong arguments")));
}
-
+
Local<Number> num = Number::New(tdTurnOff(args[0]->NumberValue()));
return scope.Close(num);
- }
-
+ }
+
Handle<Value> dim( const Arguments& args ) {
HandleScope scope;
if (!args[0]->IsNumber()|| !args[1]->IsNumber()) {
- ThrowException(Exception::TypeError(String::New("Wrong arguments")));
- return scope.Close(Undefined());
+ return ThrowException(Exception::TypeError(String::New("Wrong arguments")));
}
-
+
Local<Number> num = Number::New(tdDim(args[0]->NumberValue(),(unsigned char)args[1]->NumberValue() ));
return scope.Close(num);
- }
- /* Handle<Value> tdTurnOn(int intDeviceId);
-tdTurnOff(int intDeviceId);
-tdBell(int intDeviceId);
-tdDim(int intDeviceId, unsigned char level);
- */
+ }
+
+ int DeviceEventCallbackAfter(eio_req *req) {
+ HandleScope scope;
+ DeviceEventBatton *batton = static_cast<DeviceEventBatton *>(req->data);
+
+ Local<Value> args[] = {
+ Number::New(batton->deviceId),
+ GetDeviceStatus(batton->deviceId),
+ };
+
+ batton->callback->func->Call(batton->callback->func, 3, args);
+ scope.Close(Undefined());
+ delete batton;
+ return 0;
+ }
+
+ void DeviceEventCallback( int deviceId, int method, const char * data, int callbackId, void* callback_void ) {
+ DeviceEventBatton *batton = new DeviceEventBatton();
+ batton->callback = static_cast<Callback *>(callback_void);
+ batton->deviceId = deviceId;
+ eio_nop(EIO_PRI_DEFAULT, DeviceEventCallbackAfter, batton);
+ }
+
+ Handle<Value> addDeviceEventListener( 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(tdRegisterDeviceEvent(&DeviceEventCallback, callback));
+ return scope.Close(num);
+ }
+
+ int SensorEventCallbackAfter(eio_req *req) {
+ HandleScope scope;
+ SensorEventBatton *batton = static_cast<SensorEventBatton *>(req->data);
+
+ Local<Value> args[] = {
+ Number::New(batton->sensorId),
+ String::New(batton->model),
+ String::New(batton->protocol),
+ Number::New(batton->dataType),
+ String::New(batton->value),
+ };
+
+ batton->callback->func->Call(batton->callback->func, 5, args);
+ scope.Close(Undefined());
+
+ delete batton;
+ return 0;
+ }
+
+ void SensorEventCallback( const char *protocol, const char *model, int sensorId, int dataType, const char *value,
+ int ts, int callbackId, void *callback_void ) {
+
+ SensorEventBatton *batton = new SensorEventBatton();
+ batton->callback = static_cast<Callback *>(callback_void);
+ batton->sensorId = sensorId;
+ batton->protocol = protocol;
+ batton->model = model;
+ batton->ts = ts;
+ batton->dataType = dataType;
+ batton->value = value;
+
+ eio_nop(EIO_PRI_DEFAULT, SensorEventCallbackAfter, batton);
+ }
+
+ Handle<Value> addSensorEventListener( 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(tdRegisterSensorEvent(&SensorEventCallback, callback));
+ return scope.Close(num);
+ }
+
+ Handle<Value> removeEventListener( const Arguments &args ) {
+ HandleScope scope;
+ if (!args[0]->IsNumber()) {
+ return ThrowException(Exception::TypeError(String::New("Expected 1 argument: (int callbackId)")));
+ }
+ tdUnregisterCallback(args[0]->ToInteger()->Value());
+
+ //TODO: Fix leak of callback.
+
+ return scope.Close(Undefined());
+ }
}
extern "C"
void init(Handle<Object> target) {
-
+
HandleScope scope;
target->Set(String::NewSymbol("getNumberOfDevices"),
FunctionTemplate::New(telldus_v8::getNumberOfDevices)->GetFunction());
@@ -215,6 +308,12 @@ void init(Handle<Object> target) {
FunctionTemplate::New(telldus_v8::turnOff)->GetFunction());
target->Set(String::NewSymbol("dim"),
FunctionTemplate::New(telldus_v8::dim)->GetFunction());
+ target->Set(String::NewSymbol("addDeviceEventListener"),
+ FunctionTemplate::New(telldus_v8::addDeviceEventListener)->GetFunction());
+ target->Set(String::NewSymbol("addSensorEventListener"),
+ FunctionTemplate::New(telldus_v8::addSensorEventListener)->GetFunction());
+ target->Set(String::NewSymbol("removeEventListener"),
+ FunctionTemplate::New(telldus_v8::removeEventListener)->GetFunction());
}
NODE_MODULE(telldus, init)
View
5 telldus.js
@@ -1,8 +1,11 @@
-var tellduscore = require('./build/Release/telldus');
+var tellduscore = require('./build/Release/telldus-core-js');
(function (exports, global) {
exports.getDevices = function() { return tellduscore.getDevices(); };
exports.turnOn = function(id) { return tellduscore.turnOn(id); };
exports.turnOff = function(id) { return tellduscore.turnOff(id); };
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.removeEventListener = function(id) { return tellduscore.removeEventListener(id); };
})('object' === typeof module ? module.exports : (this.telldus = {}), this);
Please sign in to comment.
Something went wrong with that request. Please try again.