Skip to content

Commit 91df807

Browse files
committed
+ Fixes bug in SSE event
+ Refactoring RIPMatlab
1 parent e2a2e8c commit 91df807

6 files changed

Lines changed: 86 additions & 95 deletions

File tree

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
.pydevproject
44

55
# Local Virtual Environment
6+
venv/
67
venv_local/
78
octave-workspace
89
start.sh
910

1011
# Not tracked files
11-
client.html
12-
client/
1312
log/
1413
Examples

RIPOctaveServer.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

rip/RIPGeneric.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,19 @@ def get(self, expid, variables):
188188
'''
189189
pass
190190

191-
def nextSample(self, wrap='data: %s\n\n'):
191+
def _getReadables(self):
192+
readables = []
193+
for r in self.metadata['readables']:
194+
readables.append(r['name'])
195+
return readables
196+
197+
def _getWritables(self):
198+
writables = []
199+
for r in self.metadata['writables']:
200+
writables.append(r['name'])
201+
return writables
202+
203+
def nextSample(self):
192204
'''
193205
Retrieve the next periodic update
194206
'''
@@ -199,18 +211,38 @@ def nextSample(self, wrap='data: %s\n\n'):
199211
while self.sseRunning:
200212
self.sampler.wait()
201213
try:
214+
self.preGetValuesToNotify()
202215
toReturn = self.getValuesToNotify()
216+
self.postGetValuesToNotify()
203217
except:
204218
toReturn = 'ERROR'
205-
response = builder.response(result=toReturn, request_id='1')
206-
yield 'id: periodiclabdata\n data: %s\n\n' % ujson.dumps(response)
219+
response = {"result":toReturn};
220+
event = 'periodiclabdata'
221+
id = round(self.sampler.time * 1000)
222+
data = ujson.dumps(response)
223+
yield 'event: %s\nid: %s\ndata: %s\n\n' % (event, id, data)
224+
225+
def preGetValuesToNotify(self):
226+
'''
227+
To do before obtaining values to notify
228+
'''
229+
pass
207230

208231
def getValuesToNotify(self):
232+
'''
233+
Which values will be notified
234+
'''
209235
return [
210236
[ 'time' ],
211237
[ self.sampler.lastTime() ]
212238
]
213239

240+
def postGetValuesToNotify(self):
241+
'''
242+
To do after obtaining values to notify
243+
'''
244+
pass
245+
214246
class Sampler(object):
215247

216248
def __init__(self, period):

rip/RIPMatlab.py

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import os.path
55

66
from rip.RIPGeneric import RIPGeneric
7-
from .MatlabConnector import CommandBuilder, MatlabConnector
8-
from .SimulinkConnector import SimulinkConnector
7+
from rip.matlab.MatlabConnector import CommandBuilder, MatlabConnector
98

109
import matlab.engine
1110

@@ -14,87 +13,62 @@ class RIPMatlab(RIPGeneric):
1413
RIP MATLAB Adapter
1514
'''
1615

17-
def __init__(self, name='Octave', description='An implementation of RIP to control Octave', authors='J. Chacon', keywords='Octave'):
16+
def __init__(self, info):
1817
'''
1918
Constructor
2019
'''
21-
super().__init__(name, description, authors, keywords)
20+
super().__init__(info)
2221

23-
self.commandBuilder = CommandBuilder()
24-
self.matlab = MatlabConnector()
25-
self.simulink = SimulinkConnector()
22+
self.matlab = matlab.engine.start_matlab('-desktop')
2623

27-
self.addMethods({
28-
'connect': { 'description': 'Start a new MATLAB session',
29-
'params': {},
30-
'implementation': self.connect,
31-
},
32-
'disconnect': { 'description': 'Finish the current MATLAB session',
33-
'params': {},
34-
'implementation': self.disconnect,
35-
},
36-
'get': { 'description': 'Read variables from the MATLAB\'s workspace',
37-
'params': { 'expId': 'string', 'variables': '[string]' },
38-
'implementation': self.get,
39-
},
40-
'set': { 'description': 'Send values to variables in the MATLAB\'s workspace',
41-
'params': { 'expId': 'string', 'variables': '[string]', 'values':'[]' },
42-
'implementation': self.set,
43-
},
44-
'eval': { 'description': 'Run MATLAB code',
45-
'params': { 'expId': 'string', 'code': '[string]'},
46-
'implementation': self.set,
47-
},
48-
})
49-
50-
def connect(self):
51-
self._matlab = matlab.engine.start_matlab('-desktop')
52-
self.simulink.set
53-
return True
54-
55-
def disconnect(self):
56-
self._matlab.quit()
24+
def default_info(self):
25+
'''
26+
Default metadata.
27+
'''
28+
return {
29+
'name': 'Matlab',
30+
'description': 'An implementation of RIP to control MATLAB',
31+
'authors': 'J. Chacon',
32+
'keywords': 'MATLAB, Raspberry PI',
33+
'readables': [],
34+
'writables': [],
35+
}
5736

58-
def set(self, variables, values):
37+
def set(self, expid, variables, values):
38+
if isinstance(variables, list):
39+
size = len(variables)
40+
for i in range(size):
41+
try:
42+
name, value = variables[i], values[i]
43+
self.matlab.workspace[name] = value
44+
except:
45+
pass
46+
else:
47+
try:
48+
name, value = variables, values
49+
self.matlab.workspace[name] = value
50+
except:
51+
pass
5952
self.matlab.set(variables, values)
6053

61-
def get(self, variables):
62-
return self.matlab.get(variables)
54+
def get(self, expid, variables):
55+
readables = self._getReadables()
56+
values = []
57+
for n in variables:
58+
try:
59+
if n in readables:
60+
v = self.matlab.workspace[n]
61+
values.append(v)
62+
except:
63+
values.append('###ERROR###')
64+
return [variables, values]
65+
66+
def preGetValuesToNotify(self):
67+
self.matlab.workspace['time'] = self.sampler.time
6368

64-
def eval(self, command):
65-
try:
66-
result = self._matlab.eval(command, nargout=0)
67-
except:
68-
pass
69-
return result
69+
def getValuesToNotify(self, expid=None):
70+
values = self.get(expid, self._getReadables())
71+
return values
7072

71-
# def _open(self, path):
72-
# try:
73-
# #open
74-
# dirname = os.path.dirname(path)
75-
# cd = self.commandBuilder.cd(dirname)
76-
# model = os.path.basename(path)
77-
# load_system = self.commandBuilder.load_system(model)
78-
# command = cd + load_system
79-
# result = self._matlab.eval(command, nargout=0)
80-
# #addEjsSubsystem
81-
# _load_model(model)
82-
#
83-
# except:
84-
# return None
85-
# return result
86-
#
87-
# def _load_model(self, model):
88-
# command = self.commandBuilder.addEjsSubblock(model) + \
89-
# self.commandBuilder.addPauseBlock(model) + \
90-
# self.commandBuilder.set_param(model, "StartTime", "0") + \
91-
# self.commandBuilder.set_param(model, "StopTime", "inf")
92-
# self._matlab.eval(command)
93-
#
94-
#
95-
# def _step(self, command):
96-
# try:
97-
# result = self._matlab.eval(command, nargout=0)
98-
# except:
99-
# return None
100-
# return result
73+
def postGetValuesToNotify(self):
74+
pass

0 commit comments

Comments
 (0)