def sensor_state_view(id: int, sensor: Sensor): """Sensor state view. This returns a little html snippet with a plot of the most recent state of the sensor. Only works for power sensors at the moment. TODO: no need to only support power plots in the future. Might make it optional. """ if not is_power_unit(sensor.unit): return """"<script type="text/javascript"> console.log("State not available. Sensor is not a power sensor."); </script>""" time_str, plot_html_str = get_latest_power_as_plot(sensor, small=True) return plot_html_str
def _get_latest_power_plot(asset: GenericAsset) -> Tuple[str, str]: power_sensor: Optional[Sensor] = None if asset._sa_instance_state.transient: sensors = Sensor.query.filter( Sensor.generic_asset_id == asset.id).all() else: sensors = asset.sensors for sensor in sensors: if is_power_unit(sensor.unit): power_sensor = sensor break if power_sensor is None: return "", "" else: return get_latest_power_as_plot(power_sensor)
def asset_state_view(id: str, asset: GenericAsset): """Asset state view. This returns a little html snippet with a plot of the most recent state of the first found power sensor. TODO: no need to only support power plots in the future. Might make it optional. """ power_sensor: Optional[Sensor] = None for sensor in asset.sensors: if is_power_unit(sensor.unit): power_sensor = sensor break if power_sensor is None: return ( """<script type="text/javascript"> console.log("State not available. Asset %s has no power sensors."); </script>""" % id ) time_str, plot_html_str = get_latest_power_as_plot(power_sensor, small=True) return plot_html_str
def test_is_power_unit(unit: str, power_unit: bool): assert is_power_unit(unit) is power_unit
def measures_power(self) -> bool: """True if this sensor's unit is measuring power""" return is_power_unit(self.unit)