-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartpick.py
executable file
·73 lines (69 loc) · 2.43 KB
/
smartpick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Smartpi Consumption Kalkulator | smartpick.py
by rbckman
'''
import os
import time
import requests
import code
import re
from datetime import datetime
from datetime import timedelta
import argparse
from calendar import monthrange
def getargs():
parser = argparse.ArgumentParser(description='Get power consumption of today or a specific date using smartpi API.')
parser.add_argument('url', metavar='url', type=str,
help='ip address or hostname to smartpi server')
parser.add_argument('-y', '--year', metavar='y', type=int,
help='the year in which the month you wish to calculate ex. 2018')
parser.add_argument('-m', '--month', metavar='m', type=int,
help='month')
parser.add_argument('-d', '--day', metavar='d', type=int,
help='day')
args = parser.parse_args()
if args.year == None and args.month == None and args.day == None:
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
elif args.month == None and args.day == None:
year = args.year
month = datetime.now().month
day = 0
elif args.day == None:
year = args.year
month = args.month
day = 0
else:
#print('no input, getting todays consumption')
year = args.year
month = args.month
day = args.day
return year, month, day, args.url
def getconsumption(year, month, day, url):
totkwh = 0
i = 0
days = 1
if day == 0:
days = monthrange(year, month)[1]
day = 1
#print("days calculate: " + str(days))
while i < days:
gettime = (datetime.strptime(str(year) + "-" + str(month).zfill(2) + '-' + str(day).zfill(2), "%Y-%m-%d") + timedelta(days=i)).strftime("%Y-%m-%d")
#print("calculating day: " + gettime)
readsmartpi = requests.get(url=url + ":1080/api/chart/123/energy_pos/from/" + gettime + "T00:00:00.000Z/to/" + gettime + "T23:59:00.000Z").json()
#print(readsmartpi)
#code.interact(local=locals())
try:
for p in readsmartpi:
for a in p['values']:
totkwh += int(a['value'])
except:
pass
i += 1
totkwh = totkwh/1000
return totkwh
year, month, day, url = getargs()
print(str(getconsumption(year, month, day, url)))