From ff96e26649620df0f8a19f537bd3dc1ad972954b Mon Sep 17 00:00:00 2001 From: Martin Kompf Date: Sun, 27 Apr 2025 18:34:52 +0200 Subject: [PATCH] Update to Python 3 --- catserial.py | 2 +- leakdetect.py | 6 ++++-- wairc.py | 27 +++++++++++---------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/catserial.py b/catserial.py index e60397f..d5e6ff8 100755 --- a/catserial.py +++ b/catserial.py @@ -17,7 +17,7 @@ def main(): # Open serial line ser = serial.Serial(port, 9600) if not ser.isOpen(): - print "Unable to open serial port %s" % port + print("Unable to open serial port %s" % port) sys.exit(1) # set data mode ser.write(b'C\r\n') diff --git a/leakdetect.py b/leakdetect.py index 4307ffa..8fe5197 100755 --- a/leakdetect.py +++ b/leakdetect.py @@ -21,7 +21,7 @@ count_rrd = "{0}/water.rrd".format(os.path.dirname(os.path.abspath(__file__))) count_fetch = "{0}/water.fetch.txt".format(os.path.dirname(os.path.abspath(__file__))) # Length of the minimum duration without water consumption in seconds -min_pause = 3 * 60 * 60 # 3 hours +min_pause = 2 * 60 * 60 # 2 hours # Verbose output verbose = False @@ -34,7 +34,7 @@ def read_rrd(): t = start for row in data: if row[0]: - result.append((t, row[0])) + result.append((t, round(row[0], 3))) t += step return result @@ -62,6 +62,8 @@ def detect_pauses(data): data.append((time.time(), end_counter + 1)) # ensure that counter increments for (ts, counter) in data: if counter > start_counter: + if verbose: + print("counter increase:", datetime.fromtimestamp(ts), counter) duration = ts - start_time if duration >= min_pause: result.append({'start' : start_time, 'duration' : duration}) diff --git a/wairc.py b/wairc.py index c29ceac..94724a9 100755 --- a/wairc.py +++ b/wairc.py @@ -43,7 +43,7 @@ count_rrd = "%s/water.rrd" % (os.path.dirname(os.path.abspath(__file__))) # Create the Round Robin Database def create_rrd(): - print 'Creating RRD: ' + count_rrd + print('Creating RRD: ' + count_rrd) # Create RRD to store counter and consumption: # Counter is GAUGE (m^3) # Consumption is ABSOLUTE (m^3/s) @@ -65,18 +65,13 @@ def create_rrd(): 'RRA:LAST:0.5:10080:520', 'RRA:AVERAGE:0.5:10080:520') except Exception as e: - print 'Error ' + str(e) + print('Error ' + str(e)) # Get the last counter value from the rrd database def last_rrd_count(): - val = 0.0 - handle = os.popen("rrdtool lastupdate " + count_rrd) - for line in handle: - m = re.match(r"^[0-9]*: ([0-9.]*) [0-9.]*", line) - if m: - val = float(m.group(1)) - break - handle.close() + val = rrdtool.lastupdate(count_rrd)['ds']['counter'] + if val is None: + val = 0.0 return val # Main @@ -92,7 +87,7 @@ def main(): # Open serial line ser = serial.Serial(port, 9600) if not ser.isOpen(): - print "Unable to open serial port %s" % port + print("Unable to open serial port %s" % port) sys.exit(1) # set trigger mode ser.write(b'C\r\n') @@ -100,7 +95,7 @@ def main(): trigger_state = 0 counter = last_rrd_count() - print "restoring counter to %f" % counter + print("restoring counter to %f" % counter) while(1==1): # Read line from arduino and convert to trigger value @@ -108,15 +103,15 @@ def main(): line = line.strip() old_state = trigger_state - if line == '1': + if line == b'1': trigger_state = 1 - elif line == '0': + elif line == b'0': trigger_state = 0 if old_state == 1 and trigger_state == 0: # trigger active -> update count rrd counter += trigger_step - update = "N:%.3f:%.3f" % (counter, trigger_step) - #print update + update = "%d:%.3f:%.3f" % (int(time.time()), counter, trigger_step) + #print(update) rrdtool.update(count_rrd, update) if __name__ == '__main__':