Skip to content

Commit

Permalink
Merge pull request #13 from vapor-ware/mhink-no-indirect-i2c
Browse files Browse the repository at this point in the history
Remove indirect reads from i2c devices.
  • Loading branch information
MatthewHink committed Oct 13, 2017
2 parents 368a8f8 + 4d5f8a4 commit fd94419
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 168 deletions.
27 changes: 0 additions & 27 deletions synse/devicebus/devices/i2c/max116xx_adc_thermistor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,6 @@ def _read(self, command):
device_id)), None, sys.exc_info()[2]

def _read_sensor(self):
""" Convenience method to return the sensor reading.
If the sensor is configured to be read indirectly (e.g. from background)
it will do so -- otherwise, we perform a direct read.
"""
logger.debug('self.from_background: {}'.format(self.from_background))
if self.from_background:
return self.indirect_sensor_read()
return self._direct_sensor_read()

def indirect_sensor_read(self):
"""Read the sensor data from the intermediary data file.
FIXME - reading from file is only for the POC. once we can
confirm that this works and have it stable for the short-term, we
will need to move on to the longer-term plan of having this done
via socket.
Returns:
dict: the thermistor reading value.
"""
logger.debug('indirect_sensor_read')
data_file = self._get_bg_read_file('{0:04x}'.format(self.channel))
data = Max11608Thermistor.read_sensor_data_file(data_file)
return {const.UOM_TEMPERATURE: data[0]}

def _direct_sensor_read(self):
""" Internal method for reading data off of the device.
Returns:
Expand Down
85 changes: 5 additions & 80 deletions synse/devicebus/devices/i2c/pca9632_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _control_led(self, state=None, blink=None, color=None):
raise SynseException('Unknown hardware_type {}.'.format(self.hardware_type))

def _write_sensor(self, state, color, blink):
""" Write to the led controller either directly or indirectly.
""" Write to the led controller directly.
Args:
state: The LED state, on or off.
Expand All @@ -297,10 +297,9 @@ def _write_sensor(self, state, color, blink):
if blink == 'no_override':
blink = self.last_blink

if self.from_background:
return self._indirect_write(state, color, blink)

return self.direct_write(state, color, blink)
i2c_common.write_led(state=state, blink_state=blink, color=color)
self._cache_write(color, blink)
return self._led_response(state, color, blink)

def _cache_write(self, color, blink):
""" Cache data written on a write.
Expand All @@ -319,85 +318,11 @@ def _cache_write(self, color, blink):
if blink is not None and blink != 'no_override':
self.last_blink = blink

def direct_write(self, state, color, blink):
""" Direct write to the led controller.
Args:
state: The LED state, on or off.
color: The three byte hex RGB color of the LED.
Example 0xff0000.
blink: The blink state to return. steady or blink.
Returns:
The parameters in a dictionary with values as strings.
"""
i2c_common.write_led(state=state, blink_state=blink, color=color)
self._cache_write(color, blink)
return self._led_response(state, color, blink)

def _indirect_write(self, state, color, blink):
""" Write to a file that the daemon will read and send to the LED
controller.
Args:
state: The LED state, on or off.
color: The three byte hex RGB color of the LED.
Example 0xff0000.
blink: The blink state to return. steady or blink.
Returns:
The parameters in a dictionary with values as strings.
"""
logger.debug('indirect_sensor_write')
i2c_common.check_led_write_parameters(state, color, blink)

data_file = self._get_bg_write_file('{:04x}'.format(self.channel))

if color is None:
color_str = ''
else:
color_str = '{:06x}'.format(color)

if blink is None:
blink_str = ''
else:
blink_str = str(blink)

write_data = state + os.linesep + color_str + os.linesep + blink_str + os.linesep
logger.debug('LED _indirect_write() write_data: {}'.format(write_data))

with open(data_file, 'w') as f:
f.write(write_data)

self._cache_write(color, blink)
return self._led_response(state, color, blink)

def _read_sensor(self):
""" Read from the LED controller either directly or indirectly.
Returns:
A dictionary of state, color and blink.
"""
if self.from_background:
return self._indirect_read()
return self.direct_read()

def direct_read(self):
""" Direct read of the LED controller.
""" Read from the LED controller directly.
Returns:
A dictionary of state, color and blink.
"""
state, color, blink = i2c_common.read_led()
return self._led_response(state, color, blink)

def _indirect_read(self):
""" Read from a file created by a daemon.
Returns:
A dictionary of state, color and blink.
"""
logger.debug('indirect_sensor_read')
data_file = self._get_bg_read_file('{:04x}'.format(self.channel))
data = PCA9632Led.read_sensor_data_file(data_file)
return self._led_response(data[0], data[1], data[2]) # state, color, blink are data 0,1,2.
30 changes: 2 additions & 28 deletions synse/devicebus/devices/i2c/sdp610_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,34 +126,8 @@ def _read(self, command):
device_id)), None, sys.exc_info()[2]

def _read_sensor(self):
""" Convenience method to return the sensor reading.
If the sensor is configured to be read indirectly (e.g. from background)
it will do so -- otherwise, we perform a direct read.
"""
if self.from_background:
return self.indirect_sensor_read()
return self._direct_sensor_read()

def indirect_sensor_read(self):
"""Read the sensor data from the intermediary data file.
FIXME - reading from file is only for the POC. once we can
confirm that this works and have it stable for the short-term, we
will need to move on to the longer-term plan of having this done
via socket.
Returns:
dict: the thermistor reading value.
"""
logger.debug('indirect_sensor_read')
data_file = self._get_bg_read_file('{0:04x}'.format(self.channel))
data = SDP610Pressure.read_sensor_data_file(data_file)
return {const.UOM_PRESSURE: data[0]}

def _direct_sensor_read(self):
""" Internal method for reading data off of the device.
""" Convenience method to return the sensor reading from the bus.
Returns:
dict: the pressure reading value.
"""
Expand Down
36 changes: 3 additions & 33 deletions synse/devicebus/fan_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,50 +282,20 @@ def _get_i2c_config():
return i2c_config

def _read_thermistors(self):
"""Read the configured thermistors."""
if self.from_background:
self._read_thermistors_indirect()
else:
self._read_thermistors_direct()

def _read_thermistors_direct(self):
"""Read the configured thermistors by hitting the bus."""
if self.thermistor_read_count < 0:
thermistor_model = type(self.thermistors)._instance_name
thermistor_model = type(self.thermistors).get_instance_name()
readings = i2c_common.read_thermistors(self.thermistor_read_count, thermistor_model)
for i, reading in enumerate(readings):
self.thermistors[i].reading = reading

def _read_thermistors_indirect(self):
"""Read the configured thermistors without hitting the bus."""
for t in self.thermistor_devices:
channel = t.channel
data = t.indirect_sensor_read()[const.UOM_TEMPERATURE]
self.thermistors[channel].reading = data
logger.debug('_read_thermistors_indirect: {}'.format(self.thermistors))

def _read_differential_pressures(self):
"""Read the configured differential pressure sensors."""
if self.from_background:
self._read_differential_pressures_indirect()
else:
self._read_differential_pressures_direct()

def _read_differential_pressures_direct(self):
"""Read the configured differential pressure sensors by hitting the bus."""
readings = i2c_common.read_differential_pressures(self.differential_pressure_read_count)
readings = i2c_common.read_differential_pressures(
self.differential_pressure_read_count)
for i, reading in enumerate(readings):
self.differential_pressures[i].reading = reading

def _read_differential_pressures_indirect(self):
"""Read the configured differential pressure sensors without hitting the bus."""
for dp in self.differential_pressure_devices:
channel = dp.channel
ordinal = i2c_common.get_channel_ordinal(channel)
data = dp.indirect_sensor_read()[const.UOM_PRESSURE]
self.differential_pressures[ordinal].reading = data
logger.debug('_read_diff_pressures_indirect: {}'.format(self.differential_pressures))

def _thermistor_read_count(self):
""" Determine the number of thermistors to read on each
`_read_thermistors` call.
Expand Down

0 comments on commit fd94419

Please sign in to comment.