Skip to content

Commit be4938a

Browse files
committed
Add package samplers
Move Periodic Sampler to samplers
1 parent c2a9ebd commit be4938a

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

rip/RIPGeneric.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'''
22
@author: jcsombria
33
'''
4-
import time
5-
64
from jsonrpc.JsonRpcServer import JsonRpcServer
75
from jsonrpc.JsonRpcBuilder import JsonRpcBuilder
86
from rip.RIPMeta import *
7+
import samplers
98

109
builder = JsonRpcBuilder()
1110

@@ -67,7 +66,7 @@ def start(self):
6766
'''
6867
if not self.sseRunning:
6968
self.sseRunning = True
70-
self.sampler = Sampler(self.ssePeriod)
69+
self.sampler = samplers.Periodic(self.ssePeriod)
7170
self._running = True
7271

7372
@property
@@ -204,9 +203,10 @@ def nextSample(self):
204203
'''
205204
Retrieve the next periodic update
206205
'''
206+
# TO DO: Remove this code and start when the first client arrives
207207
if not self.sseRunning:
208208
self.sseRunning = True
209-
self.sampler = Sampler(self.ssePeriod)
209+
self.sampler = samplers.Periodic(self.ssePeriod)
210210

211211
while self.sseRunning:
212212
self.sampler.wait()
@@ -242,28 +242,3 @@ def postGetValuesToNotify(self):
242242
To do after obtaining values to notify
243243
'''
244244
pass
245-
246-
class Sampler(object):
247-
248-
def __init__(self, period):
249-
self.Ts = period
250-
self.reset()
251-
252-
def wait(self):
253-
self.last = self.time
254-
self.time = time.time() - self.t0
255-
self.next = self.time / self.Ts + self.Ts
256-
interval = self.Ts - self.time % self.Ts
257-
time.sleep(interval)
258-
259-
def reset(self):
260-
self.t0 = time.time()
261-
self.time = 0
262-
self.last = 0
263-
self.next = self.Ts
264-
265-
def delta(self):
266-
return self.time - self.last
267-
268-
def lastTime(self):
269-
return self.last

samplers/Samplers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
@author: jcsombria
3+
'''
4+
import time
5+
6+
class PeriodicSampler(object):
7+
8+
def __init__(self, period):
9+
# Set the sampling period
10+
self.Ts = period
11+
self.reset()
12+
13+
def wait(self):
14+
# Wait until the next sampling time
15+
self.last = self.time
16+
self.time = time.time() - self.t0
17+
self.next = self.time / self.Ts + self.Ts
18+
interval = self.Ts - self.time % self.Ts
19+
time.sleep(interval)
20+
21+
def reset(self):
22+
# Reset to the initial state
23+
self.t0 = time.time()
24+
self.time = 0
25+
self.last = 0
26+
self.next = self.Ts
27+
28+
def delta(self):
29+
# Compute the time elapsed since the last sampling time
30+
return self.time - self.last
31+
32+
def lastTime(self):
33+
# Last sampling time
34+
return self.last

samplers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .Samplers import PeriodicSampler as Periodic

0 commit comments

Comments
 (0)