import matplotlib.pyplot as plt
import socket
import struct
import numpy as np
import datetime

device = '192.168.178.79'

def network_receive():
    int_size = 4
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((device, 123))
    number_of_elements = struct.unpack('i', s.recv(int_size, socket.MSG_WAITALL))[0]
    session_start_time = struct.unpack('i', s.recv(int_size, socket.MSG_WAITALL))[0]
    dtime = datetime.datetime.fromtimestamp(session_start_time)
    print("Number of elements {}, session start {}".format(number_of_elements, dtime))
    data_buffer = struct.unpack('{}i'.format(number_of_elements), s.recv(int_size * number_of_elements, socket.MSG_WAITALL))
    s.close()
    return np.array(data_buffer, dtype=np.int32)

data = network_receive()
plt.plot(data)
plt.show()
print(network_receive())

#data = []
#while True:
#    value = network_receive()
#    data.append(value)
#    print(value, min(data), max(data))
