5
5
import sys
6
6
import time
7
7
import logging
8
+ import base64
8
9
import http .client
9
10
10
11
cf = open (sys .argv [1 ])
26
27
pulse_value = config ["pulse-value" ]
27
28
voltage_ac = config ["voltage-ac" ]
28
29
cost_kwh = config ["cost-kwh" ]
29
- elastic = config ["data-store" ]["elastic" ]
30
- index = config ["data-store" ]["index" ]
31
30
co2g_per_kwh = config ["co2g-per-kwh" ]
32
31
max_power_kw = config ["max-power-kw" ]
33
32
33
+ elastic = config ["data-store" ]["elastic" ]
34
+ index = config ["data-store" ]["index" ]
35
+ elastic_user = config ["data-store" ]["user" ]
36
+ elastic_pass = config ["data-store" ]["password" ]
37
+
34
38
logging .info ('device_id: %s' , device_id )
35
39
logging .info ('pulse_value: %s' , pulse_value )
36
40
logging .info ('voltage_ac: %s' , voltage_ac )
41
45
logging .info ('max-power-kw: %s' , max_power_kw )
42
46
logging .info ('meter_pin: %s' , meter_pin )
43
47
48
+
49
+ def get_authorization (username , password ):
50
+ auth_str = username + ":" + password
51
+ return "Basic " + base64 .b64encode (auth_str .encode ('ascii' )).decode ('ascii' )
52
+
53
+
54
+ elastic_authorization = get_authorization (elastic_user , elastic_pass )
44
55
last_timestamp = 0
56
+ logging .info ('elastic_authorization: %s' , elastic_authorization )
57
+ elastic_headers = {
58
+ "Content-type" : "application/json" ,
59
+ "Authorization" : elastic_authorization
60
+ }
45
61
46
62
def write_to_elastic (device_id , now_timestamp , interval , voltage , consumed , price , power , current , co2_produced ):
47
63
now_timestamp = int (now_timestamp * 1000 )
48
- headers = {"Content-type" : "application/json" }
49
64
body = {
50
65
"timestamp" : now_timestamp ,
51
66
"deviceId" : device_id ,
@@ -60,10 +75,11 @@ def write_to_elastic(device_id, now_timestamp, interval, voltage, consumed, pric
60
75
}
61
76
}
62
77
connection = http .client .HTTPConnection (elastic )
63
- connection .request ("POST" , index + "/_doc" , headers = headers , body = json .dumps (body ))
78
+ connection .request ("POST" , index + "/_doc" , headers = elastic_headers , body = json .dumps (body ))
64
79
response = connection .getresponse ()
65
80
logging .info ("elastic response: %s" , response .status )
66
81
82
+
67
83
while True :
68
84
GPIO .wait_for_edge (meter_pin , GPIO .FALLING )
69
85
now_timestamp = time .time ()
0 commit comments