FHEM-Log-Parser/parseLogs.py

56 lines
2.3 KiB
Python
Raw Normal View History

2018-02-18 22:02:24 +01:00
#!/usr/bin/env python3
2017-08-13 18:52:10 +02:00
import sys
2018-02-18 22:37:29 +01:00
import time
2017-08-13 18:52:10 +02:00
import argparse
import logParser
2018-02-18 22:02:24 +01:00
import dateparser
2017-08-13 18:52:10 +02:00
from math import inf
argparser = argparse.ArgumentParser(description='Parse the FHEM logfiles into a Database.')
argparser.add_argument('--parse', '-p', nargs='+', help='List of Logfiles to Parse.', required=False, metavar='FILE')
argparser.add_argument('--list', '-l', help='List all Heaters in the Database.', action='store_true')
argparser.add_argument('--heatAmmount', help='Sum of the actuator value times a time slice.', nargs=1, metavar='HEATER')
argparser.add_argument('--allHeatAmmount', help='Get the heat ammount from all known heaters.', action='store_true')
argparser.add_argument('--timeSpan', help='The time span for the heatAmmount calculation. Either t1/t2 or both may be supplied.', nargs=1, metavar='t1..t2 | t1 | ..t2')
2018-02-18 23:02:44 +01:00
argparser.add_argument('--verbose', '-v', help='Output names and dates.', action='store_true')
2018-02-18 22:37:29 +01:00
2017-08-13 18:52:10 +02:00
# Parse args. Set name to default.
args = argparser.parse_args()
2018-02-18 22:37:29 +01:00
mintime, maxtime = 0, time.time()
2017-08-13 18:52:10 +02:00
if args.timeSpan:
times = args.timeSpan[0].split('..')
2018-02-18 22:02:24 +01:00
2017-08-13 18:52:10 +02:00
if len(times) < 1 or len(times) > 2:
print('Invalid Timespan!')
exit(1)
elif len(times) == 2:
if times[0] == '':
2018-02-18 22:52:50 +01:00
maxtime = dateparser.parse(times[1]).timestamp()
2017-08-13 18:52:10 +02:00
else:
2018-02-18 22:52:50 +01:00
mintime, maxtime = dateparser.parse(times[0]).timestamp(), dateparser.parse(times[1]).timestamp()
2018-02-18 22:37:29 +01:00
2017-08-13 18:52:10 +02:00
else:
mintime = int(times[0])
if not args.parse is None:
parser = logParser.LogParser(args.parse)
parser.parse()
elif not args.list is False:
print('\n'.join(logParser.getHeaterList()))
elif not args.heatAmmount is None:
2018-02-18 22:37:29 +01:00
heat = args.heatAmmount[0], logParser.getHeaterSum(args.heatAmmount[0], mintime, maxtime)
if args.verbose:
print(', '.join([heat[0], str(heat[1]), str(int(mintime)), str(int(maxtime))]))
else:
print(heat[1])
2017-08-13 18:52:10 +02:00
elif not args.allHeatAmmount is None:
2018-02-18 22:37:29 +01:00
allheaters = logParser.getAllHeaterSums(mintime, maxtime)
for heater in allheaters:
if args.verbose:
print(', '.join([heater, str(allheaters[heater]), str(int(mintime)), str(int(maxtime))]))
else:
print(', '.join([heater, str(allheaters[heater])]))