RedRat Hub Python Example Client Code

The few lines of code below open a socket to the hub (on IP address 192.168.1.7) and uses that to send commands to the irNetBox (with IP address 192.168.1.40):


#
# Simple Python test program to use the RedRatHub.
#
# Ethan Johnson, David Blight, Chris Dodge - RedRat Ltd.
#
import RedRatHub
import time

client = RedRatHub.Client()

# Connect to the RedRatHub
client.OpenSocket('192.168.1.7', 40000)

# Send some IR signals
client.SendMessage('"ip="192.168.1.40" dataset="Sky+" signal="9" output="12:10"')
print("Sent signal\n")
time.sleep(2)

client.SendMessage('"ip="192.168.1.40" dataset="Sky+" signal="9" output="12:10"')
print("Sent signal\n")
time.sleep(2)

client.SendMessage('"ip="192.168.1.40" dataset="Sky+" signal="9" output="12:10"')
print("Sent signal\n")
time.sleep(2)

# List the datasets known by the hub
print("List of datasets:")
list = client.ReadData('hubquery="list datasets"')
print(list)

client.CloseSocket()
print("Finished.");

This uses a python class for sending and receiving RedRatHub messages:


#
# Simple Python script to send commands to the RedRatHubCmd application via a socket.
#
# Ethan Johnson, David Blight, Chris Dodge - RedRat Ltd.
#
import socket
import time

class Client():
    def __init__(self):
        self.sock = socket.socket()
        self.socketOpen = False

	#
	# Opens the socket to RedRatHubCmd.
	#
    def OpenSocket(self, ip, port):
        self.sock.connect((ip, port))
        self.socketOpen = True

	#
	# Closes the RedRatHubCmd socket.
	#
    def CloseSocket(self):
        if self.socketOpen:
            self.sock.close()
            self.socketOpen = False
        else:
            print("Socket failed to close.")

	#
	# Sends a message to the ReadData() function
	#
    def SendMessage(self, message):
        self.ReadData(message)


	#
	# Reads data back from RedRatHub.
	#
    def ReadData(self, message):
        if not self.socketOpen:
            print("\tSocket has not been opened. Call 'OpenSocket()' first.")
            exit
            
        # Send message
        self.sock.send((message + '\n').encode())
        received = ""
		
        # Check response. This is either a single line, e.g. "OK\n", or a multi-line response with 
        # '{' and '}' start/end delimiters.
        while True:
            # Receives data
            received += self.sock.recv(64).decode()
            if self.CheckEOM(received):
                return received

                
    #
	# Checks for the end of a message
	#
    def CheckEOM(self, message):
        # Multi-line message
        if ('{' in message):
            return ('}' in message)
        
        # Single line message
        if ("\n" in message):
            return True

Download Python Code