-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.py
32 lines (28 loc) · 1.22 KB
/
listener.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
import socket, json
HOST = "orion" # Endereco IP do Servidor
PORT = 1028 # Porta que o Servidor esta
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (HOST, PORT)
tcp.bind(orig)
tcp.listen(1)
i = e_consumption = acc_consumption = noise = temperature = 0
print("Listening on %s:%s..." % (HOST, str(PORT)))
while True:
con, client = tcp.accept()
print 'Connected by', client
while True:
i += 1
msg = con.recv(1024)
if not msg: break
print '\n\n==', msg, '=='
json_msg = json.loads(msg[msg.index('{'):])
print json.dumps(json_msg, indent=4)
e_consumption = float(json_msg['data'][0]['energy_consumption']['value'])
noise = float(json_msg['data'][0]['noise']['value'])
temperature = float(json_msg['data'][0]['temperature']['value'])
print '\nThe energy consumption was updated to: %s.' % e_consumption
print '\nThe noise is %sdB and the temperature is %s Celsius.' % (noise, temperature)
acc_consumption += e_consumption
print 'The accumulated consumption is %s.' % acc_consumption
print 'Closing connection with server...', client
con.close()