def init(self): self.errorMessage = '' if not self.bridge: self.bridge = TCPJSONClient('127.0.0.1', 5700) else: self.errorMessage = 'Bridge connection already established' return False if self.putValue(BRIDGE_TEST_KEY,BRIDGE_TEST_VALUE) == BRIDGE_TEST_VALUE: return True else: self.errorMessage = 'Bridge connection established but test failed' return False
def send(self, data): json = TCPJSONClient('127.0.0.1', 5700) json.send({ 'command' : 'raw', 'data' : data }) #r = self.wait_key(key, json, 10) json.close() return
def put(self, key, value): json = TCPJSONClient('127.0.0.1', 5700) json.send({'command':'put', 'key':key, 'value':value}) r = self.wait_key(key, json, 10) json.close() return r
def get(self, key): json = TCPJSONClient('127.0.0.1', 5700) json.send({'command':'get', 'key':key}) r = self.wait_key(key, json, 10) json.close() return r
def send(self, data): json = TCPJSONClient('127.0.0.1', 5700) json.send({'command': 'raw', 'data': data}) #r = self.wait_key(key, json, 10) json.close() return
def put(self, key, value): json = TCPJSONClient('127.0.0.1', 5700) json.send({'command': 'put', 'key': key, 'value': value}) r = self.wait_key(key, json, 10) json.close() return r
def get(self, key): json = TCPJSONClient('127.0.0.1', 5700) json.send({'command': 'get', 'key': key}) r = self.wait_key(key, json, 10) json.close() return r
from OSC import OSCServer, OSCClient, OSCMessage import sys from time import sleep import types #import requests sys.path.insert( 0, '/usr/lib/python2.7/bridge/') # make sure python can see the tcp module from tcp import TCPJSONClient #set up the json client and the osc client and server. The server should have the ip of your Yun. #The client.connect should have the ip address of you phone. json = TCPJSONClient('127.0.0.1', 5700) server = OSCServer(("192.168.1.199", 8000)) client = OSCClient() #client.connect( ("192.168.1.142", 9000) ) #waits for slider change def handle_timeout(self): print("Timeout") server.handle_timeout = types.MethodType(handle_timeout, server) # Adding functionality for the "light" fader: def faderLight_callback(path, tags, args, source): global faderLightFeedback if path == "/1/light":
class WnWBridge: # The constructor def __init__(self): self.bridge = None self.errorMessage = '' # The initialization of the channel (test included) def init(self): self.errorMessage = '' if not self.bridge: self.bridge = TCPJSONClient('127.0.0.1', 5700) else: self.errorMessage = 'Bridge connection already established' return False if self.putValue(BRIDGE_TEST_KEY,BRIDGE_TEST_VALUE) == BRIDGE_TEST_VALUE: return True else: self.errorMessage = 'Bridge connection established but test failed' return False # Close the channel def close(self): self.errorMessage = '' if self.bridge: self.bridge.close() # Return the error message def getErrorMessage(self): return self.errorMessage # Return the value associated to the passed key # _key the key we want to know the value associated: string # The function is synchronous and waits up to 10 seconds # before returning None (if the value is not retrieved) # Returns a string def getValue(self, _key): if self.bridge == None: self.errorMessage = 'Bridge is not connected, establish connection before getting any value' return False self.bridge.send({'command':'get', 'key':_key}) timeout = 10; while timeout>=0: r = self.bridge.recv() if not r is None: try: if r['key'] == _key: return str(r['value']) except Exception as error: self.errorMessage = 'Bridge execution exception: ' + str(error) return None timeout -= 0.1 sleep(0.1) return None # Set the couple key/value into the mailbox # _key the key: string # _value the vale: string # The function read the value just written and # returns False if the value is not the same # or the timeout has been exceeded # Otherwise it returns True def putValue(self, _key, _value): if self.bridge == None: self.errorMessage = 'Bridge is not connected, establish connection before setting any value' return False self.bridge.send({'command':'put', 'key':_key, 'value':_value}) timeout = 10; while timeout>=0: r = self.bridge.recv() if not r is None: try: if (r['key'] == _key and r['value'] == _value): return True except Exception as error: self.errorMessage = 'Bridge execution exception: ' + str(error) return False timeout -= 0.1 sleep(0.1) return False
def socket_open(self): if self.json is None: self.json = TCPJSONClient('127.0.0.1', 5700) return self.json
class BridgeClient: def __init__(self): self.json = None self.should_close_at_function_end = True def wait_response(self, json, timeout): while timeout >= 0: r = json.recv() if not r is None: return r timeout -= 0.1 sleep(0.1) return None def wait_key(self, key, json, timeout): while True: r = self.wait_response(json, timeout) if r is None: return None try: if r['key'] == key: return r['value'] except: pass def socket_open(self): if self.json is None: self.json = TCPJSONClient('127.0.0.1', 5700) return self.json def socket_close(self): if self.json is not None and self.should_close_at_function_end: self.json.close() self.json = None def begin(self): self.socket_open() self.should_close_at_function_end = False def close(self): self.should_close_at_function_end = True self.socket_close() def get(self, key): json = self.socket_open() json.send({'command': 'get', 'key': key}) r = self.wait_key(key, json, 10) self.socket_close() return r def getall(self): json = self.socket_open() json.send({'command': 'get'}) r = self.wait_response(json, 10) if not r is None: r = r['value'] self.socket_close() return r def put(self, key, value): json = self.socket_open() json.send({'command': 'put', 'key': key, 'value': value}) r = self.wait_key(key, json, 10) self.socket_close() return r def delete(self, key): json = self.socket_open() json.send({'command': 'delete', 'key': key}) r = self.wait_response(json, 10) if not r is None and not r['value'] is None: r = r['value'] self.socket_close() return r def mailbox(self, message): json = self.socket_open() json.send({'command': 'raw', 'data': message}) self.socket_close()