Fix some bugs.

- Commandreplies format their timestamp like events

Change-Id: I388b9f26bb8b0234d9209b05732e98f9ce1d01c7
This commit is contained in:
Enrico Faulhaber 2016-12-21 16:59:32 +01:00
parent 78bb3b5f96
commit 68f73b5aa1
4 changed files with 39 additions and 21 deletions

View File

@ -132,7 +132,7 @@ class Value(object):
class Client(object): class Client(object):
equipmentId = 'unknown' equipment_id = 'unknown'
secop_id = 'unknown' secop_id = 'unknown'
describing_data = {} describing_data = {}
stopflag = False stopflag = False
@ -182,12 +182,12 @@ class Client(object):
def _inner_run(self): def _inner_run(self):
data = '' data = ''
self.connection.writeline('*IDN?') self.connection.writeline('*IDN?')
idstring = self.connection.readline()
self.log.info('connected to: ' + idstring.strip())
while not self.stopflag: while not self.stopflag:
line = self.connection.readline() line = self.connection.readline()
self.log.debug('got answer %r' % line)
if line.startswith(('SECoP', 'Sine2020WP7')): if line.startswith(('SECoP', 'Sine2020WP7')):
self.log.info('connected to: ' + line.strip())
self.secop_id = line self.secop_id = line
continue continue
msgtype, spec, data = self._decode_message(line) msgtype, spec, data = self._decode_message(line)
@ -196,12 +196,20 @@ class Client(object):
self._handle_event(spec, data) self._handle_event(spec, data)
if msgtype != 'event': if msgtype != 'event':
# handle sync stuff # handle sync stuff
if msgtype == "ERROR" or msgtype in self.expected_replies: if msgtype in self.expected_replies:
# XXX: make an assignment of ERROR to an expected reply.
entry = self.expected_replies[msgtype] entry = self.expected_replies[msgtype]
entry.extend([spec, data]) entry.extend([msgtype, spec, data])
# wake up calling process # wake up calling process
entry[0].set() entry[0].set()
elif msgtype == "ERROR":
# XXX: hack!
if len(self.expected_replies) == 1:
entry = self.expected_replies.values()[0]
entry.extend([msgtype, spec, data])
# wake up calling process
entry[0].set()
# XXX: make an assignment of ERROR to an expected reply.
self.log.error('TODO: handle ERROR replies!')
else: else:
self.log.error('ignoring unexpected reply %r' % line) self.log.error('ignoring unexpected reply %r' % line)
@ -234,7 +242,7 @@ class Client(object):
def _handle_event(self, spec, data): def _handle_event(self, spec, data):
"""handles event""" """handles event"""
self.log.info('handle_event %r %r' % (spec, data)) self.log.debug('handle_event %r %r' % (spec, data))
if ':' not in spec: if ':' not in spec:
self.log.warning("deprecated specifier %r" % spec) self.log.warning("deprecated specifier %r" % spec)
spec = '%s:value' % spec spec = '%s:value' % spec
@ -281,7 +289,7 @@ class Client(object):
"activate": "active", "activate": "active",
"deactivate": "inactive", "deactivate": "inactive",
"*IDN?": "SECoP,", "*IDN?": "SECoP,",
"ping": "ping", "ping": "pong",
} }
if self.stopflag: if self.stopflag:
raise RuntimeError('alreading stopping!') raise RuntimeError('alreading stopping!')
@ -306,14 +314,18 @@ class Client(object):
"can not have more than one requests of the same type at the same time!") "can not have more than one requests of the same type at the same time!")
event = threading.Event() event = threading.Event()
self.expected_replies[rply] = [event] self.expected_replies[rply] = [event]
self.log.debug('prepared reception of %r msg' % rply)
self.connection.writeline(self._encode_message(msgtype, spec, data)) self.connection.writeline(self._encode_message(msgtype, spec, data))
self.log.debug('sent %r msg' % msgtype)
if event.wait(10): # wait 10s for reply if event.wait(10): # wait 10s for reply
result = rply, self.expected_replies[rply][ self.log.debug('checking reply')
1], self.expected_replies[rply][2] result = self.expected_replies[rply][1:4]
del self.expected_replies[rply] del self.expected_replies[rply]
# if result[0] == "ERROR":
# raise RuntimeError('Got %s! %r' % (str(result[1]), repr(result[2])))
return result return result
del self.expected_replies[rply] del self.expected_replies[rply]
raise RuntimeError("timeout upon waiting for reply!") raise RuntimeError("timeout upon waiting for reply to %r!" % msgtype)
def quit(self): def quit(self):
# after calling this the client is dysfunctional! # after calling this the client is dysfunctional!
@ -339,6 +351,10 @@ class Client(object):
if not async: if not async:
self.communicate('deactivate') self.communicate('deactivate')
@property
def equipmentId(self):
return self.equipment_id
@property @property
def protocolVersion(self): def protocolVersion(self):
return self.secop_id return self.secop_id
@ -360,5 +376,5 @@ class Client(object):
return self.describing_data['modules'][ return self.describing_data['modules'][
module]['parameters'][parameter].items() module]['parameters'][parameter].items()
def syncCommunicate(self, msg): def syncCommunicate(self, *msg):
return self.communicate(msg) return self.communicate(*msg)

View File

@ -241,13 +241,8 @@ class Dispatcher(object):
# note: exceptions are handled in handle_request, not here! # note: exceptions are handled in handle_request, not here!
func = getattr(moduleobj, 'do' + command) func = getattr(moduleobj, 'do' + command)
res = func(*arguments) res = func(*arguments)
res = CommandReply( res = CommandReply(module=modulename, command=command,
module=modulename, result=res, qualifiers=dict(t=time.time()))
command=command,
result=[
res,
dict(
t=time.time())])
# res = Value(modulename, command=command, value=func(*arguments), t=time.time()) # res = Value(modulename, command=command, value=func(*arguments), t=time.time())
return res return res

View File

@ -76,6 +76,12 @@ ERRORCLASSES = ['NoSuchDevice', 'NoSuchParameter', 'NoSuchCommand',
# starts with another # starts with another
def encode_cmd_result(msgobj):
q = msgobj.qualifiers.copy()
if 't' in q:
q['t'] = format_time(q['t'])
return msgobj.result, q
def encode_value_data(vobj): def encode_value_data(vobj):
q = vobj.qualifiers.copy() q = vobj.qualifiers.copy()
if 't' in q: if 't' in q:
@ -95,7 +101,7 @@ class DemoEncoder(MessageEncoder):
DeactivateRequest: (DISABLEEVENTSREQUEST,), DeactivateRequest: (DISABLEEVENTSREQUEST,),
DeactivateReply: (DISABLEEVENTSREPLY,), DeactivateReply: (DISABLEEVENTSREPLY,),
CommandRequest: (COMMANDREQUEST, lambda msg: "%s:%s" % (msg.module, msg.command), 'arguments',), CommandRequest: (COMMANDREQUEST, lambda msg: "%s:%s" % (msg.module, msg.command), 'arguments',),
CommandReply: (COMMANDREPLY, lambda msg: "%s:%s" % (msg.module, msg.command), 'result',), CommandReply: (COMMANDREPLY, lambda msg: "%s:%s" % (msg.module, msg.command), encode_cmd_result,),
WriteRequest: (WRITEREQUEST, lambda msg: "%s:%s" % (msg.module, msg.parameter) if msg.parameter else msg.module, 'value',), WriteRequest: (WRITEREQUEST, lambda msg: "%s:%s" % (msg.module, msg.parameter) if msg.parameter else msg.module, 'value',),
WriteReply: (WRITEREPLY, lambda msg: "%s:%s" % (msg.module, msg.parameter) if msg.parameter else msg.module, 'value',), WriteReply: (WRITEREPLY, lambda msg: "%s:%s" % (msg.module, msg.parameter) if msg.parameter else msg.module, 'value',),
PollRequest: (TRIGGERREQUEST, lambda msg: "%s:%s" % (msg.module, msg.parameter) if msg.parameter else msg.module, ), PollRequest: (TRIGGERREQUEST, lambda msg: "%s:%s" % (msg.module, msg.parameter) if msg.parameter else msg.module, ),

View File

@ -28,6 +28,7 @@ class Message(object):
is_request = False is_request = False
is_reply = False is_reply = False
is_error = False is_error = False
qualifiers = {}
def __init__(self, **kwds): def __init__(self, **kwds):
self.ARGS = set() self.ARGS = set()