summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorMagnus Feuer <mfeuer@jaguarlandrover.com>2015-09-23 11:25:34 -0700
committerMagnus Feuer <mfeuer@jaguarlandrover.com>2015-09-23 11:25:34 -0700
commitff78205abe7c50c3c0b2ce35ee8bb09e3f172502 (patch)
tree05a11b10b9db6625150d516a28935b733f81660c /python
parent29365b45d1911d42cae9ca7fb9f05829d77c4bb7 (diff)
downloadrvi_core-ff78205abe7c50c3c0b2ce35ee8bb09e3f172502.tar.gz
mfeuer_json_param: Fixed message invocation to use single JSON object instead of array of objects as parameters structure.
Passed tests: Local WS -> WS ok Local HTTP -> WS ok Local WS -> HTTP ok Local HTTP -> HTTP ok RVI1:WS -> proto_json -> RVI2:WS ok RVI1:WS -> proto_json -> RVI2:HTTP ok RVI1:HTTP -> proto_json -> RVI2:WS ok RVI1HTTP -> proto_json -> RVI2:HTTP ok
Diffstat (limited to 'python')
-rwxr-xr-xpython/rvi_call_ws.py39
-rw-r--r--python/rvi_service_ws.py123
2 files changed, 162 insertions, 0 deletions
diff --git a/python/rvi_call_ws.py b/python/rvi_call_ws.py
new file mode 100755
index 0000000..ab87d52
--- /dev/null
+++ b/python/rvi_call_ws.py
@@ -0,0 +1,39 @@
+import websocket
+import time
+import sys
+import getopt
+import json
+
+opts, args = getopt.getopt(sys.argv[1:], "n:")
+
+host = 'ws://localhost:8808'
+
+for o, a in opts:
+ if o == "-n":
+ host = a
+ else:
+ usage()
+if len(args) < 1:
+ usage()
+
+i = 0
+service = args[0]
+rvi_args = {}
+for i in args[1:]:
+ print i
+ [k, v] = i.split('=')
+ rvi_args[k] = v
+
+ws = websocket.create_connection(host)
+
+print "RVI Node: ", host
+print "Service: ", service
+print "args: ", rvi_args
+
+payload = {}
+payload['jsonrpc'] = "2.0"
+payload['params'] = {'service_name':service, 'timeout':(int(time.time())+60), 'parameters':rvi_args}
+payload['id'] = "1"
+payload['method'] = 'message'
+
+ws.send(json.dumps(payload)) \ No newline at end of file
diff --git a/python/rvi_service_ws.py b/python/rvi_service_ws.py
new file mode 100644
index 0000000..9ddfbf3
--- /dev/null
+++ b/python/rvi_service_ws.py
@@ -0,0 +1,123 @@
+#!/usr/bin/python
+
+#
+# Copyright (C) 2015, Jaguar Land Rover
+#
+# This program is licensed under the terms and conditions of the
+# Mozilla Public License, version 2.0. The full text of the
+# Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
+#
+#
+# Register a service specified by command line with an RVI node.
+# Print out a message when the service gets invoked.
+#
+import sys
+from rvilib import RVI
+# pip-install websocket-client
+import websocket
+import json
+import getopt
+
+def usage():
+ print "Usage:", sys.argv[0], "[-n <rvi_url>] <service_name>"
+ print " <rvi_url> URL of Service Edge on a local RVI node."
+ print " Default: ws://localhost:8808"
+ print " <service_name> URL of Service to register"
+ print
+ print "The Service Edge URL is logged as a notice when the"
+ print "RVI node is started."
+ print
+
+ print "Example: ./rvi_service_ws.py -n ws://rvi1.nginfotpdx.net:8808 /test/some_service"
+ sys.exit(255)
+
+
+#
+# Our general handler, registered with rvi.register_service() below.
+#
+# You can also explicitly name the arguments, but then
+# the sender has to match the argument names.
+
+# For example:
+# rvi_call.py http://localhost:8801 jlr.com/vin/test a=1 b=2 c=3 ->
+# def service(a,b,c)
+#
+def service_invoked(**args):
+ print
+ print "Service invoked!"
+ print "args:", args
+ print
+ sys.stdout.write("Press enter to quit: ")
+ sys.stdout.flush()
+ return ['ok']
+
+def services_available(**args):
+ print
+ print "Services available!"
+ print "args:", args
+ print
+ sys.stdout.write("Press enter to quit: ")
+ sys.stdout.flush()
+ return ['ok']
+
+def services_unavailable(**args):
+ print
+ print "Services unavailable!"
+ print "args:", args
+ print
+ sys.stdout.write("Press enter to quit: ")
+ sys.stdout.flush()
+ return ['ok']
+
+
+#
+# Check that we have the correct arguments
+#
+opts, args= getopt.getopt(sys.argv[1:], "n:")
+
+rvi_node_url = "ws://localhost:8808"
+for o, a in opts:
+ if o == "-n":
+ rvi_node_url = a
+ else:
+ usage()
+
+if len(args) != 1:
+ usage()
+
+service_name = args[0]
+
+
+ws = websocket.create_connection(rvi_node_url)
+
+tid = 1
+payload = {}
+payload['json-rpc'] = "2.0"
+payload['id'] =tid
+payload['method'] = "register_service"
+payload['params'] = {"service_name": service_name}
+
+# Register service
+ws.send(json.dumps(payload))
+reg_res = json.loads(ws.recv())
+
+full_service_name = reg_res['service']
+print "RVI node URL: ", rvi_node_url
+print "Service: ", full_service_name
+
+while True:
+ print
+ print 'Press Ctrl-\ to quit.'
+ inp = json.loads(ws.recv())
+ print inp
+ params=inp['params']
+ if inp['method'] == 'services_available':
+ print "Services available:", params['services']
+
+ if inp['method'] == 'services_unavailable':
+ print "Service unavailable:", params['services']
+
+ if inp['method'] == 'message':
+ print "Service invoked."
+ print "service: ", params['service_name']
+ print "parameters: ", params['parameters']