From 7ca0b9d86e1215127b48a1465cd5c8afad9171b9 Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Sun, 29 Mar 2020 13:48:27 +0200 Subject: [PATCH] python script to replace envertech SetID program for EnverBridge 202 --- home/enverbridge.py | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 home/enverbridge.py diff --git a/home/enverbridge.py b/home/enverbridge.py new file mode 100755 index 0000000..4342407 --- /dev/null +++ b/home/enverbridge.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +# This script can be used to configure the Inverter IDs +# on an Envertech EnverBridge device. This is a replacement +# for the "SetID" programm provided as a Win32 executable at +# http://www.envertec.com/uploads/bigfiles/Set%20ID.zip +# Tested online with the EnverBridge 202 and Inverters which +# have the same serial nummer on both labels. +# +# Logic seems the be the following: +# Take the inverter id, append the char 9 to 16 of the 16 char +# serial number. Do this for every pair of serial numbers. +# Sent this string in one UDP paket to the broadcast address on +# port 8765. Wait for a response paket to the broadcast address +# on port 8764. + +from optparse import OptionParser +import socket +import sys +import re + +parser = OptionParser(usage="usage: %prog [options] MIIDs") +parser.add_option("-b", "--bid", + action="store", + type="int", + dest="bid", + help="Serial Number of your EnverBridge") +(options, args) = parser.parse_args() + + +def validateMIID(miids): + error = False + idpattern = re.compile(r"^CN[0-9]{14}$") + for id in miids: + print("Validating MIID", id) + if not idpattern.search(id): + print("Error: Invalid MIID", id) + error = True + + if error: + print("Validation error, check your MIIDs, exiting") + sys.exit(23) + + +def buildMIIDList(miids): + msg = "" + for id in miids: + msg = msg + id[8:16] + id[8:16] + return msg + + +# minimal input validation +if len(args) < 1: + print("Error: missing MIIDs, provide at least one inverter serial") + sys.exit(22) + +validateMIID(args) + +# assemble message string and convert to bytes +message = str(options.bid) + buildMIIDList(args) +message = message.encode() +print('Sending to EnverBridge with ID', options.bid) + +# Create a UDP client socket w/ broadcast +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) +sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) + +# Create a UDP server socket we bind to to listen for broadcasts +ssock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +ssock.bind(('', 8764)) + +try: + # Send data via broadcast to port 8765 + print('Sending {!r}'.format(message)) + server_address = ('', 8765) + sent = sock.sendto(message, server_address) + + # Receive response on port 8764 + print('Waiting to receive') + data, server = ssock.recvfrom(8764) + print('Received {!r}'.format(data)) + +finally: + print('Closing sockets') + sock.close() + ssock.close() -- 2.39.5