Skip to content

Commit

Permalink
INITIAL: Turning lights ON/OFF
Browse files Browse the repository at this point in the history
  • Loading branch information
linkineo committed Dec 2, 2018
0 parents commit f7130ac
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
19 changes: 19 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Tinky-twinkly
This is an unofficial node.js sample code showing how to turn the twikly smart xmas lights ON/OFF.
This software is not endorsed in any way by the Twinkly team and is to be used at your own risk.

### How to execute
1. npm install
2. Change the IP of the device to yours in the IP variable of xmas.js (line 3)
3. node xmas.js
This shall turn your lights on.
To turn them off, modify line 191 to xmas(OFF).

### What it does
At the moment, you can turn ON and OFF your lights over HTTP. This shall allow an easy interfacing with your home automation system.

### Prerequisites
Any twinkly LED set
Knowing the IP address of your device


14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "tinky-twinkly",
"version": "0.0.1",
"description": "",
"main": "xmas.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"request": "^2.88.0"
}
}
194 changes: 194 additions & 0 deletions xmas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
var request = require("request");

var IP = "192.168.0.62"; // ADD YOUR OWN LAMP IP HERE
var CHALLENGE = {'challenge':'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\a'}; // This challenge will do all the time.
var XAUTHTOKEN = "";
var PRODUCT_NAME="Twinkly";
var OFF = {"mode":"off"};
var ON = {"mode": "movie","code": 1000};
var LAMP_STATUS = {
UNREACHABLE: 0,
DETECTED :1,
CHALLENGE_ERROR: 2,
CHALLENGE_OK: 3,
AUTHENTICATE_ERROR: 4,
AUTHENTICATE_OK:5,
ACTION_ERROR: 6,
ACTION_SUCCESS: 7
}

var gestalt = function(callback){
request("http://" + IP + "/xled/v1/gestalt", function(error, response, body){
if(response.statusCode == 200)
{
try{
var product = JSON.parse(body);
if(product.product_name == PRODUCT_NAME)
{
callback(LAMP_STATUS.DETECTED);
}else
{
callback(LAMP_STATUS.UNREACHABLE);
}
}catch(error)
{
callback(LAMP_STATUS.UNREACHABLE);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}

var login = function(callback){
var options = {
method: "POST",
uri: "http://" + IP + "/xled/v1/login",
json: true,
body: CHALLENGE,
headers: {
"Content-Length" : JSON.stringify(CHALLENGE).length
}
};

request(options,function(error,response,body){

if(response.statusCode == 200)
{
try{
XAUTHTOKEN = body.authentication_token;
callback(LAMP_STATUS.CHALLENGE_OK)

}catch(error)
{
callback(LAMP_STATUS.CHALLENGE_ERROR);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}


var verify = function(callback){
var options = {
method: "POST",
uri: "http://" + IP + "/xled/v1/verify",
json: true,
body: {},
headers: {
"X-Auth-Token" : XAUTHTOKEN,
"Content-Length" : 2
}
};

request(options,function(error,response,body){

if(response.statusCode == 200)
{
try{
if(body.code == 1000)
{
callback(LAMP_STATUS.AUTHENTICATE_OK)
}else
{
callback(LAMP_STATUS.AUTHENTICATE_ERROR)
}

}catch(error)
{
callback(LAMP_STATUS.AUTHENTICATE_ERROR);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}

var action = function(mode,callback){
var options = {
method: "POST",
uri: "http://" + IP + "/xled/v1/led/mode",
json: true,
body: mode,
headers: {
"X-Auth-Token" : XAUTHTOKEN,
"Content-Length" : JSON.stringify(mode).length
}
};

request(options,function(error,response,body){

if(response.statusCode == 200)
{
try{
if(body.code == 1000)
{
callback(LAMP_STATUS.ACTION_SUCCESS)
}else
{
callback(LAMP_STATUS.ACTION_ERROR)
}

}catch(error)
{
callback(LAMP_STATUS.ACTION_ERROR);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}



var connect = function(callback){

gestalt(function(status){
if(status == LAMP_STATUS.DETECTED)
{
login(function(status){
if(status == LAMP_STATUS.CHALLENGE_OK)
{
verify(function(status){
if(status == LAMP_STATUS.AUTHENTICATE_OK)
{
callback(status);
}else
{
console.log("ERROR CODE=" + status);
}
})
}else
{
console.log("ERROR CODE=" + status);
}
})
}else
{
console.log("ERROR CODE=" + status);
}
})
}


var xmas = function(mode)
{
connect(function(status) {
if(status == LAMP_STATUS.AUTHENTICATE_OK)
{
action(mode, function(status){
if(status == LAMP_STATUS.ACTION_SUCCESS)
{
console.log("SUCCESSFUL MODE CHANGE");
}
})
}
})
}

//TURN THE LAMPS ON
xmas(ON);
// xmas(OFF) to turn the lamps OFF...


0 comments on commit f7130ac

Please sign in to comment.