Skip to content

Commit

Permalink
Update scripts for newmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Sep 15, 2019
1 parent 80b4206 commit b2af2bf
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
87 changes: 87 additions & 0 deletions bin/refresh_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python

"""Refresh rtl_433 JSON outputs."""

import sys
import os
import fnmatch
import subprocess
import json


def run_rtl433(input_fn, samplerate=None, protocol=None, rtl_433_cmd="rtl_433"):
"""Run rtl_433 and return output."""
args = ['-c', '0', '-M', 'newmodel']
if protocol:
args.extend(['-R', str(protocol)])
if samplerate:
args.extend(['-s', str(samplerate)])
args.extend(['-F', 'json', '-r', input_fn])
cmd = [rtl_433_cmd] + args
# print(" ".join(cmd))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return (out, err, p.returncode)


def convert(root, filename):
output_fn = os.path.join(root, filename)

input_fn = os.path.splitext(output_fn)[0] + ".cu8"
if not os.path.isfile(input_fn):
print("WARNING: Missing '%s'" % input_fn)
return

ignore_fn = os.path.join(os.path.dirname(output_fn), "ignore")
if os.path.isfile(ignore_fn):
print("WARNING: Ignoring '%s'" % input_fn)
return

samplerate = 250000
samplerate_fn = os.path.join(os.path.dirname(output_fn), "samplerate")
if os.path.isfile(samplerate_fn):
with open(samplerate_fn, "r") as samplerate_file:
samplerate = int(samplerate_file.readline())

protocol = None
protocol_fn = os.path.join(os.path.dirname(output_fn), "protocol")
if os.path.isfile(protocol_fn):
with open(protocol_fn, "r") as protocol_file:
protocol = int(protocol_file.readline())

# Open expected data
old_data = []
with open(output_fn, "r") as output_file:
old_data = output_file.read().splitlines()

# Run rtl_433
out, _err, exitcode = run_rtl433(input_fn, samplerate, protocol)

if exitcode:
print("ERROR: Exited with %d '%s'" % (exitcode, input_fn))

# get JSON results
out = out.decode('ascii')
new_data = out.splitlines()

if len(old_data) != len(new_data):
print("\nWARNING: Different data for '%s'" % (input_fn))
print('\n'.join(old_data))
print("vs.")
print('\n'.join(new_data))

with open(output_fn, "w") as output_file:
output_file.write('\n'.join(new_data))
output_file.write('\n')


def main():
"""Process all reference json files."""

for root, _dirnames, filenames in os.walk('tests'):
for filename in fnmatch.filter(filenames, '*.json'):
convert(root, filename)


if __name__ == '__main__':
sys.exit(main())
16 changes: 9 additions & 7 deletions bin/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
from deepdiff import DeepDiff


def run_rtl433(input_fn, rtl_433_cmd="rtl_433",
samplerate=250000, protocol=None):
def run_rtl433(input_fn, samplerate=None, protocol=None, rtl_433_cmd="rtl_433"):
"""Run rtl_433 and return output."""
args = ['-F', 'json', '-s', str(samplerate), '-r', input_fn]
args = ['-c', '0', '-M', 'newmodel']
if protocol:
args = ['-R', str(protocol)] + args
args.extend(['-R', str(protocol)])
if samplerate:
args.extend(['-s', str(samplerate)])
args.extend(['-F', 'json', '-r', input_fn])
cmd = [rtl_433_cmd] + args
# print(" ".join(cmd))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Expand Down Expand Up @@ -94,14 +96,14 @@ def main():
if not json_line.strip():
continue
expected_data.append(json.loads(json_line))
except ValueError as err:
except ValueError as _err:
print("ERROR: invalid json: '%s'" % output_fn)
continue
expected_data = remove_fields(expected_data, ignore_fields)

# Run rtl_433
rtl433out, err, exitcode = run_rtl433(input_fn, rtl_433_cmd,
samplerate, protocol)
rtl433out, _err, exitcode = run_rtl433(input_fn, samplerate,
protocol, rtl_433_cmd)

if exitcode:
print("ERROR: Exited with %d '%s'" % (exitcode, input_fn))
Expand Down

0 comments on commit b2af2bf

Please sign in to comment.