From 1831ecbc8f3e4e9380d14cce45c14ef8db16bd5d Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Fri, 31 Mar 2023 12:58:03 -0400 Subject: [PATCH 1/3] fixed 240 connection drop crashing acq process --- socs/agents/lakeshore240/agent.py | 56 +++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/socs/agents/lakeshore240/agent.py b/socs/agents/lakeshore240/agent.py index 5ad9fe8d2..7d5ccb6e8 100644 --- a/socs/agents/lakeshore240/agent.py +++ b/socs/agents/lakeshore240/agent.py @@ -58,7 +58,7 @@ def init_lakeshore(self, session, params=None): if self.initialized: return True, "Already Initialized Module" - with self.lock.acquire_timeout(0, job='init') as acquired: + with self.lock.acquire_timeout(3, job='init') as acquired: if not acquired: self.log.warn("Could not start init because " "{} is already running".format(self.lock.job)) @@ -211,28 +211,56 @@ def acq(self, session, params=None): current_time = time.time() data = { 'timestamp': current_time, + 'connection': {}, 'block_name': 'temps', 'data': {} } - for chan in self.module.channels: - # Read sensor on channel - chan_string = "Channel_{}".format(chan.channel_num) - temp_reading = chan.get_reading(unit='K') - sensor_reading = chan.get_reading(unit='S') + # Try to re-initialize if connection lost + if not self.initialized: + if not self.lock.release_and_acquire(timeout=10): + self.log.warn(f"Could not re-acquire lock now held by {self.lock.job}.") + return False + self.agent.start('init_lakeshore') + self.agent.wait('init_lakeshore') + + # Only get readings if connected + if self.initialized: + session.data.update({'connection': {'last_attempt': time.time(), + 'connected': True}}) + for chan in self.module.channels: + # Read sensor on channel + chan_string = "Channel_{}".format(chan.channel_num) + + try: + temp_reading = chan.get_reading(unit='K') + sensor_reading = chan.get_reading(unit='S') + except: + self.log.info('No reponse. Check your connection.') + self.initialized = False + time.sleep(1) + break + + # For data feed + data['data'][chan_string + '_T'] = temp_reading + data['data'][chan_string + '_V'] = sensor_reading + + # For session.data + field_dict = {chan_string: {"T": temp_reading, "V": sensor_reading}} + session.data['fields'].update(field_dict) - # For data feed - data['data'][chan_string + '_T'] = temp_reading - data['data'][chan_string + '_V'] = sensor_reading + session.data.update({'timestamp': current_time}) - # For session.data - field_dict = {chan_string: {"T": temp_reading, "V": sensor_reading}} - session.data['fields'].update(field_dict) + # Continue trying to connect + if not self.initialized: + session.data.update({'connection': {'last_attempt': time.time(), + 'connected': False}}) + self.log.info('Trying to reconnect.') + time.sleep(1) + continue self.agent.publish_to_feed('temperatures', data) - session.data.update({'timestamp': current_time}) - time.sleep(sleep_time) self.agent.feeds['temperatures'].flush_buffer() From aea605c877c4e5eb8c6b6221d8966c7a63574aa4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:40:46 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- socs/agents/lakeshore240/agent.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/socs/agents/lakeshore240/agent.py b/socs/agents/lakeshore240/agent.py index 7d5ccb6e8..f1f9b6d4a 100644 --- a/socs/agents/lakeshore240/agent.py +++ b/socs/agents/lakeshore240/agent.py @@ -224,7 +224,7 @@ def acq(self, session, params=None): self.agent.start('init_lakeshore') self.agent.wait('init_lakeshore') - # Only get readings if connected + # Only get readings if connected if self.initialized: session.data.update({'connection': {'last_attempt': time.time(), 'connected': True}}) @@ -235,12 +235,12 @@ def acq(self, session, params=None): try: temp_reading = chan.get_reading(unit='K') sensor_reading = chan.get_reading(unit='S') - except: + except BaseException: self.log.info('No reponse. Check your connection.') self.initialized = False time.sleep(1) break - + # For data feed data['data'][chan_string + '_T'] = temp_reading data['data'][chan_string + '_V'] = sensor_reading From 0b5bcaf39b4864b818a6524ca5b77da52c1022fe Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Mon, 3 Apr 2023 11:42:20 -0400 Subject: [PATCH 3/3] updated session.data in docstring --- socs/agents/lakeshore240/agent.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/socs/agents/lakeshore240/agent.py b/socs/agents/lakeshore240/agent.py index 7d5ccb6e8..51e591908 100644 --- a/socs/agents/lakeshore240/agent.py +++ b/socs/agents/lakeshore240/agent.py @@ -182,6 +182,8 @@ def acq(self, session, params=None): "Channel_6": {"T": 99.58, "V": 101.75}, "Channel_7": {"T": 98.03, "V": 100.82}, "Channel_8": {"T": 101.14, "V":101.01}}, + {'connection': {'last_attempt': 1601925677.6914878, + 'connected': True}} "timestamp":1601925677.6914878} """