Friday, March 27, 2015

Simple Python Http Server

Serves up files in the current directory.

Python 3
python3 -m http.server
or
python3 -m http.server 8080

Python 2
python -m SimpleHTTPServer
or 
python -m SimpleHTTPServer 8080


Sunday, March 01, 2015

Nodejs Hue Quicksheet

Summary of the following article:
https://github.com/peter-murray/node-hue-api


// load the module
var hue = require('node-hue-api');

// define some display functions
var displayResult = function(result) {
    console.log("Created user: " + JSON.stringify(result));
};

// define some display functions
var displayError = function(err) {
    console.error(err);
};

// search for bridges
timeout = 6000  // 6 seconds
hue.upnpSearch(timeout).then(displayBridges).done();

// register a new user
hostname = '192.168.1.112';
newUserName = null;
userDescription = "some description";
hue.registerUser(hostname, newUserName, userDescription)
   .then(displayResult).fail(displayError).done();

// define connection information
var hostname = "192.168.1.112",
    username="*************************",
    api;

// create the api object
api = new hue.HueApi(hostname, username);

// get basic information about the gateway api
api.description().then(displayResult).fail(displayError).done();

// get the full state of the gateway
api.fullState().then(displayResult).fail(displayError).done();

// see the users that are registered
api.registeredUsers().then(displayResult).fail(displayError).done();

// show the lights and their states
api.lights().then(displayResult).fail(displayError).done();

// define a state and set it to light #1
state = hue.lightState.create().on().white(500, 100);
api.setLightState(1, state).then(displayResult).fail(displayError).done();

// turn light #1 on
api.setLightState(1, state.on()).then(displayResult).fail(displayError).done();

// turn light #1 off
api.setLightState(1, state.off()).then(displayResult).fail(displayError).done();

// get the status of light #1
api.lightStatus(1).then(displayResult).fail(displayError).done();

// see the light groups 
api.groups().then(displayResult).fail(displayError).done();

// see the available scenes  
api.scenes().then(displayResult).fail(displayError).done();

// create a new scene assign light #1-7 to it.  Will include a scene 'id' in the result
lightIds = [1,2,3,4,5,6,7]
sceneName = "Demo Scene"
api.createScene(lightIds, sceneName).then(displayResults).done();

// update an existing scene
lightIds = [1,2,3,4,5,6,]
sceneName = "Updated Demo Scene"
sceneId = "id-generated-by-gateway"
api.updateScene(sceneId, lightIds, sceneName).then(displayResults).done();

// set states for light #1 in a scene
state = lightState.create().on().hue(2000)
sceneId = "id-generated-by-gateway"
lightId = 1
api.setSceneLightState(sceneId, lightId, state).then(displayResults).done();

// recall scene
api.recallScene(sceneId).then(displayResults).done();