Skip to content

Commit

Permalink
Merge pull request #93 from jacquelinegarrahan/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
jacquelinegarrahan authored Feb 10, 2022
2 parents ccd3429 + cf4479f commit cb91256
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
24 changes: 15 additions & 9 deletions lume_epics/epics_ca_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,16 @@ def setup_server(self) -> None:
)
self._monitors[var_name].add_callback(self._monitor_callback)

# Register pvs with server
self._ca_server.createPV("", pvdb)
# Register pvs with server if serving
if len(pvdb):
self._ca_server.createPV("", pvdb)

# set up driver for handing read and write requests to process variables
self._ca_driver = CADriver(server=self)
# set up driver for handing read and write requests to process variables
self._ca_driver = CADriver(server=self)

# start the server thread
self._server_thread = CAServerThread(self._ca_server)
self._server_thread.start()
# start the server thread
self._server_thread = CAServerThread(self._ca_server)
self._server_thread.start()

logger.info("CA server started")
return True
Expand All @@ -333,7 +334,9 @@ def update_pvs(
"""
variables = input_variables + output_variables

self._ca_driver.update_pvs(variables)
# update variables if the driver is running
if self._ca_driver is not None:
self._ca_driver.update_pvs(variables)

def run(self) -> None:
"""Start server process.
Expand All @@ -352,7 +355,10 @@ def run(self) -> None:
time.sleep(0.05)
logger.debug("out queue empty")

self._server_thread.stop()
# if server thread running
if self._server_thread is not None:
self._server_thread.stop()

logger.info("Channel access server stopped.")
else:
logger.info("Unable to set up server. Shutting down.")
Expand Down
30 changes: 23 additions & 7 deletions lume_epics/epics_pva_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(
# monitors for read only
self._monitors = {}
self._cached_values = {}
self._field_to_parent_map = {}

# utility maps
self._pvname_to_varname_map = {
Expand Down Expand Up @@ -226,6 +227,8 @@ def setup_server(self) -> None:
logger.info("Initializing pvAccess server")

# initialize global inputs
self._structures = {}
self._structure_specs = {}
for variable_name, config in self._epics_config.items():

if config["serve"]:
Expand All @@ -239,6 +242,8 @@ def setup_server(self) -> None:
structure = {}

for field in fields:
# track fields in dict
self._field_to_parent_map[field] = variable_name

variable = variables[field]

Expand Down Expand Up @@ -267,8 +272,8 @@ def setup_server(self) -> None:
spec.append((field, "v"))

if variable.value_type == "str":
nt = NTScalar("as")
initial = nt.wrap(variable.value)
nt = NTScalar("s")
initial = variable.value

else:
nd_array = variable.value.view(NTNDArrayData)
Expand All @@ -292,8 +297,9 @@ def setup_server(self) -> None:
structure[field] = initial

# assemble pv
self._structures[variable_name] = structure
self._structure_specs[variable_name] = spec
struct_type = Type(id=variable_name, spec=spec)

struct_value = Value(struct_type, structure)
pv = SharedPV(initial=struct_value)
self._providers[pvname] = pv
Expand Down Expand Up @@ -423,7 +429,7 @@ def update_pvs(
"""
variables = input_variables + output_variables
for variable in variables:
pvname = self._varname_to_pvname_map[variable.name]
parent = self._field_to_parent_map.get(variable.name)

if variable.name in self._input_variables and variable.is_constant:
logger.debug("Cannot update constant variable.")
Expand All @@ -448,8 +454,8 @@ def update_pvs(
logger.debug(
"pvAccess array process variable %s updated.", variable.name
)
if variable.value_type == "string":
value = list(variable.value)
if variable.value_type == "str":
value = variable.value

else:
value = variable.value.view(NTNDArrayData)
Expand All @@ -463,7 +469,17 @@ def update_pvs(
)
value = variable.value

output_provider = self._providers[pvname]
# update structure or pv
if parent:
self._structures[parent][variable.name] = value
struct_type = Type(id=parent, spec=self._structure_specs[parent])
value = Value(struct_type, self._structures[parent])
pvname = self._varname_to_pvname_map[parent]
output_provider = self._providers[pvname]

else:
pvname = self._varname_to_pvname_map[variable.name]
output_provider = self._providers[pvname]

if output_provider:
output_provider.post(value)
Expand Down
4 changes: 3 additions & 1 deletion lume_epics/epics_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
import logging
import multiprocessing
import traceback

try:
multiprocessing.set_start_method("spawn")
Expand Down Expand Up @@ -282,8 +283,9 @@ def run_comm_thread(
in [protocol, "both"]
]
queue.put({"output_variables": outputs}, timeout=0.1)

except Exception as e:
print(e)
traceback.print_exc()
self._model_exec_exit_event.set()

running_indicator.value = False
Expand Down

0 comments on commit cb91256

Please sign in to comment.