-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpMult.py
245 lines (198 loc) · 8.54 KB
/
udpMult.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import socket
import datetime
from influxdb import InfluxDBClient
import struct
import json
# InfluxDB
client = InfluxDBClient(host='150.162.19.148', port=8086, database='db_smf_50')
print(client.get_list_database())
#client.switch_database('db_smf_50')
print(client.get_list_measurements())
# Multicast
#MCAST_GRP = '224.0.1.1'
#MCAST_PORT = 4713
#IS_ALL_GROUPS = True
#
UDP_IP = "150.162.19.217"
UDP_PORT = 4728
sock = socket.socket(socket.AF_INET, #internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
#sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
'''if IS_ALL_GROUPS:
# on this port, receives ALL multicast groups
sock.bind(('', MCAST_PORT))
else:
# on this port, listen ONLY to MCAST_GRP
sock.bind((MCAST_GRP, MCAST_PORT)) '''
#mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
#sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
num_pmu = 0
phnmr = [3]
stn = []
annmr = 0
dgnmr = 0
idcode = []
configuration_frame = {
"MAIN":
{
#'SYNC': None,
#'FRAMESIZE': None,
#'IDCODE': None,
#'SOC': None,
#'FRACSEC': None,
#'NUM_PMU': None
},
"TERMINALS": [{
'PHNMR': 3
}]
}
data_frame_rec = {
"MAIN":
{
#'SYNC': None,
#'FRAMESIZE': None,
#'IDCODE': None,
#'SOC': None,
#'FRACSEC': None
},
"TERMINALS": [{
#'STAT': None,
#'PHASORS': [{
#'MOD_A': None,
#'ANG_A': None,
#'MOD_B': None,
#'ANG_B': None,
#'MOD_C': None,
#'ANG_C': None
# }],
#'FREQ': None,
#'DFREQ': None
}]
}
# atualiza os canais a cada minuto com o frame de configuracao
def atualiza_canais():
# incremento para pular para as próximas medições
inc = 0
# Sincronismo
configuration_frame['MAIN']['SINC'] = struct.unpack('!h', data_frame1[0:2])[0]
# Frame Size
configuration_frame['MAIN']['FRAMESIZE'] = struct.unpack('!h', data_frame1[2:4])[0]
print('Framesize: ', configuration_frame['MAIN']['FRAMESIZE'])
# ID Code do Stream
configuration_frame['MAIN']['IDCODE'] = struct.unpack('!h', data_frame1[4:6])[0]
# SOC
configuration_frame['MAIN']['SOC'] = struct.unpack('!i', data_frame1[6:10])[0]
# FRACSEC
configuration_frame['MAIN']['FRACSEC'] = struct.unpack('!i', data_frame1[10:14])[0]
# converte o número de PMUs (hexadecimal) do frame de configuração para int (decimal)
configuration_frame['MAIN']['NUM_PMU'] = struct.unpack('!h', data_frame1[18:20])[0]
print('Num PMUs: ', configuration_frame['MAIN']['NUM_PMU'])
print('SOC: ', configuration_frame['MAIN']['SOC'])
print('FRACSEC: ', configuration_frame['MAIN']['FRACSEC'])
print('config_frame: ', data_frame1)
for i in range(0, configuration_frame['MAIN']['NUM_PMU']-1):
configuration_frame['TERMINALS'].append({
'STN': struct.unpack('!16s', data_frame1[(inc+20):(inc+36)])[0].decode().strip(),
'IDCODE': struct.unpack('!h', data_frame1[(inc+36):(inc+38)])[0],
'PHNMR': struct.unpack('!h', data_frame1[(inc+40):(inc+42)])[0],
})
print('STN: ', struct.unpack('!16s', data_frame1[(inc+20):(inc+36)])[0].decode())
inc += 30 + 20 * int(configuration_frame['TERMINALS'][i+1]['PHNMR'])
global received_command_frame
received_command_frame = 0
while received_command_frame != '31':
# Aguarda o frame de configuração
data_frame1 = sock.recv(10240)
data_frame = data_frame1.hex()
# converte tuple to string hexadecimal (hex())
received_command_frame = struct.unpack('!s', data_frame1[1:2])[0].hex()
print("aguardando")
SOC = data_frame1[6:10].hex()
print(
datetime.datetime.fromtimestamp(
int(SOC, 16)
).strftime('%Y-%m-%d %H:%M:%S')
)
# atualiza os canais (function)
print("Chegou o frame de configuração!")
print('Hexadecimal_DATA: ', data_frame1)
print('-------')
# print('Frame: ', data_frame)
# print('------')
atualiza_canais()
print('aqui o config: ', configuration_frame)
data_frame1 = 0
data_frame = 0
# Aquisição do frame de dados
while True:
data_frame1 = sock.recv(10240)
data_frame = data_frame1.hex()
received_command_frame = struct.unpack('!s', data_frame1[1:2])[0].hex()
if (received_command_frame == '31'):
atualiza_canais()
else:
# Adquire o SOC e passa para o dict
data_frame_rec['MAIN']['SOC'] = struct.unpack('!i', data_frame1[6:10])[0]
# Adquire o FRACSEC e passa para o dict
data_frame_rec['MAIN']['FRACSEC'] = struct.unpack('!i', data_frame1[10:14])[0]
TIME_BASE = 16777216 # 2^24 para compatibilida da norma IEC 61850 (Está na norma IEEE C37.118)
frac_sec = data_frame_rec['MAIN']['FRACSEC'] / TIME_BASE
SOC_DATA = data_frame_rec['MAIN']['SOC'] + frac_sec
SOC_DATA_conv = datetime.datetime.fromtimestamp(
SOC_DATA
).strftime('%Y-%m-%dT%H:%M:%S.%f%Z')
print('SOC: ',
datetime.datetime.fromtimestamp(
SOC_DATA
).strftime('%Y-%m-%dT%H:%M:%S.%f%Z')
)
SOC_3339 = datetime.datetime.fromtimestamp(SOC_DATA).timestamp()
# incremento para pular para as próximas medições
inc = 0
for i in range(0, configuration_frame['MAIN']['NUM_PMU'] - 1):
inc_freq_dfreq = 8 * int(configuration_frame['TERMINALS'][i+1]['PHNMR'])
fasores = {
'IDCODE_PMU': configuration_frame["TERMINALS"][i+1]["IDCODE"],
#'STN_PMU': configuration_frame["TERMINALS"][i+1]["STN"],
'STAT': struct.unpack('!h', data_frame1[(inc+14):(inc+16)])[0],
'PHASORS': {
'MOD_A': struct.unpack('!f', data_frame1[(inc+16):(inc+20)])[0],
'ANG_A': struct.unpack('!f', data_frame1[(inc+20):(inc+24)])[0],
'MOD_B': struct.unpack('!f', data_frame1[(inc+24):(inc+28)])[0],
'ANG_B': struct.unpack('!f', data_frame1[(inc+28):(inc+32)])[0],
'MOD_C': struct.unpack('!f', data_frame1[(inc+32):(inc+36)])[0],
'ANG_C': struct.unpack('!f', data_frame1[(inc+36):(inc+40)])[0]
},
'FREQ': struct.unpack('!f', data_frame1[(inc+inc_freq_dfreq+16):(inc+inc_freq_dfreq+20)])[0],
'DFREQ': struct.unpack('!f', data_frame1[(inc+20):(inc+24)])[0]
}
data_frame_rec['TERMINALS'].append(fasores)
if ( struct.unpack('!h', data_frame1[(inc+14):(inc+16)])[0] == 0 ):
json_body = [
{
"measurement": configuration_frame["TERMINALS"][i+1]["STN"],
"tags": {
"IDCODE_PMU": configuration_frame["TERMINALS"][i+1]["IDCODE"],
"STAT": struct.unpack('!h', data_frame1[(inc+14):(inc+16)])[0],
},
"time": SOC_DATA_conv,
"fields": {
'MOD_A': struct.unpack('!f', data_frame1[(inc+16):(inc+20)])[0],
'ANG_A': struct.unpack('!f', data_frame1[(inc+20):(inc+24)])[0],
'MOD_B': struct.unpack('!f', data_frame1[(inc+24):(inc+28)])[0],
'ANG_B': struct.unpack('!f', data_frame1[(inc+28):(inc+32)])[0],
'MOD_C': struct.unpack('!f', data_frame1[(inc+32):(inc+36)])[0],
'ANG_C': struct.unpack('!f', data_frame1[(inc+36):(inc+40)])[0],
'FREQ': struct.unpack('!f', data_frame1[(inc+inc_freq_dfreq+16):(inc+inc_freq_dfreq+20)])[0],
'DFREQ': struct.unpack('!f', data_frame1[(inc+20):(inc+24)])[0]
}
}
]
client.write_points(json_body)
print('---------------------')
print('GRAVANDO: ', json_body)
#json_body[0]["fields"].update({configuration_frame["TERMINALS"][i+1]["STN"] : {'MOD_A': struct.unpack('!f', data_frame1[(inc+16):(inc+20)])[0]}})
# json_body[0]["fields"].update({configuration_frame["TERMINALS"][i+1]["STN"] : fasores})
inc += 10 + 8 * int(configuration_frame['TERMINALS'][i+1]['PHNMR'])