From 1961f915679364adbb57829495af8a134aa1c61c Mon Sep 17 00:00:00 2001 From: zebra Date: Tue, 2 May 2023 16:39:34 +0200 Subject: [PATCH] add switching_sensor --- frappy_psi/switching_sensor.py | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 frappy_psi/switching_sensor.py diff --git a/frappy_psi/switching_sensor.py b/frappy_psi/switching_sensor.py new file mode 100644 index 0000000..08976dc --- /dev/null +++ b/frappy_psi/switching_sensor.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# ***************************************************************************** +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# Module authors: +# Markus Zolliker +# ***************************************************************************** +"""virtual sensor switching between two others depending on range""" + +from frappy.core import ERROR, Attached, BoolType, Parameter, Readable, TupleOf, EnumType, FloatRange + + +class Sensor(Readable): + upper = Attached() + lower = Attached() + + switch_range = Parameter('switching range', datatype=TupleOf(FloatRange(), FloatRange()), readonly=False, default=(0, 0)) + used_sensor = Parameter('sensor to use', EnumType(lower=0, upper=1), readonly=False, default=0) + value = Parameter(datatype=FloatRange(unit='K')) + pollinterval = Parameter(export=False) + status = Parameter(default=(Readable.Status.ERROR, 'unintialized')) + + def initModule(self): + super().initModule() + self.rawsensor.registerCallbacks(self) + + def update_value(self, value): + src = self.upper if self.used_sensor == 'upper' else self.lower + try: + self.value = src.value + self.status = src.status + except Exception as e: + self.status = ERROR, repr(e) + + def update_status(self, value): + self.update_value(None) + + def read_value(self): + src = self.upper if self.used_sensor == 'upper' else self.lower + return src.read_value() + + def read_status(self): + src = self.upper if self.used_sensor == 'upper' else self.lower + return src.read_status()