Created
December 5, 2021 17:38
-
-
Save nbuchwitz/7c7f335456db691c986fce70e657ce6b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import sys | |
import json | |
import argparse | |
import os.path | |
DEFAULT_CONFIG_NEW = "/var/www/revpi/pictory/projects/_config.rsc" | |
DEFAULT_CONFIG_OLD = "/var/www/pictory/projects/_config.rsc" | |
def current_offset(io): | |
return int(io[3]) | |
def next_offset(io): | |
current_offset = int(io[3]) | |
length = int(io[2]) / 8 | |
return int(current_offset + length) | |
def fix_memory(io, delta): | |
io[3] = str(int(io[3]) - delta) | |
return io | |
def write_configuration(config, filename): | |
print(f"Writing changes to configuration file '{filename}'...") | |
with open(filename, "w") as f: | |
json.dump(config, f) | |
parser = argparse.ArgumentParser( | |
description='Fix wrong offset of ModbusRTUMaster in Pictory config file') | |
parser.add_argument('-m', '--modify', action='store_true', dest='modify_config', | |
default=False, help='modify configuration file', required=False) | |
parser.add_argument('-f', '--filename', metavar='PICTORY_CONFIGURATION_FILE', | |
help='path to pictory configuration file', required=False, default=None) | |
args = parser.parse_args() | |
# try default configuration files | |
if args.filename is None: | |
# new path since Buster | |
if os.path.isfile(DEFAULT_CONFIG_NEW): | |
args.filename = DEFAULT_CONFIG_NEW | |
# old path Stretch and older | |
elif os.path.isfile(DEFAULT_CONFIG_OLD): | |
args.filename = DEFAULT_CONFIG_OLD | |
else: | |
print("Could not automatically detect the default pictory configuration file. Please specify a file with the argument '-f'") | |
sys.exit(1) | |
print(f"No configuration file specified. Using '{args.filename}' as default") | |
if not os.path.isfile(args.filename): | |
print(f"configuration file '{args.filename}' does not exist") | |
sys.exit(2) | |
with open(args.filename, 'r') as f: | |
data = json.load(f) | |
num_changes = 0 | |
for device in data.get("Devices", []): | |
# only check for ModbusRTUMaster | |
if device['productType'] != "24580" or not device['id'].startswith('device_ModbusRTUMaster_20180122_1_1_'): | |
continue | |
print(f"Checking device '{device['id']}' ...") | |
# get last offset + length of last output | |
last_output = device['out'][max(device['out'].keys())] | |
offset_first_mem = next_offset(last_output) | |
offset_calculated = offset_first_mem | |
for idx_m, memory in device['mem'].items(): | |
offset = current_offset(memory) | |
if offset > offset_calculated: | |
delta = offset - offset_calculated | |
print(f" found offset in memory configuration '{memory[0]}': {delta} bytes") | |
memory = fix_memory(memory, delta) | |
num_changes += 1 | |
offset_calculated = next_offset(memory) | |
if not num_changes: | |
print(" no changes necessary") | |
if num_changes: | |
print() | |
if args.modify_config: | |
try: | |
write_configuration(data, args.filename) | |
except IOError as e: | |
print(f"ERROR: Could not write configuration file. Please make sure that you have write permissions to this file.") | |
sys.exit(3) | |
else: | |
print(f"NOTE: The configuration file has not been modified. To apply {num_changes} modifications run again with argument '-m'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output should look similar to this one: