Created
July 10, 2024 11:15
-
-
Save igor-podpalchenko/ee2f919881cc554db70026e803820df6 to your computer and use it in GitHub Desktop.
Script for BLUETTI modbus discovery-log parsing to ReadHoldingRegisters parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
def find_available_ranges(filename): | |
available_addresses = [] | |
with open(filename, 'r') as file: | |
for i, line in enumerate(file): | |
log_entry = json.loads(line) | |
# Check if the "error" field exists and if it contains "MODBUS Exception" | |
if "error" not in log_entry or "MODBUS Exception" not in log_entry["error"]: | |
available_addresses.append(i + 0) # Adjust starting index as per example | |
# Finding continuous ranges | |
ranges = [] | |
if available_addresses: | |
start = available_addresses[0] | |
end = available_addresses[0] | |
for address in available_addresses[1:]: | |
if address == end + 1: | |
end = address | |
else: | |
ranges.append((start, end)) | |
start = address | |
end = address | |
ranges.append((start, end)) | |
return ranges | |
def main(): | |
if len(sys.argv) != 2: | |
print("Usage: python range_finder.py <filename>") | |
sys.exit(1) | |
filename = sys.argv[1] | |
ranges = find_available_ranges(filename) | |
print("Available ranges of starting addresses:") | |
for start, end in ranges: | |
if start == end: | |
print("{}".format(start)) | |
else: | |
print("{} - {}".format(start, end)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment