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:
parent
05004cbac0
commit
ff96e26649
@ -17,7 +17,7 @@ def main():
|
|||||||
# Open serial line
|
# Open serial line
|
||||||
ser = serial.Serial(port, 9600)
|
ser = serial.Serial(port, 9600)
|
||||||
if not ser.isOpen():
|
if not ser.isOpen():
|
||||||
print "Unable to open serial port %s" % port
|
print("Unable to open serial port %s" % port)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
# set data mode
|
# set data mode
|
||||||
ser.write(b'C\r\n')
|
ser.write(b'C\r\n')
|
||||||
|
@ -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__)))
|
count_fetch = "{0}/water.fetch.txt".format(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
# Length of the minimum duration without water consumption in seconds
|
# 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 output
|
||||||
verbose = False
|
verbose = False
|
||||||
@ -34,7 +34,7 @@ def read_rrd():
|
|||||||
t = start
|
t = start
|
||||||
for row in data:
|
for row in data:
|
||||||
if row[0]:
|
if row[0]:
|
||||||
result.append((t, row[0]))
|
result.append((t, round(row[0], 3)))
|
||||||
t += step
|
t += step
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -62,6 +62,8 @@ def detect_pauses(data):
|
|||||||
data.append((time.time(), end_counter + 1)) # ensure that counter increments
|
data.append((time.time(), end_counter + 1)) # ensure that counter increments
|
||||||
for (ts, counter) in data:
|
for (ts, counter) in data:
|
||||||
if counter > start_counter:
|
if counter > start_counter:
|
||||||
|
if verbose:
|
||||||
|
print("counter increase:", datetime.fromtimestamp(ts), counter)
|
||||||
duration = ts - start_time
|
duration = ts - start_time
|
||||||
if duration >= min_pause:
|
if duration >= min_pause:
|
||||||
result.append({'start' : start_time, 'duration' : duration})
|
result.append({'start' : start_time, 'duration' : duration})
|
||||||
|
27
wairc.py
27
wairc.py
@ -43,7 +43,7 @@ count_rrd = "%s/water.rrd" % (os.path.dirname(os.path.abspath(__file__)))
|
|||||||
|
|
||||||
# Create the Round Robin Database
|
# Create the Round Robin Database
|
||||||
def create_rrd():
|
def create_rrd():
|
||||||
print 'Creating RRD: ' + count_rrd
|
print('Creating RRD: ' + count_rrd)
|
||||||
# Create RRD to store counter and consumption:
|
# Create RRD to store counter and consumption:
|
||||||
# Counter is GAUGE (m^3)
|
# Counter is GAUGE (m^3)
|
||||||
# Consumption is ABSOLUTE (m^3/s)
|
# Consumption is ABSOLUTE (m^3/s)
|
||||||
@ -65,18 +65,13 @@ def create_rrd():
|
|||||||
'RRA:LAST:0.5:10080:520',
|
'RRA:LAST:0.5:10080:520',
|
||||||
'RRA:AVERAGE:0.5:10080:520')
|
'RRA:AVERAGE:0.5:10080:520')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print 'Error ' + str(e)
|
print('Error ' + str(e))
|
||||||
|
|
||||||
# Get the last counter value from the rrd database
|
# Get the last counter value from the rrd database
|
||||||
def last_rrd_count():
|
def last_rrd_count():
|
||||||
val = 0.0
|
val = rrdtool.lastupdate(count_rrd)['ds']['counter']
|
||||||
handle = os.popen("rrdtool lastupdate " + count_rrd)
|
if val is None:
|
||||||
for line in handle:
|
val = 0.0
|
||||||
m = re.match(r"^[0-9]*: ([0-9.]*) [0-9.]*", line)
|
|
||||||
if m:
|
|
||||||
val = float(m.group(1))
|
|
||||||
break
|
|
||||||
handle.close()
|
|
||||||
return val
|
return val
|
||||||
|
|
||||||
# Main
|
# Main
|
||||||
@ -92,7 +87,7 @@ def main():
|
|||||||
# Open serial line
|
# Open serial line
|
||||||
ser = serial.Serial(port, 9600)
|
ser = serial.Serial(port, 9600)
|
||||||
if not ser.isOpen():
|
if not ser.isOpen():
|
||||||
print "Unable to open serial port %s" % port
|
print("Unable to open serial port %s" % port)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
# set trigger mode
|
# set trigger mode
|
||||||
ser.write(b'C\r\n')
|
ser.write(b'C\r\n')
|
||||||
@ -100,7 +95,7 @@ def main():
|
|||||||
|
|
||||||
trigger_state = 0
|
trigger_state = 0
|
||||||
counter = last_rrd_count()
|
counter = last_rrd_count()
|
||||||
print "restoring counter to %f" % counter
|
print("restoring counter to %f" % counter)
|
||||||
|
|
||||||
while(1==1):
|
while(1==1):
|
||||||
# Read line from arduino and convert to trigger value
|
# Read line from arduino and convert to trigger value
|
||||||
@ -108,15 +103,15 @@ def main():
|
|||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
||||||
old_state = trigger_state
|
old_state = trigger_state
|
||||||
if line == '1':
|
if line == b'1':
|
||||||
trigger_state = 1
|
trigger_state = 1
|
||||||
elif line == '0':
|
elif line == b'0':
|
||||||
trigger_state = 0
|
trigger_state = 0
|
||||||
if old_state == 1 and trigger_state == 0:
|
if old_state == 1 and trigger_state == 0:
|
||||||
# trigger active -> update count rrd
|
# trigger active -> update count rrd
|
||||||
counter += trigger_step
|
counter += trigger_step
|
||||||
update = "N:%.3f:%.3f" % (counter, trigger_step)
|
update = "%d:%.3f:%.3f" % (int(time.time()), counter, trigger_step)
|
||||||
#print update
|
#print(update)
|
||||||
rrdtool.update(count_rrd, update)
|
rrdtool.update(count_rrd, update)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user