def __init__(self, args, shadow_command_q): self.args = args self.shadow_command_q = shadow_command_q print("Initializing shadow operations") logging.info("Initializing shadow operations") if not args.verbosity: args.verbosity = io.LogLevel.NoLogs.name io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') print("Set aws logging to %s" % args.verbosity) logging.info("Set aws logging to %s" % args.verbosity) self.mqtt_connection = None self.shadow_client = None self.robot_client = args.robot_client self.thing_name = args.thing_name self.shadow_property = args.shadow_property self.telemetry_thread = None self.is_sample_done = threading.Event() self.locked_data = LockedData() self.shadow_command_thread = threading.Thread( target=self.wait_to_end_shadow, name="Shadow Command Thread") logging.debug("Starting Shadow Command Thread") self.shadow_command_thread.start() # logging.debug("Starting Shadow Operations Main") self.main(self.args)
def __init__(self, config: Config): self.config = config self.logging = logging.getLogger(__name__) io.init_logging(io.LogLevel.Info, 'stderr') self.connected = False self.mqtt_connection = None self.event_loop_group = None self.host_resolver = None self.client_bootstrap = None self.connect_future = None
def main(): # Read in command-line parameters args = parse_args() io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') if not args.dry_run: client_id = "" thing_name = "" if not args.client_id: client_id = gethostname() else: client_id = args.client_id if not args.thing_name: thing_name = client_id else: thing_name = args.thing_name iot_client = IoTClientWrapper(args.endpoint, args.root_ca_path, args.certificate_path, args.private_key_path, client_id, args.signing_region, args.proxy_host, args.proxy_port, args.use_websocket) iot_client.connect() # client_id must match a registered thing name in your account topic = "$aws/things/" + thing_name + "/defender/metrics/" + args.format # Subscribe to the accepted/rejected topics to indicate status of published metrics reports iot_client.subscribe(topic + "/accepted", custom_callback) iot_client.subscribe(topic + "/rejected", custom_callback) sample_rate = args.upload_interval # Collector samples metrics from the system, it can track the previous metric to generate deltas coll = collector.Collector(args.short_tags, args.custom_metrics) metric = None first_sample = True # don't publish first sample, so we can accurately report delta metrics while True: metric = coll.collect_metrics() if args.dry_run: print(metric.to_json_string(pretty_print=True)) if args.format == 'cbor': with open("cbor_metrics", "w+b") as outfile: outfile.write(bytearray(metric.to_cbor())) else: if first_sample: first_sample = False elif args.format == "cbor": iot_client.publish(topic, bytearray(metric.to_cbor())) else: iot_client.publish(topic, metric.to_json_string()) sleep(float(sample_rate))
def arg_check(): """ argument check """ logging.debug("start: arg_check") parser = argparse.ArgumentParser() parser.add_argument("--device_name", required=True, help="[Must], input config file. include path") parser.add_argument("--endpoint", required=True, help="[Must], AWS IoT endpoint URI") parser.add_argument("--root_ca", required=False, help="root ca file name with path") parser.add_argument("--cert", required=False, help="device cert file name with path") parser.add_argument("--private", required=False, help="private cert key file name with path") parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name, help='Logging level') args = parser.parse_args() log_level = getattr(io.LogLevel, args.verbosity, "error") io.init_logging(log_level, 'stderr') loglevel_map = [ logging.INFO, logging.INFO, logging.INFO, logging.INFO, logging.INFO, logging.DEBUG, logging.DEBUG ] logger.setLevel(loglevel_map[log_level]) logging.basicConfig() cert_list = find_certs_file() if args.root_ca is not None: cert_list[0] = args.root_ca if args.private is not None: cert_list[1] = args.private if args.cert is not None: cert_list[2] = args.cert logging.debug(cert_list) file_exist_check(cert_list) init_dict = { "device_name": args.device_name, "endpoint": args.endpoint, "certs": cert_list } return init_dict
def __init__(self): self.settings = Settings() self.logging = logging.getLogger(__name__) sl = StreamToLogger(self.logging, logging.INFO) sys.stderr = sl io.init_logging(io.LogLevel.Error, 'stderr') self.connected = False self.mqtt_connection = None self.event_loop_group = None self.host_resolver = None self.client_bootstrap = None self.connect_future = None
def __init__(self, host: str, region:str, secretname:str, topics: List[str], profilename:str): ''' - host - mqtt host - region - aws region - secretname - secretname to pull from aws secret service - topic - list of topics to monitor ''' self.logging = logging.getLogger(__name__) self.host = host self.region = region self.secretname = secretname self.topics = topics self.profilename = profilename io.init_logging(io.LogLevel.Info, 'stderr') self.connected = False self.mqtt_connection = None self.event_loop_group = None self.host_resolver = None self.client_bootstrap = None self.connect_future = None
def arg_check(): """ argument check """ global private_key_path, certtificate_path, root_ca_path, device_name, region parser = argparse.ArgumentParser() parser.add_argument('-n', '--thing-name', action='store', required=True, dest='thing_name', help='Targeted thing name') parser.add_argument('--region', action='store', dest='region', default='ap-northeast-1') parser.add_argument('-v', '--verbosity', choices=[x.name for x in LogLevel], default=LogLevel.NoLogs.name, help='Logging level') args = parser.parse_args() log_level = getattr(io.LogLevel, args.verbosity, "error") io.init_logging(log_level, 'stderr') loglevel_map = [ logging.INFO, logging.INFO, logging.INFO, logging.INFO, logging.INFO, logging.DEBUG, logging.DEBUG ] logger.setLevel(loglevel_map[log_level]) logging.basicConfig() private_key_path = find_cert_file("private.key") certtificate_path = find_cert_file("cert.pem") root_ca_path = find_cert_file("AmazonRootCA1.pem") device_name = args.thing_name region = args.region
import uuid TIMEOUT = 5 # seconds given to each step of the test before giving up UNIQUE_ID = str(uuid.uuid4()) # prevent simultaneously-running tests from interfering with each other CLIENT_ID = 'test_pubsub_' + UNIQUE_ID TOPIC = 'test/pubsub/' + UNIQUE_ID MESSAGE = 'test message ' + UNIQUE_ID parser = argparse.ArgumentParser() parser.add_argument('--endpoint', required=True, help="Connect to this endpoint (aka host-name)") parser.add_argument('--port', type=int, help="Override default connection port") parser.add_argument('--cert', help="File path to your client certificate, in PEM format") parser.add_argument('--key', help="File path to your private key, in PEM format") parser.add_argument('--root-ca', help="File path to root certificate authority, in PEM format") io.init_logging(LogLevel.Trace, 'stderr') def on_connection_interrupted(connection, error, **kwargs): print("Connection has been interrupted with error", error) def on_connection_resumed(connection, return_code, session_present, **kwargs): print("Connection has been resumed with return code", return_code, "and session present:", session_present) if not session_present: print("Resubscribing to existing topics") resubscribe_future, packet_id = connection.resubscribe_existing_topics() def on_resubscribe_complete(resubscribe_future): try: resubscribe_results = resubscribe_future.result() print("Resubscribe results:", resubscribe_results)
# Modified from AWS IoT SDK sample code pubsub.py # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0. import json from awscrt import io, mqtt from awsiot import mqtt_connection_builder import sys from threading import Event verbosity = io.LogLevel.NoLogs.name io.init_logging(getattr(io.LogLevel, verbosity), "stderr") # Callback when connection is accidentally lost. def on_connection_interrupted(connection, error, **kwargs): print("Connection interrupted. error: {}".format(error)) # Callback when an interrupted connection is re-established. def on_connection_resumed(connection, return_code, session_present, *kwargs): print( "Connection resumed. return_code: {} session_present: {}".format( return_code, session_present ) ) if return_code == mqtt.ConnectReturnCode.ACCEPTED and not session_present:
import json from flask import Flask from flask import request from concurrent.futures import Future from awscrt import io from awscrt.io import LogLevel from awscrt.mqtt import Connection, Client, QoS from awsiot.greengrass_discovery import DiscoveryClient, DiscoverResponse from awsiot import mqtt_connection_builder root_ca_path = '/app/certs/root.ca.pem' certificate_path = '/app/certs/709da90f0c.cert.pem' private_key_path = '/app/certs/709da90f0c.private.key' thing_name = 'docker_local' io.init_logging(1, 'stderr') event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group) client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(certificate_path, private_key_path) if root_ca_path: tls_options.override_default_trust_store_from_path(None, root_ca_path) tls_context = io.ClientTlsContext(tls_options) socket_options = io.SocketOptions() socket_options.connect_timeout_ms = 3000 print('Performing greengrass discovery...') discovery_client = DiscoveryClient(client_bootstrap, socket_options, tls_context, 'ap-southeast-2')
def on_message_received(topic, payload, dup, qos, retain, **kwargs): ''' Callback when the subscribed topic receives a message ''' print(f"Received message from topic '{topic}': {payload}") if __name__ == '__main__': # Read in config with open("config.yaml", "r") as ymlfile: cfg = SimpleNamespace(**yaml.safe_load(ymlfile)) # Spin up resources io.init_logging(getattr(io.LogLevel, io.LogLevel.NoLogs.name), 'stderr') event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group) client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) mqtt_connection = mqtt_connection_builder.mtls_from_path( endpoint=cfg.thing_endpoint, cert_filepath=cfg.cert, pri_key_filepath=cfg.priv_key, client_bootstrap=client_bootstrap, ca_filepath=cfg.root_ca, on_connection_interrupted=on_connection_interrupted, on_connection_resumed=on_connection_resumed, client_id=cfg.client_id, clean_session=False, keep_alive_secs=6)
except Exception as e: print("Exception on input thread. {}".format(e)) if __name__ == '__main__': print(endpoint) print(cert) print(key) print(root_ca) print(client_id) print(thing_name) print("starting") # start AWS IOT logging to stderr # options are NoLogs, Fatal, Error, Warn, Info, Debug, Trace io.init_logging(io.LogLevel.Error, 'stderr') # Spin up resources event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group) client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) mqtt_connection = mqtt_connection_builder.mtls_from_path( endpoint=endpoint, cert_filepath=cert, pri_key_filepath=key, client_bootstrap=client_bootstrap, ca_filepath=root_ca, client_id=client_id, clean_session=True, keep_alive_secs=6,
def __init__(self): io.init_logging(getattr(io.LogLevel, conf.VERBOSITY), 'stderr')
log_level = io.LogLevel.Info elif args.verbose == 'DEBUG': log_level = io.LogLevel.Debug elif args.verbose == 'TRACE': log_level = io.LogLevel.Trace else: print('{} unsupported value for the verbose option'.format( args.verbose)) exit(-1) log_output = 'stderr' if args.trace: log_output = args.trace io.init_logging(log_level, log_output) required_version = http.HttpVersion.Unknown if args.http1_1: required_version = http.HttpVersion.Http1_1 args.alpn = ["http/1.1"] if args.http2: required_version = http.HttpVersion.Http2 args.alpn = ["h2"] # an event loop group is needed for IO operations. Unless you're a server or a client doing hundreds of connections # you only want one of these. event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group)
parser = argparse.ArgumentParser( description="Send and receive messages through and MQTT connection.") parser.add_argument( '--count', default=10, type=int, help="Number of messages to publish/receive before exiting. " + "Specify 0 to run forever.") parser.add_argument('--deviceid', default='SampleClient1', help="The unique id for this IoT sample client") # Using globals to simplify sample code args = parser.parse_args() io.init_logging(io.LogLevel.NoLogs, 'stderr') received_count = 0 received_all_event = threading.Event() with open("client.config", "r") as configfile: config_data = json.load(configfile) # Callback when connection is accidentally lost. def on_connection_interrupted(connection, error, **kwargs): print("Connection interrupted. error: {}".format(error)) # Callback when an interrupted connection is re-established. def on_connection_resumed(connection, return_code, session_present, **kwargs):
def main(): # get args logging.basicConfig(filename='output.log', filemode='w', level=logging.DEBUG) args = get_args() # get args payload = "" lora_payload = {} # set log level io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') # spin up resources event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group) client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) # set MQTT connection mqtt_connection = set_mqtt_connection(args, client_bootstrap) logging.debug("Connecting to {} with client ID '{}'...".format( args.endpoint, args.client_id)) connect_future = mqtt_connection.connect() # future.result() waits until a result is available connect_future.result() logging.debug("Connecting to REYAX RYLR896 transceiver module...") serial_conn = serial.Serial(port=args.tty, baudrate=int(args.baud_rate), timeout=5, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS) if serial_conn.isOpen(): logging.debug("Connected!") set_lora_config(serial_conn) check_lora_config(serial_conn) while True: # read data from serial port serial_payload = serial_conn.readline() logging.debug(serial_payload) if len(serial_payload) >= 1: payload = serial_payload.decode(encoding="utf-8") payload = payload[:-2] try: data = parse_payload(payload) lora_payload = { "ts": time.time(), "data": { "device_id": str(data[0]), "gateway_id": str(args.gateway_id), "temperature": float(data[1]), "humidity": float(data[2]), "pressure": float(data[3]), "color": { "red": float(data[4]), "green": float(data[5]), "blue": float(data[6]), "ambient": float(data[7]) } } } logging.debug(lora_payload) except IndexError: logging.error("IndexError: {}".format(payload)) except ValueError: logging.error("ValueError: {}".format(payload)) # publish mqtt message message_json = json.dumps(lora_payload, sort_keys=True, indent=None, separators=(',', ':')) try: mqtt_connection.publish(topic=args.topic, payload=message_json, qos=mqtt.QoS.AT_LEAST_ONCE) except mqtt.SubscribeError as err: logging.error(".SubscribeError: {}".format(err)) except exceptions.AwsCrtError as err: logging.error("AwsCrtError: {}".format(err))
# we should probably get sondehub v1 stuff in here as well # error handling - at the moment we bail on a single failure # report to the user what's happened # probably turn down logging since cloudwatch costs $$$ # env variable some of this # work out how to have a dev env patch_all() event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group) io.init_logging(io.LogLevel.Error, "stderr") def connect(): global connect_future, mqtt_connection session = boto3.session.Session() client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) credentials_provider = auth.AwsCredentialsProvider.new_default_chain( client_bootstrap ) mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing( endpoint=os.getenv("IOT_ENDPOINT"), client_bootstrap=client_bootstrap, region="us-east-1", credentials_provider=credentials_provider, client_id=str(uuid.uuid4()),
parser.add_argument('--use-websocket', default=False, action='store_true', help="To use a websocket instead of raw mqtt. If you " + "specify this option you must specify a region for signing, you can also enable proxy mode.") parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " + "is the region that will be used for computing the Sigv4 signature") parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " + "you will likely need to set --root-ca to the ca for your proxy.") parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.") parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name, help='Logging level') # Using globals to simplify sample code args = parser.parse_args() io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') received_count = 0 received_all_event = threading.Event() # Callback when connection is accidentally lost. def on_connection_interrupted(connection, error, **kwargs): print("Connection interrupted. error: {}".format(error)) # Callback when an interrupted connection is re-established. def on_connection_resumed(connection, return_code, session_present, **kwargs): print("Connection resumed. return_code: {} session_present: {}".format(return_code, session_present)) if return_code == mqtt.ConnectReturnCode.ACCEPTED and not session_present: print("Session did not persist. Resubscribing to existing topics...")
def main(): # Parse command line arguments parser, args = parse_args() global count count = args.count # set log level io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') # Print MAC address print(gma()) # Initialize and Calibrate Gas Sensor 1x mq = MQ() # Initial the dht device, with data pin connected to: dht_device = adafruit_dht.DHT22(PIN_DHT) # Initialize Light Sensor ls = LightSensor(PIN_LIGHT) # Initialize PIR Sensor pir = MotionSensor(PIN_PIR) led = LED(PIN_PIR_LED) # Spin up resources event_loop_group = io.EventLoopGroup(1) host_resolver = io.DefaultHostResolver(event_loop_group) client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) # Set MQTT connection mqtt_connection = set_mqtt_connection(args, client_bootstrap) print("Connecting to {} with client ID '{}'...".format( args.endpoint, args.client_id)) connect_future = mqtt_connection.connect() # Future.result() waits until a result is available connect_future.result() print("Connected!") # # Subscribe (this will pull in messages down from other devices) # print("Subscribing to topic '{}'...".format(args.topic)) # subscribe_future, packet_id = mqtt_connection.subscribe( # topic=args.topic, # qos=mqtt.QoS.AT_LEAST_ONCE, # callback=on_message_received) # # subscribe_result = subscribe_future.result() # print("Subscribed with {}".format(str(subscribe_result['qos']))) while True: led.off() # Create message payload payload_dht = get_sensor_data_dht(dht_device) payload_gas = get_sensor_data_gas(mq) payload_light = get_sensor_data_light(ls) payload_motion = get_sensor_data_motion(pir, led) payload = { "device_id": gma(), "ts": time.time(), "data": { "temp": payload_dht["temp"], "humidity": payload_dht["humidity"], "lpg": payload_gas["lpg"], "co": payload_gas["co"], "smoke": payload_gas["smoke"], "light": payload_light["light"], "motion": payload_motion["motion"] } } # Don't send bad messages! if payload["data"]["temp"] is not None \ and payload["data"]["humidity"] is not None \ and payload["data"]["co"] is not None: # Publish Message message_json = json.dumps(payload, sort_keys=True, indent=None, separators=(',', ':')) try: mqtt_connection.publish(topic=args.topic, payload=message_json, qos=mqtt.QoS.AT_LEAST_ONCE) except mqtt.SubscribeError as err: print(".SubscribeError: {}".format(err)) except exceptions.AwsCrtError as err: print("AwsCrtError: {}".format(err)) else: time.sleep(args.frequency) else: print("sensor failure...retrying...")