Getting Started

Installing

Knut runs using Python 3.7 and to install the Knut server, run first:

pip install -r requirements.txt

to install all needed packages.

Note

Some services may require additional installation steps. Please follow the instructions of the services if an error occurs.

To finally install the Knut server run:

python setup.py install

How Knut Works

Knut has various apis to control e.g. lights or supply temperature data from various sources. Each API can be connected to multiple service back-ends which are implementing the actions needed to execute a command.

How it works internally

For example, lets switch a light which is plugged into a RF controllable socket. The light API Light class calls a method of the light service RFLight, which switches the socket according to a parsed status in a back-end.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from knut.apis import Light
from knut.services.light.rflight import RFLight

# first we define a service back-end using the RFLight module
rf_light = RFLight('Side Board',  # the location of the light
                   'side_board_lamp',  # the unique name of the light
                   'Living Room',  # the room in which the light is located
                   17,  # the GPIO to which the RF module is connected
                   1328465,  # the code which should be send to switch the light on
                   1328468)  # the code which should be send to switch the light off

# now we setup the API
light_api = Light()
light_api.add_backend(rf_light)

# lets switch the defined light on
light_api.request_handler(2,  # LIGHT_STATUS_RESPONSE to change a light
                          {'id': 'side_board_lamp', 'state': True})

Now to interact with all APIs from another client program, Knut has a TCP interface, the KnutTCPServer. Lets extend the code of the previous example by the following lines to get working Knut server:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from knut.server.tcpserver import KnutTCPServer
import threading
import time

server = KnutTCPServer(("localhost", 8080))
server.add_api(light_api)  # our light API from the previous example

with server:
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    time.sleep(60)  # run for one minute

server.shutdown()

Using netcat as client, the following request is redirected to the request handler of the API by the server and we can switch our light:

echo -ne '{"apiId": 2, "msgId": 2, "msg": {"id": "side_board_lamp", "state": true}}\0' | netcat localhost 8080