From be4938ae9cae269974213111c298b2e597fef4ce Mon Sep 17 00:00:00 2001 From: Jesus Chacon Date: Tue, 12 Nov 2019 19:45:21 +0100 Subject: [PATCH] Add package samplers Move Periodic Sampler to samplers --- rip/RIPGeneric.py | 33 ++++----------------------------- samplers/Samplers.py | 34 ++++++++++++++++++++++++++++++++++ samplers/__init__.py | 1 + 3 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 samplers/Samplers.py create mode 100644 samplers/__init__.py diff --git a/rip/RIPGeneric.py b/rip/RIPGeneric.py index 00d2e26..5504454 100644 --- a/rip/RIPGeneric.py +++ b/rip/RIPGeneric.py @@ -1,11 +1,10 @@ ''' @author: jcsombria ''' -import time - from jsonrpc.JsonRpcServer import JsonRpcServer from jsonrpc.JsonRpcBuilder import JsonRpcBuilder from rip.RIPMeta import * +import samplers builder = JsonRpcBuilder() @@ -67,7 +66,7 @@ def start(self): ''' if not self.sseRunning: self.sseRunning = True - self.sampler = Sampler(self.ssePeriod) + self.sampler = samplers.Periodic(self.ssePeriod) self._running = True @property @@ -204,9 +203,10 @@ def nextSample(self): ''' Retrieve the next periodic update ''' + # TO DO: Remove this code and start when the first client arrives if not self.sseRunning: self.sseRunning = True - self.sampler = Sampler(self.ssePeriod) + self.sampler = samplers.Periodic(self.ssePeriod) while self.sseRunning: self.sampler.wait() @@ -242,28 +242,3 @@ def postGetValuesToNotify(self): To do after obtaining values to notify ''' pass - -class Sampler(object): - - def __init__(self, period): - self.Ts = period - self.reset() - - def wait(self): - self.last = self.time - self.time = time.time() - self.t0 - self.next = self.time / self.Ts + self.Ts - interval = self.Ts - self.time % self.Ts - time.sleep(interval) - - def reset(self): - self.t0 = time.time() - self.time = 0 - self.last = 0 - self.next = self.Ts - - def delta(self): - return self.time - self.last - - def lastTime(self): - return self.last diff --git a/samplers/Samplers.py b/samplers/Samplers.py new file mode 100644 index 0000000..8bba5ac --- /dev/null +++ b/samplers/Samplers.py @@ -0,0 +1,34 @@ +''' +@author: jcsombria +''' +import time + +class PeriodicSampler(object): + + def __init__(self, period): + # Set the sampling period + self.Ts = period + self.reset() + + def wait(self): + # Wait until the next sampling time + self.last = self.time + self.time = time.time() - self.t0 + self.next = self.time / self.Ts + self.Ts + interval = self.Ts - self.time % self.Ts + time.sleep(interval) + + def reset(self): + # Reset to the initial state + self.t0 = time.time() + self.time = 0 + self.last = 0 + self.next = self.Ts + + def delta(self): + # Compute the time elapsed since the last sampling time + return self.time - self.last + + def lastTime(self): + # Last sampling time + return self.last diff --git a/samplers/__init__.py b/samplers/__init__.py new file mode 100644 index 0000000..64c4096 --- /dev/null +++ b/samplers/__init__.py @@ -0,0 +1 @@ +from .Samplers import PeriodicSampler as Periodic