Skip to content

Commit

Permalink
Na vervanging meter DSMR Reader instabiel #444
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Feb 26, 2018
1 parent 6f9e6f9 commit 7879927
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions dsmr_backend/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
class InfiniteManagementCommandMixin(object):
""" Mixin for long running management commands, only stopping (gracefully) on SIGHUP signal. """
sleep_time = None
max_run_count = None
_keep_alive = None
_pid_file = None
_run_count = None

def add_arguments(self, parser):
parser.add_argument(
Expand Down Expand Up @@ -58,12 +60,23 @@ def handle(self, **options):
self._keep_alive = True
print('Starting infinite command loop...') # Just to make sure it gets printed.

self._run_count = 0

while self._keep_alive:

try:
self.run(**options)
except Exception as error:
self.stdout.write(' [!] Exception raised in run(): {}'.format(error))

# If we have a run cap, make sure to maintain it.
if self.max_run_count is not None:
self._run_count += 1

if self._run_count >= self.max_run_count:
self.stdout.write('Reached max number of runs ({}), exiting....'.format(self.max_run_count))
sys.exit(0)

if self.sleep_time is not None:
self.stdout.write('Command completed. Sleeping for {} second(s)...'.format(self.sleep_time))
self.stdout.write('')
Expand Down
2 changes: 1 addition & 1 deletion dsmr_datalogger/management/commands/dsmr_datalogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class Command(InfiniteManagementCommandMixin, BaseCommand):
help = _('Performs an DSMR P1 telegram reading on the COM port.')
name = __name__ # Required for PID file.
sleep_time = 0.25
sleep_time = settings.DSMRREADER_DATALOGGER_SLEEP
max_run_count = 10

def run(self, **options):
""" InfiniteManagementCommandMixin listens to handle() and calls run() in a loop. """
Expand Down

0 comments on commit 7879927

Please sign in to comment.