Samples REST - wuellueb/openhab GitHub Wiki

Examples for accessing REST API

Introduction

This page has samples in multiple languages for utilizing the three main REST interfaces for Items in OpenHAB:

  • Send Command - This will send a command to an item to tell it to take some action (e.g. turn on light).

  • Send Status - This is to indicate that the status of an item has changed (e.g. window is now open).

  • Get Status - Gets the current status of an item. (This can also be used to continuously get updates as shown in the Python section).

There is also a sitemap interface.

Language Samples

jquery

Accessing REST API via jquery (tested with jquery 2.0 and Chrome v26.0

Get state of an item:

function getState()
{
	var request = $.ajax
	({
		type       : "GET",
		url        : "http://192.168.100.21:8080/rest/items/MyLight/state"
	});

	request.done( function(data) 
	{ 
		console.log( "Success: Status=" + data );
	});

	request.fail( function(jqXHR, textStatus ) 
	{ 
		console.log( "Failure: " + textStatus );
	});
}

Set state of an item:

function setState( txtNewState )
{
	var request = $.ajax
	({
		type       : "PUT",
		url        : "http://192.168.100.21:8080/rest/items/MyLight/state",
		data       : txtNewState, 
		headers    : { "Content-Type": "text/plain" }
	});

	request.done( function(data) 
	{ 
		console.log( "Success" );
	});

	request.fail( function(jqXHR, textStatus ) 
	{ 
		console.log( "Failure: " + textStatus );
	});
}

Send command to an item:

function sendCommand( txtCommand )
{
	var request = $.ajax
	({
		type       : "POST",
		url        : "http://192.168.100.21:8080/rest/items/MyLight",
		data       : txtCommand,
		headers    : { 'Content-Type': 'text/plain' }
	});

	request.done( function(data) 
	{ 
		console.log( "Success: Status=" + data );
	});

	request.fail( function(jqXHR, textStatus ) 
	{ 
		console.log( "Failure: " + textStatus );
	});
}

cURL

Accessing REST API via cURL. cURL is useful on shell scripts (Win/Linux/OS X) or e.g. on Automator (OS X).

Get state of an item:

curl http://192.168.100.21:8080/rest/items/MyLight/state

Set state of an item:

curl --header "Content-Type: text/plain" --request PUT --data "OFF" http://192.168.100.21:8080/rest/items/MyLight/state

Send command to an item:

curl --header "Content-Type: text/plain" --request POST --data "ON" http://192.168.100.21:8080/rest/items/MyLight

PHP

Accessing REST API via PHP. Simple PHP function to post a command to a switch using the REST interface.

Send command to an item:

function sendCommand($item, $data) {
  $url = "http://192.168.1.121:8080/rest/items/" . $item;

  $options = array(
    'http' => array(
        'header'  => "Content-type: text/plain\r\n",
        'method'  => 'POST',
        'content' => $data  //http_build_query($data),
    ),
  );

  $context  = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  return $result;
}

Example function use:

sendCommand("doorbellSwitch", "ON");

If the post was successful the function will return the state you set, EG above returns "ON"

Python

Accessing REST API via Python. Note that the rget status interface is set up to continuously receive updates rather than just getting a one time response. This is done with the "polling header" and the last section decodes the JSON response.

Send command to an item

def post_command(self, key, value):
    """ Post a command to OpenHAB - key is item, value is command """
    url = 'http://%s:%s/rest/items/%s'%(self.openhab_host,
                                self.openhab_port, key)
    req = requests.post(url, data=value,
                            headers=self.basic_header())
    if req.status_code != requests.codes.ok:
        req.raise_for_status()

Set state of an item

def put_status(self, key, value):
    """ Put a status update to OpenHAB  key is item, value is state """
    url = 'http://%s:%s/rest/items/%s/state'%(self.openhab_host,
                                self.openhab_port, key)
    req = requests.put(url, data=value, headers=self.basic_header())
    if req.status_code != requests.codes.ok:
        req.raise_for_status()     

Get state updates of an item

def get_status(self, name):
    """ Request updates for any item in group NAME from OpenHAB.
     Long-polling will not respond until item updates.
    """
    # When an item in Group NAME changes we will get all items in the group 
    # and need to determine which has changed
    url = 'http://%s:%s/rest/items/%s'%(self.openhab_host,
                                    self.openhab_port, name)
    payload = {'type': 'json'}
    try:
        req = requests.get(url, params=payload,
                            headers=self.polling_header())
        if req.status_code != requests.codes.ok:
            req.raise_for_status()
        # Try to parse JSON response
        # At top level, there is type, name, state, link and members array
        members = req.json()["members"]
        for member in members:
            # Each member has a type, name, state and link
            name = member["name"]
            state = member["state"]
            do_publish = True
            # Pub unless we had key before and it hasn't changed
            if name in self.prev_state_dict:
                if self.prev_state_dict[name] == state:
                    do_publish = False
            self.prev_state_dict[name] = state
            if do_publish:
                self.publish(name, state)

HTTP Header definitions

def polling_header(self):
    """ Header for OpenHAB REST request - polling """
    self.auth = base64.encodestring('%s:%s'
                       %(self.username, self.password)
                       ).replace('\n', '')
    return {
        "Authorization" : "Basic %s" % self.cmd.auth,
        "X-Atmosphere-Transport" : "long-polling",
        "X-Atmosphere-tracking-id" : self.atmos_id,
        "X-Atmosphere-Framework" : "1.0",
        "Accept" : "application/json"}

def basic_header(self):
    """ Header for OpenHAB REST request - standard """
    self.auth = base64.encodestring('%s:%s'
                       %(self.username, self.password)
                       ).replace('\n', '')
    return {
            "Authorization" : "Basic %s" %self.auth,
            "Content-type": "text/plain"}