Skip to content

Commit

Permalink
Changed the reconnect logic again.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonscryo committed Sep 7, 2024
1 parent 737343c commit a51c156
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions socs/agents/scpi_psu/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from ocs import ocs_agent, site_config
from ocs.ocs_twisted import TimeoutLock

from socs.agents.scpi_psu.drivers import PsuInterface
from drivers import PsuInterface
#from socs.agents.scpi_psu.drivers import PsuInterface


class ScpiPsuAgent:
Expand Down Expand Up @@ -50,12 +51,13 @@ def _initialize_module(self):
"""Initialize the ScpiPsu module."""
try:
self.psu = PsuInterface(self.ip_address, self.gpib_slot)
self.idn = self.psu.identify()
except (socket.timeout, OSError) as e:
self.log.warn(f"Error establishing connection: {e}")
self.psu = None
return False
self.log.info("Reconnected to psu: {}".format(self.idn))

self.idn = self.psu.identify()
self.log.info("Connected to psu: {}".format(self.idn))
self.log.info("Clearing event registers and error queue")
self.psu.clear()
return True
Expand All @@ -80,38 +82,36 @@ def monitor_output(self, session, params=None):
self.monitor = True

while self.monitor:
time.sleep(params['wait'])
with self.lock.acquire_timeout(1) as acquired:
if not acquired:
self.log.warn("Could not acquire in monitor_current")
continue

elif not self._initialize_module():
time.sleep(5)
if not self.psu:
self._initialize_module()
continue

else:
data = {
'timestamp': time.time(),
'block_name': 'output',
'data': {}
}
data = {
'timestamp': time.time(),
'block_name': 'output',
'data': {}
}

try:
for chan in params['channels']:
try:
data['data']["Voltage_{}".format(chan)] = self.psu.get_volt(chan)
data['data']["Current_{}".format(chan)] = self.psu.get_curr(chan)
except socket.timeout as e:
self.log.warn(f"TimeoutError: {e}")
self.log.info("Attempting to reconnect")
self.psu = None
break
# Only publish if the loop completes without timeout errors
# as publishing incomplete data creates block errors
if self.psu:
self.agent.publish_to_feed('psu_output', data)

# Allow this git process to be queried to return current data
session.data = data

time.sleep(params['wait'])
data['data']["Voltage_{}".format(chan)] = self.psu.get_volt(chan)
data['data']["Current_{}".format(chan)] = self.psu.get_curr(chan)
except socket.timeout as e:
self.log.warn(f"TimeoutError: {e}")
self.log.info("Attempting to reconnect")
self.psu = None
continue

self.agent.publish_to_feed('psu_output', data)

# Allow this git process to be queried to return current data
session.data = data

if params['test_mode']:
break
Expand Down

0 comments on commit a51c156

Please sign in to comment.