-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplogger.py
78 lines (68 loc) · 2.83 KB
/
templogger.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
import sys
import zmq, time
from datetime import datetime
from ast import literal_eval
class TemperatureLogger:
def __init__(self,roomNumber, ts, host='localhost',port=5577,watch=False):
#print('inicio de ', roomNumber)
self.host = host
self.port = port
self.topicfilter = roomNumber
self.ts = ts
self.watch = watch
# Socket to talk to server
self.context = zmq.Context()
self.socket = self.context.socket(zmq.SUB)
#print("Collecting updates from weather server...")
self.socket.connect ("tcp://%s:%d" % (self.host,self.port))
# Subscribe to room
self.socket.setsockopt(zmq.SUBSCRIBE, str(self.topicfilter).encode()) # subscribe to a room
#print('subscribed')
def logTemperature(self,N=10):
#print('logging')
# Process N updates
total_value = 0
string = ''
for update_nbr in range (N):
try:
string = self.socket.recv(zmq.NOBLOCK)
topic, temp, humidity = string.split()
total_value += float(temp)
print(topic, temp, humidity)
except zmq.Again as e:
err = e.args[0]
print(e)
print('No data available')
break
print(total_value/N)
print("Average temperature value for topic '%s' was %.1fC at" % (self.topicfilter, total_value / N), time.asctime())
return total_value/N
def logTemperatureForever(self,N=10):
# Process N updates
print('logging forever for room %d' % (self.topicfilter))
while True:
#time.sleep(1)
total = 0
for i in range (N):
string = self.socket.recv()
#print('recv: ',string)
d = literal_eval(string[2:].decode())
topic = d['sala']
temp = d['temperatura']
humidity = d['humidity']
timerecv = d['timestamp']
#print('sala',topic, 'this thread',self.topicfilter)
#print('temperature',temp)
#print('time',timerecv)
#print('humidity',humidity)
#topic, temp, humidity, timerecv = string.split()
total += float(temp)
#print(topic, temp, humidity, timerecv)
averageTemp = total/N
tempTuple = (self.topicfilter,"%.2f" % averageTemp,datetime.timestamp(datetime.now()))
#print(total/N)
#print(tempTuple)
self.ts.insert(tempTuple)
#f.write('%s %.2f %f' % (self.topicfilter, totalValue, datetime.timestamp(datetime.now())))
if self.watch:
print("Room: '%s' \tAvg temp: %.1fC \tTime: %.6f" % (self.topicfilter, total / N,datetime.timestamp(datetime.now())))