def waitChannelUpdate(self, chanName, waitValue = None, timeout = None): """Wait for a channel update Arguments: chanName -- channel name waitValue -- particular value to wait (defaults to None, meaning any value) timeout -- optional timeout (defaults to None) """ with gevent.Timeout(timeout, SpecClientTimeoutError): self.waitConnection() connection = self.connection() if connection is not None: self.channelWasUnregistered = False channel = connection.getChannel(chanName) self.channel_updated_event.clear() if not channel.registered: self.channelWasUnregistered = True connection.registerChannel(chanName, self.channelUpdated) #channel.register() else: SpecEventsDispatcher.connect(channel, 'valueChanged', self.channelUpdated) if waitValue is None: try: self.channel_updated_event.wait(timeout) except: raise SpecClientTimeoutError else: while waitValue != self.value: self.channel_updated_event.wait(timeout) if self.channelWasUnregistered: connection.unregisterChannel(chanName) #channel.unregister()
def registerChannel(self, chanName, receiverSlot, registrationFlag = SpecChannel.DOREG, dispatchMode = SpecEventsDispatcher.UPDATEVALUE): """Register a channel Tell the remote Spec we are interested in receiving channel update events. If the channel is not already registered, create a new SpecChannel object, and connect the channel 'valueChanged' signal to the receiver slot. If the channel is already registered, simply add a connection to the receiver slot. Arguments: chanName -- a string representing the channel name, i.e. 'var/toto' receiverSlot -- any callable object in Python Keywords arguments: registrationFlag -- internal flag dispatchMode -- can be SpecEventsDispatcher.UPDATEVALUE (default) or SpecEventsDispatcher.FIREEVENT, depending on how the receiver slot will be called. UPDATEVALUE means we don't mind skipping some channel update events as long as we got the last one (for example, a motor position). FIREEVENT means we want to call the receiver slot for every event. """ if dispatchMode is None: return chanName = str(chanName) if not chanName in self.registeredChannels: newChannel = SpecChannel.SpecChannel(self, chanName, registrationFlag) self.registeredChannels[chanName] = newChannel SpecEventsDispatcher.connect(self.registeredChannels[chanName], 'valueChanged', receiverSlot, dispatchMode) channelValue = self.registeredChannels[chanName].value if channelValue is not None: # we received a value, so emit an update signal self.registeredChannels[chanName].update(channelValue)
def connectToSpec(self, varName, specVersion, dispatchMode=UPDATEVALUE, prefix=True): """Connect to a remote Spec Connect to Spec and register channel for monitoring variable Arguments: varName -- name of the variable specVersion -- 'host:port' string representing a Spec server to connect to """ self.varName = varName self.specVersion = specVersion if prefix: self.channelName = 'var/%s' % varName else: self.channelName = varName self.connection = SpecConnectionsManager.SpecConnectionsManager( ).getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) self.dispatchMode = dispatchMode if self.connection.isSpecConnected(): self._connected()
def connectToSpec(self, varName, specVersion, dispatchMode = UPDATEVALUE, prefix=True): """Connect to a remote Spec Connect to Spec and register channel for monitoring variable Arguments: varName -- name of the variable specVersion -- 'host:port' string representing a Spec server to connect to """ self.varName = varName self.specVersion = specVersion if prefix: self.channelName = 'var/%s' % varName else: self.channelName = varName self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) # # register channel # self.connection.registerChannel(self.channelName, self.update, dispatchMode = dispatchMode) cb = self.__callbacks.get("update") if cb is not None: cb = cb() self.connection.registerChannel(self.channelName, cb, dispatchMode = dispatchMode) if self.connection.isSpecConnected(): self.connected()
def connectToSpec(self, specVersion, timeout=200): if self.connection is not None: SpecEventsDispatcher.disconnect(self.connection, 'connected', self.connected) SpecEventsDispatcher.disconnect(self.connection, 'disconnected', self.disconnected) self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) self.specVersion = specVersion SpecEventsDispatcher.connect(self.connection, 'connected', self.connected) cb = self.__callbacks.get("connected") if cb is not None: cb = cb() SpecEventsDispatcher.connect(self.connection, 'connected', cb) SpecEventsDispatcher.connect(self.connection, 'disconnected', self.disconnected) cb = self.__callbacks.get("disconnected") if cb is not None: cb = cb() SpecEventsDispatcher.connect(self.connection, 'disconnected', cb) self.connection.registerChannel("status/ready", self.statusChanged) cb = self.__callbacks.get("statusChanged") if cb is not None: cb = cb() SpecEventsDispatcher.connect(self.connection, 'statusChanged', cb) if self.connection.isSpecConnected(): self.connected() else: try: SpecWaitObject.waitConnection(self.connection, timeout) except SpecClientTimeoutError: pass SpecEventsDispatcher.dispatch()
def connectToSpec(self, specName, specVersion): """Connect to a remote Spec Connect to Spec and register channels of interest for the specified motor Arguments: specName -- name of the motor in Spec specVersion -- 'host:port' string representing a Spec server to connect to """ self.specName = specName self.specVersion = specVersion self.chanNamePrefix = 'motor/%s/%%s' % specName self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, 'connected', self.__connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self.__disconnected) # # register channels # self.connection.registerChannel(self.chanNamePrefix % 'low_limit', self._motorLimitsChanged) self.connection.registerChannel(self.chanNamePrefix % 'high_limit', self._motorLimitsChanged) self.connection.registerChannel(self.chanNamePrefix % 'position', self.__motorPositionChanged, dispatchMode=SpecEventsDispatcher.FIREEVENT) self.connection.registerChannel(self.chanNamePrefix % 'move_done', self.motorMoveDone, dispatchMode = SpecEventsDispatcher.FIREEVENT) self.connection.registerChannel(self.chanNamePrefix % 'high_lim_hit', self.__motorLimitHit) self.connection.registerChannel(self.chanNamePrefix % 'low_lim_hit', self.__motorLimitHit) self.connection.registerChannel(self.chanNamePrefix % 'sync_check', self.__syncQuestion) self.connection.registerChannel(self.chanNamePrefix % 'unusable', self.__motorUnusable) self.connection.registerChannel(self.chanNamePrefix % 'offset', self.motorOffsetChanged) self.connection.registerChannel(self.chanNamePrefix % 'sign', self.signChanged) #self.connection.registerChannel(self.chanNamePrefix % 'dial_position', self.dialPositionChanged) if self.connection.isSpecConnected(): self.__connected()
def waitChannelUpdate(self, chanName, waitValue=None, timeout=None): """Wait for a channel update Arguments: chanName -- channel name waitValue -- particular value to wait (defaults to None, meaning any value) timeout -- optional timeout (defaults to None) """ connection = self.connection() if connection is not None: self.channelWasUnregistered = False channel = connection.getChannel(chanName) if not channel.registered: self.channelWasUnregistered = True connection.registerChannel( chanName, self.channelUpdated) #channel.register() else: SpecEventsDispatcher.connect(channel, 'valueChanged', self.channelUpdated) self.wait(waitValue=waitValue, timeout=timeout) if self.channelWasUnregistered: connection.unregisterChannel(chanName) #channel.unregister()
def __init__(self, conn, command, callbacks = {}, timeout=5): self.command = command self._conn = conn self.timeout = timeout self.reply_pending = False self.retvalue = None self.specapp = self._conn.get_specname() # save callbacks self.__callback = None self.__error_callback = None self.__callbacks = { 'connected': None, 'disconnected': None, 'statusChanged': None, } for cb_name in iter(self.__callbacks.keys()): if callable(callbacks.get(cb_name)): self.__callbacks[cb_name] = SpecEventsDispatcher.callableObjectRef(callbacks[cb_name]) if self.synchronous: self._conn.wait_connected() else: SpecEventsDispatcher.connect(self._conn, 'connected', self._connected) SpecEventsDispatcher.connect(self._conn, 'disconnected', self._disconnected) if self._conn.is_connected(): self._connected()
def register(self, chan_name, receiver_slot, registrationFlag=SpecChannel.DOREG, dispatchMode=UPDATEVALUE): """Register a channel Tell the remote Spec we are interested in receiving channel update events. If the channel is not already registered, create a new SpecChannel object, and connect the channel 'valueChanged' signal to the receiver slot. If the channel is already registered, simply add a connection to the receiver slot. Arguments: chan_name -- a string representing the channel name, i.e. 'var/toto' receiver_slot -- any callable object in Python Keywords arguments: registrationFlag -- internal flag dispatchMode -- can be SpecEventsDispatcher.UPDATEVALUE (default) or SpecEventsDispatcher.FIREEVENT, depending on how the receiver slot will be called. UPDATEVALUE means we don't mind skipping some channel update events as long as we got the last one (for example, a motor position). FIREEVENT means we want to call the receiver slot for every event. """ if dispatchMode is None: return chan_name = str(chan_name) try: if not chan_name in self.reg_channels: channel = SpecChannel.SpecChannel(self, chan_name, registrationFlag) self.reg_channels[chan_name] = channel if channel.spec_chan_name != chan_name: # for assoc array elements channel.registered = True def valueChanged(value, chan_name=chan_name): channel = self.reg_channels[chan_name] channel.update(value) self.aliasedChannels[chan_name] = valueChanged self.registerChannel(channel.spec_chan_name, valueChanged, registrationFlag, dispatchMode) else: channel = self.reg_channels[chan_name] SpecEventsDispatcher.connect(channel, 'valueChanged', receiver_slot, dispatchMode) channelValue = self.reg_channels[channel.spec_chan_name].value if channelValue is not None: # we received a value, so emit an update signal channel.update(channelValue, force=True) except Exception: traceback.print_exc()
def connectToSpec(self, specVersion): self.connection = SpecConnectionsManager().getConnection(specVersion) self.__specVersion = specVersion SpecEventsDispatcher.connect(self.connection, 'connected', self.__connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self.__disconnected) if self.connection.isSpecConnected(): self.__connected()
def connectToSpec(self, specVersion, timeout=None): if self.connection is not None: SpecEventsDispatcher.disconnect(self.connection, 'connected', self._connected) SpecEventsDispatcher.disconnect(self.connection, 'disconnected', self._disconnected) self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) self.specVersion = specVersion SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) if self.connection.isSpecConnected(): self._connected()
def connectToSpec(self, specVersion, timeout=None): if self.connection is not None: SpecEventsDispatcher.disconnect(self.connection, "connected", self._connected) SpecEventsDispatcher.disconnect(self.connection, "disconnected", self._disconnected) self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) self.specVersion = specVersion SpecEventsDispatcher.connect(self.connection, "connected", self._connected) SpecEventsDispatcher.connect(self.connection, "disconnected", self._disconnected) if self.connection.isSpecConnected(): self._connected()
def __init__(self, connection): """Constructor Arguments: connection -- a SpecConnection object """ self.connection = weakref.ref(connection) self.isdisconnected = True self.channelWasUnregistered = False self.value = None SpecEventsDispatcher.connect(connection, 'connected', self.connected) SpecEventsDispatcher.connect(connection, 'disconnected', self.disconnected) if connection.isSpecConnected(): self.connected()
def __init__(self, connection, channelName, registrationFlag = DOREG): """Constructor Arguments: connection -- a SpecConnection object channelName -- string representing a channel name, i.e. 'var/toto' Keyword arguments: registrationFlag -- defines how the channel is registered, possible values are : SpecChannel.DOREG (default), SpecChannel.DONTREG (do not register), SpecChannel.WAITREG (delayed registration until Spec is reconnected) """ self.connection = weakref.ref(connection) self.name = channelName if channelName.startswith("var/") and '/' in channelName[4:]: l = channelName.split('/') self.spec_chan_name = "/".join((l[0], l[1])) if self.spec_chan_name in SpecChannel.channel_aliases: SpecChannel.channel_aliases[self.spec_chan_name].append(self.name) else: SpecChannel.channel_aliases[self.spec_chan_name] = [self.name] if len(l)==3: self.access1=l[2] self.access2=None else: self.access1=l[2] self.access2=l[3] else: self.spec_chan_name = self.name if not self.spec_chan_name in SpecChannel.channel_aliases: SpecChannel.channel_aliases[self.spec_chan_name]=[self.name] self.access1=None self.access2=None self.registrationFlag = registrationFlag self.isdisconnected = True self.registered = False self.value = None SpecEventsDispatcher.connect(connection, 'connected', self.connected) SpecEventsDispatcher.connect(connection, 'disconnected', self.disconnected) if connection.isSpecConnected(): self.connected()
def connectToSpec(self, specName, specVersion, timeout=None): """Connect to a remote Spec Connect to Spec and register channels of interest for the specified motor Arguments: specName -- name of the motor in Spec specVersion -- 'host:port' string representing a Spec server to connect to """ self.specName = specName self.specVersion = specVersion self.chanNamePrefix = 'motor/%s/%%s' % specName self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) if self.connection.isSpecConnected(): self._connected()
def connectToSpec(self, specVersion, timeout=200): if self.connection is not None: SpecEventsDispatcher.disconnect(self.connection, 'connected', self._connected) SpecEventsDispatcher.disconnect(self.connection, 'disconnected', self._disconnected) self.connection = SpecConnectionsManager().getConnection(specVersion) self.specVersion = specVersion SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) if self.connection.isSpecConnected(): self._connected() else: try: waitConnection(self.connection, timeout) except SpecClientTimeoutError: pass SpecEventsDispatcher.dispatch()
def __init__(self, conn, chan_name, registrationFlag = DOREG): """Constructor Arguments: * conn -- a SpecConnection object * chan_name -- string representing a channel name, i.e. 'var/toto' Keyword arguments: * registrationFlag -- defines how the channel is registered, possible values are : SpecChannel.DOREG (default), SpecChannel.DONTREG (do not register), SpecChannel.WAITREG (delayed registration until Spec is reconnected) """ self.conn = weakref.ref(conn) self.name = chan_name if chan_name.startswith("var/") and '/' in chan_name[4:]: l = chan_name.split('/') self.spec_chan_name = "/".join((l[0], l[1])) if len(l)==3: self.access1=l[2] self.access2=None else: self.access1=l[2] self.access2=l[3] else: self.spec_chan_name = self.name self.access1=None self.access2=None self.registrationFlag = registrationFlag self._connected = False self.registered = False self.value = None SpecEventsDispatcher.connect(conn, 'connected', self.connected) SpecEventsDispatcher.connect(conn, 'disconnected', self.disconnected) if conn.is_connected(): self.connected()
def connectToSpec(self, specName, specVersion, timeout=None): """Connect to a remote Spec Connect to Spec Arguments: specName -- name of the counter in Spec specVersion -- 'host:port' string representing a Spec server to connect to timeout -- optional timeout for connection (defaults to None) """ self.specName = specName self.specVersion = specVersion self.chanNamePrefix = "scaler/%s/%%s" % specName self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, "connected", self._connected) SpecEventsDispatcher.connect(self.connection, "disconnected", self._disconnected) if self.connection.isSpecConnected(): self._connected()
def __send_msg_with_reply(self, reply, message, replyReceiverObject = None): """Send a message to the remote Spec, and return the reply id. The reply object is added to the registeredReplies dictionary, with its reply id as the key. The reply id permits then to register for the reply using the 'registerReply' method. Arguments: reply -- SpecReply object which will receive the reply message -- SpecMessage object defining the message to send """ replyID = reply.id self.registeredReplies[replyID] = reply if hasattr(replyReceiverObject, 'replyArrived'): SpecEventsDispatcher.connect(reply, 'replyFromSpec', replyReceiverObject.replyArrived) self.sendq.insert(0, message) return replyID
def __send_msg_with_reply(self, reply, message, replyReceiverObject=None): """Send a message to the remote Spec, and return the reply id. The reply object is added to the registeredReplies dictionary, with its reply id as the key. The reply id permits then to register for the reply using the 'registerReply' method. Arguments: reply -- SpecReply object which will receive the reply message -- SpecMessage object defining the message to send """ replyID = reply.id self.registeredReplies[replyID] = reply if hasattr(replyReceiverObject, 'replyArrived'): SpecEventsDispatcher.connect(reply, 'replyFromSpec', replyReceiverObject.replyArrived) self.sendq.insert(0, message) return replyID
def connectToSpec(self, specName, specVersion): """Connect to a remote Spec Connect to Spec and register channels of interest for the specified motor Arguments: specName -- name of the motor in Spec specVersion -- 'host:port' string representing a Spec server to connect to """ self.specName = specName self.specVersion = specVersion self.chanNamePrefix = 'motor/%s/%%s' % specName self.connection = SpecConnectionsManager.SpecConnectionsManager( ).getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, 'connected', self.__connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self.__disconnected) if self.connection.isSpecConnected(): self.__connected()
def __init__(self, *args): """Constructor""" self.dispatcher = SpecConnectionDispatcher(*args) SpecEventsDispatcher.connect(self.dispatcher, 'connected', self.connected) SpecEventsDispatcher.connect(self.dispatcher, 'disconnected', self.disconnected) #SpecEventsDispatcher.connect(self.dispatcher, 'replyFromSpec', self.replyFromSpec) SpecEventsDispatcher.connect(self.dispatcher, 'error', self.error)
def connectToSpec(self, specName, specVersion, timeout=None): """Connect to a remote Spec Connect to Spec Arguments: specName -- name of the counter in Spec specVersion -- 'host:port' string representing a Spec server to connect to timeout -- optional timeout for connection (defaults to None) """ self.specName = specName self.specVersion = specVersion self.chanNamePrefix = 'scaler/%s/%%s' % specName self.connection = SpecConnectionsManager.SpecConnectionsManager( ).getConnection(specVersion) SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) if self.connection.isSpecConnected(): self._connected()
def waitChannelUpdate(self, chanName, waitValue = None, timeout = None): """Wait for a channel update Arguments: chanName -- channel name waitValue -- particular value to wait (defaults to None, meaning any value) timeout -- optional timeout (defaults to None) """ connection = self.connection() if connection is not None: self.channelWasUnregistered = False channel = connection.getChannel(chanName) if not channel.registered: self.channelWasUnregistered = True connection.registerChannel(chanName, self.channelUpdated) #channel.register() else: SpecEventsDispatcher.connect(channel, 'valueChanged', self.channelUpdated) self.wait(waitValue = waitValue, timeout = timeout) if self.channelWasUnregistered: connection.unregisterChannel(chanName) #channel.unregister()
def __send_msg_with_reply(self, reply, msg, receiver_obj=None): """Send a message to the remote Spec, and return the reply id. The reply object is added to the reg_replies dictionary, with its reply id as the key. The reply id permits then to register for the reply using the 'registerReply' method. Arguments: reply -- SpecReply object which will receive the reply message -- SpecMessage object defining the message to send """ reply_id = reply.id self.reg_replies[reply_id] = reply if hasattr(receiver_obj, 'replyArrived'): log.log( 2, "connecting future reply with object: %s" % type(receiver_obj)) SpecEventsDispatcher.connect(reply, 'replyArrived', receiver_obj.replyArrived) self.sendq.insert(0, msg) return reply_id
def __init__(self, *args): """Constructor""" self.dispatcher = SpecConnectionDispatcher(*args) #self.dispatcher.makeConnection() SpecEventsDispatcher.connect(self.dispatcher, 'connected', self.connected) SpecEventsDispatcher.connect(self.dispatcher, 'disconnected', self.disconnected) #SpecEventsDispatcher.connect(self.dispatcher, 'replyFromSpec', self.replyFromSpec) SpecEventsDispatcher.connect(self.dispatcher, 'error', self.error)
def connect(self, signal, cb): SpecEventsDispatcher.connect(self, signal, cb)