-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvoyS2Emoncms.py
165 lines (133 loc) · 6.02 KB
/
EnvoyS2Emoncms.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
#!/usr/bin/env python3
# This utility send a EnvoyS (Enphase) data to emoncms
#
# coded by:
# auteur : Frederic Metrich
# Email : Frederic.Metrich@Live.COM
version = "v3.00"
# if errors during executing this scrip make sure you installed phyton and the required modules/libraries
import configparser
import logging
import logging.handlers
import json
import urllib.request
import time
import ssl
DataJson_all = { }
LogFile = "/var/log/EnvoyS2Emoncms.log"
LogFileLastMessage = "/tmp/EnvoyS2Emoncms.log"
WatchdogFile = "/tmp/EnvoyS2Emoncms_Watchdog"
# Set logging params
def log_setup():
log_handler = logging.handlers.WatchedFileHandler(LogFile)
formatter = logging.Formatter('%(asctime)s PID(%(process)d) %(levelname)s: %(message)s')
formatter.converter = time.gmtime # if you want UTC time
log_handler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(log_handler)
logger.setLevel(logging.DEBUG)
###############################################################################################################
# Procedures
###############################################################################################################
log_setup()
logging.info("****** Starting EnvoyS2Emoncms.py ******")
Config = configparser.ConfigParser()
Config.read("/etc/Enphase/EnvoyS2Emoncms.cfg")
emon_privateKey = Config['emoncms']['privatekey']
emon_node_all = Config['emoncms']['node_all']
emon_host = Config['emoncms']['host']
emon_protocol = Config['emoncms']['protocol']
emon_url = Config['emoncms']['url']
envoy_url_all = Config['envoy']['url_all']
envoy_host = Config['envoy']['host']
envoy_protocol = Config['envoy']['protocol']
envoy_token = Config['envoy']['token']
logging.info('Configuration settings:')
logging.info('emoncms/privatekey: ' + emon_privateKey)
logging.info('emoncms/node_all: ' + emon_node_all)
logging.info('emoncms/host: ' + emon_host)
logging.info('emoncms/protocol: ' + emon_protocol)
logging.info('emoncms/url: ' + emon_url)
logging.info('envoy/url_all: ' + envoy_url_all)
logging.info('envoy/host: ' + envoy_host)
logging.info('envoy/protocol: ' + envoy_protocol)
logging.info('***********************')
###############################################################################################################
# Main program
###############################################################################################################
# Construct urls
url_envoy_all = envoy_protocol + envoy_host + envoy_url_all
myssl = ssl.create_default_context()
myssl.check_hostname=False
myssl.verify_mode=ssl.CERT_NONE
req = urllib.request.Request(url=url_envoy_all, method='GET')
req.add_header("Authorization", "Bearer " + envoy_token)
req.add_header("Accept", "application/json")
envoy_keys = ['wNow', 'whLifetime']
def ProceedData():
try:
# Fetch page with ENVOY detailed data
the_page_all = urllib.request.urlopen(req, context = myssl).read()
if len(the_page_all) < 4300:
logging.warning("Page size: " + str(len(the_page_all)) + " \'" + str(the_page_all) + "\'")
else:
logging.info("Page size: " + str(len(the_page_all)))
except ValueError as err:
logging.error('Caught http exception: ' + str(err))
logging.error('Received data: \'' + str(the_page_all) + '\'')
return
# Write raw output from envoy to file
f1=open(LogFileLastMessage, "w")
f1.write("INFO: page length(" + str(len(the_page_all)) + "): \'" + str(the_page_all) + "\'")
f1.close()
try:
data_all = json.loads(the_page_all)
except ValueError as err:
logging.error('Caught json exception: ' + str(err))
logging.error('Received data: \'' + str(the_page_all) + '\'')
return
DataJson_all.clear()
# "measurementType": "production"
DataJson_all['prod_' + 'whToday'] = data_all['production'][1]['whToday']
for data in range(len(envoy_keys)):
DataJson_all['prod_' + envoy_keys[data]] = data_all['production'][1][envoy_keys[data]]
# for 3-phase details, comment out if you don't need it
for phase in range(3):
for data in range(len(envoy_keys)):
DataJson_all['prod_L' + str(phase + 1) + '_' + envoy_keys[data]] = data_all['production'][1]['lines'][phase][envoy_keys[data]]
# "measurementType": "total-consumption"
DataJson_all['cons_' + 'whToday'] = data_all['consumption'][0]['whToday']
for data in range(len(envoy_keys)):
DataJson_all['cons_' + envoy_keys[data]] = data_all['consumption'][0][envoy_keys[data]]
# for 3-phase details, comment out if you don't need it
for phase in range(3):
for data in range(len(envoy_keys)):
DataJson_all['cons_L' + str(phase + 1) + '_' + envoy_keys[data]] = data_all['consumption'][0]['lines'][phase][envoy_keys[data]]
# "measurementType": "net-consumption"
# The next field is always zero, so skip it
# DataJson_all['cons_' + 'whToday'] = data_all['consumption'][1]['whToday']
for data in range(len(envoy_keys)):
DataJson_all['net_' + envoy_keys[data]] = data_all['consumption'][1][envoy_keys[data]]
# for 3-phase details, comment out if you don't need it
for phase in range(3):
for data in range(len(envoy_keys)):
DataJson_all['net_L' + str(phase + 1) + '_' + envoy_keys[data]] = data_all['consumption'][1]['lines'][phase][envoy_keys[data]]
url_all = emon_protocol + emon_host + emon_url + "time=" + str(data_all['production'][1]['readingTime']) + "&node=" + emon_node_all + "&apikey=" + emon_privateKey + "&fulljson=" + json.dumps(DataJson_all, separators=(',', ':'))
HTTPresult_all = urllib.request.urlopen(url_all)
if HTTPresult_all.getcode() == 200:
logging.info("Response code : " + str(HTTPresult_all.getcode()))
else:
logging.error("Response code : " + str(HTTPresult_all.getcode()))
logging.error("Original url was: '" + url_all + "'")
# Do forever ....
while True:
try:
time.sleep(5)
# Write time to watchdog file, as a sign of life
f3=open(WatchdogFile, "w")
timestamp = int(time.time())
f3.write (str(timestamp))
f3.close()
ProceedData()
except Exception as e:
logging.error('Caught exception: ' + str(e))