-
Notifications
You must be signed in to change notification settings - Fork 2
/
greenbutton_import.py
186 lines (157 loc) · 5.83 KB
/
greenbutton_import.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#### NOTE ####
# Please make sure your python binary is listed in the first line.
# i.e. if your python is just python and not python 3 change it.
# If your python is in /bin or /opt change it
# I know the above statement is normal shell programming,
# but I have recevied several questions on 'What does bad interpreter mean'
#
# greenbutton_import.py
# Input - SCE Green Data (TOU Style)
# Output - Inluxdb mesurements (V1)
#
# utilparse.py must be in same directory as this program
__author__ = 'Garrett Gauthier'
__copyright__ = 'Copyright 2022, Garrett Gauthier'
__author__ = 'Garrett Gauthier'
__copyright__ = 'Copyright 2022, Garrett Gauthier'
__credits__ = ['Garrett Gauthier', 'Others soon?']
__license__ = 'GPL'
__version__ = '1.5'
__versiondate__ = '12/29/2022'
__maintainer__ = 'gauthig@github'
__github__ = 'https://github.com/gauthig/scegreenbutton'
__email__ = 'garrett-g@outlook.com'
__status__ = 'Production'
__status__ = 'Production'
from influxdb import InfluxDBClient
#from influxdb.exceptions import InfluxDBClientError
from datetime import datetime
import sys
#import time
import argparse
import json
from utilparse import parse_data
metricsout = []
def get_config_value(key, default_value):
if key in config:
return config[key]
return default_value
def write_csv():
textout = ''
current_date = datetime.now()
fileout = 'energy' + str(int(current_date.strftime('%Y%m%d%H%M'))) + '.csv'
with open(fileout, 'w', encoding='UTF-8') as f:
csv_columns = 'measurement,month,time,value'
f.write(csv_columns)
for data in metricsout:
textout = '\n' + json.dumps(data)
textout = textout.replace(
'{"measurement": "SCE", "tags": {"type": "', '')
textout = textout.replace('"}, "time": "', ',')
textout = textout.replace('", "fields": {"value":', ',')
textout = textout.replace('"month":"', '')
textout = textout.replace('", ', ',')
textout = textout.replace('}}', '')
f.write(textout)
print('CSV File is: ', fileout)
return ()
def write_json():
textout = ''
current_date = datetime.now()
fileout = 'energy' + str(int(current_date.strftime('%Y%m%d%H%M'))) + '.json'
with open(fileout, 'w', encoding='UTF-8') as f:
for data in metricsout:
textout = '\n' + json.dumps(data)
f.write(textout)
print('json File is: ', fileout)
return ()
def send_data(hostname, port, user, password,
dbname, createdb):
client = InfluxDBClient(hostname, port, user, password, dbname)
if createdb:
print('Deleting database %s' % dbname)
client.drop_database(dbname)
print('Creating database %s' % dbname)
client.create_database(dbname)
if len(metricsout) > 0:
client.switch_user(user, password)
response = client.write_points(metricsout, time_precision='m')
if args.verbose:
print('influxdb response', response)
return ()
if __name__ == '__main__':
config_filename = 'greenbutton.json'
config = {}
with open(config_filename) as configFile:
config = json.load(configFile)
parser = \
argparse.ArgumentParser(description="""Loads Green Button csv file
and send formated results to influxdb. """)
parser.add_argument('--version',
help='display version number',
action='store_true')
parser.add_argument(
'-f',
'--file',
required=True,
help='*REQUIRED* filename of the utility provided csv kwh file')
parser.add_argument(
'-v',
'--verbose',
help='verbose output - send copy of each line to stdout',
action='store_true')
parser.add_argument('-q',
'--quiet',
help='do not print totals output',
action='store_true')
parser.add_argument(
'-c',
'--csv',
help='sends parsed data to a csv delimited file',
action='store_true')
parser.add_argument(
'-j',
'--json',
help='sends parsed data to a json file',
action='store_true')
parser.add_argument(
'--createdb',
action='store_true',
default=False,
help='Drop database and create a new one')
parser.add_argument(
'--nodb',
action='store_true',
default='False',
help='Will NOT import to influxdb. Use with -o for local file only'
)
args = parser.parse_args()
if config['util_format'] not in ('sce-tou', 'pep'):
print('\n*** json file error ***')
print('Only valid Utility formats are: sce-tou, pep.')
print('Current json value for util_format is ',
config['util_format'])
print('Change util_format or create an enhancement request')
sys.exit()
if args.version:
print('sceinfluxdb.py - version', __version__)
sys.exit(-1)
(delivered, generated, metricsout) = parse_data(args.file, args.verbose,
config['util_format'], metricsout)
if args.csv:
write_csv()
if args.json:
write_json()
#if not args.nodb:
send_data(config['host'], config['port'], config['user'],
config['password'], config['dbname'], args.createdb)
if args.verbose:
print('Influx Host:', config['host'])
print('Influx Port:', config['port'])
if not args.quiet:
print('Import Complete')
print('Energy Delivered rows ', delivered)
print('Energy Generated rows ', generated)
sys.exit()