Skip to content

Commit

Permalink
+ Fixes bug in SSE event
Browse files Browse the repository at this point in the history
+ Refactoring RIPMatlab
  • Loading branch information
jcsombria committed Oct 30, 2019
1 parent e2a2e8c commit 91df807
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 95 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
.pydevproject

# Local Virtual Environment
venv/
venv_local/
octave-workspace
start.sh

# Not tracked files
client.html
client/
log/
Examples
14 changes: 0 additions & 14 deletions RIPOctaveServer.py

This file was deleted.

38 changes: 35 additions & 3 deletions rip/RIPGeneric.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,19 @@ def get(self, expid, variables):
'''
pass

def nextSample(self, wrap='data: %s\n\n'):
def _getReadables(self):
readables = []
for r in self.metadata['readables']:
readables.append(r['name'])
return readables

def _getWritables(self):
writables = []
for r in self.metadata['writables']:
writables.append(r['name'])
return writables

def nextSample(self):
'''
Retrieve the next periodic update
'''
Expand All @@ -199,18 +211,38 @@ def nextSample(self, wrap='data: %s\n\n'):
while self.sseRunning:
self.sampler.wait()
try:
self.preGetValuesToNotify()
toReturn = self.getValuesToNotify()
self.postGetValuesToNotify()
except:
toReturn = 'ERROR'
response = builder.response(result=toReturn, request_id='1')
yield 'id: periodiclabdata\n data: %s\n\n' % ujson.dumps(response)
response = {"result":toReturn};
event = 'periodiclabdata'
id = round(self.sampler.time * 1000)
data = ujson.dumps(response)
yield 'event: %s\nid: %s\ndata: %s\n\n' % (event, id, data)

def preGetValuesToNotify(self):
'''
To do before obtaining values to notify
'''
pass

def getValuesToNotify(self):
'''
Which values will be notified
'''
return [
[ 'time' ],
[ self.sampler.lastTime() ]
]

def postGetValuesToNotify(self):
'''
To do after obtaining values to notify
'''
pass

class Sampler(object):

def __init__(self, period):
Expand Down
126 changes: 50 additions & 76 deletions rip/RIPMatlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import os.path

from rip.RIPGeneric import RIPGeneric
from .MatlabConnector import CommandBuilder, MatlabConnector
from .SimulinkConnector import SimulinkConnector
from rip.matlab.MatlabConnector import CommandBuilder, MatlabConnector

import matlab.engine

Expand All @@ -14,87 +13,62 @@ class RIPMatlab(RIPGeneric):
RIP MATLAB Adapter
'''

def __init__(self, name='Octave', description='An implementation of RIP to control Octave', authors='J. Chacon', keywords='Octave'):
def __init__(self, info):
'''
Constructor
'''
super().__init__(name, description, authors, keywords)
super().__init__(info)

self.commandBuilder = CommandBuilder()
self.matlab = MatlabConnector()
self.simulink = SimulinkConnector()
self.matlab = matlab.engine.start_matlab('-desktop')

self.addMethods({
'connect': { 'description': 'Start a new MATLAB session',
'params': {},
'implementation': self.connect,
},
'disconnect': { 'description': 'Finish the current MATLAB session',
'params': {},
'implementation': self.disconnect,
},
'get': { 'description': 'Read variables from the MATLAB\'s workspace',
'params': { 'expId': 'string', 'variables': '[string]' },
'implementation': self.get,
},
'set': { 'description': 'Send values to variables in the MATLAB\'s workspace',
'params': { 'expId': 'string', 'variables': '[string]', 'values':'[]' },
'implementation': self.set,
},
'eval': { 'description': 'Run MATLAB code',
'params': { 'expId': 'string', 'code': '[string]'},
'implementation': self.set,
},
})

def connect(self):
self._matlab = matlab.engine.start_matlab('-desktop')
self.simulink.set
return True

def disconnect(self):
self._matlab.quit()
def default_info(self):
'''
Default metadata.
'''
return {
'name': 'Matlab',
'description': 'An implementation of RIP to control MATLAB',
'authors': 'J. Chacon',
'keywords': 'MATLAB, Raspberry PI',
'readables': [],
'writables': [],
}

def set(self, variables, values):
def set(self, expid, variables, values):
if isinstance(variables, list):
size = len(variables)
for i in range(size):
try:
name, value = variables[i], values[i]
self.matlab.workspace[name] = value
except:
pass
else:
try:
name, value = variables, values
self.matlab.workspace[name] = value
except:
pass
self.matlab.set(variables, values)

def get(self, variables):
return self.matlab.get(variables)
def get(self, expid, variables):
readables = self._getReadables()
values = []
for n in variables:
try:
if n in readables:
v = self.matlab.workspace[n]
values.append(v)
except:
values.append('###ERROR###')
return [variables, values]

def preGetValuesToNotify(self):
self.matlab.workspace['time'] = self.sampler.time

def eval(self, command):
try:
result = self._matlab.eval(command, nargout=0)
except:
pass
return result
def getValuesToNotify(self, expid=None):
values = self.get(expid, self._getReadables())
return values

# def _open(self, path):
# try:
# #open
# dirname = os.path.dirname(path)
# cd = self.commandBuilder.cd(dirname)
# model = os.path.basename(path)
# load_system = self.commandBuilder.load_system(model)
# command = cd + load_system
# result = self._matlab.eval(command, nargout=0)
# #addEjsSubsystem
# _load_model(model)
#
# except:
# return None
# return result
#
# def _load_model(self, model):
# command = self.commandBuilder.addEjsSubblock(model) + \
# self.commandBuilder.addPauseBlock(model) + \
# self.commandBuilder.set_param(model, "StartTime", "0") + \
# self.commandBuilder.set_param(model, "StopTime", "inf")
# self._matlab.eval(command)
#
#
# def _step(self, command):
# try:
# result = self._matlab.eval(command, nargout=0)
# except:
# return None
# return result
def postGetValuesToNotify(self):
pass
File renamed without changes.
File renamed without changes.

0 comments on commit 91df807

Please sign in to comment.