Skip to content

Commit

Permalink
Merge pull request #15 from mattgibbs/rw_in_psp
Browse files Browse the repository at this point in the history
Add support for read/write state callbacks in psp.Pv
  • Loading branch information
ZLLentz authored Jun 25, 2018
2 parents d094617 + 882106b commit bf11aa2
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions psp/Pv.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,19 @@ def __init__(self, name, initialize=False, count=None,
self.cbid = 1
self.con_cbs = {}
self.mon_cbs = {}
self.rwaccess_cbs = {}
self.connect_cb = self.__connection_handler
self.monitor_cb = self.__monitor_handler
self.getevt_cb = self.__getevt_handler
self.rwaccess_cb = self.__rwaccess_handler

# State variables
self.ismonitored = False
self.isconnected = False
self.isinitialized = False
self.monitor_append = False
self.read_access = False
self.write_access = False

self.count = count
self.control = control
Expand Down Expand Up @@ -194,6 +198,7 @@ def __getevt_handler(self, e=None):
self.isinitialized = True
self.do_initialize = False
self.getevt_cb = None
self.replace_access_rights_event()
if self.do_monitor:
self.monitor(pyca.DBE_VALUE | pyca.DBE_LOG | pyca.DBE_ALARM,
self.control, self.count)
Expand Down Expand Up @@ -225,6 +230,22 @@ def __monitor_handler(self, e=None):
logprint(self.value)
else:
logprint("%-30s %s" % (self.name, e))

def __rwaccess_handler(self, read_access_state, write_access_state):
"""
Called during a read/write access change event
"""
self.read_access = read_access_state
self.write_access = write_access_state
for (id, (cb, once)) in self.rwaccess_cbs.items():
try:
cb(read_access_state, write_access_state)
except Exception:
err = "Exception in read/write access callback for {}:"
logprint(err.format(self.name))
traceback.print_exc()
if once:
self.del_rwaccess_callback(id)

def add_connection_callback(self, cb):
"""
Expand Down Expand Up @@ -310,6 +331,50 @@ def del_monitor_callback(self, id):
"""
del self.mon_cbs[id]

def add_rwaccess_callback(self, cb, once=False):
"""
Add a read/write access state change callback
Parameters
----------
cb : callable
A function to be run when the PV object makes a Channel Access sees
a read/write access state change event. The function must have the
following signature: rwcb(read_access, write_access). Both
read_access and write_access are booleans.
Returns
-------
id : int
An integer ID number for the callback, which can be later used to
delete the callback
See Also
--------
:meth:`.add_connection_callback`
:meth:`.add_monitor_callback`
"""
id = self.cbid
self.cbid += 1
self.rwaccess_cbs[id] = (cb, once)
return id

def del_rwaccess_callback(self, id):
"""
Delete a read/write access state change callback
Parameters
----------
id : int
The id of the callback to be deleted
Raises
------
KeyError
If the id does not correspond to an existing callback
"""
del self.rwaccess_cbs[id]

def connect(self, timeout=None):
"""
Create a connection to the PV through Channel Access
Expand Down

0 comments on commit bf11aa2

Please sign in to comment.