Skip to content

Commit

Permalink
update do graph em intervalos fixos e novo cliente de teste
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaabe committed Jun 29, 2018
1 parent b196893 commit c7ac500
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 6 additions & 2 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import socket
import time
import random

if __name__ == '__main__':
port = int(input("Enter the port number that you would like to connect: "))
Expand All @@ -8,7 +10,9 @@
sock.connect(server_address)
print("Connected to the server")
while True:
msg = str(input("Send your message to the server: "))
# msg = str(input("Send your message to the server: "))
msg = str(random.randint(0, 60))
sock.sendall(msg.encode('utf-8'))
data = sock.recv(16)
print("The server answered: {answer}".format(answer=data))
print("The server answered: {answer}".format(answer=data))
time.sleep(0.5)
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def open_socket(self):


if __name__ == '__main__':
temp_monitor_server = ServerThread(5555)
temp_monitor_server = ServerThread(5500)
temp_monitor_server.setName('Temperature Monitor Server')


Expand Down
22 changes: 18 additions & 4 deletions server2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# Socket server in python using select function

import socket, select
import traceback
from collections import deque
import matplotlib.pyplot as plt
from threading import Thread
import time


CONNECTION_LIST = []
TEMP_MONITORS = []
RECV_BUFFER = 4096
PORT = 4001
GRAPH_WAIT_TIME = 5


def update_line():
plt.clf()
for monitor in TEMP_MONITORS:
plt.plot(range(len(monitor.temps)), monitor.temps)
plt.pause(0.05)


def get_temp_monitor(monitors, socket):
Expand Down Expand Up @@ -55,8 +63,15 @@ def update_line(self):
CONNECTION_LIST.append(server_socket)

print("Server started on port " + str(PORT))

last_time = time.time()

while 1:

if time.time() - last_time >= GRAPH_WAIT_TIME:
update_line()
last_time = time.time()

read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])

for sock in read_sockets:
Expand All @@ -74,7 +89,6 @@ def update_line(self):
data = sock.recv(RECV_BUFFER)
monitor = get_temp_monitor(TEMP_MONITORS, sock)
monitor.log_temperature(data)
monitor.update_line()
print("Received {data} from monitor {monitor}".format(data=data, monitor=monitor.number))
if data:
sock.send('OK ... '.encode('utf-8'))
Expand All @@ -88,4 +102,4 @@ def update_line(self):
CONNECTION_LIST.remove(sock)
continue

server_socket.close()
server_socket.close()
6 changes: 6 additions & 0 deletions test_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)

0 comments on commit c7ac500

Please sign in to comment.