Skip to content

Commit

Permalink
Updated README and setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe committed Oct 20, 2019
1 parent 057e5eb commit 7ce9e91
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ myApp.run()

```

Though your acquired data is available via raspend's HTTP interface, you probably want to push this data somewhere, a database for instance. Therefore version 1.3.0 introduces the **Publishing** module. You just need to create a handler derived from the **Publishing.PublishDataHandler** class, similar to the data acquisition part, and override it's **publishData** method. Here you can publish all or parts of the data contained in the shared dictionary to wherever you want. You then pass this handler to the **createPublishDataThread** method of **RaspendApplication**. The example below posts the whole shared dictionary as a JSON string to a PHP backend, which in turn writes the data into a MySQL database (see *raspend_demo* for details).

``` python
from raspend.application import RaspendApplication
from raspend.utils import publishing as Publishing

class PublishOneWireTemperatures(Publishing.PublishDataHandler):
def __init__(self, endPointURL, userName, password):
self.endPoint = endPointURL
self.userName = userName
self.password = password

def prepare(self):
# Nothing to prepare so far.
pass

def publishData(self):
data = json.dumps(self.sharedDict)
try:
response = requests.post(self.endPoint, data, auth=(self.userName, self.password))
response.raise_for_status()
except HTTPError as http_err:
print("HTTP error occurred: {}".format(http_err))
except Exception as err:
print("Unexpected error occurred: {}".format(err))
else:
print(response.text)


myApp.createPublishDataThread(PublishOneWireTemperatures("http://localhost/raspend_demo/api/post_data.php", username, password), 60)

```

The other idea is to expose different functionalities, such as switching on/off your door bell via GPIO, as a command you can send to your RPi via HTTP POST request. All you have to do is to encapsulate the functionality you want to make available to the outside world into a method of a Python class. Then instantiate your class and call the **addCommand** method of **RaspendApplication** providing the method you want to expose. Now you can execute your method using a simple HTTP POST request.

``` python
Expand Down Expand Up @@ -87,9 +120,9 @@ myApp.run()

```

When all initialization stuff is done (adding commands, creating data acquisition threads), then you start your application by calling the **run** method of **RaspendApplication**. The **RaspendApplication** class installs signal handlers for SIGTERM and SIGINT, so you can quit your application by pressing CTRL+C or sending one of the signals via the **kill** command of your shell.
When all initialization stuff is done (adding commands, creating threads), then you start your application by calling the **run** method of **RaspendApplication**. The **RaspendApplication** class installs signal handlers for SIGTERM and SIGINT, so you can quit your application by pressing CTRL+C or sending one of the signals via the **kill** command of your shell.

Please have a look at the examples included in this project to get a better understanding. *example1.py* and *example2.py* show how to do most of the work yourself, while *example3.py* shows you the most convenient way of using this framework.
Please have a look at the examples included in this project to get a better understanding. *example1.py* and *example2.py* show how to do most of the work yourself, while *example3.py* shows you the most convenient way of using this framework. *example4.py* is identical to *example3.py* but extended by a **PublishDataHandler**.

## How to use the HTTP interface?

Expand Down
8 changes: 8 additions & 0 deletions raspend_demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Purpose

**raspend_demo** is used for testing **raspend**'s publishing module.

# Usage

The easiest way to use **raspend_demo** is using Visual Studio Code with the PHP Debug extension installed (that's what I did). Then you need a web server with PHP enabled (I use [XAMPP](https://www.apachefriends.org/index.html)). To use the PHP Debug extension you further need to enable XDebug. See [PHP Debug extension](https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug) for more information and follow the installations steps.

3 changes: 0 additions & 3 deletions raspend_demo/api/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
Expand Down
6 changes: 3 additions & 3 deletions raspend_demo/api/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

class Database
{
// Lokale Einstellungen
// local settings

private $db_name = "raspend_demo";
private $host = "localhost";
private $username = "root";
private $password = "";

// Entfernte Einstellungen
// remote settings
/*
private $db_name = "";
private $db_name = "raspend_demo";
private $host = "";
private $username = "";
private $password = "";
Expand Down
1 change: 1 addition & 0 deletions raspend_demo/api/post_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
" VALUES (?, ?, ?, ?, ?);";

$stmt = $db->prepare($insertStmt);

$succeeded = $stmt->execute(array($temperatures->basement->party_room,
$temperatures->basement->fitness_room,
$temperatures->basement->heating_room,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setuptools.setup(
name="raspend",
version="1.2.3",
version="1.3.0",
author="Joerg Beckers",
author_email="pypi@jobe-software.de",
description="A small and easy to use HTTP backend framework for the Raspberry Pi which is ideal for small to medium-sized home automation projects.",
Expand Down

0 comments on commit 7ce9e91

Please sign in to comment.