def accum_update(self): field = self._cmdp['Units/accumulator'] if self._accum_history is None: time_str = '0 s' units = self._cmdp.preferences.get('Units/' + field) v = convert_units(0.0, units, units) else: time_str = self._accum_history['time_str'] a = self._accum_history['accumulators'] v = a[field] units = self._cmdp.preferences.get('Units/' + field, default=v['units']) v = convert_units(v['value'], v['units'], units) s = three_sig_figs(v['value'], v['units']) txt = ACCUM_TEMPLATE.format(field=field.capitalize(), value=s, time=time_str) self._accumLabel.setText(txt)
def test_energy(self): self.assertEqual({ 'value': 1, 'units': 'J' }, units.convert_units(1, 'J', 'J')) self.assertEqual({ 'value': 1 / 3600, 'units': 'Wh' }, units.convert_units(1, 'J', 'Wh')) self.assertEqual({ 'value': 1, 'units': 'Wh' }, units.convert_units(1, 'Wh', 'Wh')) self.assertEqual({ 'value': 3600, 'units': 'J' }, units.convert_units(1, 'Wh', 'J'))
def test_charge(self): self.assertEqual({ 'value': 1, 'units': 'C' }, units.convert_units(1, 'C', 'C')) self.assertEqual({ 'value': 1 / 3600, 'units': 'Ah' }, units.convert_units(1, 'C', 'Ah')) self.assertEqual({ 'value': 1, 'units': 'Ah' }, units.convert_units(1, 'Ah', 'Ah')) self.assertEqual({ 'value': 3600, 'units': 'C' }, units.convert_units(1, 'Ah', 'C'))
def convert_units(self, field, value, units=None): """Convert a field value into user-configurable preferred units. :param field: The field name, such as 'current'. :param value: The float value to convert or the dict of {'value': value, 'units': units}. :param units: The units for when value is a float. Ignored otherwise. :return: dict of {'value': value, 'units': units}. """ if value is None: value = 0.0 if units is None: units = FIELD_UNITS_SI.get(field) if units is None: units = value['units'] value = value['value'] output_units = self.preferences.get('Units/' + field, default=units) return convert_units(value, units, output_units)