2018-09-21 12:35:23 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
|
|
|
|
@file: timer.py
|
|
|
|
|
@date: 21.09.2018
|
|
|
|
|
@author: Bastian Schroll
|
|
|
|
|
@description: Timer class for interval timed events
|
|
|
|
|
"""
|
|
|
|
|
import logging
|
|
|
|
|
import time
|
|
|
|
|
from threading import Thread, Event
|
|
|
|
|
|
|
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RepeatedTimer:
|
|
|
|
|
|
|
|
|
|
def __init__(self, interval, targetFunction, *args, **kwargs):
|
|
|
|
|
"""!Create a new instance of the RepeatedTimer
|
|
|
|
|
|
|
|
|
|
@param interval: interval in sec. to recall target function
|
|
|
|
|
@param targetFunction: function to call on timer event
|
|
|
|
|
@param *args: arguments for the called function
|
|
|
|
|
@param *kwargs: keyword arguments for the called function
|
|
|
|
|
"""
|
|
|
|
|
self._interval = interval
|
|
|
|
|
self._function = targetFunction
|
|
|
|
|
self._args = args
|
|
|
|
|
self._kwargs = kwargs
|
|
|
|
|
self._start = 0
|
2019-10-26 18:56:39 +02:00
|
|
|
self.overdueCount = 0
|
|
|
|
|
self.lostEvents = 0
|
2018-09-23 21:08:44 +02:00
|
|
|
self._isRunning = False
|
2018-09-21 12:35:23 +02:00
|
|
|
self._event = Event()
|
|
|
|
|
self._thread = None
|
|
|
|
|
|
|
|
|
|
def start(self):
|
2018-09-21 14:21:33 +02:00
|
|
|
"""!Start a new timer worker thread
|
|
|
|
|
|
|
|
|
|
@return True or False"""
|
2019-03-02 09:15:40 +01:00
|
|
|
if self._thread is None:
|
|
|
|
|
self._event.clear()
|
|
|
|
|
self._thread = Thread(target=self._target)
|
|
|
|
|
self._thread.name = "RepTim(" + str(self._interval) + ")"
|
|
|
|
|
self._thread.daemon = True # start as daemon (thread dies if main program ends)
|
|
|
|
|
self._thread.start()
|
|
|
|
|
logging.debug("start repeatedTimer: %s", self._thread.name)
|
|
|
|
|
return True
|
2019-03-03 19:39:53 +01:00
|
|
|
logging.debug("repeatedTimer always started")
|
|
|
|
|
return True
|
2018-09-21 12:35:23 +02:00
|
|
|
|
|
|
|
|
def stop(self):
|
2018-09-21 13:30:41 +02:00
|
|
|
"""!Stop the timer worker thread
|
|
|
|
|
|
|
|
|
|
@return True or False"""
|
2019-03-02 09:15:40 +01:00
|
|
|
self._event.set()
|
|
|
|
|
if self._thread is not None:
|
|
|
|
|
logging.debug("stop repeatedTimer: %s", self._thread.name)
|
|
|
|
|
self._thread.join()
|
2018-09-21 13:30:41 +02:00
|
|
|
return True
|
2019-03-02 09:15:40 +01:00
|
|
|
logging.warning("repeatedTimer always stopped")
|
|
|
|
|
return True
|
2018-09-21 12:35:23 +02:00
|
|
|
|
|
|
|
|
def _target(self):
|
2018-09-21 20:14:56 +02:00
|
|
|
"""!Runs the target function with his arguments in own thread"""
|
2018-09-21 13:30:41 +02:00
|
|
|
self._start = time.time()
|
2018-09-21 12:35:23 +02:00
|
|
|
while not self._event.wait(self.restTime):
|
|
|
|
|
logging.debug("work")
|
|
|
|
|
startTime = time.time()
|
|
|
|
|
|
2018-09-21 13:30:41 +02:00
|
|
|
try:
|
|
|
|
|
self._function(*self._args, **self._kwargs)
|
2018-09-23 21:08:44 +02:00
|
|
|
except: # pragma: no cover
|
2018-09-21 13:30:41 +02:00
|
|
|
logging.exception("target throws an exception")
|
|
|
|
|
|
|
|
|
|
runTime = time.time() - startTime
|
2018-09-21 12:35:23 +02:00
|
|
|
if runTime < self._interval:
|
|
|
|
|
logging.debug("ready after: %0.3f sec. - next call in: %0.3f sec.", runTime, self.restTime)
|
|
|
|
|
else:
|
2018-09-23 21:08:44 +02:00
|
|
|
lostEvents = int(runTime / self._interval)
|
|
|
|
|
logging.warning("timer overdue! interval: %0.3f sec. - runtime: %0.3f sec. - "
|
|
|
|
|
"%d events lost - next call in: %0.3f sec.", self._interval, runTime, lostEvents, self.restTime)
|
2019-10-26 18:56:39 +02:00
|
|
|
self.lostEvents += lostEvents
|
|
|
|
|
self.overdueCount += 1
|
2018-09-23 18:44:58 +02:00
|
|
|
logging.debug("repeatedTimer thread stopped: %s", self._thread.name)
|
2018-09-23 21:39:50 +02:00
|
|
|
self._thread = None # set to none after leave teh thread (running recognize)
|
2018-09-23 21:15:37 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def isRunning(self):
|
2018-09-23 21:39:50 +02:00
|
|
|
"""!Property for repeatedTimer running state"""
|
2018-09-23 21:15:37 +02:00
|
|
|
if self._thread:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2018-09-23 18:44:58 +02:00
|
|
|
|
2018-09-21 12:35:23 +02:00
|
|
|
@property
|
|
|
|
|
def restTime(self):
|
|
|
|
|
"""!Property to get remaining time till next call"""
|
|
|
|
|
return self._interval - ((time.time() - self._start) % self._interval)
|