@@ -14,27 +14,16 @@ class RIPGeneric(JsonRpcServer):
1414 RIP Server - Reference Implementation
1515 '''
1616
17- def __init__ (self , name = 'RIP Generic' , description = 'Generic RIP Server Implementation.' , authors = 'J. Chacon' , keywords = 'RIP' ):
17+ def __init__ (self , info = {} ):
1818 '''
1919 Constructor
2020 '''
21- super ().__init__ (name , description )
22-
23- self .authors = authors
24- self .keywords = keywords
21+ metadata = self ._parse_info (info )
22+ super ().__init__ (metadata ['name' ], metadata ['description' ])
23+ self .metadata = metadata
2524 self .ssePeriod = 0.5
2625 self .sseRunning = False
2726 self ._running = False
28- self .readables = [{
29- 'name' :'time' ,
30- 'description' :'Server time in seconds' ,
31- 'type' :'float' ,
32- 'min' :'0' ,
33- 'max' :'Inf' ,
34- 'precision' :'0'
35- },
36- ]
37- self .writables = []
3827 self .addMethods ({
3928 'get' : { 'description' : 'To read server variables' ,
4029 'params' : { 'expId' : 'string' , 'variables' : '[string]' },
@@ -46,6 +35,32 @@ def __init__(self, name='RIP Generic', description='Generic RIP Server Implement
4635 },
4736 })
4837
38+ def default_info (self ):
39+ return {
40+ 'name' :'RIP Generic' ,
41+ 'description' :'Generic RIP Server Implementation.' ,
42+ 'authors' : 'J. Chacon' ,
43+ 'keywords' : 'Raspberry PI, RIP' ,
44+ 'readables' : [{
45+ 'name' :'time' ,
46+ 'description' :'Server time in seconds' ,
47+ 'type' :'float' ,
48+ 'min' :'0' ,
49+ 'max' :'Inf' ,
50+ 'precision' :'0' ,
51+ }],
52+ 'writables' : [],
53+ }
54+
55+ def _parse_info (self , info ):
56+ metadata = self .default_info ()
57+ for p in info :
58+ try :
59+ metadata [p ] = info [p ]
60+ except :
61+ print ('[WARNING] Property: %s not specified. Setting default value.' % p )
62+ return metadata
63+
4964 def start (self ):
5065 '''
5166 Iniatilizes the server. Any code meant to be run at init should be here.
@@ -81,16 +96,16 @@ def build_info(self, address):
8196 info = RIPServerInfo (
8297 self .name ,
8398 self .description ,
84- authors = self .authors ,
85- keywords = self .keywords
99+ authors = self .metadata [ ' authors' ] ,
100+ keywords = self .metadata [ ' keywords' ]
86101 )
87102 readables = RIPVariablesList (
88- list_ = self .readables ,
103+ list_ = self .metadata [ ' readables' ] ,
89104 methods = [self .buildSSEGetInfo (address ), self .buildPOSTGetInfo (address )],
90105 read_notwrite = True
91106 )
92107 writables = RIPVariablesList (
93- list_ = self .writables ,
108+ list_ = self .metadata [ ' writables' ] ,
94109 methods = [self .buildPOSTSetInfo (address )],
95110 read_notwrite = False
96111 )
@@ -108,7 +123,7 @@ def buildSSEGetInfo(self, address):
108123 RIPParam (name = 'variables' ,required = 'no' ,location = 'query' ,type_ = 'array' ,subtype = 'string' ),
109124 ],
110125 returns = 'text/event-stream' ,
111- example = '%s/RIP/SSE?expId=%s' % (address , self .name ),
126+ example = '%s/RIP/SSE?expId=%s' % (address , self .metadata [ ' name' ] ),
112127 )
113128
114129 def buildPOSTGetInfo (self , address ):
@@ -129,7 +144,7 @@ def buildPOSTGetInfo(self, address):
129144 returns = 'application/json' ,
130145 example = { '%s/RIP/POST' % address : {
131146 'headers' : {'Accept' : 'application/json' ,'Content-Type' : 'application/json' },
132- 'body' : {'jsonrpc' :'2.0' , 'method' :'get' , 'params' :['%s' % self .name , [r ['name' ] for r in self .readables ]], 'id' :'1' }
147+ 'body' : {'jsonrpc' :'2.0' , 'method' :'get' , 'params' :['%s' % self .metadata [ ' name' ] , [r ['name' ] for r in self .metadata [ ' readables' ] ]], 'id' :'1' }
133148 }}
134149 )
135150
@@ -149,7 +164,7 @@ def buildPOSTSetInfo(self, address):
149164 example_post_set = {
150165 '%s/RIP/POST' % address : {
151166 'headers' : {'Accept' : 'application/json' ,'Content-Type' : 'application/json' },
152- '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' }
167+ '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' }
153168 }
154169 }
155170 return RIPMethod (
0 commit comments