Skip to content

Instantly share code, notes, and snippets.

@nbuchwitz
Created December 5, 2021 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbuchwitz/7c7f335456db691c986fce70e657ce6b to your computer and use it in GitHub Desktop.
Save nbuchwitz/7c7f335456db691c986fce70e657ce6b to your computer and use it in GitHub Desktop.
#!/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'")
@nbuchwitz
Copy link
Author

nbuchwitz commented Dec 5, 2021

  1. Login on your RevPi via SSH
  2. Download the script to your RevPi:
wget https://gist.github.com/nbuchwitz/7c7f335456db691c986fce70e657ce6b/raw/5d77381b9b4b09c328a818ba9bc62c76415de4d0/fix-modbus-offset.py -O /home/pi/fix-modbus-offset.py
  1. Run the script without changing anything:
python3 /home/pi/fix-modbus-offset.py
  1. Run the script as super user and write changes:
sudo python3 /home/pi/fix-modbus-offset.py --modify

The output should look similar to this one:

No configuration file specified. Using '/var/www/revpi/pictory/projects/_config.rsc' as default
Checking device 'device_ModbusRTUMaster_20180122_1_1_001' ...
  found offset in memory configuration 'baud_rate': 2 bytes
  found offset in memory configuration 'parity': 2 bytes
  found offset in memory configuration 'data_bits': 2 bytes
  found offset in memory configuration 'stop_bits': 2 bytes
Checking device 'device_ModbusRTUMaster_20180122_1_1_002' ...
  found offset in memory configuration 'baud_rate_i04': 2 bytes
  found offset in memory configuration 'parity_i04': 2 bytes
  found offset in memory configuration 'data_bits_i04': 2 bytes
  found offset in memory configuration 'stop_bits_i04': 2 bytes
Checking device 'device_ModbusRTUMaster_20180122_1_1_003' ...
  found offset in memory configuration 'baud_rate_i05': 2 bytes
  found offset in memory configuration 'parity_i05': 2 bytes
  found offset in memory configuration 'data_bits_i05': 2 bytes
  found offset in memory configuration 'stop_bits_i05': 2 bytes

Writing changes to configuration file '/var/www/revpi/pictory/projects/_config.rsc'...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment