-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleNetworkClient.py
88 lines (72 loc) · 3.18 KB
/
SampleNetworkClient.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
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import math
import socket
# Lab 6, Task 1: Remove Hardcoded Password
from patch_01 import pwAsByte
class SimpleNetworkClient :
def __init__(self, port1, port2) :
self.fig, self.ax = plt.subplots()
now = time.time()
self.lastTime = now
self.times = [time.strftime("%H:%M:%S", time.localtime(now-i)) for i in range(30, 0, -1)]
self.infTemps = [0]*30
self.incTemps = [0]*30
self.infLn, = plt.plot(range(30), self.infTemps, label="Infant Temperature")
self.incLn, = plt.plot(range(30), self.incTemps, label="Incubator Temperature")
plt.xticks(range(30), self.times, rotation=45)
plt.ylim((20,50))
plt.legend(handles=[self.infLn, self.incLn])
self.infPort = port1
self.incPort = port2
self.infToken = None
self.incToken = None
self.ani = animation.FuncAnimation(self.fig, self.updateInfTemp, interval=500)
self.ani2 = animation.FuncAnimation(self.fig, self.updateIncTemp, interval=500)
def updateTime(self) :
now = time.time()
if math.floor(now) > math.floor(self.lastTime) :
t = time.strftime("%H:%M:%S", time.localtime(now))
self.times.append(t)
#last 30 seconds of of data
self.times = self.times[-30:]
self.lastTime = now
plt.xticks(range(30), self.times,rotation = 45)
plt.title(time.strftime("%A, %Y-%m-%d", time.localtime(now)))
def getTemperatureFromPort(self, p, tok) :
s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
s.sendto(b"%s;GET_TEMP" % tok, ("127.0.0.1", p))
msg, addr = s.recvfrom(1024)
m = msg.decode("utf-8")
return (float(m))
def authenticate(self, p, pw) :
s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
s.sendto(b"AUTH %s" % pw, ("127.0.0.1", p))
msg, addr = s.recvfrom(1024)
return msg.strip()
def updateInfTemp(self, frame) :
self.updateTime()
# print("self.infToken:",self.infToken)
# Lab 6, Task 1: Remove Hardcoded Password
if self.infToken is None : #not yet authenticated
self.infToken = self.authenticate(self.infPort, pwAsByte)
self.infTemps.append(self.getTemperatureFromPort(self.infPort, self.infToken)-273)
#self.infTemps.append(self.infTemps[-1] + 1)
self.infTemps = self.infTemps[-30:]
self.infLn.set_data(range(30), self.infTemps)
return self.infLn,
def updateIncTemp(self, frame) :
self.updateTime()
# print("self.incToken",self.incToken)
# Lab 6, Task 1: Remove Hardcoded Password
if self.incToken is None : #not yet authenticated
self.incToken = self.authenticate(self.incPort, pwAsByte)
self.incTemps.append(self.getTemperatureFromPort(self.incPort, self.incToken)-273)
#self.incTemps.append(self.incTemps[-1] + 1)
self.incTemps = self.incTemps[-30:]
self.incLn.set_data(range(30), self.incTemps)
return self.incLn,
snc = SimpleNetworkClient(23456, 23457)
plt.grid()
plt.show()