Science Event Publisher

Unmanaged application that publishes the utility values for random “science events”.

This application publishes random “science events” for testing other NOS-T applications. The message payload contains the current time, a random location, and the science utility function value at that time step.

The following code is used to set user credentials from a .env file and connect to the broker.

    # Note that these are loaded from a .env file in current working directory
    credentials = dotenv_values(".env")
    HOST, PORT = credentials["HOST"], int(credentials["PORT"])
    USERNAME, PASSWORD = credentials["USERNAME"], credentials["PASSWORD"]
    # build the MQTT client
    client = mqtt.Client()
    # set client username and password
    client.username_pw_set(username=USERNAME, password=PASSWORD)
    # set tls certificate
    client.tls_set()
    # connect to MQTT server
    client.connect(HOST, PORT)
    # start a background thread to let MQTT do things
    client.loop_start()

Next, a loop is created which randomly triggers science events. When triggered, these science events occur at a random location with a deterministic utility function. The code then publishes the utility score for each time step until it reaches zero.

    # for loop
    t = 0
    

    while True:

        # publish event utility message
        eventMessage = {"time":currentTime,
                        "latitude":eventLat,
                        "longitude":eventLon,
                        "utility":utility}
        client.publish("BCtest/ADCS/heartbeat", payload=json.dumps(eventMessage))
        print(eventMessage)

        #wait for next utility step
        time.sleep(1)

        # time step between possible events
        next_step = datetime.now() + timedelta(seconds=1)

        # allows application to be stopped with GUI every second
        while datetime.now() < next_step:
           time.sleep(1)