-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddpciaoclient.py
executable file
·67 lines (52 loc) · 1.79 KB
/
ddpciaoclient.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
from Queue import Queue
import logging
import json
import os.path, sys
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'modules'))
from DDPClient import DDPClient
class DDPCiaoClient():
def __init__(self, ddp_params, ciao_queue):
#validate params - START
missing_params = []
required_params = ["host", "port"]
for p in required_params:
if not p in ddp_params:
missing_params.append(p)
if len(missing_params) > 0:
raise RuntimeError("DDP configuration error, missing: %s" % ",".join(missing_params))
#validate params - END
#reference to Queue for exchanging data with CiaoCore
self.ciao_queue = ciao_queue
#local instance of DDP Client
self.handle = DDPClient('ws://'+ddp_params["host"]+':'+str(ddp_params["port"])+'/websocket')
self.handle.on('connected', self.on_connect)
self.host = ddp_params["host"]
self.port = ddp_params["port"]
self.logger = logging.getLogger("ddp.client")
def on_connect():
self.logger.info ("Connected to DDP Server %s" % self.host)
'''
TODO from METEOR to SKETCH
def on_message(self, client, userdata, msg):
self.logger.debug("Got new message. Topic: %s Message: %s" % (str(msg.topic), str(msg.payload)))
entry = {
"data" : [str(msg.topic), str(msg.payload)]
}
self.ciao_queue.put(entry)
'''
def connect(self):
try:
self.handle.connect()
return True
except:
return False
def disconnect(self):
self.handle.close()
def callback_function(a, b):
self.logger.debug("Callback Function. Param1: %s Param2: %s" % (str(a), str(b)))
def publish(self, function, parameter):
self.logger.debug("Call function: %s Parameter: %s" % (function, str(parameter)))
try:
self.handle.call(function, json.loads(parameter), None)
except Exception, e:
self.logger.error('Failed handle call: '+ str(e))