Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed LS240 connection drop crashing acq process #430

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions socs/agents/lakeshore240/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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}

"""
Expand Down Expand Up @@ -211,28 +213,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 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

# 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()
Expand Down