-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gateway.py
53 lines (46 loc) · 1.28 KB
/
Gateway.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
import websocket
import json
import SensorData
def assureNotClosed(self, ws, timeout = 1):
ws.socket.timeout = timeout
with self.assertRaises(websocket.WebSocketTimeoutException):
ws.recv()
def assureIsClosed(self, ws):
with self.assertRaises(websocket.WebSocketConnectionClosedException):
ws.recv()
"""
This class is responsible for emulation of real BeeeOn Gateway.
It is able to connect to GW Server and manage some of the
control messages.
"""
class Gateway:
def __init__(self, port, id, autoRegister, host = "localhost"):
self.socket = None
self.port = port
self.host = host
self.id = id
self.autoRegister = autoRegister
def connect(self):
self.socket = websocket.WebSocket()
self.socket.connect("ws://%s:%u" %(self.host, self.port))
if self.autoRegister:
msg = {}
msg['gateway_id'] = self.id
msg['message_type'] = "gateway_register"
msg['version'] = "v1.0"
msg['ip_address'] = "192.168.1.1"
self.socket.send(json.dumps(msg))
answer = json.loads(self.socket.recv())
if answer['message_type'] != "gateway_accepted":
raise Exception("error registering")
def send(self, msg):
self.socket.send(msg)
def recv(self):
return self.socket.recv()
def close(self):
try:
self.socket.close()
except:
pass
def socket(self):
return self.socket