-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprova_stream_25_01.py
66 lines (53 loc) · 1.76 KB
/
prova_stream_25_01.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
import asyncio
import random
import time
import json
import streamlit as st
import pandas as pd
import csv
df = pd.DataFrame({})
async def send_command(time_stamp, command):
reader, writer = await asyncio.open_connection(
'127.0.0.1', 8888)
# IMPORTANTE -> Terminare sempre il comando con \n
# accertarsi che in ciò che si spedisce
# non sia compreso un \n spurio che deve
# servire solo come terminatore.
message = f"{command} {str(time_stamp)}\n"
#print(f'Send: {message!r}')
writer.write(message.encode())
#Questo passaggio è necessario per comunicare con il server
await writer.drain()
data = await reader.read()
json_obj = json.loads(data.decode('utf-8'))
#Prima un comando viene inviato con il writer, queso comando viene letto dal reader del server e
#convertito in stringa per poi essere riconvertito in json nel server
#print(f"Received: {json_obj['values']}")
writer.close()
print('Close the connection')
return json_obj['values']
def get_values(time_stamp):
lista = asyncio.run(send_command(time_stamp, command='send'))
return lista
def converter (lista):
dictionary = []
for el in lista:
for key, value in el.items():
el[key] = value
dictionary.append(el)
return dictionary
def main():
global df
while True:
tmpTS = pd.Timestamp.now()
time_stamp = str(tmpTS)
lista = get_values(time_stamp)
#print (lista)
list_dict = converter(lista)
df1 = pd.DataFrame.from_dict(list_dict)
df = pd.concat([df,df1], ignore_index = True)
print (len(df))
if len(df) >= 80000:
break
df.to_csv('data_collection.csv')
main()