Ejemplo n.º 1
0
def hello():
    global msg
    if request.method == 'POST':
        ## set destination with lat and long
        if request.form['action'] == 'post':
            data = {
                'latitude': request.form['latitude'],
                'longitude': request.form['longitude']
            }
            request_json = pyjsonrpc.create_request_json(
                "DriverlessCar.set_destination", data)
            parsed_response = json.loads(request_json)
            print json.dumps(parsed_response, indent=4)
            send_socketio.send_data(socketio, request_json)
        ## set fixed destination with key value
        if request.form['action'] == 'setfixed':
            data = {'location_name': request.form['fixedkey']}
            request_json = pyjsonrpc.create_request_json(
                "DriverlessCar.set_fixed_destination", data)
            parsed_response = json.loads(request_json)
            print json.dumps(parsed_response, indent=4)
            send_socketio.send_data(socketio, request_json)
        ## get current status of driverlesscar
        if request.form['action'] == 'getpos':
            request_json = pyjsonrpc.create_request_json(
                "DriverlessCar.get_status")
            send_socketio.send_data(socketio, request_json)
        ## load keyvalue of fixed destination
        if request.form['action'] == 'loadpos':
            request_json = pyjsonrpc.create_request_json(
                "DriverlessCar.load_fixed_destination")
            send_socketio.send_data(socketio, request_json)
    return render_template('index.html', message=msg)
Ejemplo n.º 2
0
 def write(self, methodName, params, srcEpid=255, destEpid=1):
     json_obj = pyjsonrpc.create_request_json(methodName, **params)
     ascii_gs = 29
     plen = socket.htons(len(json_obj))
     reserved = 0
     json_stream = struct.pack('=BHBBHB', ascii_gs, plen, destEpid, srcEpid,
                               reserved, ascii_gs)
     json_stream += json_obj
     print('--> ' + json_obj)
     self._socket.send(json_stream)
     json_obj = ast.literal_eval(json_obj)
     return json_obj['id']
Ejemplo n.º 3
0
def mixed_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = JsonRpc()

    # Create JSON-RPC-string with mixed params
    request_json = pyjsonrpc.create_request_json("add", 1, b = 2)
    # '{"params": {"b": 2, "__args": [1]}, "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        print "Result:", response.result
Ejemplo n.º 4
0
def positional_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods={"add": add})

    # Create JSON-RPC-string with positional params
    request_json = pyjsonrpc.create_request_json("add", 1, 2)
    # '{"params": [1, 2], "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        print "Result:", response.result
Ejemplo n.º 5
0
def named_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods={"add": add})

    # Create JSON-RPC-string with named params
    request_json = pyjsonrpc.create_request_json("add", a=1, b=2)
    # '{"params": {"a": 1, "b": 2}, "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print("Request-JSON:", repr(request_json))

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print("Response-JSON:", repr(response_json))

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print("Error:", response.error.code, response.error.message)
    else:
        # 3
        print("Result:", response.result)
Ejemplo n.º 6
0
def positional_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

    # Create JSON-RPC-string with positional params
    request_json = pyjsonrpc.create_request_json("add", 1, 2)
    # '{"params": [1, 2], "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        print "Result:", response.result
Ejemplo n.º 7
0
def test_named_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

    # Create JSON-RPC-string with named params
    request_json = pyjsonrpc.create_request_json("add", a = 1, b = 2)
    # '{"params": {"a": 1, "b": 2}, "jsonrpc": "2.0", "method": "add", "id": "..."}'
    #print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    #print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        #print "Result:", response.result
        assert response.result == 3
 def __managerAgentAdd(self, agent_id):
     return pyjsonrpc.create_request_json("manager.agent.add",
                                          agent_id=agent_id)
 def __subscribeEvent(self, name):
     return pyjsonrpc.create_request_json("sys.notifications.subscribe",
                                          name=name)
Ejemplo n.º 10
0
# END --- required only for testing, remove in real world code --- END


import pyjsonrpc


def add(a, b):
    """Test function"""
    return a + b


# 1. Initialize JSON-RPC class with JSON-RPC method(s)
rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

# 2. Create JSON-RPC string with parameters (= request string)
request_json = pyjsonrpc.create_request_json("add", 1, 2)
# request_json = '{"method": "add", "params": [1, 2], "id": "...", "jsonrpc": "2.0"}'

# 3. Call the JSON-RPC function and get back the JSON-RPC result (= response string)
response_json = rpc.call(request_json)
# response_json = '{"result": 3, "id": "...", "jsonrpc": "2.0"}'

# 4. Convert JSON-RPC string to Python objects
response = pyjsonrpc.parse_response_json(response_json)

# 5. Print result or error
if response.error:
    print "Error:", response.error.code, response.error.message
else:
    print "Result:", response.result
Ejemplo n.º 11
0
 def __onReady(self):
     request_json = pyjsonrpc.create_request_json("onReady")
     self.send_msg(request_json)
Ejemplo n.º 12
0
sys.path.insert(0, APPDIR)
# END --- required only for testing, remove in real world code --- END

import pyjsonrpc


def add(a, b):
    """Test function"""
    return a + b


# 1. Initialize JSON-RPC class with JSON-RPC method(s)
rpc = pyjsonrpc.JsonRpc(methods={"add": add})

# 2. Create JSON-RPC string with parameters (= request string)
request_json = pyjsonrpc.create_request_json("add", 1, 2)
# request_json = '{"method": "add", "params": [1, 2], "id": "...", "jsonrpc": "2.0"}'

# 3. Call the JSON-RPC function and get back the JSON-RPC result (= response string)
response_json = rpc.call(request_json)
# response_json = '{"result": 3, "id": "...", "jsonrpc": "2.0"}'

# 4. Convert JSON-RPC string to Python objects
response = pyjsonrpc.parse_response_json(response_json)

# 5. Print result or error
if response.error:
    print "Error:", response.error.code, response.error.message
else:
    print "Result:", response.result
Ejemplo n.º 13
0
current_time = pygame.time.get_ticks()
received_buffer = "\n\n\n"
sent_buffer = "\n\n\n"

while True:
    DISPLAYSURF.fill(color1)

    puttext("Arrow keys: control    c: -><-    d: <-->    v: /\\\\/    f: \\//\\", (5, 5))

    # send modification in states
    ellapsed_seconds = (pygame.time.get_ticks() - current_time) / 1000.0
    if (ellapsed_seconds > inter_message_time):
        changes = message(state, old_state)
        if bool(changes):
            request = pyjsonrpc.create_request_json("set", changes) + "\n"
            ser.write(request)
            sent_buffer += request

        bytesToRead = ser.inWaiting()
        try:
            output = ser.read(bytesToRead)
        except ValueError:
            print 'Did not manage to read output'
            continue
        if output != "":
            received_buffer += output + "\n"

        current_time = pygame.time.get_ticks()

    temp = sent_buffer.split('\n')