Skip to content

Commit

Permalink
Add package samplers
Browse files Browse the repository at this point in the history
Move Periodic Sampler to samplers
  • Loading branch information
jcsombria committed Nov 12, 2019
1 parent c2a9ebd commit be4938a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
33 changes: 4 additions & 29 deletions rip/RIPGeneric.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
34 changes: 34 additions & 0 deletions samplers/Samplers.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions samplers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .Samplers import PeriodicSampler as Periodic

0 comments on commit be4938a

Please sign in to comment.