def run()

in cdk-iot-analytics/simulator.py [0:0]


def run(region, overtemp, undertemp):

    if not os.path.exists(cert_file_path):
        c = boto3.client(
            'iot',
            region_name=region,
            config=Config(connect_timeout=5)
        )
        with open(cert_file_path, 'w') as pem:
            pem.write(
                c.describe_certificate(
                    certificateId=outputs['cdk-iot-for-sap-iot']['CertificateId']
                )['certificateDescription']['certificatePem']
            )

    def on_connect(client, userdata, flags, rc):
        click.echo(f"Connected with result code: {rc}")

    client = mqtt.Client(client_id=thing_name)
    client.on_connect = on_connect
    client.tls_set(
        ca_certs='certs/AmazonRootCA1.pem', 
        certfile=f'certs/{thing_name}.cert.pem', 
        keyfile=f'certs/{thing_name}.key.pem'
    )
    client.connect(
        outputs['cdk-iot-for-sap-iot']['DescribeEndpoint'], 
        8883, 
        60
    )

    client.loop_start()
    time.sleep(1.0)


    while True:
        try:
            if overtemp:
                temperature = random.uniform(temp_max + 0.001, temp_max + 4)
            elif undertemp:
                temperature = random.uniform(temp_min - 4, temp_min - 0.001)
            else:
                temperature = random.uniform(temp_min + 0.001, temp_max - 0.001)

            payload = json.dumps({
                "e": {
                    # NOTE: to trigger alarm, edit the "t" value
                    # to be below/above min/max temperatures
                    "t": temperature,
                    "h": random.uniform(0.0, 25.0),
                    "d": random.uniform(0.0, 25.0),
                    "i": random.uniform(20.0, 25.0)
                },
                "l": {
                    "c": {
                        "o": "AT&T",
                        "a": [
                            {
                                "i": 1704630,
                                "l": 56986,
                                "c": 310,
                                "n": 410
                            }
                        ]
                    }
                }
            })
            client.publish(
                f'dt/{thing_name}',
                payload=payload,
                qos=1
            )
            click.echo(f"published: {payload}")
            time.sleep(1.0)
        except KeyboardInterrupt:
            exit(0)