#!/usr/bin/env python3 import urllib3 import sys sys.path.insert(1, '/etc/ed7039e/client-library-python') import arrowhead_client.api as ar urllib3.disable_warnings() # Creating the consumer_app consumer_app = ar.ArrowheadHttpClient( system_name='consumer', address='127.0.0.1', port=5001, keyfile='certificates/consumer.key', certfile='certificates/consumer.crt', ) # Add services to consume consumer_app.add_consumed_service('pick_up', 'POST') consumer_app.add_consumed_service('place', 'POST') if __name__ == '__main__': while True: # Consume services and print out the response message print('Enter service:') x = input() if x == 'pick_up l' or x == 'pick up l': response = consumer_app.consume_service( 'pick_up', json={'position': 'loading'}) message = consumer_app.consumer.extract_payload(response, 'json')
""" HttpProvider example app """ import arrowhead_client.api as ar provider = ar.ArrowheadHttpClient(system_name='provider', address='127.0.0.1', port=7655, keyfile='crypt/provider.key', certfile='crypt/provider.pem') @provider.provided_service('hello-arrowhead', 'hello', 'HTTP-SECURE-JSON', 'GET', payload_format='JSON', access_policy='TOKEN') def hello_arrowhead(request): return {"msg": "Hello, Arrowhead!"} if __name__ == '__main__': provider.setup() provider.run_forever()
""" HttpProvider example app """ import arrowhead_client.api as ar provider_app = ar.ArrowheadHttpClient( system_name='example-provider', address='127.0.0.1', port=7655, keyfile='certificates/example-provider.key', certfile='certificates/example-provider.crt', ) @provider_app.provided_service( 'hello-arrowhead', 'hello', 'HTTP-INSECURE-JSON', 'GET', ) def hello_arrowhead(request): return {"msg": "Hello, Arrowhead!"} if __name__ == '__main__': provider_app.run_forever()
# time_provider.py import arrowhead_client.api as ar time_provider = ar.ArrowheadHttpClient('time_provider', 'localhost', 1337, '', keyfile='crypt/provider.key', certfile='crypt/provider.pem') what_to_hello = 'Arrowhead' @time_provider.provided_service('echo', 'echo', 'HTTP-SECURITY_SECURE-JSON', 'GET', hello_what=what_to_hello) def echo(request, hello_what) -> Dict[str, str]: return {'msg': f'Hello {hello_what}'} ############################################################ # api.py from arrowhead_client.client import ArrowheadClient, provided_service class ArrowheadHttpClient(ArrowheadClient): """ Attributes: authentication_info: A string to assign the system authentication info """
# Creating ssh tunnel for the communication between robot and server server = SSHTunnelForwarder( (remote_host, remote_port), ssh_username='******', ssh_private_key='/etc/ed7039e/.ssh/id_rsa', remote_bind_address=(local_host, local_port), local_bind_address=(local_host, local_port), ) # Creating Provider app which will be listening on request from consumer through arrowhead # The ip and port are registred in arrowhead, which the consumer will later use to reach the provider provider_app = ar.ArrowheadHttpClient( system_name='lcm_provider', address='127.0.0.1', port=5000, keyfile='certificates/provider.key', certfile='certificates/provider.crt', ) # A service pick_up which will notify the robot to pick up a piece and where @provider_app.provided_service( 'pick_up', 'pick_up', 'HTTPS-SECURE-JSON', 'POST', ) def pick_up(request): if not 'position' in request.json: print('FAULT')
""" HttpConsumer example app """ import arrowhead_client.api as ar consumer = ar.ArrowheadHttpClient( system_name='consumer', address='127.0.0.1', port=7656, keyfile='crypt/consumer-app.key', certfile='crypt/consumer-app.pem' ) if __name__ == '__main__': consumer.setup() consumer.add_orchestration_rule('hello-arrowhead', 'GET') response = consumer.consume_service('hello-arrowhead') print(response.read_json()['msg']) consumer.add_orchestration_rule('echo', 'PUT') echo_response = consumer.consume_service('echo', json={'msg': 'ECHO'}) print(echo_response.read_json()['msg'])
from typing import Dict import arrowhead_client.api as ar time_provider = ar.ArrowheadHttpClient('time_provider', 'localhost', 1337, '', keyfile='certificates/time_provider.key', certfile='certificates/time_provider.crt') what_to_hello = 'Arrowhead' @time_provider.provided_service('echo', 'echo', 'HTTP-SECURE-JSON', 'GET', hello_what=what_to_hello) def echo(request, hello_what) -> Dict[str, str]: return {'msg': f'Hello {hello_what}'} @time_provider.provided_service('hej', 'hej', 'HTTP-SECURE-JSON', 'POST') def post(request) -> Dict[str, str]: print(request.json) what_to_hello = request.json['what_to_hello'] return {'response': 'OK'} @time_provider.provided_service('decorator', 'decorator', 'HTTP-SECURE-JSON', 'GET') def decorator(request) -> Dict[str, str]: return {'Decorator': 'Success'} if __name__ == '__main__': time_provider.run_forever()
from typing import Dict import arrowhead_client.api as ar cpu_temp_provider = ar.ArrowheadHttpClient('cpu_temp_provider', 'localhost', 1337, '') def echo(request) -> Dict[str, str]: return { 'cpu_temp': str(check_output("/opt/vc/bin/vcgencmd measure_temp", shell=True))[7:11] } def cpu_temp(): return { 'cpu_temp': str(check_output("/opt/vc/bin/vcgencmd measure_temp", shell=True))[7:11] } if __name__ == '__main__': cpu_temp_provider.provided_service(service_definition='echo', service_uri='echo', protocol='HTTP-SECURE-JSON', method='GET', payload_format='JSON', access_policy='')