-
Notifications
You must be signed in to change notification settings - Fork 1
/
nem12topvoutput.py
executable file
·119 lines (100 loc) · 3.21 KB
/
nem12topvoutput.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python3
import csv, sys, argparse, requests
from collections import defaultdict
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="The file to process", required=True)
parser.add_argument("-i", "--sysid", help="System ID to apply changes to", required=True)
parser.add_argument("-a", "--apikey", help="API key to use", required=True)
parser.add_argument("-d", "--debug", action="store_true", help="Debugging output")
parser.add_argument("-s", "--start", type=int, help="Start date in yyyymmdd format")
parser.add_argument("-e", "--end", type=int, help="End date in yyyymmdd format")
parser.add_argument("-ip", "--importpeak", help="Import peak register", required=True)
parser.add_argument("-er", "--export", help="Export register", required=True)
args = parser.parse_args()
dailyTotals = defaultdict(dict)
with open(args.file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line = 0
processed = 0
for row in csv_reader:
line += 1
if row == []:
continue
try:
rowid = row[0]
except IndexError:
print("Invalid csv on line %d: %s" % (line, row), file=sys.stderr)
break
if rowid == "200":
if args.debug:
print("200: %s" % row, file=sys.stderr)
meter = row[3]
interval = row[8]
if interval == "30":
samples = 48
elif interval == "15":
samples = 96
else:
print("Invalid interval on line %d: %s, %s" % (line, interval, row), file=sys.stderr)
sys.exit(1)
break
processed += 1
continue
elif rowid == "300":
date = int(row[1])
if (args.start and date < args.start) or (args.end and date > args.end):
skip = True
continue
else:
skip = False
if args.debug:
print("300: %s" % row, file=sys.stderr)
rowtype = row[samples + 2]
if rowtype != "A" and rowtype != "V":
print("Unhandled 300 type on line %d: %s, %s" % (line, rowtype, row), file=sys.stderr)
sys.exit(1)
break
total = 0
for sample in range(samples):
total += int(float(row[2 + sample]) * 1000)
dailyTotals[date][meter] = total
processed += 1
continue
elif rowid == "400" and not skip:
if args.debug:
print("400: %s" % row, file=sys.stderr)
processed += 1
continue
elif rowid == "500" and not skip:
if args.debug:
print("500: %s" % row, file=sys.stderr)
processed += 1
continue
elif rowid == "900":
if args.debug:
print("900: %s" % row, file=sys.stderr)
meter = ""
date = ""
processed += 1
continue
print('Processed %d lines.' % (processed), file=sys.stderr)
for date in sorted(dailyTotals):
try:
exportwh = dailyTotals[date][args.export]
except KeyError:
exportwh = 0
try:
importwh = dailyTotals[date][args.importpeak]
except KeyError:
importwh = 0
try:
# See https://pvoutput.org/help.html#api-addoutput for the positional parameters
response = requests.post("https://pvoutput.org/service/r2/addoutput.jsp",
params={"data" : "%s,,%d,,,,,,,%d,,,," % (date, exportwh, importwh)},
headers={"X-Pvoutput-Apikey" : args.apikey, "X-Pvoutput-SystemId" : args.sysid}
)
response.raise_for_status()
print("%s: %s" % (date, response.text))
except (requests.exceptions.HTTPError) as e:
print("%s: %s" % (date, response.text))
raise e