Converting Python To NodeRed A Simple Example

Looking for suggestions in converting the below python to NR:

import socket
import sys
import time

#
#https://support.tesmart.com/hc/en-us/categories/7606227532953-Download
#

# --- Configuration ---
SWITCH_IP = "192.168.1.29"  # Default IP address
SWITCH_PORT = 5000          # Default port

def send_tesmart_command(input_port):
    """
    Sends a command to the TESmart switch to change the input port.
    The command structure for an 8x1 switch might be 0xAA 0xBB 0x03 0x01 0x0n 0xEE,
    where 'n' is the input number (01 to 08).
    """
    try:
        # Create a socket connection
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((SWITCH_IP, SWITCH_PORT))

        # Format the command to switch to the specified input port
        # Input ports are typically 1-indexed in documentation, but often 0x01 to 0x08 in hex commands
        # The example below uses the format found in some documentation
        # The value 'n' should be the desired port number (e.g., 1 for Input 1)
        command_hex = f"AABB0301{int(input_port):02x}EE"
        data = bytes.fromhex(command_hex)

        print(f"Sending command: {command_hex} to {SWITCH_IP}:{SWITCH_PORT}")

        # Send the command
        s.send(data)

        # Optional: receive a response (often not needed for simple switching)
        # response = s.recv(1024)
        # print(f"Received response: {response.hex()}")

        print("Command sent successfully.")

    except socket.error as e:
        print(f"Socket error: {e}")
        print("Please check the IP address, port, and network connection.")

    finally:
        # Close the connection
        s.close()

if __name__ == "__main__":
    # Get input port from command line argument, or default to 1 if none provided
    if len(sys.argv) > 1:
        port_to_switch = sys.argv[1]
    else:
        port_to_switch = 1 # Switch to Input 1 by default
        print(f"No port specified, defaulting to input {port_to_switch}")

Yes could call this as an execute node. But that is neither elegant nor NR friendly per se.

Yes could wrote in function node, but again still a bit of side step. Using a TCP out node.

Is there maybe a more NR friendly way? I did a very brief quick look for maybe a module library that would do the heavy lifting, but did not see anything? Missed it?

Give this a try

3 Likes

Or a simple flow.

[{"id":"57cd2f4290dbeccb","type":"inject","z":"992ee185d38fe602","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0xAA\",\"0xBB\",\"0x03\",\"0x01\",\"0x01\",\"0xEE\"]","payloadType":"bin","x":155,"y":412.5,"wires":[["3f0fd38d9f4ba68e"]]},{"id":"3f0fd38d9f4ba68e","type":"tcp request","z":"992ee185d38fe602","name":"","server":"192.168.1.29","port":"5000","out":"time","ret":"buffer","splitc":"0","newline":"","trim":false,"tls":"","x":362.5,"y":412.5,"wires":[["52acdf12cafee046"]]},{"id":"52acdf12cafee046","type":"debug","z":"992ee185d38fe602","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":572.5,"y":412.5,"wires":[]}]

Just an inject, tcp request node and a debug to see any response.

1 Like

Thanks guys for the suggestions!