-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleClient.py
80 lines (65 loc) · 2.91 KB
/
SampleClient.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
import threading
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# from Group8Incubator import infinc
import infinc
import time
import math
class SimpleClient :
def __init__(self, therm1, therm2) :
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.infTherm = therm1
self.incTherm = therm2
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 updateInfTemp(self, frame) :
self.updateTime()
self.infTemps.append(self.infTherm.getTemperature()-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()
self.incTemps.append(self.incTherm.getTemperature()-273)
#self.incTemps.append(self.incTemps[-1] + 1)
self.incTemps = self.incTemps[-30:]
self.incLn.set_data(range(30), self.incTemps)
return self.incLn,
UPDATE_PERIOD = .05 #in seconds
SIMULATION_STEP = .1 #in seconds
#create a new instance of IncubatorSimulator
bob = infinc.Human(mass = 8, length = 1.68, temperature = 36 + 273)
bobThermo = infinc.SmartThermometer(bob, UPDATE_PERIOD)
bobThermo.start() #start the thread
inc = infinc.Incubator(width = 1, depth=1, height = 1, temperature = 37 + 273, roomTemperature = 20 + 273)
incThermo = infinc.SmartThermometer(inc, UPDATE_PERIOD)
incThermo.start() #start the thread
incHeater = infinc.SmartHeater(powerOutput = 1500, setTemperature = 45 + 273, thermometer = incThermo, updatePeriod = UPDATE_PERIOD)
inc.setHeater(incHeater)
incHeater.start() #start the thread
sim = infinc.Simulator(infant = bob, incubator = inc, roomTemp = 20 + 273, timeStep = SIMULATION_STEP, sleepTime = SIMULATION_STEP / 10)
sim.start()
sc = SimpleClient(bobThermo, incThermo)
plt.grid()
plt.show()