close
OpenRTLS
OpenRTLS
Search

Software:APIPythonExample

Contents

Example of communication with RTLS API using Python

Download: File:Openrtls-api-example.py

import socket
import select
import threading
import json

class SvcClient:
    master_ip = "192.168.8.201"
    svc_port = 8784
    def __init__(self):
        self.svcSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.svcSocket.connect((self.master_ip, self.svc_port))
        self.svcSocket.setblocking(0)

    def request(self, jsonStr, timeout = 20):
        try:
            msgToSend = jsonStr.encode(encoding = "utf-8");
            bytesSent = 0
            while bytesSent < len(msgToSend):
                bytesSent += self.svcSocket.send(msgToSend[bytesSent:])

            recvMessage = ""
            while True:
                ready = select.select([self.svcSocket], [], [], timeout)
                if ready[0]:
                    recvMessage += self.svcSocket.recv(1024 * 1024).decode(encoding = "utf-8")
                    if "\n" in recvMessage: #Complete message ends with \n
                        break
                else:
                    #timeout...
                    break
                
        except Exception as e:
            print(e)
            #handle exceptions...

        if not "\n" in recvMessage:
            print("SVC: Empty or partial message received: {0}".format(recvMessage))
            return None
        else:
            print("SVC: Complete message received: {0}".format(recvMessage))
            return recvMessage

    def close(self):
        self.svcSocket.close()

        
print("RTLS example start.\n")

svcClient = SvcClient()
listener = svcClient.request('{"command":"getLsListener"}')

lsAddress = json.loads(listener)
if lsAddress["mode"] == "unicast":
    print("Received LS listener: {0}\n".format(lsAddress["mode"]))

    lsSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    lsSocket.bind((lsAddress["ip"], 8787))
    lsSocket.setblocking(0)

    try:
        while True:
            ready = select.select([lsSocket], [], [], 1)
            if ready[0]:
                recvData = lsSocket.recv(16384)

                if len(recvData) != 0:
                    recv = recvData[0:len(recvData)].decode("utf-8")
                    print("LS: Received: " + recv)

                else:
                    print("LS: Received empty")
            
            else:
                print("LS: No data, timeout")
    except Exception as e:
        print(e)
        #handle exceptions...
        svcClient.close()  
    
else:
    print("No listener received")
    svcClient.close()

Program execution

Please download the example file File:Openrtls-api-example.py and run it with the Python interpreter

$ python3 openrtls-api-example.py

Output of the program

The RTLS data are in JSON format and separated by newlines ('\n')

SVC: Complete message received: {"response":"getLsListener","unicast":"192.168.8.200"}

Received LS listener: 192.168.8.200
LS: Received: {"id":"0xDECA3430326141A4","timestamp":1865316,"msgid":0,
"coordinates":{"x":0.290,"y":-0.448,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.512,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.091,"dqf":85},
{"anchor":"0xDECA343034314143","dist":4.628,"dqf":100}]}

LS: Received: {"id":"0xDECA3430326141A4","timestamp":1865565,"msgid":1,
"coordinates":{"x":0.282,"y":-0.442,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.479,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.077,"dqf":82},
{"anchor":"0xDECA343034314143","dist":4.694,"dqf":100}]}

LS: Received: {"id":"0xDECA3430326141A4","timestamp":1866558,"msgid":2,
"coordinates":{"x":0.274,"y":-0.437,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.484,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.091,"dqf":81},
{"anchor":"0xDECA343034314143","dist":4.670,"dqf":100}]}

LS: Received: {"id":"0xDECA3430326141A4","timestamp":1867551,"msgid":3,
"coordinates":{"x":0.274,"y":-0.437,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.484,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.077,"dqf":0},
{"anchor":"0xDECA343034314143","dist":4.675,"dqf":100}]}

LS: Received: {"id":"0xDECA3430326141A4","timestamp":1868544,"msgid":4,
"coordinates":{"x":0.267,"y":-0.434,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.503,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.087,"dqf":84},
{"anchor":"0xDECA343034314143","dist":4.689,"dqf":100}]}

LS: Received: {"id":"0xDECA3430326141A4","timestamp":1869537,"msgid":5,
"coordinates":{"x":0.267,"y":-0.434,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.493,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.138,"dqf":0},
{"anchor":"0xDECA343034314143","dist":4.670,"dqf":100}]}

LS: Received: {"id":"0xDECA3430326141A4","timestamp":1870530,"msgid":6,
"coordinates":{"x":0.242,"y":-0.423,"z":0.000,"heading":0.000,"pqf":100},
"distances":[{"anchor":"0xDECA31303451415D","dist":0.610,"dqf":100},
{"anchor":"0xDECA32303471415C","dist":3.428,"dqf":69},
{"anchor":"0xDECA343034314143","dist":4.722,"dqf":82}]}
...

References