1
0
mirror of https://github.com/skaringa/water-counter.git synced 2025-05-19 18:53:36 +02:00

Update to Python 3

This commit is contained in:
Martin Kompf 2025-04-27 18:34:52 +02:00
parent 05004cbac0
commit ff96e26649
3 changed files with 16 additions and 19 deletions

View File

@ -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')

View File

@ -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})

View File

@ -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__':