|
| 1 | +""" |
| 2 | + sensor_fs.py |
| 3 | +
|
| 4 | + File system based sensors implementation. |
| 5 | +""" |
| 6 | + |
| 7 | +from sonic_platform_base.sensor_base import SensorBase |
| 8 | +from sonic_platform_base.sensor_base import VoltageSensorBase |
| 9 | +from sonic_platform_base.sensor_base import CurrentSensorBase |
| 10 | +import logging |
| 11 | + |
| 12 | + |
| 13 | +class SensorFs(SensorBase): |
| 14 | + """Implementation of file system based sensor class""" |
| 15 | + |
| 16 | + __defaults = { |
| 17 | + 'name': '', |
| 18 | + 'sensor': '', |
| 19 | + 'high_thresholds': ['N/A', 'N/A', 'N/A'], #minor, major, critical |
| 20 | + 'low_thresholds': ['N/A', 'N/A', 'N/A'], #minor, major, critical |
| 21 | + 'position': -1, |
| 22 | + } |
| 23 | + |
| 24 | + def __init__(self, sensor_type='sensor', **kw): |
| 25 | + |
| 26 | + super(SensorFs, self).__init__() |
| 27 | + |
| 28 | + if 'sensor' not in kw: |
| 29 | + raise Exception('Failed to initialize sensor') |
| 30 | + |
| 31 | + for k,v in self.__defaults.items(): |
| 32 | + setattr(self, k, kw.get(k,v)) |
| 33 | + |
| 34 | + if (len(self.high_thresholds) != 3 or len(self.low_thresholds) != 3): |
| 35 | + raise Exception('{}: Missing sensor thresholds'.format(self.name)) |
| 36 | + |
| 37 | + self.minimum_sensor = self.get_value() |
| 38 | + self.maximum_sensor = self.minimum_sensor |
| 39 | + |
| 40 | + def get_name(self): |
| 41 | + """Returns the sensor name""" |
| 42 | + return self.name |
| 43 | + |
| 44 | + def get_value(self): |
| 45 | + """Returns the sensor measurement""" |
| 46 | + try: |
| 47 | + with open(self.sensor) as f: |
| 48 | + return int(f.readline().rstrip()) |
| 49 | + except: |
| 50 | + return None |
| 51 | + |
| 52 | + def get_high_threshold(self): |
| 53 | + """Returns the sensor high threshold value""" |
| 54 | + return self.high_thresholds[1] |
| 55 | + |
| 56 | + def get_low_threshold(self): |
| 57 | + """Returns the sensor low threshold value""" |
| 58 | + return self.low_thresholds[1] |
| 59 | + |
| 60 | + def set_high_threshold(self, value): |
| 61 | + """Sets the sensor high threshold value""" |
| 62 | + self.high_thresholds[1] = value |
| 63 | + return True |
| 64 | + |
| 65 | + def set_low_threshold(self, value): |
| 66 | + """Sets the sensor low threshold value""" |
| 67 | + self.low_thresholds[1] = value |
| 68 | + return True |
| 69 | + |
| 70 | + def get_high_critical_threshold(self): |
| 71 | + """Returns the sensor critical high threshold value""" |
| 72 | + return self.high_thresholds[2] |
| 73 | + |
| 74 | + def get_low_critical_threshold(self): |
| 75 | + """Returns the sensor critical low threshold value""" |
| 76 | + return self.low_thresholds[2] |
| 77 | + |
| 78 | + def set_high_critical_threshold(self, value): |
| 79 | + """Sets the sensor critical high threshold value""" |
| 80 | + self.high_thresholds[2] = value |
| 81 | + return True |
| 82 | + |
| 83 | + def set_low_critical_threshold(self, value): |
| 84 | + """Sets the sensor critical low threshold value""" |
| 85 | + self.low_thresholds[2] = value |
| 86 | + return True |
| 87 | + |
| 88 | + def get_minimum_recorded(self): |
| 89 | + """Retrieves the minimum recorded sensor measurement""" |
| 90 | + tmp = self.get_value() |
| 91 | + if tmp is None: |
| 92 | + return None |
| 93 | + if self.minimum_sensor is None or tmp < self.minimum_sensor: |
| 94 | + self.minimum_sensor = tmp |
| 95 | + return self.minimum_sensor |
| 96 | + |
| 97 | + def get_maximum_recorded(self): |
| 98 | + """Retrieves the maximum recorded sensor measurement""" |
| 99 | + tmp = self.get_value() |
| 100 | + if tmp is None: |
| 101 | + return None |
| 102 | + if self.maximum_sensor is None or tmp > self.maximum_sensor: |
| 103 | + self.maximum_sensor = tmp |
| 104 | + return self.maximum_sensor |
| 105 | + |
| 106 | + def get_position_in_parent(self): |
| 107 | + """Retrieves 1-based relative physical position in parent device""" |
| 108 | + return self.position |
| 109 | + |
| 110 | + @staticmethod |
| 111 | + def factory(sensor_cls, sensors_data): |
| 112 | + """Factory method for retrieving a list of Sensor objects""" |
| 113 | + logging.basicConfig() |
| 114 | + logger = logging.getLogger() |
| 115 | + |
| 116 | + result = [] |
| 117 | + |
| 118 | + for idx, sensor in enumerate(sensors_data): |
| 119 | + sensor['position'] = idx + 1 |
| 120 | + try: |
| 121 | + result.append(sensor_cls(**sensor)) |
| 122 | + except Exception as e: |
| 123 | + logger.warning('Sensor.factory: {}'.format(e)) |
| 124 | + |
| 125 | + return result |
| 126 | + |
| 127 | + |
| 128 | +class VoltageSensorFs(SensorFs, VoltageSensorBase): |
| 129 | + """File system based voltage sensor class""" |
| 130 | + |
| 131 | + DEVICE_TYPE = "voltage_sensor" |
| 132 | + |
| 133 | + def __init__(self, **kw): |
| 134 | + super(VoltageSensorFs, self).__init__(self.DEVICE_TYPE, **kw) |
| 135 | + |
| 136 | + |
| 137 | +class CurrentSensorFs(SensorFs, CurrentSensorBase): |
| 138 | + """File systems based Current sensor class""" |
| 139 | + |
| 140 | + DEVICE_TYPE = "current_sensor" |
| 141 | + |
| 142 | + def __init__(self, **kw): |
| 143 | + super(CurrentSensorFs, self).__init__(self.DEVICE_TYPE, **kw) |
0 commit comments