-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmaem2influxdb.py
134 lines (115 loc) · 4.81 KB
/
smaem2influxdb.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
import socket
import struct
import binascii
import struct
import influxdb
import time
import threading
MCAST_GRP = '239.12.255.254'
MCAST_PORT = 9522
#Localhost is your influxdb host, database your influxdb.
db_client=influxdb.InfluxDBClient('localhost', database='sma_em')
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.settimeout(10)
def log_to_influx(sock, db_client):
threading.Timer(1.0, log_to_influx, args=[sock, db_client]).start()
try:
smainfo=sock.recv(600)
#DEG is the dough holding the information we need to unpack
deg=smainfo[28:]
smatext={}
currtime = int(time.time())
while len(deg)>8:
index=struct.unpack('>b',deg[1:2])[0]
bytesnext=struct.unpack('>b',deg[2:3])[0]
deg=deg[4:]
if bytesnext==4:
smatext[str(index) +'.'+str(bytesnext)]=struct.unpack('>I',deg[0:bytesnext])[0]
elif bytesnext==8:
smatext[str(index) +'.'+str(bytesnext)]=struct.unpack('>q',deg[0:bytesnext])[0]
deg=deg[bytesnext:]
json_body = [
{
"measurement": "grid",
"tags": {
"phase": "SUMS",
},
"fields": {
"active_power+": smatext['1.4']/10.0,
"energy+": smatext['1.8']/3600.0,
"active_power-": smatext['2.4']/10.0,
"energy-": smatext['2.8']/3600.0,
"reactive_power+": smatext['3.4']/10.0,
"reactive_power-": smatext['4.4']/10.0,
"apparent_power+": smatext['9.4']/10.0,
"apparent_power-": smatext['10.4']/10.0,
"power_factor": smatext['13.4']/1000.0,
}
},
{
"measurement": "grid",
"tags": {
"phase": "L1",
},
"fields": {
"active_power+": smatext['21.4']/10.0,
"energy+": smatext['21.8']/3600.0,
"active_power-": smatext['22.4']/10.0,
"energy-": smatext['22.8']/3600.0,
"reactive_power+": smatext['23.4']/10.0,
"reactive_power-": smatext['24.4']/10.0,
"apparent_power+": smatext['29.4']/10.0,
"apparent_power-": smatext['30.4']/10.0,
"current": smatext['31.4']/1000.0,
"voltage": smatext['32.4']/1000.0,
"power_factor": smatext['33.4']/1000.0,
}
},
{
"measurement": "grid",
"tags": {
"phase": "L2",
},
"fields": {
"active_power+": smatext['41.4']/10.0,
"energy+": smatext['41.8']/3600.0,
"active_power-": smatext['42.4']/10.0,
"energy-": smatext['42.8']/3600.0,
"reactive_power+": smatext['43.4']/10.0,
"reactive_power-": smatext['44.4']/10.0,
"apparent_power+": smatext['49.4']/10.0,
"apparent_power-": smatext['50.4']/10.0,
"current": smatext['51.4']/1000.0,
"voltage": smatext['52.4']/1000.0,
"power_factor": smatext['53.4']/1000.0,
}
},
{
"measurement": "grid",
"tags": {
"phase": "L3",
},
"fields": {
"active_power+": smatext['61.4']/10.0,
"energy+": smatext['61.8']/3600.0,
"active_power-": smatext['62.4']/10.0,
"energy-": smatext['62.8']/3600.0,
"reactive_power+": smatext['63.4']/10.0,
"reactive_power-": smatext['64.4']/10.0,
"apparent_power+": smatext['69.4']/10.0,
"apparent_power-": smatext['70.4']/10.0,
"current": smatext['71.4']/1000.0,
"voltage": smatext['72.4']/1000.0,
"power_factor": smatext['73.4']/1000.0,
}
},
]
db_client.write_points(json_body)
return
finally:
pass
log_to_influx(sock, db_client)