Skip to content

Commit

Permalink
Definition of Readables/Writables in AppConfig.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jcsombria committed Oct 28, 2019
1 parent 9a4a4ad commit e2a2e8c
Show file tree
Hide file tree
Showing 4,328 changed files with 269 additions and 1,052,988 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
7 changes: 1 addition & 6 deletions App.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ def load_control(control):
RIPControl = getattr(module, control_name)

info = config['control']['info']
return RIPControl(
info['name'],
info['description'],
info['authors'],
info['keywords'],
)
return RIPControl(info)

if __name__ == "__main__":
control = load_control(config['control'])
Expand Down
25 changes: 18 additions & 7 deletions AppConfig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file contains the configuration of the RIP server applicationself.
# This file contains the configuration of the RIP server application.
config = {
# TO DO: The server will listen to host:port
'server': {
Expand All @@ -10,14 +10,25 @@
# The 'impl' field should contain the name of the module (.py) and the
# class that implement the control interface
'control': {
'impl_module': 'RIPOctave',
'impl_module': 'RIPGeneric',
# Also, if the class name is not the same as the module name:
#'impl_name': 'RIPOctave',
'info': {
'name': 'Octave',
'description': 'An implementation of RIP to control Octave',
'authors': 'D. Garcia, J. Chacon',
'keywords': 'Octave, Raspberry PI, Robot',
}
'name': 'RIP Generic',
'description': 'A generic implementation of RIP',
'authors': 'J. Chacon',
'keywords': 'Raspberry PI, RIP',
# Server readable objects
'readables': [{
'name':'time',
'description':'Server time in seconds',
'type':'float',
'min':'0',
'max':'Inf',
'precision':'0'
}],
# Server writable objects
'writables': []
},
}
}
19 changes: 19 additions & 0 deletions config-examples/AppConfig-AdapterTemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file contains the configuration of the RIP server application.
config = {
# TO DO: The server will listen to host:port
'server': {
'host': '127.0.0.1',
'port': 8080,
},
# The 'control' section configures the mapping between the RIP protocol
# and the actual implementation of the functionality.
# The 'impl' field should contain the name of the module (.py) and the
# class that implement the control interface
'control': {
'impl_module': 'RIPAdapterTemplate',
'info': {
'name': 'RIPRandom',
'description': 'A random numbers generator',
},
}
}
34 changes: 34 additions & 0 deletions config-examples/AppConfig-Generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file contains the configuration of the RIP server application.
config = {
# TO DO: The server will listen to host:port
'server': {
'host': '127.0.0.1',
'port': 8080,
},
# The 'control' section configures the mapping between the RIP protocol
# and the actual implementation of the functionality.
# The 'impl' field should contain the name of the module (.py) and the
# class that implement the control interface
'control': {
'impl_module': 'RIPGeneric',
# Also, if the class name is not the same as the module name:
#'impl_name': 'RIPOctave',
'info': {
'name': 'RIP Generic',
'description': 'A generic implementation of RIP',
'authors': 'J. Chacon',
'keywords': 'Raspberry PI, RIP',
# Server readable objects
'readables': [{
'name':'time',
'description':'Server time in seconds',
'type':'float',
'min':'0',
'max':'Inf',
'precision':'0'
}],
# Server writable objects
'writables': []
},
}
}
22 changes: 20 additions & 2 deletions config-examples/AppConfig-Octave.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file contains the configuration of the RIP server applicationself.
# This file contains the configuration of the RIP server application.
config = {
# TO DO: The server will listen to host:port
'server': {
Expand All @@ -18,6 +18,24 @@
'description': 'An implementation of RIP to control Octave',
'authors': 'D. Garcia, J. Chacon',
'keywords': 'Octave, Raspberry PI, Robot',
}
# Server readable objects
'readables': [{
'name':'x',
'description':'Testing readable variable',
'type':'float',
'min':'-Inf',
'max':'Inf',
'precision':'0'
}],
# Server writable objects
'writables': [{
'name':'x',
'description':'Testing readable variable',
'type':'float',
'min':'-Inf',
'max':'Inf',
'precision':'0'
}],
},
}
}
6 changes: 4 additions & 2 deletions config-examples/AppConfig-RedPitaya.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file contains the configuration of the RIP server applicationself.
# This file contains the configuration of the RIP server application.
config = {
# TO DO: The server will listen to host:port
'server': {
Expand All @@ -12,10 +12,12 @@
'control': {
'impl_module': 'RIPRedPitaya',
'info': {
'name': 'Octave',
'name': 'RedPitaya',
'description': 'An implementation of RIP to control Red Pitaya',
'authors': 'Amine my-taj',
'keywords': 'Red Pitaya, Raspberry PI',
'readables': [],
'writables': [],
}
}
}
102 changes: 102 additions & 0 deletions rip/RIPAdapterTemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'''
@author: jcsombria
'''
import random
from rip.RIPGeneric import RIPGeneric

class RIPAdapterTemplate(RIPGeneric):
'''
RIP Adapter Template
'''

def __init__(self, info={}):
'''
Constructor
'''
super().__init__(info)

def default_info(self):
'''
You can provide default metadata here. AppConfig will override this definition.
'''
return {
'name':'RIPAdapterTemplate',
'description':'A template to extend RIP Generic',
'authors':'J. Chacon',
'keywords':'Adapter Template',
'readables':[{
'name':'time',
'description':'Server time in seconds',
'type':'float',
'min':'0',
'max':'Inf',
'precision':'0',
}, {
'name':'random',
'description':'Random value generator',
'type':'float',
'min':'0',
'max':'1',
'precision':'0'
}],
'writables': [{
'name':'seed',
'description':'Random seed',
'type':'float',
'min':'0',
'max':'1',
'precision':'0'
}],
}

def set(self, expid, variables, values):
'''
Write on or more variables
'''
n = len(variables)
for i in range(n):
try:
n, v = variables[i], values[i]
if v in writables:
self.n = v
except:
pass

def get(self, expid, variables):
'''
Retrieve one or more variables under request
'''
toReturn = {}
n = len(variables)
for i in range(n):
name = variables[i]
try:
toReturn[name] = random.rand
except:
pass
return toReturn

def getValuesToNotify(self):
'''
Variables to include in periodic SSE updates
'''
return [
['time', 'random'],
[self.sampler.lastTime(), self.random]
]

@property
def seed(self):
return self._seed

@seed.setter
def seed(self, value):
random.seed(value)

@property
def random(self):
return random.random()

@random.setter
def random(self, value):
pass
59 changes: 37 additions & 22 deletions rip/RIPGeneric.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,16 @@ class RIPGeneric(JsonRpcServer):
RIP Server - Reference Implementation
'''

def __init__(self, name='RIP Generic', description='Generic RIP Server Implementation.', authors='J. Chacon', keywords='RIP'):
def __init__(self, info={}):
'''
Constructor
'''
super().__init__(name, description)

self.authors = authors
self.keywords = keywords
metadata = self._parse_info(info)
super().__init__(metadata['name'], metadata['description'])
self.metadata = metadata
self.ssePeriod = 0.5
self.sseRunning = False
self._running = False
self.readables = [{
'name':'time',
'description':'Server time in seconds',
'type':'float',
'min':'0',
'max':'Inf',
'precision':'0'
},
]
self.writables = []
self.addMethods({
'get': { 'description': 'To read server variables',
'params': { 'expId': 'string', 'variables': '[string]' },
Expand All @@ -46,6 +35,32 @@ def __init__(self, name='RIP Generic', description='Generic RIP Server Implement
},
})

def default_info(self):
return {
'name':'RIP Generic',
'description':'Generic RIP Server Implementation.',
'authors': 'J. Chacon',
'keywords': 'Raspberry PI, RIP',
'readables': [{
'name':'time',
'description':'Server time in seconds',
'type':'float',
'min':'0',
'max':'Inf',
'precision':'0',
}],
'writables': [],
}

def _parse_info(self, info):
metadata = self.default_info()
for p in info:
try:
metadata[p] = info[p]
except:
print('[WARNING] Property: %s not specified. Setting default value.' % p)
return metadata

def start(self):
'''
Iniatilizes the server. Any code meant to be run at init should be here.
Expand Down Expand Up @@ -81,16 +96,16 @@ def build_info(self, address):
info = RIPServerInfo(
self.name,
self.description,
authors=self.authors,
keywords=self.keywords
authors=self.metadata['authors'],
keywords=self.metadata['keywords']
)
readables = RIPVariablesList(
list_=self.readables,
list_=self.metadata['readables'],
methods=[self.buildSSEGetInfo(address), self.buildPOSTGetInfo(address)],
read_notwrite=True
)
writables = RIPVariablesList(
list_=self.writables,
list_=self.metadata['writables'],
methods=[self.buildPOSTSetInfo(address)],
read_notwrite=False
)
Expand All @@ -108,7 +123,7 @@ def buildSSEGetInfo(self, address):
RIPParam(name='variables',required='no',location='query',type_='array',subtype='string'),
],
returns='text/event-stream',
example='%s/RIP/SSE?expId=%s' % (address, self.name),
example='%s/RIP/SSE?expId=%s' % (address, self.metadata['name']),
)

def buildPOSTGetInfo(self, address):
Expand All @@ -129,7 +144,7 @@ def buildPOSTGetInfo(self, address):
returns='application/json',
example={ '%s/RIP/POST' % address: {
'headers': {'Accept': 'application/json','Content-Type': 'application/json'},
'body': {'jsonrpc':'2.0', 'method':'get', 'params':['%s' % self.name, [r['name'] for r in self.readables]], 'id':'1'}
'body': {'jsonrpc':'2.0', 'method':'get', 'params':['%s' % self.metadata['name'], [r['name'] for r in self.metadata['readables']]], 'id':'1'}
}}
)

Expand All @@ -149,7 +164,7 @@ def buildPOSTSetInfo(self, address):
example_post_set = {
'%s/RIP/POST' % address: {
'headers': {'Accept': 'application/json','Content-Type': 'application/json'},
'body': {'jsonrpc':'2.0','method':'set','params':['%s' % self.name,[w['name'] for w in self.writables],['val' for w in self.writables]],'id':'1'}
'body': {'jsonrpc':'2.0','method':'set','params':['%s' % self.metadata['name'],[w['name'] for w in self.metadata['writables']],['val' for w in self.metadata['writables']]],'id':'1'}
}
}
return RIPMethod(
Expand Down
Loading

0 comments on commit e2a2e8c

Please sign in to comment.